// File: /workspace/github_cpp_makefile_star2_O1/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 $0x5d8, %rsp            # imm = 0x5D8
movq %rsi, %rbx
movl %edi, %r14d
movq 0x3177(%rip), %rdx      # 0x6248
leaq 0x3168(%rip), %rdi      # 0x6240
leaq 0x10ea(%rip), %rcx      # 0x41c9
movl $0xa, %r8d
xorl %esi, %esi
callq 0x2340
cmpl $0x2, %r14d
jne 0x338b
movq 0x8(%rbx), %rdi
callq 0x2350
movl %eax, %edi
callq 0x2721
movl %eax, %r14d
movq 0x8(%rbx), %rsi
leaq 0x10c0(%rip), %rdi      # 0x41d4
xorl %eax, %eax
callq 0x2060
movl %r14d, %edi
movl $0x5, %esi
callq 0x2290
testl %eax, %eax
js 0x3399
leal 0x3f(%r14), %edx
testl %r14d, %r14d
cmovnsl %r14d, %edx
movl %edx, %eax
andl $-0x40, %eax
movl %r14d, %ecx
subl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
sarl $0x6, %edx
movq %rdx, -0x50(%rbp)
movq %rax, -0x48(%rbp)
orq %rax, -0xf0(%rbp,%rdx,8)
leaq -0x2c(%rbp), %rax
movq %r14, -0x40(%rbp)
movl %r14d, (%rax)
leaq -0x600(%rbp), %rbx
leaq 0x309f(%rip), %r12      # 0x6218
movq 0x2e58(%rip), %r13      # 0x5fd8
movq $0x0, -0x38(%rbp)
movl $0x10, %ecx
leaq -0x1f0(%rbp), %rdi
leaq -0xf0(%rbp), %rax
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x10, %ecx
leaq -0x170(%rbp), %r15
movq %r15, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x401, %edx            # imm = 0x401
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2170
movl -0x2c(%rbp), %r14d
leal 0x1(%r14), %edi
movq %r15, %rsi
xorl %edx, %edx
leaq -0x1f0(%rbp), %rcx
xorl %r8d, %r8d
callq 0x2230
testl %eax, %eax
js 0x3363
movl %eax, %r15d
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
testq %rcx, -0x170(%rbp,%rax,8)
je 0x3254
movq -0x40(%rbp), %rdi
leaq -0x6c(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x20d0
leal 0x3f(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
movl %edx, %esi
andl $-0x40, %esi
movl %eax, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rsi, -0xf0(%rbp,%rcx,8)
cmpl %eax, %r14d
cmovlel %eax, %r14d
movl %r14d, -0x2c(%rbp)
decl %r15d
leaq 0xfe0(%rip), %rdi       # 0x422b
movl %eax, %esi
xorl %eax, %eax
callq 0x2060
testl %r15d, %r15d
jle 0x3357
movq %r15, -0x58(%rbp)
movq 0x2fc0(%rip), %r14      # 0x6228
cmpq %r12, %r14
je 0x3344
movq 0x28(%r14), %r15
movl (%r15), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0x170(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0x3330
movl $0x401, %edx            # imm = 0x401
movq %rbx, %rsi
movl $0x40, %ecx
callq 0x20e0
testq %rax, %rax
je 0x3304
movq %rbx, %rdi
callq 0x2130
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x22a0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl (%r15), %edi
leaq -0xf0(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rbx, %rcx
callq 0x2805
jmp 0x3330
movl (%r15), %esi
leaq 0xf3d(%rip), %rdi       # 0x424b
xorl %eax, %eax
callq 0x2060
movl (%r15), %edi
callq 0x23c0
movl (%r15), %edi
leaq -0xf0(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x2656
movq %r14, %rdi
callq 0x21a0
movq %rax, %r14
cmpq %r12, %rax
jne 0x3271
movq -0x58(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x2, %ecx
movl %eax, %r15d
jge 0x325d
testb $0x1, -0x38(%rbp)
je 0x3188
jmp 0x3377
leaq 0xea3(%rip), %rdi       # 0x420d
callq 0x2200
movb $0x1, %al
movq %rax, -0x38(%rbp)
jmp 0x3357
xorl %eax, %eax
addq $0x5d8, %rsp            # imm = 0x5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf27(%rip), %rdi       # 0x42b9
callq 0x2330
jmp 0x33ab
movq 0x8(%rbx), %rsi
leaq 0xe4b(%rip), %rdi       # 0x41ef
xorl %eax, %eax
callq 0x2060
xorl %edi, %edi
callq 0x22c0