// File: /workspace/github_cpp_makefile_star2_O1/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)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2080
cmpl $-0x1, %eax
je 0x2735
movl %eax, %ebx
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x22f0
cmpl $-0x1, %eax
je 0x2754
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movl $0xe9070002, %eax       # imm = 0xE9070002
movq %rax, (%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x2060
cmpl $-0x1, %eax
je 0x2769
movl %ebx, %edi
movl $0x64, %esi
callq 0x2240
cmpl $-0x1, %eax
je 0x2796
movq 0x3a43(%rip), %r14      # 0x5fd0
leaq 0x1ad1(%rip), %rsi      # 0x4065
movl $0x14, %edx
movq %r14, %rdi
callq 0x2250
movq %r14, %rdi
movl %ebx, %esi
callq 0x22c0
movq %rax, %r15
leaq 0x1ac5(%rip), %rsi      # 0x407a
movl $0x9, %edx
movq %rax, %rdi
callq 0x2250
movq %r15, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x22c0
movq %rax, %r15
leaq 0x1aab(%rip), %rsi      # 0x4084
movl $0x3, %edx
movq %rax, %rdi
callq 0x2250
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq 0x20(%rsp), %r15
xorl %ebp, %ebp
leaq 0x4(%rsp), %r13
movl $0x10, 0x8(%rsp)
leaq (%rsp,%rbp), %rsi
addq $0x340, %rsi            # imm = 0x340
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
movl %ebx, %edi
leaq 0x8(%rsp), %rdx
callq 0x20c0
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0x2687
movl $0x1a, %edx
movq %r14, %rdi
leaq 0x1a4b(%rip), %rsi      # 0x40a1
callq 0x2250
movl 0x344(%rsp,%rbp), %edi
callq 0x2120
testq %rax, %rax
je 0x26a4
movq %rax, %r12
movq %rax, %rdi
callq 0x2110
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
jmp 0x26bc
movl $0x18, %edx
movq 0x395d(%rip), %r12      # 0x5ff0
movq %r12, %rdi
leaq 0x19eb(%rip), %rsi      # 0x4088
callq 0x2250
jmp 0x26df
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22b0
movl $0xf, %edx
movq %r14, %rdi
leaq 0x19f1(%rip), %rsi      # 0x40bc
callq 0x2250
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0x22c0
movq %rax, %r12
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movq %r15, %rdi
xorl %esi, %esi
leaq 0xd9(%rip), %rdx        # 0x27ec
movq %r13, %rcx
callq 0x2310
addq $0x8, %r15
addq $0x10, %rbp
cmpq $0x640, %rbp            # imm = 0x640
jne 0x2619
jmp 0x27d8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2330
movq 0x38aa(%rip), %rbx      # 0x5ff0
leaq 0x18d3(%rip), %rsi      # 0x4020
movl $0x16, %edx
jmp 0x27a9
movq 0x3895(%rip), %rbx      # 0x5ff0
leaq 0x18d5(%rip), %rsi      # 0x4037
movl $0xa, %edx
jmp 0x27a9
movq 0x3880(%rip), %rbx      # 0x5ff0
leaq 0x18cb(%rip), %rsi      # 0x4042
movl $0x14, %edx
movq %rbx, %rdi
callq 0x2250
movq %rbx, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x22c0
movq %rax, %rbx
jmp 0x27b1
movq 0x3853(%rip), %rbx      # 0x5ff0
leaq 0x18b3(%rip), %rsi      # 0x4057
movl $0xd, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
xorl %eax, %eax
addq $0x988, %rsp            # imm = 0x988
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq