// File: /workspace/github_c_makefile_star2_O3/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 0x3f98
movl $0x1, -0x30(%rbp)
movl $0x80, -0x2c(%rbp)
movl $0x10, %edi
callq 0x30c4
movq %rax, %r15
leaq -0xe8(%rbp), %rbx
movq %rbx, %rdi
callq 0x2360
movl $0x1, %r13d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x23a0
leaq 0x30d(%rip), %rdx       # 0x4e34
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r15, -0x40(%rbp)
movq %r15, %rcx
callq 0x24a0
movq %rbx, %rdi
callq 0x2450
leaq 0x26b7(%rip), %rsi      # 0x7200
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
xorl %eax, %eax
callq 0x309c
leaq 0x26b2(%rip), %rsi      # 0x7215
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
movq 0x8(%r14), %rbx
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x20f0
testq %rax, %rax
leaq 0x15d5(%rip), %rax      # 0x615f
leaq 0x26b8(%rip), %rdx      # 0x7249
cmoveq %rax, %rdx
leaq 0x23f6(%rip), %r8       # 0x6f92
cmoveq %rax, %r8
movq (%r14), %r9
leaq 0x267f(%rip), %rsi      # 0x7229
movl $0x2, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3fa4
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 0x2652(%rip), %rsi      # 0x724b
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
movq (%rbx), %rax
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x2090
cmpl $-0x1, %eax
je 0x4d0e
movl %eax, %r12d
leaq 0x264a(%rip), %rsi      # 0x7273
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
leaq -0x30(%rbp), %rcx
movl %r12d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2480
testl %eax, %eax
je 0x4c6f
callq 0x2040
movl (%rax), %edx
leaq 0x261f(%rip), %rsi      # 0x7282
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
movq -0x38(%rbp), %rax
cmpl $0xa, 0x4(%rax)
jne 0x4cd0
movq -0x48(%rbp), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x4cd0
leaq 0x262d(%rip), %rsi      # 0x72b7
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
leaq -0x30(%rbp), %rcx
movl %r12d, %edi
movl $0x29, %esi
movl $0x1a, %edx
movl $0x4, %r8d
callq 0x2480
testl %eax, %eax
je 0x4cd0
callq 0x2040
movl (%rax), %edx
leaq 0x2615(%rip), %rsi      # 0x72d9
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
leaq 0x2636(%rip), %rsi      # 0x730d
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
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 0x4d36
leaq 0x2614(%rip), %rsi      # 0x7320
jmp 0x4d15
leaq 0x2546(%rip), %rsi      # 0x725b
movl $0x5, %edi
xorl %eax, %eax
callq 0x3fa4
movl %r13d, %eax
addq $0x928, %rsp            # imm = 0x928
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25fd(%rip), %rsi      # 0x733a
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
leaq -0x940(%rbp), %rsi
leaq -0x168(%rbp), %r8
leaq -0x2c(%rbp), %r9
movl $0x7d0, %edx            # imm = 0x7D0
movl %r12d, %edi
xorl %ecx, %ecx
callq 0x2240
movq %rax, %rbx
testl %ebx, %ebx
je 0x4e05
leaq -0x168(%rbp), %r13
leaq -0x80(%rbp), %r15
leaq -0x940(%rbp), %r14
cmpl $-0x1, %ebx
je 0x4e16
movl -0x2c(%rbp), %esi
movl $0x1, (%rsp)
movq %r13, %rdi
movq %r15, %rdx
movl $0x2e, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x21a0
movl $0x1, %edi
leaq 0x25a9(%rip), %rsi      # 0x7365
movl %ebx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x3fa4
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r13, %rdx
movq %r14, %rcx
movl %ebx, %r8d
callq 0x4f2e
movl $0x80, -0x2c(%rbp)
movl $0x7d0, %edx            # imm = 0x7D0
movl %r12d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
movq %r13, %r8
leaq -0x2c(%rbp), %r9
callq 0x2240
movq %rax, %rbx
testl %ebx, %ebx
jne 0x4d87
movq -0x40(%rbp), %rdi
callq 0x349f
xorl %r13d, %r13d
jmp 0x4d21
leaq 0x252d(%rip), %rsi      # 0x734a
movl $0x5, %edi
xorl %eax, %eax
callq 0x3fa4
movl $0x1, %r13d
jmp 0x4d21