// File: /workspace/github_cpp_makefile_star2_O2/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, 0xc(%rsp)
movl %ecx, %r12d
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x28d0
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, 0x10(%rsp)
addq $0x8, %r15
movq %r15, %rdi
callq 0x6f1a
movl %r12d, %ebp
testq %rax, %rax
je 0x5da7
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x6faa
movl %r12d, %ebp
cmpl %r14d, 0x4(%rax)
jl 0x5da7
movl (%rax), %r13d
movl 0x8(%rax), %eax
testl %eax, %eax
je 0x5f37
cmpl $0x2, %eax
je 0x5d92
movl %r12d, %ebp
cmpl $0x1, %eax
jne 0x5d9d
cmpl %r12d, %r13d
movl %r12d, %ebp
cmovgl %r13d, %ebp
jmp 0x5d9d
movl 0xc(%rsp), %ebp
cmpl %ebp, %r13d
cmovll %r13d, %ebp
cmpl 0xc(%rsp), %ebp
jge 0x5f37
testl %r14d, %r14d
je 0x5db8
movq %rbx, %rdi
callq 0x2754
testl %eax, %eax
je 0x5dcd
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5f64
movl %eax, %r13d
jmp 0x5f37
movq %r15, 0x40(%rsp)
movq %r14, 0x20(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x5a42
movq (%r13), %r14
movq 0x8(%r13), %rbx
cmpq %rbx, %r14
je 0x5e6b
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 0x10(%rsp), %rcx
callq 0x6499
cmpq $0x81, %r15
jl 0x5e5b
leaq 0x80(%r14), %r13
movq %r14, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x684c
cmpq %rbx, %r13
je 0x5e6b
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0x68d7
addq $0x8, %r13
jmp 0x5e43
movq %r14, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
callq 0x684c
movl %r12d, 0x1c(%rsp)
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x20(%rsp), %rax
leal -0x1(%rax), %ebx
movl 0xc(%rsp), %r14d
negl %r14d
movl $0xff676980, %r13d      # imm = 0xFF676980
cmpq %r12, %r15
je 0x5ec8
movq (%r15), %rax
movq (%rax), %rsi
movl %ebp, %r8d
negl %r8d
movq 0x10(%rsp), %rdi
movl %ebx, %edx
movl %r14d, %ecx
callq 0x5d0e
negl %eax
cmpl %eax, %r13d
cmovlel %eax, %r13d
cmpl %eax, %ebp
cmovlel %eax, %ebp
addq $0x8, %r15
cmpl 0xc(%rsp), %ebp
jl 0x5e90
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r12
pushq $0x58
popq %rbx
cmpq %r12, %r15
je 0x5efb
movq (%r15), %r14
testq %r14, %r14
je 0x5eea
movq %r14, %rdi
callq 0x57f0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21f0
addq $0x8, %r15
jmp 0x5ed5
leaq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x6faa
xorl %ecx, %ecx
cmpl 0xc(%rsp), %r13d
setge %cl
cmpl 0x1c(%rsp), %r13d
pushq $0x2
popq %rdx
cmovgl %ecx, %edx
movl %r13d, (%rax)
movq 0x20(%rsp), %rcx
movl %ecx, 0x4(%rax)
movl %edx, 0x8(%rax)
leaq 0x28(%rsp), %rdi
callq 0x6ee6
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5f4f
jmp 0x5f4f
jmp 0x5f4f
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6ee6
movq %rbx, %rdi
callq 0x2300