// File: /workspace/github_cpp_makefile_star2_O2/AraKhachatryan[P]messenger/server.cpp
// Function: main
int main(int argc, char* argv[])
{
    const int port_number = 2025;
    const int max_clients = 100;

    int master_socket_fd;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr[max_clients];
    const int opt = 1;

    int client_socket_fd;
    pthread_t thread[max_clients];

    // create socket
    master_socket_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    if( master_socket_fd == -1)
    {
        close(master_socket_fd);
        std::cerr << "Cannot open tcp socket" << std::endl;
        return 0;
    }

    // Forcefully attaching socket to the same port again after restart
    if( setsockopt(master_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, (const void *)&opt, (socklen_t)sizeof(opt)) == -1 )
    {
        std::cerr << "setsockopt" << std::endl;
        return 0;
    }

    bzero((void *)&server_addr, sizeof(server_addr));

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(port_number);

    // bind socket
    if( bind(master_socket_fd, (struct sockaddr *)&server_addr, (socklen_t)sizeof(server_addr)) == -1 )
    {
        std::cerr << "Cannot bind to port " << port_number << std::endl;
        return 0;
    }


    if ( listen(master_socket_fd, max_clients) == -1 )
    {
        std::cerr << "Cannot listen" << std::endl;
        return 0;
    }

    std::cout << "Listening socket_fd " << master_socket_fd << " on port " << port_number << "..." << std::endl;

    for ( int i = 0; i < max_clients; ++i )
    {
        socklen_t client_addr_len = sizeof(client_addr[i]);
        bzero((void *)&client_addr[i], sizeof(client_addr[i]));

        // Accept incoming connections
        client_socket_fd = accept(master_socket_fd, (struct sockaddr *)&client_addr[i], &client_addr_len);
        if ( client_socket_fd == -1 )
        {
            std::cerr << "Cannot accept connection" << std::endl;
        }
        else
        {
            std::cout << "Incoming connection from: " << inet_ntoa(client_addr[i].sin_addr) << " on socket_fd: " << client_socket_fd << std::endl;
        }

        pthread_create( &thread[i], NULL, client_thread, (void*)&client_socket_fd );
    }

    return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x988, %rsp            # imm = 0x988
movl $0x1, 0xc(%rsp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x2070
cmpl $-0x1, %eax
je 0x2687
movl %eax, %ebx
pushq $0x1
popq %rsi
pushq $0xf
popq %rdx
leaq 0xc(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x22e0
cmpl $-0x1, %eax
je 0x269f
leaq 0x10(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movl $0xe9070002, %eax       # imm = 0xE9070002
movq %rax, (%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x2050
cmpl $-0x1, %eax
je 0x26af
pushq $0x64
popq %rsi
movl %ebx, %edi
callq 0x2230
cmpl $-0x1, %eax
je 0x26d1
movq 0x3a64(%rip), %rdi      # 0x5fd0
leaq 0x1af2(%rip), %rsi      # 0x4065
callq 0x21f0
movq %rax, %rdi
movl %ebx, %esi
callq 0x22b0
leaq 0x1af1(%rip), %rsi      # 0x407a
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x22b0
leaq 0x1adf(%rip), %rsi      # 0x4084
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2120
leaq 0x20(%rsp), %r15
xorl %r12d, %r12d
leaq 0x13c(%rip), %r13       # 0x2700
leaq 0x4(%rsp), %rbp
cmpq $0x640, %r12            # imm = 0x640
je 0x26ec
movl $0x10, 0x8(%rsp)
leaq (%rsp,%r12), %rsi
addq $0x340, %rsi            # imm = 0x340
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
movl %ebx, %edi
leaq 0x8(%rsp), %rdx
callq 0x20a0
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x264f
movq 0x39c5(%rip), %rdi      # 0x5fd0
leaq 0x1a8f(%rip), %rsi      # 0x40a1
callq 0x21f0
movq %rax, %r14
movl 0x344(%rsp,%r12), %edi
callq 0x2100
movq %r14, %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, %rdi
leaq 0x1a80(%rip), %rsi      # 0x40bc
callq 0x21f0
movl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x22b0
jmp 0x2662
movq 0x399a(%rip), %rdi      # 0x5ff0
leaq 0x1a2b(%rip), %rsi      # 0x4088
callq 0x21f0
movq %rax, %rdi
callq 0x2120
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x2300
addq $0x10, %r12
addq $0x8, %r15
jmp 0x25c9
pushq $-0x1
popq %rdi
callq 0x2330
movq 0x395a(%rip), %rdi      # 0x5ff0
leaq 0x1983(%rip), %rsi      # 0x4020
jmp 0x26df
movq 0x394a(%rip), %rdi      # 0x5ff0
leaq 0x198a(%rip), %rsi      # 0x4037
jmp 0x26df
movq 0x393a(%rip), %rdi      # 0x5ff0
leaq 0x1985(%rip), %rsi      # 0x4042
callq 0x21f0
movq %rax, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x22b0
jmp 0x26e4
movq 0x3918(%rip), %rdi      # 0x5ff0
leaq 0x1978(%rip), %rsi      # 0x4057
callq 0x21f0
movq %rax, %rdi
callq 0x2120
xorl %eax, %eax
addq $0x988, %rsp            # imm = 0x988
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq