// File: 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:
subq $0xe8, %rsp
movq %rdi, %rax
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x16935(%rip), %rax     # 0x1ab38
movq (%rax), %rsi
callq 0x4af0
testb $0x1, %al
jne 0x4214
jmp 0x4515
movq 0x30(%rsp), %rdi
callq 0x4b20
movl %eax, 0xdc(%rsp)
movl $0x0, 0xd8(%rsp)
movq 0x38(%rsp), %rdi
movl 0xd8(%rsp), %eax
movq %rax, 0x28(%rsp)
callq 0x4f60
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x43b9
movq 0x38(%rsp), %rdi
movl 0xd8(%rsp), %eax
movl %eax, %esi
callq 0x53d0
movq 0x38(%rsp), %rdx
movq %rax, 0xd0(%rsp)
movl 0xd8(%rsp), %ecx
xorl %eax, %eax
cmpl 0x18(%rdx), %ecx
movb %al, 0x27(%rsp)
je 0x42d5
movq 0xd0(%rsp), %rdi
callq 0x4cd0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x27(%rsp)
jne 0x42a5
jmp 0x42d5
movq 0xd0(%rsp), %rdi
callq 0x4cf0
movq %rax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x4ae0
cmpl 0xdc(%rsp), %eax
sete %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x42e2
jmp 0x43a1
movq 0xd0(%rsp), %rdi
callq 0x53f0
movq 0x38(%rsp), %rdi
movq %rax, 0xc0(%rsp)
callq 0x4f10
movq %rax, %rdi
movq 0xc0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rsi
callq 0x5430
movq 0x38(%rsp), %rdi
callq 0x4f10
movq %rax, %rdi
callq 0x4f30
movq %rax, %rsi
movq 0x16c93(%rip), %rdi     # 0x1afd0
callq 0x2150
movq %rax, %rdi
leaq 0xdd53(%rip), %rsi      # 0x1209f
callq 0x2170
movq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x39b0
movq %rax, %rdi
leaq 0xdd3c(%rip), %rsi      # 0x120a7
callq 0x2170
movq %rax, 0x18(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x4f30
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x2150
movq %rax, %rdi
movl $0xa, %esi
callq 0x21c0
jmp 0x46f6
jmp 0x43a3
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0x4230
movq 0x38(%rsp), %rdi
addq $0x20, %rdi
callq 0x4d40
movq %rax, 0xb0(%rsp)
movq 0x38(%rsp), %rdi
addq $0x20, %rdi
callq 0x4d70
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4da0
testb $0x1, %al
jne 0x4403
jmp 0x4513
movl 0xdc(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5460
movq %rax, %rdi
callq 0x4ae0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jl 0x44f5
leaq 0xb0(%rsp), %rdi
callq 0x4dc0
movq 0x38(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xa0(%rsp)
callq 0x4f10
movq %rax, %rdi
movq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rsi
callq 0x5430
movq 0x38(%rsp), %rdi
addq $0x20, %rdi
movq 0xb0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rsi
callq 0x5480
movq 0x38(%rsp), %rdi
movq %rax, 0x88(%rsp)
callq 0x4f10
movq %rax, %rdi
callq 0x4f30
movq %rax, %rsi
movq 0x16b13(%rip), %rdi     # 0x1afd0
callq 0x2150
movq %rax, %rdi
leaq 0xdbbb(%rip), %rsi      # 0x12087
callq 0x2170
movq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x39b0
movq %rax, %rdi
leaq 0xdbc3(%rip), %rsi      # 0x120ae
callq 0x2170
jmp 0x46f6
jmp 0x44f7
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x54d0
movq %rax, 0x80(%rsp)
jmp 0x43cf
jmp 0x4515
movq 0x38(%rsp), %rdi
movq 0x16d47(%rip), %rax     # 0x1b268
movq %rax, 0x78(%rsp)
callq 0x4f10
movq %rax, %rdi
callq 0x4cd0
testb $0x1, %al
jne 0x453c
jmp 0x45c7
movq 0x38(%rsp), %rdi
callq 0x4f10
movq %rax, %rdi
callq 0x53f0
movq 0x38(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x78(%rsp)
addq $0x20, %rdi
leaq 0x78(%rsp), %rsi
callq 0x5510
movq 0x38(%rsp), %rdi
movq %rax, 0x60(%rsp)
movb %dl, 0x68(%rsp)
callq 0x4f10
movq %rax, %rdi
callq 0x4f30
movq %rax, %rsi
movq 0x16a3b(%rip), %rdi     # 0x1afd0
callq 0x2150
movq %rax, %rdi
leaq 0xdb1b(%rip), %rsi      # 0x120bf
callq 0x2170
movq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0x39b0
movq %rax, %rdi
leaq 0xdb0a(%rip), %rsi      # 0x120ca
callq 0x2170
jmp 0x45f7
movq 0x38(%rsp), %rdi
callq 0x4f10
movq %rax, %rdi
callq 0x4f30
movq %rax, %rsi
movq 0x169ed(%rip), %rdi     # 0x1afd0
callq 0x2150
movq %rax, %rdi
leaq 0xdae7(%rip), %rsi      # 0x120d9
callq 0x2170
movq 0x38(%rsp), %rdi
addq $0x20, %rdi
callq 0x51d0
xorb $-0x1, %al
testb $0x1, %al
jne 0x460d
jmp 0x460f
jmp 0x462e
leaq 0xdadc(%rip), %rdi      # 0x120f2
leaq 0xdae6(%rip), %rsi      # 0x12103
movl $0xb8, %edx
leaq 0xdae2(%rip), %rcx      # 0x1210b
callq 0x2100
movq 0x38(%rsp), %rdi
addq $0x20, %rdi
callq 0x4d40
movq %rax, 0x58(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x4dc0
movq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0x3940
testb $0x1, %al
jne 0x465e
jmp 0x4676
movq 0x1696b(%rip), %rdi     # 0x1afd0
leaq 0xdabf(%rip), %rsi      # 0x1212b
callq 0x2170
jmp 0x46f6
movq 0x16953(%rip), %rdi     # 0x1afd0
leaq 0xdaea(%rip), %rsi      # 0x1216e
callq 0x2170
movq 0x38(%rsp), %rdi
movq %rax, (%rsp)
addq $0x20, %rdi
callq 0x4d40
movq %rax, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x4dc0
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x39b0
movq %rax, %rdi
movl $0xa, %esi
callq 0x21c0
movq 0x38(%rsp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, 0x8(%rsp)
addq $0x20, %rdi
callq 0x4d40
movq 0x8(%rsp), %rdi
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rsi
callq 0x5480
movq %rax, 0x40(%rsp)
addq $0xe8, %rsp
retq
nop