import struct


def unpack_compact_int(bytestr):
    """ See
    https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers

    Args:
        bytestr (bytes): bytes containing an unsigned integer to be
            deserialized.

    Returns:
        n (int): deserialized integer.
    """

    b0 = bytestr[0]
    if b0 < 0xfd:
        return (b0, bytestr[1:])
    elif b0 == 0xfd:
        return (struct.unpack('<H', bytestr[1:3])[0], bytestr[3:])
    elif b0 == 0xfe:
        return (struct.unpack('<I', bytestr[1:5])[0], bytestr[5:])
    elif b0 == 0xff:
        return (struct.unpack('<Q', bytestr[1:9])[0], bytestr[9:])
    else:
        return None