def render_int(n):
    """ Renders an int in the shortest possible form.

    When packing the height into the coinbase script, the integer
    representing the height must be encoded in the shortest possible
    manner. See: https://bitcoin.org/en/developer-reference#coinbase.

    Args:
        n (int): number to be encoded.

    Returns:
        b (bytes): bytes representing n in the shortest possible form.
    """
    # little-endian byte stream
    if n < 0:
        neg = True
        n = -n
    else:
        neg = False
    r = []
    while n:
        r.append(n & 0xff)
        n >>= 8
    if neg:
        if r[-1] & 0x80:
            r.append(0x80)
        else:
            r[-1] |= 0x80
    elif r and (r[-1] & 0x80):
        r.append(0)
    return bytes(r)