// File: /workspace/github_c_makefile_star2_O2/TimeToogo[P]ff-proxy/src/server.c
// Function: ff_proxy_start
int ff_proxy_start(struct ff_config *config)
{
    ff_set_logging_level(config->logging_level);

    int err;
    int sockfd;
    int optval = 1;
    socklen_t optlen = sizeof(optval);
    struct addrinfo hints;
    struct addrinfo *res;
    char buffer[FF_PROXY_BUFF_SIZE];
    int recv_len;
    struct sockaddr_storage src_address;
    socklen_t src_address_length = sizeof(src_address);
    struct ff_hash_table *requests = ff_hash_table_init(16);

    pthread_t cleanup_thread;
    pthread_attr_t cleanup_thread_attrs;
    pthread_attr_init(&cleanup_thread_attrs);
    pthread_attr_setdetachstate(&cleanup_thread_attrs, PTHREAD_CREATE_DETACHED);
    pthread_create(&cleanup_thread, &cleanup_thread_attrs, (void *)ff_proxy_clean_up_old_requests_loop, (void *)requests);
    pthread_attr_destroy(&cleanup_thread_attrs);

    ff_log(FF_DEBUG, "Initialising OpenSSL");
    ff_init_openssl();
    ff_log(FF_DEBUG, "Initialised OpenSSL");

    ff_log(FF_INFO, "Starting UDP proxy on %s%s%s:%s",
           strchr(config->ip_address, ':') ? "[" : "", config->ip_address,
           strchr(config->ip_address, ':') ? "]" : "", config->port);

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_PASSIVE;
    getaddrinfo(config->ip_address, config->port, &hints, &res);

    ff_log(FF_DEBUG, "Creating socket");
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd == -1)
    {
        ff_log(FF_FATAL, "Failed to create socket");
        return EXIT_FAILURE;
    }

    ff_log(FF_DEBUG, "Created socket");

    err = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, optlen);
    if (err)
    {
        ff_log(FF_WARNING, "Failed to set socket option SO_REUSEADDR (errno: %d)", errno);
    }

    if (res->ai_family == AF_INET6 && config->ipv6_v6only)
    {
        ff_log(FF_DEBUG, "Setting IPV6_V6ONLY socket option");
        err = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, optlen);
        if (err)
        {
            ff_log(FF_WARNING, "Failed to set socket option IPV6_V6ONLY (errno: %d)", errno);
        }
    }

    ff_log(FF_DEBUG, "Binding to address");
    err = bind(sockfd, res->ai_addr, res->ai_addrlen);
    freeaddrinfo(res);

    if (err)
    {
        ff_log(FF_FATAL, "Failed to bind to address");
        return EXIT_FAILURE;
    }

    ff_log(FF_DEBUG, "Bound to socket");

    // A single packet at a time
    while ((recv_len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&src_address, &src_address_length)))
    {
        char ip_string[INET6_ADDRSTRLEN];

        if (recv_len == -1)
        {
            ff_log(FF_FATAL, "Failed to read from socket");
            return EXIT_FAILURE;
        }

        getnameinfo((struct sockaddr *)&src_address, src_address_length, ip_string, sizeof(ip_string), NULL, 0, NI_NUMERICHOST);
        ff_log(FF_DEBUG, "Received packet of %d bytes from %s", recv_len, ip_string);

        ff_proxy_process_incoming_packet(config, requests, (struct sockaddr *)&src_address, buffer, recv_len);

        /* need to reset for subsequent recvfrom()'s */
        src_address_length = sizeof(src_address);
    }

    ff_hash_table_free(requests);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x928, %rsp            # imm = 0x928
