// File: /workspace/github_cpp_makefile_star2_O2/hinrikHelga[P]Botnet/server.cpp
// Function: main
int main(int argc, char* argv[])
{

    bool finished;
    int listenTCPSock;              // Socket for TCP connections to server
    int clientSock;                 // Socket of connecting client
    fd_set openSockets;             // Current open sockets
    fd_set readSockets;             // Socket list for select()
    fd_set exceptSockets;           // Exception socket list
    int maxfds;
                      // Passed to select() as max fd in set
    struct sockaddr_in client;
    socklen_t client_len;
    char buffer[1025];              // buffer for reading from clients

    myName = "V_group_65";

    if(argc != 2)
    {
        printf("Usage: chat_server <tcp port>\n");
        exit(0);
    }

    // Setup TCP socket for server to listen to

    listenTCPSock = openTcpSocket(atoi(argv[1]));
    printf("Listening on TCP port: %s\n", argv[1]);


    if(listen(listenTCPSock, BACKLOG) < 0)
    {
        printf("Listen failed on tcp port %s\n", argv[1]);
        exit(0);
    }
    else
    // Add listen socket to socket set
    {
        FD_SET(listenTCPSock, &openSockets);
        maxfds = listenTCPSock;
    }

    finished = false;

    while(!finished)
    {
        // Get modifiable copy of readSockets
        readSockets = exceptSockets = openSockets;
        memset(buffer, 0, sizeof(buffer));

        int n = select(maxfds + 1, &readSockets, NULL, &exceptSockets, NULL);

        if(n < 0)
        {
            perror("select failed - closing down\n");
            finished = true;
        }
        else
        {
            // Accept  any new connections to the server
            if(FD_ISSET(listenTCPSock, &readSockets))
            {
               clientSock = accept(listenTCPSock, (struct sockaddr *)&client,
                                   &client_len);

               FD_SET(clientSock, &openSockets);
               maxfds = std::max(maxfds, clientSock);

               n--;

               printf("Client connected on server: %d\n", clientSock);
            }
            // Now check for commands from clients
            while(n-- > 0)
            {
               for(auto const& pair : connectedServers)
               {
                  Node *client = pair.second;

                  if(FD_ISSET(client->sock, &readSockets))
                  {
                      if(recv(client->sock, buffer, sizeof(buffer), MSG_DONTWAIT) == 0)
                      {
                          printf("Client closed connection: %d", client->sock);
                          close(client->sock);

                          closeClient(client->sock, &openSockets, &maxfds);

                      }
                      else
                      {
                          std::cout << buffer << std::endl;
                          inputCommand(client->sock, &openSockets, &maxfds,
                                        buffer);
                      }
                  }
               }
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x340d(%rip), %rdi      # 0x6210
leaq 0x13bf(%rip), %rsi      # 0x41c9
callq 0x2340
cmpl $0x2, %r14d
jne 0x305a
movq 0x8(%rbx), %rdi
callq 0x22e0
movl %eax, %edi
callq 0x2624
movl %eax, %r15d
movq 0x8(%rbx), %rsi
leaq 0x139d(%rip), %rdi      # 0x41d4
xorl %eax, %eax
callq 0x2050
pushq $0x5
popq %rsi
movl %r15d, %edi
callq 0x2220
testl %eax, %eax
js 0x3068
pushq $0x40
popq %r14
movl %r15d, %eax
cltd
idivl %r14d
movl %eax, %edx
shll $0x6, %edx
movl %r15d, %ecx
subl %edx, %ecx
pushq $0x1
popq %rdx
shlq %cl, %rdx
movq %rax, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
orq %rdx, -0xe8(%rbp,%rax,8)
leaq -0x2c(%rbp), %rax
movl %r15d, -0x3c(%rbp)
movl %r15d, (%rax)
leaq -0x5f0(%rbp), %rbx
leaq 0x3353(%rip), %r12      # 0x61e8
pushq $0x10
popq %rax
movq %rax, %rcx
leaq -0x1e8(%rbp), %rdi
leaq -0xe8(%rbp), %rdx
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rcx
leaq -0x168(%rbp), %r13
movq %r13, %rdi
movq %rdx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x401, %edx            # imm = 0x401
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
movl -0x2c(%rbp), %r15d
leal 0x1(%r15), %edi
movq %r13, %rsi
xorl %edx, %edx
leaq -0x1e8(%rbp), %rcx
xorl %r8d, %r8d
callq 0x21d0
movq %rax, -0x38(%rbp)
testl %eax, %eax
js 0x3038
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
testq %rcx, -0x168(%rbp,%rax,8)
je 0x2f5f
movl -0x3c(%rbp), %edi
leaq -0x64(%rbp), %rsi
leaq -0x54(%rbp), %rdx
callq 0x20b0
movl %eax, %esi
cltd
idivl %r14d
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, -0xe8(%rbp,%rdx,8)
cmpl %esi, %r15d
cmovlel %esi, %r15d
movl %r15d, -0x2c(%rbp)
movq -0x38(%rbp), %rax
leal -0x1(%rax), %r15d
leaq 0x12d8(%rip), %rdi      # 0x422b
xorl %eax, %eax
callq 0x2050
jmp 0x3023
movq -0x38(%rbp), %rax
movl %eax, %r15d
jmp 0x3023
decl %r15d
movl %r15d, -0x40(%rbp)
movq 0x327f(%rip), %r13      # 0x61f8
cmpq %r12, %r13
je 0x301f
movq 0x28(%r13), %r15
movl (%r15), %edi
movl %edi, %eax
cltd
idivl %r14d
cltq
movq -0x168(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0x300f
movl $0x401, %edx            # imm = 0x401
movq %rbx, %rsi
movl %r14d, %ecx
callq 0x20c0
testq %rax, %rax
je 0x2fe3
movq 0x301d(%rip), %rdi      # 0x5fd8
movq %rbx, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
movl (%r15), %edi
leaq -0xe8(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rbx, %rcx
callq 0x26d2
jmp 0x300f
movl (%r15), %esi
leaq 0x125e(%rip), %rdi      # 0x424b
xorl %eax, %eax
callq 0x2050
movl (%r15), %edi
callq 0x2360
movl (%r15), %edi
leaq -0xe8(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x2574
movq %r13, %rdi
callq 0x2150
movq %rax, %r13
jmp 0x2f79
movl -0x40(%rbp), %r15d
testl %r15d, %r15d
jg 0x2f6b
cmpl $0x0, -0x38(%rbp)
jns 0x2e95
jmp 0x3046
leaq 0x11ce(%rip), %rdi      # 0x420d
callq 0x2190
jmp 0x302c
xorl %eax, %eax
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x122e(%rip), %rdi      # 0x428f
callq 0x22d0
jmp 0x307a
movq 0x8(%rbx), %rsi
leaq 0x117c(%rip), %rdi      # 0x41ef
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x2240
nop