import struct


def read_varint(buffer: bytearray) -> int:
    """ Parse a varint, read bytes are consumed.
    """
    i, = struct.unpack('<B', buffer[:1])
    if i < 0xfd:
        del buffer[:1]
        res = i
    elif i < 0xfe:
        res, = struct.unpack('<H', buffer[1:3])
        del buffer[:3]
    elif i < 0xff:
        res, = struct.unpack('<I', buffer[1:5])
        del buffer[:5]
    else:
        res, = struct.unpack('<Q', buffer[1:9])
        del buffer[:9]
    return res