// File: /workspace/github_c_makefile_star2_O3/TimeToogo[P]ff-proxy/src/parser.c
// Function: ff_request_parse_options
size_t ff_request_parse_options(struct ff_request *request, uint32_t buff_size, void *buff)
{
    struct __raw_ff_request_option_header *option_header = NULL;
    struct ff_request_option_node *options[FF_REQUEST_MAX_OPTIONS];
    uint8_t options_i = 0;
    uint32_t i = 0;
    size_t current_options_size = request->options_length;

    // Parse TLV options
    while (1)
    {
        if (current_options_size + options_i >= FF_REQUEST_MAX_OPTIONS)
        {
            ff_log(FF_WARNING, "Encountered request with too many options");
            goto error;
        }

        if (buff_size < i + sizeof(struct __raw_ff_request_option_header))
        {
            ff_log(FF_WARNING, "Packet buffer ran out while processing TLV options");
            goto error;
        }

        option_header = (struct __raw_ff_request_option_header *)(buff + i);

        // Normalise endianess
        uint16_t option_length = ntohs(option_header->length);

        i += sizeof(struct __raw_ff_request_option_header);

        if (option_header->type == FF_REQUEST_OPTION_TYPE_BREAK)
        {
            if (option_length != 0)
            {
                ff_log(FF_WARNING, "Request option FF_REQUEST_OPTION_TYPE_BREAK must have length = 0");
                goto error;
            }

            request->payload_contains_options = true;
            break;
        }

        if (option_header->type == FF_REQUEST_OPTION_TYPE_EOL)
        {
            if (option_length != 0)
            {
                ff_log(FF_WARNING, "Request option FF_REQUEST_OPTION_TYPE_EOL must have length = 0");
                goto error;
            }

            request->payload_contains_options = false;
            break;
        }

        if (buff_size < i + option_length)
        {
            ff_log(FF_WARNING, "Packet buffer ran out while processing TLV options");
            goto error;
        }

        options[options_i] = ff_request_option_node_alloc();
        options[options_i]->type = option_header->type;
        options[options_i]->length = option_length;
        ff_request_option_load_buff(
            options[options_i],
            option_length,
            buff + i);

        i += option_length;
        options_i++;
    }

    if (options_i == 0)
    {
        return i;
    }

    request->options_length = current_options_size + options_i;

    if (request->options == NULL)
    {
        request->options = malloc(sizeof(struct ff_request_option_node *) * request->options_length);
    }
    else
    {
        request->options = realloc(request->options, sizeof(struct ff_request_option_node *) * request->options_length);
    }

    memcpy(
        request->options + current_options_size,
        options,
        sizeof(struct ff_request_option_node *) * options_i);

    goto done;

done:
    goto cleanup;

error:
    i = 0;

    for (uint8_t j = 0; j < options_i; j++) {
        ff_request_option_node_free(options[j]);
    }

    goto cleanup;

cleanup:
    return i;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, -0x48(%rbp)
movl %esi, %edx
movzbl 0x28(%rdi), %ecx
xorl %r13d, %r13d
leaq 0x2a31(%rip), %rsi      # 0x6ff4
movq %rcx, -0x38(%rbp)
cmpq $0x13, %rcx
jbe 0x45d5
xorl %r14d, %r14d
jmp 0x4697
movq %rdi, -0x50(%rbp)
movl %edx, %eax
movq %rax, -0x58(%rbp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %edx, -0x3c(%rbp)
movq %r14, -0x30(%rbp)
movl %r13d, %r15d
leaq 0x3(%r15), %r14
cmpq -0x58(%rbp), %r14
ja 0x4670
movq -0x48(%rbp), %rcx
movzwl 0x1(%rcx,%r15), %eax
movzbl (%rcx,%r15), %ecx
testl %ecx, %ecx
je 0x4679
cmpl $0x7, %ecx
je 0x4687
rolw $0x8, %ax
movzwl %ax, %ebx
leal (%rbx,%r14), %r13d
cmpl %edx, %r13d
ja 0x4670
callq 0x4898
movq %rax, -0x100(%rbp,%r12,8)
movq -0x48(%rbp), %rdx
movzbl (%rdx,%r15), %ecx
movl %ecx, (%rax)
movw %bx, 0x4(%rax)
addq %rdx, %r14
movq %rax, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x48bc
movl -0x3c(%rbp), %edx
movq -0x30(%rbp), %r14
incb %r14b
movzbl %r14b, %r12d
movq -0x38(%rbp), %rax
addl %r12d, %eax
cmpl $0x13, %eax
jbe 0x45e8
leaq 0x2986(%rip), %rsi      # 0x6ff4
jmp 0x4697
leaq 0x29a7(%rip), %rsi      # 0x701e
jmp 0x4693
testw %ax, %ax
je 0x46dd
leaq 0x2a0d(%rip), %rsi      # 0x7092
jmp 0x4693
testw %ax, %ax
je 0x46e1
leaq 0x29be(%rip), %rsi      # 0x7051
movq -0x30(%rbp), %r14
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
testb %r14b, %r14b
je 0x46c8
movzbl %r14b, %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
movq -0x100(%rbp,%r15,8), %rdi
callq 0x48fb
incq %r15
cmpq %r15, %r14
jne 0x46b3
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x46e3
movb $0x1, %al
movq -0x50(%rbp), %r15
movb %al, 0x38(%r15)
movq -0x30(%rbp), %rax
testb %al, %al
je 0x4739
movzbl %al, %ebx
addb -0x38(%rbp), %al
movb %al, 0x28(%r15)
movq 0x30(%r15), %rdi
movzbl %al, %esi
shll $0x3, %esi
testq %rdi, %rdi
je 0x4713
callq 0x2440
jmp 0x471b
movq %rsi, %rdi
callq 0x23c0
movq %rax, 0x30(%r15)
movq -0x38(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
shll $0x3, %ebx
leaq -0x100(%rbp), %rsi
movq %rbx, %rdx
callq 0x2260
movl %r14d, %ebx
jmp 0x46c8