// File: /workspace/github_cpp_makefile_star2_O2/Thomas1995[P]Rentz/src/client.cpp
// Function: Client::Client(std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>> const&)
Client(const std::unordered_map<std::string, std::string> &opts) {

    srand (time(NULL));

    const std::string name = opts.count("name") ? opts.at("name") : random_name();

    if(name == "TH")  bot = std::unique_ptr<Bot>(new Bot_Thomas);
    if(name == "ER")  bot = std::unique_ptr<Bot>(new Bot_Eric);
    if(name == "BC")  bot = std::unique_ptr<Bot>(new Bot_Bicsi);
    if(name == "EU")  bot = std::unique_ptr<Bot>(new Bot_Eugen);
    if(name == "RD")  bot = std::unique_ptr<Bot>(new Bot_Random);

    require(bot != nullptr, "Name not available. Choose from: EU | TH | ER | BC | RD\n");

    addrinfo hints, *rez;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    const std::string address = opts.count("address") ? opts.at("address") : "127.0.0.1";

    getaddrinfo(address.c_str(), PORT, &hints, &rez);

    for(addrinfo *p = rez; p != NULL; p = p->ai_next) {
      sfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);

      if(sfd == -1)
        continue;

      if(connect(sfd, p->ai_addr, p->ai_addrlen) != -1) {
        printf("Connected to %s\n", address.c_str());
        break;
      }

      close(sfd);
    }

    fcntl(sfd, F_SETFL, O_NONBLOCK);

    freeaddrinfo(rez);

    handshake(const_cast<char *>(name.c_str()));

    while(true) {
      event e = readEvent();
      event resp;
      resp.type = e.type;
      resp.len = 0;
      resp.data = NULL;

      switch(e.type) {
        case event::EType::SEND_CARDS: {
        //the server is sending us what cards were played
        //and are on the table

        //debug("Receiving cards on table...\n");
          std::vector<Card> cards(e.getCards());
          bot->ReceiveCardsOnTable(cards);
          resp.send(sfd);
          break;
      }

      case event::EType::SEND_HAND: {
      //the server is sending us our hand

            debug("Receiving hand: ");
            std::vector<Card> hand(e.getCards());
            for(auto &x: hand)
              debug("%s ", x.to_string().c_str());
            debug("\n");

            bot->ReceiveHand(hand);
            resp.send(sfd);
            break;
        }

        case event::EType::TURN_END: {
        //the server is telling us that turn has ended

          debug("Turn has ended.\n");
          bot->TurnEnd();
          resp.send(sfd);

          break;
        }

        case event::EType::ROUND_END: {
        //the server is sending us the scores so far

            debug("Round ended.\n Scores: ");

            std::vector<int> scores;
            scores.reserve(e.len / 4);

            for(uint32_t i = 0; i < e.len; i += 4)
              scores.push_back(e.getInt(e.data + i));

            for(auto i: scores)
              debug(" %d", i);
            debug("\n\n");

            bot->RoundEnd(scores);
            resp.send(sfd);
            break;
        }

        case event::EType::ASK_GAME: {
        //the server is asking us for a minigame to choose

            debug("Being asked for minigame...\n");
            uint8_t ans = bot->ChooseMinigame();
            debug("I chose: %s\n", GameName[ans].c_str());
            resp.len = 1;
            resp.data = &ans;
            resp.send(sfd);
            break;
        }

        case event::EType::ROUND_START: {
        //the server is sending us the chosen game type

            debug("Round is starting...\n");
            const uint8_t choice = e.data[0];
            bot->RoundStart(choice);
            resp.send(sfd);
            break;
        }

        case event::EType::ASK_CARD: {
        //the server is asking us for a card to play

            debug("Being asked for card...\n");
            Card c = bot->PlayCard();
            debug("I chose: %s\n", c.to_string().c_str());
            resp.len = 1;
            uint8_t code = c.encode();
            resp.data = &code;
            resp.send(sfd);
            break;
        }

        case event::EType::ASK_NV: {
        //the server is asking us if we want NV
          
            debug("Being asked for NV...\n");
            bool ans = bot->AskIfNV();

            if(ans) debug("Chose NV\n");
            else debug("Did not choose NV\n");

            resp.len = 1;
            resp.data = reinterpret_cast<uint8_t *>(&ans);
            resp.send(sfd);
            break;
        }

        case event::EType::GAME_END: {
        //the server is telling us that game has ended

          debug("The game has ended.\n\n");
          resp.send(sfd);
          goto end;
        }
      }
      e.free();

      std::this_thread::sleep_for(std::chrono::milliseconds(5));
    } // end while

  end:

    return;
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %r14
movq %rdi, %rbx
callq 0x54f2
leaq 0x2028(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
andq $0x0, 0x2028(%rbx)
xorl %edi, %edi
callq 0x3220
movl %eax, %edi
callq 0x3230
leaq 0x6271(%rip), %rsi      # 0xc47d
leaq 0xd0(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x4006
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x7584
testq %rax, %rax
jne 0x6247
leaq 0x90(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x6d24
jmp 0x6284
leaq 0x622f(%rip), %rsi      # 0xc47d
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x4006
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x7684
leaq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0x3320
leaq 0x70(%rsp), %rdi
callq 0x3120
leaq 0xd0(%rsp), %rdi
callq 0x3120
leaq 0x618c(%rip), %rsi      # 0xc424
leaq 0x90(%rsp), %rdi
callq 0x6d5d
testb %al, %al
je 0x62d4
movl $0xe8, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x6d6a
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r15, (%rax)
testq %rdi, %rdi
je 0x62d4
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x614c(%rip), %rsi      # 0xc427
leaq 0x90(%rsp), %rdi
callq 0x6d5d
testb %al, %al
je 0x6317
movl $0xb8, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x6e6c
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r15, (%rax)
testq %rdi, %rdi
je 0x6317
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x610c(%rip), %rsi      # 0xc42a
leaq 0x90(%rsp), %rdi
callq 0x6d5d
testb %al, %al
je 0x635a
movl $0xe8, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x6f1e
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r15, (%rax)
testq %rdi, %rdi
je 0x635a
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x614d(%rip), %rsi      # 0xc4ae
leaq 0x90(%rsp), %rdi
callq 0x6d5d
testb %al, %al
je 0x639d
movl $0xc8, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x7048
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r15, (%rax)
testq %rdi, %rdi
je 0x639d
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x6089(%rip), %rsi      # 0xc42d
leaq 0x90(%rsp), %rdi
callq 0x6d5d
testb %al, %al
je 0x63e0
movl $0x98, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
callq 0x70f0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %r15, (%rax)
testq %rdi, %rdi
je 0x63e0
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rbx
leaq 0x60c2(%rip), %rsi      # 0xc4b1
leaq 0x118(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x4006
xorl %edi, %edi
testq %rbx, %rbx
setne %dil
leaq 0x118(%rsp), %rsi
callq 0x54a8
leaq 0x118(%rsp), %rdi
callq 0x3120
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xf0(%rsp)
movl $0x1, 0xd8(%rsp)
leaq 0x600f(%rip), %rsi      # 0xc463
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x4006
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x7584
testq %rax, %rax
jne 0x648d
leaq 0x606e(%rip), %rsi      # 0xc4ea
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x4006
jmp 0x64ca
leaq 0x5fcf(%rip), %rsi      # 0xc463
leaq 0x50(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x4006
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x7684
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x3320
leaq 0x50(%rsp), %rdi
callq 0x3120
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x3120
movq 0x70(%rsp), %rdi
leaq 0x62a1(%rip), %rsi      # 0xc784
leaq 0xd0(%rsp), %rdx
leaq 0xc8(%rsp), %r14
movq %r14, %rcx
callq 0x32d0
movq (%r14), %r14
testq %r14, %r14
je 0x6561
movl 0x4(%r14), %edi
movl 0x8(%r14), %esi
movl 0xc(%r14), %edx
callq 0x3080
movq 0x8(%rsp), %rcx
movl %eax, 0x201c(%rcx)
cmpl $-0x1, %eax
je 0x6548
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movl %eax, %edi
callq 0x31e0
cmpl $-0x1, %eax
jne 0x654e
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %edi
callq 0x3400
addq $0x28, %r14
jmp 0x64fb
movq 0x70(%rsp), %rsi
leaq 0x5f9a(%rip), %rdi      # 0xc4f4
xorl %eax, %eax
callq 0x3050
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %edi
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x3170
movq 0xc8(%rsp), %rdi
callq 0x3300
movq 0x90(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x7160
leaq 0x50(%rsp), %r12
movq 0xca2a(%rip), %rbx      # 0x12fd0
leaq 0xb0(%rsp), %r14
movq %r12, %rdi
movq 0x8(%rsp), %rsi
callq 0x5780
movl 0x50(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x20(%rsp), %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
cmpq $0xa, %rax
ja 0x663a
leaq 0x5de5(%rip), %rcx      # 0xc3c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r15, %rdi
movq %r12, %rsi
callq 0x7288
movq 0x10(%rsp), %rax
movq (%rax), %r13
leaq 0x150(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x42dc
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e82
movq %rbp, %rdi
callq 0x43ce
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
movq %r15, %rdi
callq 0x43ce
movq %r12, %rdi
callq 0x7520
movq $0x5, 0x30(%rsp)
movq %r15, %rdi
callq 0x752e
jmp 0x65ae
movq (%rbx), %rcx
leaq 0x5f20(%rip), %rdi      # 0xc582
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x3f48
movq (%rbx), %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0x4fbc
movq 0x30(%rsp), %rdx
movq %r13, %rdi
leaq 0x5ebd(%rip), %rsi      # 0xc55f
xorl %eax, %eax
callq 0x3330
movq %r15, %rdi
callq 0x3120
movq (%rbx), %rdi
callq 0x32a0
movq $0x1, 0x20(%rsp)
movq %r14, %rdi
callq 0x5142
movb %al, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
jmp 0x663a
movq (%rbx), %rcx
leaq 0x5e4b(%rip), %rdi      # 0xc542
pushq $0x1c
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x3e8c
movb %al, 0x30(%rsp)
movq (%rbx), %rdi
movzbl %al, %eax
movq 0xcb58(%rip), %rcx      # 0x13280
shll $0x5, %eax
movq (%rcx,%rax), %rdx
leaq 0x5e29(%rip), %rsi      # 0xc55f
xorl %eax, %eax
callq 0x3330
movq (%rbx), %rdi
callq 0x32a0
movq $0x1, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
jmp 0x663a
movq (%rbx), %rcx
leaq 0x5df5(%rip), %rdi      # 0xc56c
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x60(%rsp), %rax
movzbl (%rax), %esi
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x3ef0
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
jmp 0x663a
movq (%rbx), %rcx
leaq 0x5d64(%rip), %rdi      # 0xc527
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
andq $0x0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq 0x58(%rsp), %rsi
shrq $0x2, %rsi
movq %r15, %rdi
callq 0x748a
xorl %r13d, %r13d
movl %r13d, %r13d
cmpq %r13, 0x58(%rsp)
jbe 0x6998
movq 0x60(%rsp), %rax
movl (%rax,%r13), %eax
bswapl %eax
movl %eax, 0xb0(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xae9a
addl $0x4, %r13d
jmp 0x67f8
movq (%rbx), %rcx
leaq 0x5cd2(%rip), %rdi      # 0xc505
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq %r14, %rdi
movq %r12, %rsi
callq 0x7288
movq 0xb0(%rsp), %rbp
movq 0xb8(%rsp), %rax
movq (%rbx), %r13
cmpq %rax, %rbp
je 0x6935
movq %rax, %r12
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4fbc
movq 0x30(%rsp), %rdx
movq %r13, %rdi
leaq 0x583a(%rip), %rsi      # 0xc0c4
xorl %eax, %eax
callq 0x3330
movq %r15, %rdi
callq 0x3120
movq (%rbx), %rdi
callq 0x32a0
addq $0x8, %rbp
movq %r12, %rax
jmp 0x6861
movq (%rbx), %rcx
leaq 0x5c62(%rip), %rdi      # 0xc516
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x3f06
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
jmp 0x663a
movq (%rbx), %rcx
leaq 0x5ca3(%rip), %rdi      # 0xc59b
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x3eea
movb %al, 0x30(%rsp)
movq (%rbx), %rcx
testb %al, %al
je 0x6a3c
leaq 0x5c84(%rip), %rdi      # 0xc5b2
pushq $0x9
jmp 0x6a45
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x32c0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %r13
leaq 0x138(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x42dc
leaq 0x50(%rsp), %r12
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3be0
movq %rbp, %rdi
callq 0x43ce
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
movq %r14, %rdi
jmp 0x6635
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
cmpq %rbp, %r13
je 0x69ca
movl (%r13), %edx
movq (%rbx), %rdi
leaq 0x5b89(%rip), %rsi      # 0xc53e
xorl %eax, %eax
callq 0x3330
movq (%rbx), %rdi
callq 0x32a0
addq $0x4, %r13
jmp 0x69a2
movq (%rbx), %rcx
leaq 0x5718(%rip), %rdi      # 0xc0ec
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
movq (%rax), %r13
leaq 0x100(%rsp), %rdi
movq %r15, %rsi
callq 0x428e
movq %r13, %rdi
leaq 0x100(%rsp), %r13
movq %r13, %rsi
callq 0x3f0c
movq %r13, %rdi
callq 0x4436
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
movq %r15, %rdi
callq 0x4436
jmp 0x663a
leaq 0x5b79(%rip), %rdi      # 0xc5bc
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq $0x1, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
jmp 0x663a
movq (%rbx), %rcx
leaq 0x5b47(%rip), %rdi      # 0xc5cf
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x33f0
movq (%rbx), %rdi
callq 0x32a0
movq 0x8(%rsp), %rax
movl 0x201c(%rax), %esi
leaq 0x18(%rsp), %rdi
callq 0x72da
leaq 0x70(%rsp), %rdi
callq 0x3120
leaq 0x90(%rsp), %rdi
callq 0x3120
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6bf0
jmp 0x6b34
jmp 0x6b34
movq %rax, %r14
movl $0x98, %esi
jmp 0x6b12
jmp 0x6b53
movq %rax, %r14
movl $0xc8, %esi
jmp 0x6b12
jmp 0x6b53
jmp 0x6b0a
jmp 0x6b53
movq %rax, %r14
movl $0xb8, %esi
jmp 0x6b12
jmp 0x6b53
movq %rax, %r14
movl $0xe8, %esi
movq %r15, %rdi
callq 0x3260
jmp 0x6c0c
jmp 0x6b53
jmp 0x6b6a
jmp 0x6b6a
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x3120
jmp 0x6b37
movq %rax, %r14
leaq 0x30(%rsp), %rdi
jmp 0x6c07
jmp 0x6b53
movq %rax, %r14
leaq 0x118(%rsp), %rdi
jmp 0x6c07
movq %rax, %r14
jmp 0x6c0c
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x3120
jmp 0x6b6d
movq %rax, %r14
leaq 0xd0(%rsp), %rdi
jmp 0x6c14
movq %rax, %r14
jmp 0x6c19
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x4436
jmp 0x6bf8
movq %rax, %r14
leaq 0x138(%rsp), %rdi
callq 0x43ce
jmp 0x6bd5
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bd2
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf0
movq %rax, %r14
leaq 0x150(%rsp), %rdi
callq 0x43ce
jmp 0x6be2
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf0
jmp 0x6bf5
movq %rax, %r14
leaq 0xb0(%rsp), %rdi
jmp 0x6be7
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x43ce
jmp 0x6c02
jmp 0x6bf0
movq %rax, %r14
jmp 0x6c02
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4436
leaq 0x70(%rsp), %rdi
callq 0x3120
leaq 0x90(%rsp), %rdi
callq 0x3120
movq 0x10(%rsp), %rdi
callq 0x6e54
movq 0x8(%rsp), %rdi
callq 0x4436
movq %r14, %rdi
callq 0x33b0
nop