// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d
movl %edx, %r12d
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x2a0c
leaq 0x18(%rsp), %rsi
movl %eax, (%rsi)
movq %r15, 0x10(%rsp)
addq $0x8, %r15
movq %r15, %rdi
callq 0x742a
movl %r14d, %ebp
testq %rax, %rax
je 0x611d
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x74a4
cmpl %r12d, 0x4(%rax)
jge 0x60dd
movb $0x1, %al
movl %r14d, %ebp
jmp 0x6119
movl (%rax), %r13d
movl 0x8(%rax), %eax
testl %eax, %eax
je 0x6114
cmpl $0x2, %eax
je 0x6100
movl %r14d, %ebp
cmpl $0x1, %eax
jne 0x610b
cmpl %r14d, %r13d
movl %r14d, %ebp
cmovgl %r13d, %ebp
jmp 0x610b
movl 0xc(%rsp), %ebp
cmpl %ebp, %r13d
cmovll %r13d, %ebp
cmpl 0xc(%rsp), %ebp
setl %al
jmp 0x6119
xorl %eax, %eax
movl %r14d, %ebp
testb %al, %al
je 0x613e
testl %r12d, %r12d
je 0x612e
movq %rbx, %rdi
callq 0x2822
testl %eax, %eax
je 0x6150
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x6314
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, 0x1c(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x5c5c
movq (%r13), %r14
movq 0x8(%r13), %rbx
cmpq %rbx, %r14
je 0x61ee
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 0x6971
cmpq $0x81, %r15
jl 0x61de
leaq 0x80(%r14), %r13
movq %r14, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x6dd2
cmpq %rbx, %r13
je 0x61ee
movq %r13, %rdi
movq 0x10(%rsp), %rsi
callq 0x6e62
addq $0x8, %r13
jmp 0x61c6
movq %r14, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
callq 0x6dd2
movq %r12, 0x20(%rsp)
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r15
cmpq %r15, %r12
je 0x625c
movq 0x20(%rsp), %rax
leal -0x1(%rax), %ebx
movl 0xc(%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 0x10(%rsp), %rdi
movl %ebx, %edx
movl %r14d, %ecx
callq 0x607c
negl %eax
cmpl %eax, %r13d
cmovlel %eax, %r13d
cmpl %eax, %ebp
cmovlel %eax, %ebp
cmpl 0xc(%rsp), %ebp
jge 0x6262
leaq 0x8(%r12), %rax
cmpq %r15, %r12
movq %rax, %r12
jne 0x621c
jmp 0x6262
movl $0xff676980, %r13d      # imm = 0xFF676980
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %r15
cmpq %r15, %r14
movq 0x20(%rsp), %r12
je 0x629c
movq (%r14), %rbx
testq %rbx, %rbx
je 0x6293
movq %rbx, %rdi
callq 0x59cc
movl $0x58, %esi
movq %rbx, %rdi
callq 0x21d0
addq $0x8, %r14
cmpq %r15, %r14
jne 0x6276
leaq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x74a4
xorl %ecx, %ecx
cmpl 0xc(%rsp), %r13d
setge %cl
cmpl 0x1c(%rsp), %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 0x613e
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
jmp 0x613e
jmp 0x62f2
jmp 0x62f2
jmp 0x62f2
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x630c
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x22d0