// File: /workspace/github_c_makefile_star2_O1/TimeToogo[P]ff-proxy/src/server.c
// Function: ff_proxy_process_incoming_packet
void ff_proxy_process_incoming_packet(struct ff_config *config, struct ff_hash_table *requests, struct sockaddr *src_address, void *packet_buff, int buff_len)
{
    bool is_raw_http = ff_request_is_raw_http(buff_len, packet_buff);
    uint64_t request_id = 0;
    struct ff_request *request;
    pthread_t thread;
    pthread_attr_t thread_attrs;
    struct ff_process_request_args *thread_args;

    if (is_raw_http)
    {
        ff_log(FF_DEBUG, "Incoming packet is raw HTTP request");
        request = ff_request_alloc();
        ff_request_parse_chunk(request, buff_len, packet_buff);
    }
    else
    {
        ff_log(FF_DEBUG, "Parsing incoming packet");
        request_id = ff_request_parse_id(buff_len, packet_buff);

        if (request_id == 0)
        {
            ff_log(FF_DEBUG, "Could not parse request ID from incoming packet");
            goto done;
        }

        request = (struct ff_request *)ff_hash_table_get_item(requests, request_id);

        if (request == NULL)
        {
            request = ff_request_alloc();
            time(&request->received_at);
            memcpy(&request->source, src_address, sizeof(struct sockaddr));
            ff_hash_table_put_item(requests, request_id, (void *)request);
        }

        if (memcmp(&request->source, src_address, sizeof(struct sockaddr)) != 0)
        {
            ff_log(FF_WARNING, "Incoming packet IP address does not match original source IP address/port for request %lu (will discard)", request->request_id);
            goto done;
        }

        ff_request_parse_chunk(request, buff_len, packet_buff);
    }

    switch (request->state)
    {
    case FF_REQUEST_STATE_RECEIVING:
        break;

    case FF_REQUEST_STATE_RECEIVING_FAIL:
        if (request_id != 0)
        {
            ff_hash_table_remove_item(requests, request_id);
        }
        ff_request_free(request);
        break;

    case FF_REQUEST_STATE_RECEIVED:
        ff_log(FF_DEBUG, "Finished receiving incoming request");

        thread_args = malloc(sizeof(struct ff_process_request_args));
        thread_args->config = config;
        thread_args->request = request;
        thread_args->requests = requests;

        pthread_attr_init(&thread_attrs);
        pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED);

        pthread_create(&thread, &thread_attrs, (void *)ff_proxy_process_request, (void *)thread_args);

        pthread_attr_destroy(&thread_attrs);
        break;
    default:
        ff_log(FF_ERROR, "Encountered invalid request state: %d", request->state);
        break;
    }

done:
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r12d
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
movl %r8d, %edi
movq %rcx, %rsi
callq 0x4676
testb %al, %al
je 0x51f3
movq %r14, -0x30(%rbp)
leaq 0x21ff(%rip), %rsi      # 0x7389
xorl %r13d, %r13d
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
callq 0x4bbb
movq %rax, %r14
movq %r14, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x42cc
movl (%r14), %edx
testl %edx, %edx
je 0x5329
cmpl $0x1, %edx
je 0x52bf
cmpl $0x2, %edx
jne 0x5338
testq %r13, %r13
je 0x51dd
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x34aa
movq %r14, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4bd6
leaq 0x21b3(%rip), %rsi      # 0x73ad
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movl %r12d, %edi
movq %r15, %rsi
callq 0x4652
testq %rax, %rax
je 0x529e
movq %rax, %r13
movq %r14, -0x30(%rbp)
movq %r14, %rdi
movq %rax, %rsi
callq 0x3384
movq %rax, %r14
testq %rax, %rax
jne 0x525c
callq 0x4bbb
movq %rax, %r14
leaq 0x18(%rax), %rdi
callq 0x2290
movups (%rbx), %xmm0
movups %xmm0, 0x8(%r14)
movq -0x30(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x33f0
movdqu 0x8(%r14), %xmm0
movdqu (%rbx), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x51a1
movq 0x20(%r14), %rdx
leaq 0x2171(%rip), %rsi      # 0x73f5
movl $0x3, %edi
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4100
leaq 0x2120(%rip), %rsi      # 0x73c5
movl $0x1, %edi
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4100
leaq 0x2198(%rip), %rsi      # 0x745e
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movl $0x18, %edi
callq 0x23c0
movq %rax, %rbx
movq -0x38(%rbp), %rax
movq %rax, (%rbx)
movq %r14, 0x8(%rbx)
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rbx)
leaq -0x78(%rbp), %r14
movq %r14, %rdi
callq 0x2360
movq %r14, %rdi
movl $0x1, %esi
callq 0x23a0
leaq 0x47(%rip), %rdx        # 0x5359
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0x24a0
movq %r14, %rdi
callq 0x2450
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2143(%rip), %rsi      # 0x7482
movl $0x4, %edi
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4100