// File: /workspace/github_cpp_makefile_star2_O2/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 0x8e4a(%rip), %rax      # 0xbb70
movq (%rax), %rax
movl (%rax), %eax
cmpl $0x0, 0x50(%rsp,%rax,4)
jle 0x2db0
leaq 0x50(%rsp), %rdi
callq 0x31e0
movl %eax, %ebp
movq (%rbx), %r14
movq 0x8(%rbx), %rax
subq %r14, %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
movl 0x18(%rbx), %r15d
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x2d83
cmpl %ecx, %r15d
je 0x2d7f
imulq $0x28, %rcx, %r13
addq %r14, %r13
movq 0x18(%r13), %rdx
cmpq %rdx, 0x10(%r13)
je 0x2d7f
movq -0x8(%rdx), %rsi
cmpl %esi, %ebp
je 0x2f64
incl %ecx
jmp 0x2d56
leaq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rbx), %r13
leaq 0x28(%rbx), %r12
cmpq %r12, %r13
je 0x2db7
cmpl 0x20(%r13), %ebp
jge 0x2e48
movq %r13, %rdi
callq 0x2190
movq %rax, %r13
jmp 0x2d94
movl 0x18(%rbx), %r15d
movq (%rbx), %r14
movq 0x947a(%rip), %rax      # 0xc238
movq %rax, 0x8(%rsp)
movl %r15d, %eax
imulq $0x28, %rax, %rdx
leaq (%r14,%rdx), %rax
movq 0x18(%r14,%rdx), %rcx
cmpq %rcx, 0x10(%r14,%rdx)
je 0x2eb8
movq -0x8(%rcx), %rdx
addq $-0x8, %rcx
movq %rcx, 0x18(%rax)
leaq 0x8(%rsp), %r14
movq %rdx, (%r14)
leaq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x372e
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
imulq $0x28, %rax, %rax
movq (%rcx,%rax), %rsi
addq $0x8, %rsi
movq 0x91b9(%rip), %rdi      # 0xbfd0
callq 0x2140
leaq 0x4328(%rip), %rsi      # 0x714b
movq %rax, %rdi
callq 0x2160
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x2893
leaq 0x4316(%rip), %rsi      # 0x7156
movq %r15, %rdi
jmp 0x2ed5
movq 0x20(%r13), %rsi
leaq 0x8(%rsp), %rbp
movq %rsi, (%rbp)
imulq $0x28, %r15, %rax
addq %rax, %r14
movq %r14, %rdi
callq 0x324a
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x36e2
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
imulq $0x28, %rax, %rax
movq (%rcx,%rax), %rsi
addq $0x8, %rsi
movq 0x9146(%rip), %rdi      # 0xbfd0
callq 0x2140
leaq 0x427d(%rip), %rsi      # 0x7113
movq %rax, %rdi
callq 0x2160
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2893
leaq 0x4287(%rip), %rsi      # 0x713a
movq %rbx, %rdi
jmp 0x2f14
movq (%rax), %rsi
addq $0x8, %rsi
movq 0x910a(%rip), %rdi      # 0xbfd0
callq 0x2140
leaq 0x4293(%rip), %rsi      # 0x7165
movq %rax, %rdi
callq 0x2160
cmpq $0x0, 0x48(%rbx)
je 0x2ff8
movq 0x38(%rbx), %rax
movq 0x20(%rax), %xmm0
movq 0x8(%rsp), %xmm1
pcmpeqd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm0     # xmm0 = xmm1[0,0,1,1]
movmskpd %xmm0, %eax
cmpl $0x3, %eax
jne 0x2f1e
movq 0x90c3(%rip), %rdi      # 0xbfd0
leaq 0x42a3(%rip), %rsi      # 0x71b7
callq 0x2160
jmp 0x2fe9
leaq 0x20(%rbx), %r14
movq 0x90a7(%rip), %rdi      # 0xbfd0
leaq 0x42ca(%rip), %rsi      # 0x71fa
callq 0x2160
movq %rax, %r15
movq 0x38(%rbx), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x2893
pushq $0xa
popq %rsi
movq %r15, %rdi
callq 0x21a0
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x36e2
jmp 0x2fe9
addq $-0x8, %rdx
movq %rdx, 0x18(%r13)
leaq 0x8(%rsp), %r12
movq %rsi, (%r12)
imulq $0x28, %r15, %rax
addq %rax, %r14
movq %r14, %rdi
callq 0x324a
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
imulq $0x28, %rax, %rax
movq (%rcx,%rax), %rsi
addq $0x8, %rsi
movq 0x9033(%rip), %rdi      # 0xbfd0
callq 0x2140
leaq 0x4182(%rip), %rsi      # 0x712b
movq %rax, %rdi
callq 0x2160
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2893
leaq 0x416d(%rip), %rsi      # 0x7133
movq %rbx, %rdi
callq 0x2160
movq (%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0x2140
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x21a0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x417f(%rip), %rdi      # 0x717e
leaq 0x4189(%rip), %rsi      # 0x718f
leaq 0x418a(%rip), %rcx      # 0x7197
movl $0xb8, %edx
callq 0x2100
nop