// File: /workspace/github_c_makefile_star2_O0/TimeToogo[P]ff-proxy/client/c/client.c
// Function: ff_client_packetise_request
struct ff_client_packet *ff_client_packetise_request(struct ff_request *request, uint16_t *packet_count)
{
    struct ff_client_packet *packets = calloc(1, sizeof(struct ff_client_packet *) * FF_CLIENT_MAX_PACKETS);
    uint64_t request_id = ff_client_generate_request_id();
    uint32_t chunk_offset = 0;
    uint16_t bytes_left = request->payload_length;

    *packet_count = 0;

    while (bytes_left > 0)
    {
        uint8_t *buffer = calloc(1, FF_CLIENT_MAX_PACKET_LENGTH);
        uint16_t packet_length = 0;
        struct __raw_ff_request_header *header = (struct __raw_ff_request_header *)buffer;

        header->version = htons(FF_VERSION_1);
        header->request_id = htonll(request_id);
        header->total_length = htonl(request->payload_length);
        header->chunk_offset = htonl(chunk_offset);
        packet_length += sizeof(struct __raw_ff_request_header);

        if (*packet_count == 0)
        {
            // If first packet, send options
            packet_length += ff_client_write_options(buffer + packet_length, request->options, request->options_length);
        }
        else
        {
            // If subsequent packet provide empty option list
            struct __raw_ff_request_option_header *option_header = (struct __raw_ff_request_option_header *)(buffer + packet_length);
            option_header->type = FF_REQUEST_OPTION_TYPE_EOL;
            option_header->length = 0;
            packet_length += sizeof(struct __raw_ff_request_option_header);
        }

        uint16_t bytes_left_in_packet = FF_CLIENT_MAX_PACKET_LENGTH - packet_length;
        uint16_t chunk_length = bytes_left > bytes_left_in_packet ? bytes_left_in_packet : bytes_left;

        header->chunk_length = htons(chunk_length);

        memcpy(buffer + packet_length, request->payload->value + chunk_offset, chunk_length);
        packet_length += chunk_length;
        bytes_left -= chunk_length;
        chunk_offset += chunk_length;

        packets[*packet_count].length = packet_length;
        packets[*packet_count].value = buffer;
        (*packet_count)++;
    }

    ff_log(FF_DEBUG, "Packetised payload into %u packets", *packet_count);

    return packets;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, %edi
movl $0x2000, %esi           # imm = 0x2000
callq 0x2140
movq %rax, -0x18(%rbp)
callq 0x3270
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rax
movw $0x0, (%rax)
movzwl -0x26(%rbp), %eax
cmpl $0x0, %eax
jle 0x2cae
movl $0x1, %edi
movl $0x514, %esi            # imm = 0x514
callq 0x2140
movq %rax, -0x30(%rbp)
movw $0x0, -0x32(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x1, %edi
callq 0x2050
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, (%rax)
movl $0x1, %edi
callq 0x2160
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x2ae9
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x2b26
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq -0x20(%rbp), %rax
movl %eax, %edi
callq 0x2160
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
shrq $0x20, %rax
movl %eax, %edi
callq 0x2160
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, %ecx
orq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x2(%rax)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edi
callq 0x2160
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xa(%rax)
movl -0x24(%rbp), %edi
callq 0x2160
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xe(%rax)
movzwl -0x32(%rbp), %eax
addq $0x14, %rax
movw %ax, -0x32(%rbp)
movq -0x10(%rbp), %rax
movzwl (%rax), %eax
cmpl $0x0, %eax
jne 0x2ba3
movq -0x30(%rbp), %rdi
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x8(%rbp), %rax
movzbl 0x28(%rax), %edx
callq 0x3070
movl %eax, %ecx
movzwl -0x32(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x32(%rbp)
jmp 0x2bd2
movq -0x30(%rbp), %rax
movzwl -0x32(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
movq -0x48(%rbp), %rax
movw $0x0, 0x1(%rax)
movzwl -0x32(%rbp), %eax
addq $0x3, %rax
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %ecx
movl $0x514, %eax            # imm = 0x514
subl %ecx, %eax
movw %ax, -0x4a(%rbp)
movzwl -0x26(%rbp), %eax
movzwl -0x4a(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2bf6
movzwl -0x4a(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x2bfd
movzwl -0x26(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movw %ax, -0x4c(%rbp)
movzwl -0x4c(%rbp), %edi
callq 0x2050
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x12(%rax)
movq -0x30(%rbp), %rdi
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rsi
movl -0x24(%rbp), %eax
addq %rax, %rsi
movzwl -0x4c(%rbp), %eax
movl %eax, %edx
callq 0x2180
movzwl -0x4c(%rbp), %ecx
movzwl -0x32(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x32(%rbp)
movzwl -0x4c(%rbp), %ecx
movzwl -0x26(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x26(%rbp)
movzwl -0x4c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movw -0x32(%rbp), %cx
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rdx
movzwl (%rdx), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movw %cx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rdx
movzwl (%rdx), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
jmp 0x2a88
movq -0x10(%rbp), %rax
movzwl (%rax), %edx
movl $0x1, %edi
leaq 0x3402(%rip), %rsi      # 0x60c3
movb $0x0, %al
callq 0x4480
movq -0x18(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)