// File: /workspace/github_c_makefile_star2_O1/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 0x40f4
movl $0x1, -0x30(%rbp)
movl $0x80, -0x2c(%rbp)
movl $0x10, %edi
callq 0x322c
movq %rax, %r15
leaq -0xe8(%rbp), %rbx
movq %rbx, %rdi
callq 0x2360
movl $0x1, %r12d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x23a0
leaq 0x2ec(%rip), %rdx       # 0x5057
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r15, -0x40(%rbp)
movq %r15, %rcx
callq 0x24a0
movq %rbx, %rdi
callq 0x2450
leaq 0x2473(%rip), %rsi      # 0x7200
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
xorl %eax, %eax
callq 0x3206
leaq 0x246e(%rip), %rsi      # 0x7215
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movq 0x8(%r14), %rbx
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x20f0
testq %rax, %rax
leaq 0x1391(%rip), %rax      # 0x615f
leaq 0x2474(%rip), %rdx      # 0x7249
cmoveq %rax, %rdx
leaq 0x21b2(%rip), %r8       # 0x6f92
cmoveq %rax, %r8
movq (%r14), %r9
leaq 0x243b(%rip), %rsi      # 0x7229
movl $0x2, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4100
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 0x240e(%rip), %rsi      # 0x724b
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movq (%rbx), %rax
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x2090
cmpl $-0x1, %eax
je 0x4f52
movl %eax, %r15d
leaq 0x2406(%rip), %rsi      # 0x7273
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
leaq -0x30(%rbp), %rcx
movl %r15d, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x2480
testl %eax, %eax
je 0x4eb3
callq 0x2040
movl (%rax), %edx
leaq 0x23db(%rip), %rsi      # 0x7282
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
movq -0x38(%rbp), %rax
cmpl $0xa, 0x4(%rax)
jne 0x4f14
movq -0x48(%rbp), %rax
cmpb $0x1, 0x2c(%rax)
jne 0x4f14
leaq 0x23e9(%rip), %rsi      # 0x72b7
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
leaq -0x30(%rbp), %rcx
movl %r15d, %edi
movl $0x29, %esi
movl $0x1a, %edx
movl $0x4, %r8d
callq 0x2480
testl %eax, %eax
je 0x4f14
callq 0x2040
movl (%rax), %edx
leaq 0x23d1(%rip), %rsi      # 0x72d9
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
leaq 0x23f2(%rip), %rsi      # 0x730d
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
movl %r15d, %edi
callq 0x2070
movl %eax, %ebx
movq -0x38(%rbp), %rdi
callq 0x2390
testl %ebx, %ebx
je 0x4f7a
leaq 0x23d0(%rip), %rsi      # 0x7320
jmp 0x4f59
leaq 0x2302(%rip), %rsi      # 0x725b
movl $0x5, %edi
xorl %eax, %eax
callq 0x4100
movl %r12d, %eax
addq $0x928, %rsp            # imm = 0x928
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x23b9(%rip), %rsi      # 0x733a
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
leaq -0x940(%rbp), %r12
leaq -0x168(%rbp), %r13
leaq -0x80(%rbp), %r14
movl $0x7d0, %edx            # imm = 0x7D0
movl %r15d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
movq %r13, %r8
leaq -0x2c(%rbp), %r9
callq 0x2240
movq %rax, %rbx
testl %ebx, %ebx
je 0x5046
cmpl $-0x1, %ebx
je 0x501f
movl -0x2c(%rbp), %esi
movl $0x1, (%rsp)
movq %r13, %rdi
movq %r14, %rdx
movl $0x2e, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x21a0
movl $0x1, %edi
leaq 0x2371(%rip), %rsi      # 0x7365
movl %ebx, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x4100
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r13, %rdx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x514f
movl $0x80, -0x2c(%rbp)
jmp 0x5032
movl $0x5, %edi
leaq 0x231f(%rip), %rsi      # 0x734a
xorl %eax, %eax
callq 0x4100
cmpl $-0x1, %ebx
jne 0x4f9f
movl $0x1, %r12d
jmp 0x4f65
movq -0x40(%rbp), %rdi
callq 0x3607
xorl %r12d, %r12d
jmp 0x4f65