// File: /workspace/github_cpp_makefile_star2_O3/czinn[P]homeworlds/negamax.cc
// Function: Negamax::negamax(Game const*, int, int, int)
int Negamax::negamax(const Game *game, int depth, int a, int b) {
  int olda = a;

  int h = game->hash();
  if (transpositions.count(h) > 0) {
    Transposition t = transpositions[h];
    if (t.depth >= depth) {
      if (t.flag == EXACT) {
        return t.value;
      } else if (t.flag == LOWERBOUND) {
        a = std::max(a, t.value);
      } else if (t.flag == UPPERBOUND) {
        a = std::min(b, t.value);
      }
      if (a >= b) {
        return t.value;
      }
    }
  }

  if (depth == 0 || game->winner() != 0) {
    return heuristic(game);
  }

  int best = -10000000;
  std::vector<Turn*> turns = get_turns(game);
  std::sort(turns.begin(), turns.end(),
      [this](const Turn *a, const Turn *b) {
        int ha = a->game->hash();
        int hb = b->game->hash();
        int sa = this->transpositions.count(ha) > 0 ? this->transpositions[ha].value : 0;
        int sb = this->transpositions.count(hb) > 0 ? this->transpositions[hb].value : 0;
        return sa > sb;
      });
  int num = 0;
  for (const Turn *turn : turns) {
    int value = -negamax(turn->game, depth - 1, -b, -a);
    best = std::max(best, value);
    a = std::max(a, value);
    num++;
    if (a >= b) {
      break;
    }
  }
  for (Turn *turn : turns) {
    delete turn;
  }

  Transposition t{best, depth};
  if (best <= olda) {
    t.flag = UPPERBOUND;
  } else if (best >= b) {
    t.flag = LOWERBOUND;
  } else {
    t.flag = EXACT;
  }
  transpositions[h] = t;

  return best;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, 0x4(%rsp)
movl %ecx, %r14d
movl %edx, %r12d
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x29de
leaq 0x14(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, 0x8(%rsp)
addq $0x8, %r15
movq %r15, %rdi
callq 0x726c
movl %r14d, %ebp
testq %rax, %rax
je 0x602f
leaq 0x14(%rsp), %rsi
movq %r15, %rdi
callq 0x72c8
movl %r14d, %ebp
cmpl %r12d, 0x4(%rax)
jl 0x602f
movl (%rax), %r13d
movl 0x8(%rax), %eax
testl %eax, %eax
je 0x6050
cmpl $0x2, %eax
je 0x601e
movl %r14d, %ebp
cmpl $0x1, %eax
jne 0x6029
cmpl %r14d, %r13d
movl %r14d, %ebp
cmovgl %r13d, %ebp
jmp 0x6029
movl 0x4(%rsp), %ebp
cmpl %ebp, %r13d
cmovll %r13d, %ebp
cmpl 0x4(%rsp), %ebp
jge 0x6050
testl %r12d, %r12d
je 0x6040
movq %rbx, %rdi
callq 0x283e
testl %eax, %eax
je 0x6062
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x6232
movl %eax, %r13d
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x40(%rsp)
movl %r14d, 0x24(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x5b52
movq (%r13), %r14
movq 0x8(%r13), %rbx
cmpq %rbx, %r14
je 0x6100
movq %rbx, %r15
subq %r14, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rcx
callq 0x6833
cmpq $0x81, %r15
jl 0x60f0
leaq 0x80(%r14), %r13
movq %r14, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x6c47
cmpq %rbx, %r13
je 0x6100
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x6cc6
addq $0x8, %r13
jmp 0x60d8
movq %r14, %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
callq 0x6c47
movq %r12, 0x18(%rsp)
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r15
cmpq %r15, %r12
je 0x61ac
movq 0x18(%rsp), %rax
leal -0x1(%rax), %ebx
movl 0x4(%rsp), %r14d
negl %r14d
addq $0x8, %r12
movl $0xff676980, %r13d      # imm = 0xFF676980
movq -0x8(%r12), %rax
movq (%rax), %rsi
movl %ebp, %r8d
negl %r8d
movq 0x8(%rsp), %rdi
movl %ebx, %edx
movl %r14d, %ecx
callq 0x5f9e
negl %eax
cmpl %eax, %r13d
cmovlel %eax, %r13d
cmpl %eax, %ebp
cmovlel %eax, %ebp
cmpl 0x4(%rsp), %ebp
jge 0x6170
leaq 0x8(%r12), %rax
cmpq %r15, %r12
movq %rax, %r12
jne 0x6132
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r15
cmpq %r15, %r14
je 0x61b2
movq 0x18(%rsp), %r12
movq (%r14), %rbx
testq %rbx, %rbx
je 0x6194
movq %rbx, %rdi
callq 0x58d8
movl $0x58, %esi
movq %rbx, %rdi
callq 0x21d0
addq $0x8, %r14
cmpq %r15, %r14
jne 0x6184
jmp 0x61b7
movl $0xff676980, %r13d      # imm = 0xFF676980
movq 0x18(%rsp), %r12
leaq 0x14(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x72c8
movl 0x24(%rsp), %edx
xorl %ecx, %ecx
cmpl 0x4(%rsp), %r13d
setge %cl
cmpl %edx, %r13d
movl $0x2, %edx
cmovgl %ecx, %edx
movl %r13d, (%rax)
movl %r12d, 0x4(%rax)
movl %edx, 0x8(%rax)
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6050
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x6050
jmp 0x620f
jmp 0x620f
jmp 0x620f
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6229
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22d0
nop