// File: /workspace/github_c_makefile_star2_O0/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
subq $0x970, %rsp            # imm = 0x970
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x28(%rax), %edi
callq 0x4d50
movl $0x1, -0x1c(%rbp)
movl $0x4, -0x20(%rbp)
movl $0x80, -0x8bc(%rbp)
movl $0x10, %edi
callq 0x34a0
movq %rax, -0x8c8(%rbp)
leaq -0x908(%rbp), %rdi
callq 0x2370
leaq -0x908(%rbp), %rdi
movl $0x1, %esi
callq 0x23c0
movq -0x8c8(%rbp), %rcx
leaq -0x8d0(%rbp), %rdi
leaq -0x908(%rbp), %rsi
leaq 0x3a3(%rip), %rdx       # 0x6590
callq 0x24c0
leaq -0x908(%rbp), %rdi
callq 0x2470
movl $0x1, %edi
leaq 0x2026(%rip), %rsi      # 0x8230
movb $0x0, %al
callq 0x4d70
movb $0x0, %al
callq 0x3470
movl $0x1, %edi
leaq 0x2021(%rip), %rsi      # 0x8245
movb $0x0, %al
callq 0x4d70
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x3a, %esi
callq 0x2100
movq %rax, %rdx
leaq 0xf45(%rip), %rax       # 0x718c
leaq 0x202b(%rip), %rcx      # 0x8279
cmpq $0x0, %rdx
cmovneq %rcx, %rax
movq %rax, -0x960(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x958(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x3a, %esi
callq 0x2100
movq -0x960(%rbp), %rdx
movq -0x958(%rbp), %rcx
movq %rax, %rsi
leaq 0xef6(%rip), %r8        # 0x718c
leaq 0x1d29(%rip), %rax      # 0x7fc6
cmpq $0x0, %rsi
cmovneq %rax, %r8
movq -0x10(%rbp), %rax
movq (%rax), %r9
movl $0x2, %edi
leaq 0x1fa1(%rip), %rsi      # 0x8259
movb $0x0, %al
callq 0x4d70
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x21a0
movl $0x0, -0x4c(%rbp)
movl $0x2, -0x48(%rbp)
movl $0x405, -0x50(%rbp)     # imm = 0x405
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x58(%rbp), %rcx
callq 0x2380
movl $0x1, %edi
leaq 0x1f6f(%rip), %rsi      # 0x827b
movb $0x0, %al
callq 0x4d70
movq -0x58(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x20a0
movl %eax, -0x18(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x6355
movl $0x5, %edi
leaq 0x1f49(%rip), %rsi      # 0x828b
movb $0x0, %al
callq 0x4d70
movl $0x1, -0x4(%rbp)
jmp 0x6584
movl $0x1, %edi
leaq 0x1f42(%rip), %rsi      # 0x82a3
movb $0x0, %al
callq 0x4d70
movl -0x18(%rbp), %edi
movl -0x20(%rbp), %r8d
movl $0x1, %esi
movl $0x2, %edx
leaq -0x1c(%rbp), %rcx
callq 0x24a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x63a5
callq 0x2040
movl (%rax), %edx
movl $0x3, %edi
leaq 0x1f14(%rip), %rsi      # 0x82b2
movb $0x0, %al
callq 0x4d70
movq -0x58(%rbp), %rax
cmpl $0xa, 0x4(%rax)
jne 0x640b
movq -0x10(%rbp), %rax
testb $0x1, 0x2c(%rax)
je 0x640b
movl $0x1, %edi
leaq 0x1f22(%rip), %rsi      # 0x82e7
movb $0x0, %al
callq 0x4d70
movl -0x18(%rbp), %edi
movl -0x20(%rbp), %r8d
movl $0x29, %esi
movl $0x1a, %edx
leaq -0x1c(%rbp), %rcx
callq 0x24a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x6409
callq 0x2040
movl (%rax), %edx
movl $0x3, %edi
leaq 0x1f07(%rip), %rsi      # 0x8309
movb $0x0, %al
callq 0x4d70
jmp 0x640b
movl $0x1, %edi
leaq 0x1f26(%rip), %rsi      # 0x833d
movb $0x0, %al
callq 0x4d70
movl -0x18(%rbp), %edi
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x910(%rbp)
movq -0x58(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x910(%rbp), %rsi
callq 0x2070
movl %eax, -0x14(%rbp)
movq -0x58(%rbp), %rdi
callq 0x23b0
cmpl $0x0, -0x14(%rbp)
je 0x6474
movl $0x5, %edi
leaq 0x1eef(%rip), %rsi      # 0x8350
movb $0x0, %al
callq 0x4d70
movl $0x1, -0x4(%rbp)
jmp 0x6584
movl $0x1, %edi
leaq 0x1eea(%rip), %rsi      # 0x836a
movb $0x0, %al
callq 0x4d70
movl -0x18(%rbp), %edi
leaq -0x830(%rbp), %rsi
leaq -0x8b8(%rbp), %rax
movq %rax, -0x918(%rbp)
movq -0x918(%rbp), %r8
movl $0x7d0, %edx            # imm = 0x7D0
xorl %ecx, %ecx
leaq -0x8bc(%rbp), %r9
callq 0x2250
movl %eax, -0x834(%rbp)
cmpl $0x0, %eax
je 0x6571
cmpl $-0x1, -0x834(%rbp)
jne 0x64f0
movl $0x5, %edi
leaq 0x1e9d(%rip), %rsi      # 0x837a
movb $0x0, %al
callq 0x4d70
movl $0x1, -0x4(%rbp)
jmp 0x6584
movl -0x8bc(%rbp), %esi
leaq -0x950(%rbp), %rdx
leaq -0x8b8(%rbp), %rdi
movl $0x2e, %ecx
xorl %eax, %eax
movl %eax, %r8d
xorl %r9d, %r9d
movl $0x1, (%rsp)
callq 0x21b0
movl -0x834(%rbp), %edx
leaq -0x950(%rbp), %rcx
movl $0x1, %edi
leaq 0x1e5f(%rip), %rsi      # 0x8395
movb $0x0, %al
callq 0x4d70
movq -0x10(%rbp), %rdi
movq -0x8c8(%rbp), %rsi
leaq -0x830(%rbp), %rcx
movl -0x834(%rbp), %r8d
leaq -0x8b8(%rbp), %rdx
callq 0x66d0
movl $0x80, -0x8bc(%rbp)
jmp 0x6487
movq -0x8c8(%rbp), %rdi
callq 0x3b90
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x970, %rsp            # imm = 0x970
popq %rbp
retq