// File: /workspace/github_c_makefile_star2_O2/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 $0xc8, %rsp
movq %rdx, -0x40(%rbp)
movq %rdi, -0x48(%rbp)
movzbl 0x28(%rdi), %eax
movq %rax, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movl %esi, %eax
movq %rax, -0x50(%rbp)
xorl %eax, %eax
xorl %r13d, %r13d
movzbl %al, %r15d
movl %r15d, %eax
addl -0x30(%rbp), %eax
cmpl $0x13, %eax
ja 0x4416
movl %r13d, %r12d
leaq 0x3(%r12), %r14
cmpq -0x50(%rbp), %r14
ja 0x440d
movq -0x40(%rbp), %rcx
movzwl 0x1(%rcx,%r12), %eax
movzbl (%rcx,%r12), %ecx
testl %ecx, %ecx
je 0x441f
cmpl $0x7, %ecx
je 0x442d
rolw $0x8, %ax
movzwl %ax, %ebx
leal (%rbx,%r14), %r13d
cmpl -0x34(%rbp), %r13d
ja 0x440d
callq 0x4628
movq %rax, -0xf0(%rbp,%r15,8)
movq -0x40(%rbp), %rdx
movzbl (%rdx,%r12), %ecx
movl %ecx, (%rax)
movw %bx, 0x4(%rax)
addq %rdx, %r14
movq %rax, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x4643
leal 0x1(%r15), %eax
jmp 0x4395
leaq 0x1c0a(%rip), %rsi      # 0x601e
jmp 0x4439
leaq 0x1bd7(%rip), %rsi      # 0x5ff4
jmp 0x4439
testw %ax, %ax
je 0x445f
leaq 0x1c67(%rip), %rsi      # 0x6092
jmp 0x4439
testw %ax, %ax
je 0x4463
leaq 0x1c18(%rip), %rsi      # 0x6051
xorl %ebx, %ebx
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x44bc
movq -0xf0(%rbp,%r14,8), %rdi
callq 0x4682
incq %r14
jmp 0x4448
xorl %eax, %eax
jmp 0x4465
movb $0x1, %al
movq -0x48(%rbp), %rbx
movb %al, 0x38(%rbx)
testq %r15, %r15
je 0x44b9
movq -0x30(%rbp), %rax
addl %r15d, %eax
movb %al, 0x28(%rbx)
movq 0x30(%rbx), %rdi
movzbl %al, %esi
shll $0x3, %esi
testq %rdi, %rdi
je 0x4491
callq 0x2440
jmp 0x4499
movq %rsi, %rdi
callq 0x23c0
movq %rax, 0x30(%rbx)
movq -0x30(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
leal (,%r15,8), %edx
leaq -0xf0(%rbp), %rsi
callq 0x2260
movl %r14d, %ebx
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq