// File: /workspace/github_cpp_makefile_star2_O3/ttencate[P]pickomino/game.cc
// Function: Game::takeOrLoseTile(Dice)
void Game::takeOrLoseTile(Dice taken) {
  if (taken.contains(DieSide::WORM)) {
    Score score = taken.sum();

    for (unsigned i = 0; i < m_players.size(); i++) {
      Player &victim = m_players[i];
      if (i != m_currentPlayer && victim.hasTiles() && victim.topTile().score() == score) {
        Tile stolen = victim.popTile();
        currentPlayer().takeTile(stolen);
        cout << currentPlayer().name() << " stole " << stolen << " from " << victim.name() << '\n';
        return;
      }
    }

    for (auto i = m_tiles.begin(); i != m_tiles.end(); i++) {
      if (score >= i->score()) {
        Tile taken = *i;
        currentPlayer().takeTile(taken);
        m_tiles.erase(i);
        cout << currentPlayer().name() << " took " << taken << " from the table\n";
        return;
      }
    }
  }

  Tile returned = Tile::INVALID;
  if (currentPlayer().hasTiles()) {
    returned = currentPlayer().popTile();
    m_tiles.insert(returned);  
    cout << currentPlayer().name() << " returned " << returned << " to the table\n";
  } else {
    cout << currentPlayer().name() << " has nothing to hand in\n";
  }

  assert(!m_tiles.empty());
  if (*m_tiles.begin() == returned) {
    cout << "Returned tile is highest on the table, not turning any tiles over\n";
  } else {
    cout << "Turning over highest tile, " << *m_tiles.begin() << '\n';
    m_tiles.erase(m_tiles.begin());
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x7b5a(%rip), %rax      # 0xab70
movq (%rax), %rax
movl (%rax), %eax
cmpl $0x0, 0x50(%rsp,%rax,4)
jle 0x3055
leaq 0x81c7(%rip), %rcx      # 0xb1f0
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
je 0x305d
leaq 0x50(%rsp), %rdx
xorl %ebp, %ebp
movq (%rax), %rsi
movl (%rsi), %edi
movl 0x4(%rsi), %esi
imull (%rdx,%rdi,4), %esi
addl %esi, %ebp
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x303c
jmp 0x305f
movq (%rbx), %r13
jmp 0x30ee
xorl %ebp, %ebp
movq (%rbx), %r13
movq 0x8(%rbx), %rax
subq %r13, %rax
je 0x30c3
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
movl 0x18(%rbx), %eax
movl %eax, %edx
negl %edx
movl $0x1, %edi
xorl %esi, %esi
leal (%rdx,%rdi), %r8d
cmpl $0x1, %r8d
je 0x30ba
leaq (%rsi,%rsi,4), %rsi
leaq (,%rsi,8), %r15
addq %r13, %r15
movq 0x18(%r15), %r8
cmpq %r8, 0x10(%r15)
je 0x30ba
movq -0x8(%r8), %rsi
cmpl %esi, %ebp
je 0x32d5
movl %edi, %esi
incl %edi
cmpq %rsi, %rcx
ja 0x308b
movq 0x38(%rbx), %r15
leaq 0x28(%rbx), %r14
cmpq %r14, %r15
je 0x30ee
leaq 0x20(%rbx), %r12
cmpl 0x20(%r15), %ebp
jge 0x324f
movq %r15, %rdi
callq 0x2190
movq %rax, %r15
cmpq %r14, %rax
jne 0x30d4
movq 0x814b(%rip), %rax      # 0xb240
movq %rax, 0x10(%rsp)
movl 0x18(%rbx), %eax
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %rax
addq %r13, %rax
movq 0x18(%rax), %rcx
cmpq %rcx, 0x10(%rax)
je 0x318b
movq -0x8(%rcx), %rdx
addq $-0x8, %rcx
movq %rcx, 0x18(%rax)
leaq 0x10(%rsp), %r14
movq %rdx, (%r14)
leaq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x3a2a
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x7e7d(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %r15
leaq 0x3fe9(%rip), %rsi      # 0x714b
movl $0xa, %edx
movq %rax, %rdi
callq 0x2180
movq %r15, %rdi
movq %r14, %rsi
callq 0x29f7
leaq 0x3fd5(%rip), %rsi      # 0x7156
movl $0xe, %edx
movq %r15, %rdi
jmp 0x31b1
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x7e33(%rip), %rdi      # 0xafd0
callq 0x2180
leaq 0x3fbc(%rip), %rsi      # 0x7165
movl $0x18, %edx
movq %rax, %rdi
callq 0x2180
cmpq $0x0, 0x48(%rbx)
je 0x3388
movq 0x38(%rbx), %rax
movq 0x20(%rax), %xmm0
movq 0x10(%rsp), %xmm1
pcmpeqd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm0     # xmm0 = xmm1[0,0,1,1]
movmskpd %xmm0, %eax
cmpl $0x3, %eax
jne 0x31fa
movq 0x7de7(%rip), %rdi      # 0xafd0
leaq 0x3fc7(%rip), %rsi      # 0x71b7
movl $0x42, %edx
jmp 0x3374
leaq 0x20(%rbx), %r14
movq 0x7dcb(%rip), %r15      # 0xafd0
leaq 0x3fee(%rip), %rsi      # 0x71fa
movl $0x1b, %edx
movq %r15, %rdi
callq 0x2180
movq 0x38(%rbx), %rsi
addq $0x20, %rsi
movq %r15, %rdi
callq 0x29f7
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %r15, %rdi
callq 0x2180
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x39f2
jmp 0x3379
movq 0x20(%r15), %rsi
leaq 0x10(%rsp), %r14
movq %rsi, (%r14)
movl 0x18(%rbx), %eax
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
callq 0x362e
movq %r12, %rdi
movq %r15, %rsi
callq 0x39f2
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x7d36(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %rbx
leaq 0x3e6a(%rip), %rsi      # 0x7113
movl $0x6, %edx
movq %rax, %rdi
callq 0x2180
movq %rbx, %rdi
movq %r14, %rsi
callq 0x29f7
leaq 0x3e72(%rip), %rsi      # 0x713a
movl $0x10, %edx
movq %rbx, %rdi
jmp 0x3374
addq $-0x8, %r8
movq %r8, 0x18(%r15)
leaq 0x10(%rsp), %r14
movq %rsi, (%r14)
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %rdi
addq %r13, %rdi
callq 0x362e
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x7cba(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %rbx
leaq 0x3e06(%rip), %rsi      # 0x712b
movl $0x7, %edx
movq %rax, %rdi
callq 0x2180
movq %rbx, %rdi
movq %r14, %rsi
callq 0x29f7
leaq 0x3def(%rip), %rsi      # 0x7133
movl $0x6, %edx
movq %rbx, %rdi
callq 0x2180
movq (%r15), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %rbx, %rdi
callq 0x2180
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x2180
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3def(%rip), %rdi      # 0x717e
leaq 0x3df9(%rip), %rsi      # 0x718f
leaq 0x3dfa(%rip), %rcx      # 0x7197
movl $0xb8, %edx
callq 0x20e0
nop