// File: /workspace/github_cpp_makefile_star2_O3/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 0x316f(%rip), %rdx      # 0x6248
leaq 0x3160(%rip), %rdi      # 0x6240
leaq 0x10e2(%rip), %rcx      # 0x41c9
movl $0xa, %r8d
xorl %esi, %esi
callq 0x2340
cmpl $0x2, %r14d
jne 0x338e
movq 0x8(%rbx), %rdi
callq 0x2350
movl %eax, %edi
callq 0x2729
movl %eax, %r14d
movq 0x8(%rbx), %rsi
leaq 0x10b8(%rip), %rdi      # 0x41d4
xorl %eax, %eax
callq 0x2060
movl %r14d, %edi
movl $0x5, %esi
callq 0x2290
testl %eax, %eax
js 0x339c
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, -0x48(%rbp)
movq %rax, -0x40(%rbp)
orq %rax, -0xf0(%rbp,%rdx,8)
leaq -0x2c(%rbp), %rax
movq %r14, -0x38(%rbp)
movl %r14d, (%rax)
leaq -0x600(%rbp), %rbx
leaq 0x3097(%rip), %r14      # 0x6218
movq 0x2e50(%rip), %r13      # 0x5fd8
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), %r12
movq %r12, %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), %r15d
leal 0x1(%r15), %edi
movq %r12, %rsi
xorl %edx, %edx
leaq -0x1f0(%rbp), %rcx
xorl %r8d, %r8d
callq 0x2230
testl %eax, %eax
js 0x3380
movl %eax, %r12d
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
testq %rcx, -0x170(%rbp,%rax,8)
movq %r12, -0x50(%rbp)
je 0x325a
movq -0x38(%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, %r15d
cmovlel %eax, %r15d
movl %r15d, -0x2c(%rbp)
decl %r12d
leaq 0xfdc(%rip), %rdi       # 0x422b
movl %eax, %esi
xorl %eax, %eax
callq 0x2060
jmp 0x325a
testl %r12d, %r12d
jle 0x3362
movq %r12, -0x58(%rbp)
movq 0x2fba(%rip), %r15      # 0x6228
cmpq %r14, %r15
je 0x334f
movq 0x28(%r15), %r12
movl (%r12), %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 0x333b
movl $0x401, %edx            # imm = 0x401
movq %rbx, %rsi
movl $0x40, %ecx
callq 0x20e0
testq %rax, %rax
je 0x330c
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 (%r12), %edi
leaq -0xf0(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rbx, %rcx
callq 0x280f
jmp 0x333b
movl (%r12), %esi
leaq 0xf34(%rip), %rdi       # 0x424b
xorl %eax, %eax
callq 0x2060
movl (%r12), %edi
callq 0x23c0
movl (%r12), %edi
leaq -0xf0(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
callq 0x2656
movq %r15, %rdi
callq 0x21a0
movq %rax, %r15
cmpq %r14, %rax
jne 0x3277
movq -0x58(%rbp), %rcx
leal -0x1(%rcx), %eax
cmpl $0x2, %ecx
movl %eax, %r12d
jge 0x3263
cmpl $0x0, -0x50(%rbp)
jns 0x3188
xorl %eax, %eax
addq $0x5d8, %rsp            # imm = 0x5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe86(%rip), %rdi       # 0x420d
callq 0x2200
jmp 0x336c
leaq 0xf24(%rip), %rdi       # 0x42b9
callq 0x2330
jmp 0x33ae
movq 0x8(%rbx), %rsi
leaq 0xe48(%rip), %rdi       # 0x41ef
xorl %eax, %eax
callq 0x2060
xorl %edi, %edi
callq 0x22c0
nop