// File: davidcairuz[P]network-internet-relay-chat/server.cpp
// Function: main
int main(int argc, char* argv[]) {
    socket_server->Bind();
    socket_server->Check();

    cout << "Chat Initialized =D\n";

    socket_server->Listen();
    socket_server->Check();

    pthread_t tid;

    while (1) {
        string ip;
        int new_client;
        tie(new_client, ip) = socket_server->Accept();
        socket_server->Check();
        
        if (active->clients.size() == Socket::max_clients) {
            socket_server->Write("Too many people here, you are not welcome\n");
            continue;
        } 

        string nickname = socket_server->Read(new_client);
        
        cout << "New client: " << nickname << endl;
        active->insert(new_client, nickname, ip);

        if (pthread_create(&tid, NULL, server_thread, &new_client) != 0)
            cout << "Failed to create thread" << endl;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0xdd40(%rip), %rdi      # 0x152c0
callq 0x4050
movq 0xdd34(%rip), %rdi      # 0x152c0
callq 0x4770
movq 0xda40(%rip), %rdi      # 0x14fd8
leaq 0x6dc7(%rip), %rsi      # 0xe366
callq 0x32b0
movq 0xdd15(%rip), %rdi      # 0x152c0
callq 0x4150
movq 0xdd09(%rip), %rdi      # 0x152c0
callq 0x4770
leaq -0x38(%rbp), %rdi
callq 0x33d0
movq 0xdcf4(%rip), %rsi      # 0x152c0
leaq -0x68(%rbp), %rdi
callq 0x4290
jmp 0x75d7
leaq -0x88(%rbp), %rdi
movq %rdi, -0x120(%rbp)
leaq -0x3c(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x87e0
movq -0x120(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movq %rsi, -0x118(%rbp)
callq 0x8820
movq -0x118(%rbp), %rdi
callq 0x8880
movq 0xdca4(%rip), %rdi      # 0x152c0
callq 0x4770
jmp 0x7623
movq 0xdd26(%rip), %rdi      # 0x15350
callq 0x88a0
cmpq $0x40, %rax
jne 0x76ff
movq 0xdc80(%rip), %rax      # 0x152c0
movq %rax, -0x130(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x34b0
movq -0x128(%rbp), %rdx
leaq 0x6d13(%rip), %rsi      # 0xe37b
leaq -0xa8(%rbp), %rdi
callq 0x3a20
jmp 0x7676
movq -0x130(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x45b0
jmp 0x7690
leaq -0xa8(%rbp), %rdi
callq 0x31c0
leaq -0xa9(%rbp), %rdi
callq 0x3330
movl $0x2, -0xb0(%rbp)
jmp 0x7884
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x78af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x76ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x31c0
leaq -0xa9(%rbp), %rdi
callq 0x3330
jmp 0x78af
movq 0xdbba(%rip), %rsi      # 0x152c0
movl -0x3c(%rbp), %edx
leaq -0xd0(%rbp), %rdi
callq 0x43f0
jmp 0x7717
movq 0xd8ba(%rip), %rdi      # 0x14fd8
leaq 0x6c81(%rip), %rsi      # 0xe3a6
callq 0x32b0
movq %rax, -0x138(%rbp)
jmp 0x7733
movq -0x138(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3290
movq %rax, -0x140(%rbp)
jmp 0x774f
movq -0x140(%rbp), %rdi
movq 0xd86b(%rip), %rsi      # 0x14fc8
callq 0x3310
jmp 0x7764
movq 0xdbe5(%rip), %rax      # 0x15350
movq %rax, -0x150(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x144(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3140
jmp 0x7790
leaq -0x110(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x3140
jmp 0x77a2
movl -0x144(%rbp), %esi
movq -0x150(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
leaq -0x110(%rbp), %rcx
callq 0x88d0
jmp 0x77c4
leaq -0x110(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0x31c0
leaq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x25db(%rip), %rdx     # 0x5210
leaq -0x3c(%rbp), %rcx
callq 0x34d0
cmpl $0x0, %eax
je 0x786e
movq 0xd7d8(%rip), %rdi      # 0x14fd8
leaq 0x6bac(%rip), %rsi      # 0xe3b3
callq 0x32b0
movq %rax, -0x158(%rbp)
jmp 0x7815
movq -0x158(%rbp), %rdi
movq 0xd7a5(%rip), %rsi      # 0x14fc8
callq 0x3310
jmp 0x782a
jmp 0x786e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x78a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x7860
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0x31c0
jmp 0x78a3
leaq -0xd0(%rbp), %rdi
callq 0x31c0
movl $0x0, -0xb0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31c0
movl -0xb0(%rbp), %eax
testl %eax, %eax
je 0x789e
jmp 0x7899
jmp 0x75bc
jmp 0x75bc
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0x38(%rbp), %rdi
callq 0x31c0
movq -0x70(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)