movq %rdi, %r14
movl 0x28(%rdi), %edi
callq 0x3da8
movl $0x1, -0x30(%rbp)
movl $0x80, -0x2c(%rbp)
pushq $0x10
popq %rdi
callq 0x2fe8
movq %rax, %r12
leaq -0xe8(%rbp), %rbx
movq %rbx, %rdi
callq 0x2360
pushq $0x1
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x23a0
leaq 0x2c0(%rip), %rdx       # 0x4b41
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r12, -0x40(%rbp)
movq %r12, %rcx
callq 0x24a0
movq %rbx, %rdi
callq 0x2450
leaq 0x195d(%rip), %rsi      # 0x6200
movl %r15d, %edi
xorl %eax, %eax
callq 0x3db4
xorl %eax, %eax
callq 0x2fc3
leaq 0x195a(%rip), %rsi      # 0x6215
movl %r15d, %edi
xorl %eax, %eax
callq 0x3db4
movq 0x8(%r14), %rbx
pushq $0x3a
popq %rsi
movq %rbx, %rdi
callq 0x20f0
testq %rax, %rax
leaq 0x881(%rip), %rax       # 0x515f
leaq 0x1964(%rip), %rdx      # 0x6249
cmoveq %rax, %rdx
leaq 0x16a2(%rip), %r8       # 0x5f92
cmoveq %rax, %r8
movq (%r14), %r9
leaq 0x192b(%rip), %rsi      # 0x6229
pushq $0x2
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3db4
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
movl $0x405, (%rdx)          # imm = 0x405
movq (%r14), %rsi
movq %r14, -0x48(%rbp)
movq 0x8(%r14), %rdi
leaq -0x38(%rbp), %rbx
movq %rbx, %rcx
callq 0x2370
leaq 0x1900(%rip), %rsi      # 0x624b
movl %r15d, %edi
xorl %eax, %eax
callq 0x3db4
movq (%rbx), %rax
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x2090
cmpl $-0x1, %eax
je 0x4a49
movl %eax, %r12d
leaq 0x18fa(%rip), %rsi      # 0x6273
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x3db4
pushq $0x2
popq %rdx
leaq -0x30(%rbp), %rcx
pushq $0x4
popq %r8
movl %r12d, %edi
movl %ebx, %esi
callq 0x2480
testl %eax, %eax
je 0x49b6
callq 0x2040
movl (%rax), %edx
leaq 0x18d6(%rip), %rsi      # 0x6282
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
movq -0x38(%rbp), %rax
cmpl $0xa, 0x4(%rax)
jne 0x4a0d
movq -0x48(%rbp), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x4a0d
leaq 0x18e6(%rip), %rsi      # 0x62b7
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3db4
pushq $0x29
popq %rsi
pushq $0x1a
popq %rdx
leaq -0x30(%rbp), %rcx
pushq $0x4
popq %r8
movl %r12d, %edi
callq 0x2480
testl %eax, %eax
je 0x4a0d
callq 0x2040
movl (%rax), %edx
leaq 0x18d6(%rip), %rsi      # 0x62d9
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
leaq 0x18f9(%rip), %rsi      # 0x630d
movl %r15d, %edi
xorl %eax, %eax
callq 0x3db4
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %r12d, %edi
callq 0x2070
movl %eax, %ebx
movq -0x38(%rbp), %rdi
callq 0x2390
testl %ebx, %ebx
je 0x4a6f
leaq 0x18d9(%rip), %rsi      # 0x6320
jmp 0x4a50
leaq 0x180b(%rip), %rsi      # 0x625b
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x3db4
movl %r15d, %eax
addq $0x928, %rsp            # imm = 0x928
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18c4(%rip), %rsi      # 0x633a
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3db4
leaq -0x940(%rbp), %r13
leaq -0x168(%rbp), %r14
leaq -0x80(%rbp), %rbx
movl $0x7d0, %edx            # imm = 0x7D0
movl %r12d, %r15d
movl %r12d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
movq %r14, %r8
leaq -0x2c(%rbp), %r9
callq 0x2240
movq %rax, %r12
testl %r12d, %r12d
je 0x4b16
cmpl $-0x1, %r12d
je 0x4b27
movl -0x2c(%rbp), %esi
movl $0x1, (%rsp)
movq %r14, %rdi
movq %rbx, %rdx
pushq $0x2e
popq %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x21a0
pushq $0x1
popq %rdi
leaq 0x1881(%rip), %rsi      # 0x6365
movl %r12d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3db4
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movl %r12d, %r8d
callq 0x4c24
movl $0x80, -0x2c(%rbp)
movl %r15d, %r12d
jmp 0x4a92
movq -0x40(%rbp), %rdi
callq 0x3374
xorl %r15d, %r15d
jmp 0x4a5a
leaq 0x181c(%rip), %rsi      # 0x634a
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x3db4
pushq $0x1
popq %r15
jmp 0x4a5a