// File: /workspace/github_c_makefile_star2_O1/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)
movq %rdi, -0x30(%rbp)
movzbl 0x28(%rdi), %r13d
movl %esi, -0x34(%rbp)
movl %esi, %eax
movq %rax, -0x58(%rbp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r13, -0x40(%rbp)
movzbl %r15b, %r12d
leal (%r12,%r13), %eax
cmpl $0x13, %eax
ja 0x48c0
movl %ebx, %r14d
leaq 0x3(%r14), %rbx
cmpq -0x58(%rbp), %rbx
ja 0x48c9
movq -0x48(%rbp), %rcx
movzwl 0x1(%rcx,%r14), %eax
movzbl (%rcx,%r14), %ecx
testl %ecx, %ecx
je 0x47cb
cmpl $0x7, %ecx
jne 0x47f1
testw %ax, %ax
je 0x4867
movl $0x3, %edi
leaq 0x2888(%rip), %rsi      # 0x7051
jmp 0x47e0
testw %ax, %ax
je 0x4871
movl $0x3, %edi
leaq 0x28b2(%rip), %rsi      # 0x7092
xorl %eax, %eax
callq 0x4100
movl $0x4, %eax
jmp 0x487e
movq %r15, -0x50(%rbp)
rolw $0x8, %ax
movzwl %ax, %r13d
leal (%rbx,%r13), %r15d
cmpl -0x34(%rbp), %r15d
jbe 0x4829
movl $0x3, %edi
leaq 0x280b(%rip), %rsi      # 0x701e
xorl %eax, %eax
callq 0x4100
movl $0x4, %eax
movq -0x40(%rbp), %r13
movq -0x50(%rbp), %r15
jmp 0x487e
callq 0x4aa0
movq %rax, -0x100(%rbp,%r12,8)
movq -0x48(%rbp), %rsi
movzbl (%rsi,%r14), %ecx
movl %ecx, (%rax)
movw %r13w, 0x4(%rax)
movl %ebx, %edx
addq %rsi, %rdx
movq %rax, %rdi
movl %r13d, %esi
callq 0x4ac4
incb %r12b
xorl %eax, %eax
movl %r15d, %ebx
movq %r12, %r15
movq -0x40(%rbp), %r13
jmp 0x487e
movq -0x30(%rbp), %rax
movb $0x1, 0x38(%rax)
jmp 0x4879
movq -0x30(%rbp), %rax
movb $0x0, 0x38(%rax)
movl $0x3, %eax
testl %eax, %eax
je 0x477a
cmpl $0x4, %eax
je 0x48dc
cmpl $0x3, %eax
jne 0x4905
testb %r15b, %r15b
je 0x492e
movq %r15, %rax
movzbl %al, %r15d
addl %r13d, %eax
movq -0x30(%rbp), %rcx
movb %al, 0x28(%rcx)
movq 0x30(%rcx), %rdi
movzbl %al, %esi
shll $0x3, %esi
testq %rdi, %rdi
je 0x4907
callq 0x2440
jmp 0x490f
leaq 0x272d(%rip), %rsi      # 0x6ff4
jmp 0x48d0
leaq 0x274e(%rip), %rsi      # 0x701e
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
testb %r15b, %r15b
je 0x4901
movzbl %r15b, %r14d
xorl %ebx, %ebx
xorl %r15d, %r15d
movq -0x100(%rbp,%r15,8), %rdi
callq 0x4b03
incq %r15
cmpq %r15, %r14
jne 0x48ea
jmp 0x4930
xorl %ebx, %ebx
jmp 0x4930
jmp 0x4930
movq %rsi, %rdi
callq 0x23c0
movq -0x30(%rbp), %rcx
movq %rax, 0x30(%rcx)
leaq (%rax,%r13,8), %rdi
shll $0x3, %r15d
leaq -0x100(%rbp), %rsi
movq %r15, %rdx
callq 0x2260
movl %ebx, %ebx
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq