def signed_leb128_decode(data) -> int:
    """Read variable length encoded 128 bits signed integer.

    .. doctest::

        >>> from ppci.utils.leb128 import signed_leb128_decode
        >>> signed_leb128_decode(iter(bytes([0x9b, 0xf1, 0x59])))
        -624485
    """
    result = 0
    shift = 0
    while True:
        byte = next(data)
        result |= (byte & 0x7F) << shift
        shift += 7
        # Detect last byte:
        if byte & 0x80 == 0:
            break

    if byte & 0x40:
        # We have sign bit set!
        mask = (1 << shift) - 1
        result = result ^ mask
        result = -result - 1

    return result