// File: 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
movq %rsp, %rbp
subq $0x9c0, %rsp            # imm = 0x9C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x7e9, -0x14(%rbp)     # imm = 0x7E9
movl $0x64, -0x18(%rbp)
movl $0x1, -0x674(%rbp)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20a0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x2684
movl -0x1c(%rbp), %edi
callq 0x2430
movq 0x8993(%rip), %rdi      # 0xaff0
leaq 0x49a0(%rip), %rsi      # 0x7004
callq 0x2260
movq %rax, %rdi
movq 0x894d(%rip), %rsi      # 0xafc0
callq 0x22a0
movl $0x0, -0x4(%rbp)
jmp 0x2948
movl -0x1c(%rbp), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0x674(%rbp), %rcx
movl $0x4, %r8d
callq 0x23f0
cmpl $-0x1, %eax
jne 0x26d6
movq 0x8941(%rip), %rdi      # 0xaff0
leaq 0x4965(%rip), %rsi      # 0x701b
callq 0x2260
movq %rax, %rdi
movq 0x88fb(%rip), %rsi      # 0xafc0
callq 0x22a0
movl $0x0, -0x4(%rbp)
jmp 0x2948
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2170
movw $0x2, -0x2c(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x7e9, %edi            # imm = 0x7E9
callq 0x2050
movw %ax, -0x2a(%rbp)
movl -0x1c(%rbp), %edi
leaq -0x2c(%rbp), %rsi
movl $0x10, %edx
callq 0x2060
cmpl $-0x1, %eax
jne 0x2752
movq 0x88d2(%rip), %rdi      # 0xaff0
leaq 0x4901(%rip), %rsi      # 0x7026
callq 0x2260
movq %rax, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x23b0
movq %rax, %rdi
movq 0x887f(%rip), %rsi      # 0xafc0
callq 0x22a0
movl $0x0, -0x4(%rbp)
jmp 0x2948
movl -0x1c(%rbp), %edi
movl $0x64, %esi
callq 0x2300
cmpl $-0x1, %eax
jne 0x2792
movq 0x8885(%rip), %rdi      # 0xaff0
leaq 0x48c9(%rip), %rsi      # 0x703b
callq 0x2260
movq %rax, %rdi
movq 0x883f(%rip), %rsi      # 0xafc0
callq 0x22a0
movl $0x0, -0x4(%rbp)
jmp 0x2948
movq 0x8837(%rip), %rdi      # 0xafd0
leaq 0x48a9(%rip), %rsi      # 0x7049
callq 0x2260
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x23b0
movq %rax, %rdi
leaq 0x48a4(%rip), %rsi      # 0x705e
callq 0x2260
movq %rax, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x23b0
movq %rax, %rdi
leaq 0x4892(%rip), %rsi      # 0x7068
callq 0x2260
movq %rax, %rdi
movq 0x87db(%rip), %rsi      # 0xafc0
callq 0x22a0
movl $0x0, -0x9a4(%rbp)
cmpl $0x64, -0x9a4(%rbp)
jge 0x2941
movl $0x10, -0x9a8(%rbp)
movslq -0x9a4(%rbp), %rax
leaq -0x670(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2170
movl -0x1c(%rbp), %edi
movslq -0x9a4(%rbp), %rax
leaq -0x670(%rbp), %rsi
shlq $0x4, %rax
addq %rax, %rsi
leaq -0x9a8(%rbp), %rdx
callq 0x20d0
movl %eax, -0x678(%rbp)
cmpl $-0x1, -0x678(%rbp)
jne 0x2883
movq 0x878a(%rip), %rdi      # 0xaff0
leaq 0x47ff(%rip), %rsi      # 0x706c
callq 0x2260
movq %rax, %rdi
movq 0x8744(%rip), %rsi      # 0xafc0
callq 0x22a0
jmp 0x2901
movq 0x8746(%rip), %rdi      # 0xafd0
leaq 0x47f4(%rip), %rsi      # 0x7085
callq 0x2260
movq %rax, -0x9b8(%rbp)
movslq -0x9a4(%rbp), %rcx
leaq -0x670(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x9ac(%rbp)
movl -0x9ac(%rbp), %edi
callq 0x2140
movq -0x9b8(%rbp), %rdi
movq %rax, %rsi
callq 0x2260
movq %rax, %rdi
leaq 0x47c1(%rip), %rsi      # 0x70a0
callq 0x2260
movq %rax, %rdi
movl -0x678(%rbp), %esi
callq 0x23b0
movq %rax, %rdi
movq 0x86c4(%rip), %rsi      # 0xafc0
callq 0x22a0
movslq -0x9a4(%rbp), %rax
leaq -0x9a0(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x3f(%rip), %rdx        # 0x2960
leaq -0x678(%rbp), %rcx
callq 0x2410
movl -0x9a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9a4(%rbp)
jmp 0x27f4
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x9c0, %rsp            # imm = 0x9C0
popq %rbp
retq
nopw %cs:(%rax,%rax)