// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
leaq 0x7af8(%rip), %rax      # 0xab38
movq (%rax), %rax
movl (%rax), %eax
cmpl $0x0, 0x40(%rsp,%rax,4)
jle 0x3110
leaq 0x8199(%rip), %rcx      # 0xb1f0
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
je 0x3083
leaq 0x40(%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 0x306a
jmp 0x3085
xorl %ebp, %ebp
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
je 0x30e5
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
movl 0x18(%rbx), %ecx
movl %ecx, %edi
negl %edi
movl $0x1, %r8d
xorl %esi, %esi
leal (%rdi,%r8), %r9d
cmpl $0x1, %r9d
je 0x30da
leaq (%rsi,%rsi,4), %rsi
leaq (%rax,%rsi,8), %r15
movq 0x18(%r15), %r9
cmpq %r9, 0x10(%r15)
je 0x30da
movq -0x8(%r9), %rsi
cmpl %esi, %ebp
je 0x32ef
movl %r8d, %esi
incl %r8d
cmpq %rsi, %rdx
ja 0x30b2
movq 0x38(%rbx), %r15
leaq 0x28(%rbx), %r14
cmpq %r14, %r15
je 0x3110
leaq 0x20(%rbx), %r12
cmpl 0x20(%r15), %ebp
jge 0x326d
movq %r15, %rdi
callq 0x2190
movq %rax, %r15
cmpq %r14, %rax
jne 0x30f6
movq 0x8129(%rip), %rax      # 0xb240
movq %rax, 0x8(%rsp)
movl 0x18(%rbx), %eax
movq (%rbx), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rcx
cmpq %rcx, 0x10(%rax)
je 0x31a9
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 0x3a64
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 0x7e5f(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %r15
leaq 0x3fdb(%rip), %rsi      # 0x715b
movl $0xa, %edx
movq %rax, %rdi
callq 0x2180
movq %r15, %rdi
movq %r14, %rsi
callq 0x2a03
leaq 0x3fc7(%rip), %rsi      # 0x7166
movl $0xe, %edx
movq %r15, %rdi
jmp 0x31cf
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x7e15(%rip), %rdi      # 0xafd0
callq 0x2180
leaq 0x3fae(%rip), %rsi      # 0x7175
movl $0x18, %edx
movq %rax, %rdi
callq 0x2180
cmpq $0x0, 0x48(%rbx)
je 0x3399
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 0x3218
movq 0x7dc9(%rip), %rdi      # 0xafd0
leaq 0x3fb9(%rip), %rsi      # 0x71c7
movl $0x42, %edx
jmp 0x3387
leaq 0x20(%rbx), %r14
movq 0x7dad(%rip), %r15      # 0xafd0
leaq 0x3fe0(%rip), %rsi      # 0x720a
movl $0x1b, %edx
movq %r15, %rdi
callq 0x2180
movq 0x38(%rbx), %rsi
addq $0x20, %rsi
movq %r15, %rdi
callq 0x2a03
leaq 0x7(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %r15, %rdi
callq 0x2180
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x3a2c
jmp 0x338c
movq 0x20(%r15), %rsi
leaq 0x8(%rsp), %r14
movq %rsi, (%r14)
movl 0x18(%rbx), %eax
leaq (%rax,%rax,4), %rdi
shlq $0x3, %rdi
addq (%rbx), %rdi
callq 0x3634
movq %r12, %rdi
movq %r15, %rsi
callq 0x3a2c
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 0x7d1c(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %rbx
leaq 0x3e60(%rip), %rsi      # 0x7123
movl $0x6, %edx
movq %rax, %rdi
callq 0x2180
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a03
leaq 0x3e68(%rip), %rsi      # 0x714a
movl $0x10, %edx
movq %rbx, %rdi
jmp 0x3387
addq $-0x8, %r9
movq %r9, 0x18(%r15)
leaq 0x8(%rsp), %r14
movq %rsi, (%r14)
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rdi
callq 0x3634
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 0x7ca7(%rip), %rdi      # 0xafd0
callq 0x2180
movq %rax, %rbx
leaq 0x3e03(%rip), %rsi      # 0x713b
movl $0x7, %edx
movq %rax, %rdi
callq 0x2180
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a03
leaq 0x3dec(%rip), %rsi      # 0x7143
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 0x7(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x2180
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3dee(%rip), %rdi      # 0x718e
leaq 0x3df8(%rip), %rsi      # 0x719f
leaq 0x3df9(%rip), %rcx      # 0x71a7
movl $0xb8, %edx
callq 0x20e0