// File: /workspace/github_cpp_makefile_star2_O3/milzar[P]Multiplayer-Hangman-Game/src/server.cpp
// Function: main
int main(int argc,char *argv[]){

        int maxPlayers =atoi(argv[2]);
        bool beginGame=false;
        vector<Player> players;
        cout<<"Server runnin on Port "<<argv[1]<<endl;


        Server myserver(argv[1],maxPlayers);

        fd_set masterfds;
        FD_ZERO(&masterfds);
        FD_SET(myserver.mastersocket,&masterfds);
        int max_file ;
        max_file = myserver.mastersocket;


        while(!beginGame){
          masterfds = initFdset(players,myserver.mastersocket);
          select( max_file + 1 , &masterfds , NULL , NULL , NULL);
          //Activity on main socket : Incoming Connection before game started
          if(FD_ISSET(myserver.mastersocket, &masterfds)){
              int newSocket = accept(myserver.mastersocket,(sockaddr*)NULL  ,NULL);
              string newName = myserver.receive(newSocket);
              Player tmp = Player(newName,newSocket);
              players.push_back(tmp);

              cout<<"New User Added:\t"<<tmp.name<<endl;
              max_file = updateMaxFD(players,max_file);
              myserver.forward(newSocket,"Welcome to Battle Royale Hangman(2018)" );
          }

          cout<<"No of Players:\t"<<players.size()<<endl;
          if(players.size()>= maxPlayers  )
              beginGame=true;

        }
        sleep(1);
        //Begin Game
        string root_word = GameData::getRandomWord();
        GameData game(root_word.size());
        sendGameNotification(players,myserver,game);
        //GAMEDATA

        int turn=0;
        while(true){
            int currentPlayer = players[turn].socket_id;
            for(auto player : players){
                if(player.socket_id == currentPlayer)
                  myserver.forward(player.socket_id, "play");
                else
                  myserver.forward(player.socket_id, "wait");
            }
            //Receive Client guess
            string guess = myserver.receive(currentPlayer);
            if(game.play(root_word,guess) ){
                cout<<"Game Over\t"<<players[turn].name<<" Wins\t"<<endl;
                string res = "gameover"+players[turn].name + "||" +root_word;
                for(auto player : players){
                      myserver.forward(player.socket_id, res);
                }
                // cout<<"Play Again?"<<endl;
                // sleep(1);
                // res = myserver.receive();
                break;
            }
            else{
                string res = game.serialize();
                for(auto player : players){
                      myserver.forward(player.socket_id, res);
                }
                turn = (turn +1)%maxPlayers;
            }

            sleep(1);
          }
     return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rsi, %rbx
movq 0x10(%rsi), %rdi
callq 0x22e0
movl %eax, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x49bd(%rip), %r14      # 0x6fd8
leaq 0x29e2(%rip), %rsi      # 0x5004
movl $0x16, %edx
movq %r14, %rdi
callq 0x2270
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x2654
movq %r15, %rdi
callq 0x2100
movq 0x4991(%rip), %rdi      # 0x6fd8
movq %r15, %rsi
movq %rax, %rdx
callq 0x2270
jmp 0x266c
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2320
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x494e(%rip), %rdi      # 0x6fd8
callq 0x2030
movq %rax, %rdi
callq 0x2160
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdi
movl %ebp, %edx
callq 0x4082
xorps %xmm0, %xmm0
leaq 0x270(%rsp), %rax
movaps %xmm0, 0x70(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x8(%rsp), %rax
movl (%rax), %r15d
leal 0x3f(%r15), %eax
testl %r15d, %r15d
cmovnsl %r15d, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %r15d, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0x270(%rsp,%rax,8)
movl %ebp, 0x84(%rsp)
movslq %ebp, %rax
movq %rax, 0x50(%rsp)
leaq 0x238(%rsp), %rbx
leaq 0x88(%rsp), %rbp
movq 0x48af(%rip), %r14      # 0x6fd8
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3232
movl 0x8(%rsp), %edx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x42a2
movl $0x10, %ecx
leaq 0x270(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x32cc
leal 0x1(%r15), %edi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x21f0
movl 0x8(%rsp), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x270(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x28fe
xorl %esi, %esi
xorl %edx, %edx
callq 0x20c0
movl %eax, %ebx
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl %eax, %edx
callq 0x40a6
leaq 0x1b8(%rsp), %rax
movq %rax, 0x1a8(%rsp)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x1a8(%rsp), %r13
movq %r13, %rdi
callq 0x3410
movq %rbp, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x4212
movq 0x1a8(%rsp), %rdi
leaq 0x1b8(%rsp), %rax
cmpq %rax, %rdi
je 0x2817
movq 0x1b8(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x332a
movl $0x10, %edx
movq %r14, %rdi
leaq 0x27e8(%rip), %rsi      # 0x501b
callq 0x2270
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq %r14, %rdi
callq 0x2270
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2160
leaq 0x220(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3232
leaq 0x220(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
callq 0x4262
movl %eax, %r15d
movq %r13, %rdi
callq 0x32cc
leaq 0x8(%rsp), %rdi
movl %ebx, %esi
leaq 0x2774(%rip), %rdx      # 0x502c
callq 0x4160
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x28e2
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x28fe
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
movl $0xf, %edx
movq %r14, %rdi
leaq 0x2746(%rip), %rsi      # 0x5053
callq 0x2270
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rsi
movq %r14, %rdi
callq 0x2150
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2160
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq 0x50(%rsp), %rax
movq %r12, %rbx
jb 0x2729
movl $0x1, %edi
callq 0x2180
leaq 0x60(%rsp), %rdi
callq 0x3a00
movl 0x68(%rsp), %esi
leaq 0x200(%rsp), %rdi
callq 0x37dc
leaq 0x1e8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3232
movl 0x8(%rsp), %esi
movups 0x200(%rsp), %xmm0
movups 0x210(%rsp), %xmm1
leaq 0x250(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm1, 0x10(%rdx)
leaq 0x1e8(%rsp), %rdi
callq 0x24ec
leaq 0x1e8(%rsp), %rdi
callq 0x32cc
movl $0x0, 0xc(%rsp)
leaq 0x8(%rsp), %r15
leaq 0x88(%rsp), %r14
leaq 0x148(%rsp), %r12
movslq 0xc(%rsp), %rax
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rbp
leaq (%rax,%rax,4), %rax
movq %rax, 0x50(%rsp)
movl 0x20(%rbx,%rax,8), %r13d
cmpq %rbp, %rbx
je 0x2ab1
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x3410
movl 0x20(%rbx), %esi
movl %esi, 0xa8(%rsp)
cmpl %r13d, %esi
leaq 0x25f5(%rip), %rdx      # 0x5068
leaq 0x25e9(%rip), %rax      # 0x5063
cmoveq %rax, %rdx
movq %r15, %rdi
callq 0x4160
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2aab
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %rbx
jmp 0x2a38
leaq 0x188(%rsp), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x40a6
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x3410
leaq 0x168(%rsp), %rbx
movq 0x50(%rsp), %rbp
leaq 0x178(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x188(%rsp), %rsi
movq 0x190(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x3410
leaq 0x200(%rsp), %rdi
leaq 0x108(%rsp), %rsi
movq %rbx, %rdx
callq 0x3810
movl %eax, 0x5c(%rsp)
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x2b67
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2b8c
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2240
cmpl $0x0, 0x5c(%rsp)
je 0x2c75
movl $0xa, %edx
movq 0x4435(%rip), %rdi      # 0x6fd8
leaq 0x24c3(%rip), %rsi      # 0x506d
callq 0x2270
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq 0x8(%rax,%rbp,8), %rdx
movq 0x4414(%rip), %rdi      # 0x6fd8
callq 0x2270
movq %rax, %r13
movl $0x6, %edx
movq %rax, %rdi
leaq 0x249d(%rip), %rsi      # 0x5078
callq 0x2270
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2160
shlq $0x3, %rbp
addq 0x10(%rsp), %rbp
leaq 0x1c8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x245c(%rip), %rsi      # 0x507f
movq %rbp, %rdx
callq 0x337d
movq %rbx, %rdi
leaq 0x2453(%rip), %rsi      # 0x5088
callq 0x2370
leaq 0x98(%rsp), %rsi
movq %rsi, 0x88(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2daf
movq %rdx, 0x88(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x98(%rsp)
jmp 0x2db5
leaq 0x30(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x3942
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq %rax, %r13
je 0x2d72
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x3410
movq %r12, %rbx
movq %r14, %r12
movq %r15, %r14
movl 0x20(%r13), %ebp
movl %ebp, 0xa8(%rsp)
leaq 0x138(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
leaq 0x128(%rsp), %r15
movq %r15, %rdi
callq 0x3410
movq %r14, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x4106
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x2d35
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
movq %r14, %r15
je 0x2d5d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %r13
cmpq 0x50(%rsp), %r13
movq %r12, %r14
movq %rbx, %r12
jne 0x2c9f
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2d8e
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2240
movl $0x1, %edi
callq 0x2180
movl 0xc(%rsp), %eax
incl %eax
cltd
idivl 0x84(%rsp)
movl %edx, 0xc(%rsp)
jmp 0x2f54
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %r14, %rdi
callq 0x20b0
leaq 0x40(%rsp), %rsi
movq %rsi, 0x30(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2e0a
movq %rdx, 0x30(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x40(%rsp)
jmp 0x2e10
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x38(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2e4d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x1c8(%rsp), %rdi
leaq 0x1d8(%rsp), %rax
cmpq %rax, %rdi
je 0x2e72
movq 0x1d8(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbx
cmpq %rbx, %r13
je 0x2f38
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x3410
movl 0x20(%r13), %ebp
movl %ebp, 0xa8(%rsp)
leaq 0x158(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x3410
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x4106
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0x2f0a
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2f2f
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %r13
jmp 0x2e7c
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2f54
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x2f79
movq 0x198(%rsp), %rsi
incq %rsi
callq 0x2240
cmpl $0x0, 0x5c(%rsp)
je 0x2a1b
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2fa0
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
callq 0x32cc
xorl %eax, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x32cc
jmp 0x3204
jmp 0x31d7
jmp 0x31d7
jmp 0x3117
jmp 0x3117
jmp 0x3117
jmp 0x3117
jmp 0x310f
jmp 0x310f
movq %rax, %rbx
jmp 0x3028
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3028
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x1c8(%rsp), %rdi
leaq 0x1d8(%rsp), %rax
cmpq %rax, %rdi
je 0x31b8
movq 0x1d8(%rsp), %rsi
jmp 0x31b0
movq %rax, %rbx
movq 0x1a8(%rsp), %rdi
leaq 0x1b8(%rsp), %rax
cmpq %rax, %rdi
je 0x3204
movq 0x1b8(%rsp), %rsi
jmp 0x31fc
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0x32cc
jmp 0x31df
jmp 0x3117
jmp 0x3117
jmp 0x31d7
jmp 0x31d7
movq %rax, %rbx
jmp 0x30cd
movq %rax, %rbx
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x30cd
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x31b8
movq 0x118(%rsp), %rsi
jmp 0x31b0
jmp 0x3117
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x32cc
jmp 0x3220
jmp 0x31dc
movq %rax, %rbx
jmp 0x31b8
movq %rax, %rbx
jmp 0x3220
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x3177
movq 0x138(%rsp), %rsi
jmp 0x3165
jmp 0x3174
jmp 0x316f
movq %rax, %rbx
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0x3177
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x2240
jmp 0x3177
movq %rax, %rbx
jmp 0x319c
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x319c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x40(%rsp), %rax
movq 0x30(%rsp), %rdi
cmpq %rax, %rdi
je 0x31b8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x3204
movq 0x198(%rsp), %rsi
jmp 0x31fc
movq %rax, %rbx
jmp 0x3204
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3204
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x3220
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
callq 0x32cc
movq %rbx, %rdi
callq 0x2330