// File: /workspace/github_cpp_makefile_star2_O1/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 0x40ba
xorps %xmm0, %xmm0
leaq 0x270(%rsp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
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
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
sarl $0x6, %eax
cltq
orq %rdx, 0x270(%rsp,%rax,8)
movl %ebp, 0x84(%rsp)
movslq %ebp, %rax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x238(%rsp), %rbx
movq 0x48b5(%rip), %r14      # 0x6fd8
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x324e
movl 0x8(%rsp), %edx
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x42da
movl $0x10, %ecx
leaq 0x270(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x32e8
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 0x290a
xorl %esi, %esi
xorl %edx, %edx
callq 0x20c0
movl %eax, %ebx
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl %eax, %edx
callq 0x40de
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 0x342a
leaq 0x88(%rsp), %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x424a
movq 0x1a8(%rsp), %rdi
leaq 0x1b8(%rsp), %rax
cmpq %rax, %rdi
je 0x281e
movq 0x1b8(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3344
movl $0x10, %edx
movq %r14, %rdi
leaq 0x27dc(%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 0x324e
leaq 0x220(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
callq 0x429a
movl %eax, %r15d
movq %r13, %rdi
callq 0x32e8
leaq 0x8(%rsp), %rdi
movl %ebx, %esi
leaq 0x2768(%rip), %rdx      # 0x502c
callq 0x4198
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x28ee
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x290a
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
movl $0xf, %edx
movq %r14, %rdi
leaq 0x273a(%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
movzbl %bpl, %ebp
movl $0x1, %eax
cmovael %eax, %ebp
testb $0x1, %bpl
movq %r12, %rbx
je 0x2723
movl $0x1, %edi
callq 0x2180
leaq 0x60(%rsp), %rdi
callq 0x3a3c
movl 0x68(%rsp), %esi
leaq 0x200(%rsp), %rdi
callq 0x3820
leaq 0x1e8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x324e
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 0x32e8
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 0x2acd
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x342a
movl 0x20(%rbx), %esi
movl %esi, 0xa8(%rsp)
cmpl %r13d, %esi
leaq 0x25d9(%rip), %rdx      # 0x5068
leaq 0x25cd(%rip), %rax      # 0x5063
cmoveq %rax, %rdx
movq %r15, %rdi
callq 0x4198
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2ac7
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %rbx
jmp 0x2a54
leaq 0x188(%rsp), %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x40de
leaq 0x118(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x108(%rsp), %rdi
callq 0x342a
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 0x342a
leaq 0x200(%rsp), %rdi
leaq 0x108(%rsp), %rsi
movq %rbx, %rdx
callq 0x3854
movl %eax, 0x5c(%rsp)
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x2b83
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x2ba8
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2240
cmpl $0x0, 0x5c(%rsp)
je 0x2c91
movl $0xa, %edx
movq 0x4419(%rip), %rdi      # 0x6fd8
leaq 0x24a7(%rip), %rsi      # 0x506d
callq 0x2270
movq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq 0x8(%rax,%rbp,8), %rdx
movq 0x43f8(%rip), %rdi      # 0x6fd8
callq 0x2270
movq %rax, %r13
movl $0x6, %edx
movq %rax, %rdi
leaq 0x2481(%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 0x2440(%rip), %rsi      # 0x507f
movq %rbp, %rdx
callq 0x3397
movq %rbx, %rdi
leaq 0x2437(%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 0x2dcb
movq %rdx, 0x88(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x98(%rsp)
jmp 0x2dd1
leaq 0x30(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x397e
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq %rax, %r13
je 0x2d8e
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x342a
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 0x342a
movq %r14, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x413e
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x2d51
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
movq %r14, %r15
je 0x2d79
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %r13
cmpq 0x50(%rsp), %r13
movq %r12, %r14
movq %rbx, %r12
jne 0x2cbb
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2daa
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 0x2f70
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 0x2e26
movq %rdx, 0x30(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x40(%rsp)
jmp 0x2e2c
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 0x2e69
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x1c8(%rsp), %rdi
leaq 0x1d8(%rsp), %rax
cmpq %rax, %rdi
je 0x2e8e
movq 0x1d8(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbx
cmpq %rbx, %r13
je 0x2f54
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x342a
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 0x342a
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x413e
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0x2f26
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x2f4b
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
addq $0x28, %r13
jmp 0x2e98
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2f70
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x2f95
movq 0x198(%rsp), %rsi
incq %rsi
callq 0x2240
cmpl $0x0, 0x5c(%rsp)
je 0x2a37
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2fbc
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
callq 0x32e8
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 0x32e8
jmp 0x3220
jmp 0x31f3
jmp 0x31f3
jmp 0x3133
jmp 0x3133
jmp 0x3133
jmp 0x3133
jmp 0x312b
jmp 0x312b
movq %rax, %rbx
jmp 0x3044
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3044
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x1c8(%rsp), %rdi
leaq 0x1d8(%rsp), %rax
cmpq %rax, %rdi
je 0x31d4
movq 0x1d8(%rsp), %rsi
jmp 0x31cc
movq %rax, %rbx
movq 0x1a8(%rsp), %rdi
leaq 0x1b8(%rsp), %rax
cmpq %rax, %rdi
je 0x3220
movq 0x1b8(%rsp), %rsi
jmp 0x3218
movq %rax, %rbx
leaq 0x220(%rsp), %rdi
callq 0x32e8
jmp 0x31fb
jmp 0x3133
jmp 0x3133
jmp 0x31f3
jmp 0x31f3
movq %rax, %rbx
jmp 0x30e9
movq %rax, %rbx
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x30e9
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x31d4
movq 0x118(%rsp), %rsi
jmp 0x31cc
jmp 0x3133
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x32e8
jmp 0x323c
jmp 0x31f8
movq %rax, %rbx
jmp 0x31d4
movq %rax, %rbx
jmp 0x323c
movq %rax, %rbx
movq 0x128(%rsp), %rdi
leaq 0x138(%rsp), %rax
cmpq %rax, %rdi
je 0x3193
movq 0x138(%rsp), %rsi
jmp 0x3181
jmp 0x3190
jmp 0x318b
movq %rax, %rbx
movq 0x148(%rsp), %rdi
leaq 0x158(%rsp), %rax
cmpq %rax, %rdi
je 0x3193
movq 0x158(%rsp), %rsi
incq %rsi
callq 0x2240
jmp 0x3193
movq %rax, %rbx
jmp 0x31b8
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x31b8
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x40(%rsp), %rax
movq 0x30(%rsp), %rdi
cmpq %rax, %rdi
je 0x31d4
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
je 0x3220
movq 0x198(%rsp), %rsi
jmp 0x3218
movq %rax, %rbx
jmp 0x3220
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3220
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2240
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x323c
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x10(%rsp), %rdi
callq 0x32e8
movq %rbx, %rdi
callq 0x2330