import struct


def write_varint(data: int) -> bytes:
    """Given an integer, encode the integer into a varint.

    Args:
        data (int): The integer to encode into a varint.

    Returns:
        bytes: The encoded varint.
    """
    packed_packets = list()
    while data != 0:
        current_byte = data & 0x7F
        data >>= 7
        compiled_bytes = struct.pack("B", current_byte | (0x80 if data > 0 else 0))
        packed_packets.append(compiled_bytes)
    return b"".join(packed_packets)