// File: czinn[P]homeworlds/negamax.cc
// Function: Negamax::get_actions(int)
std::vector<Action> Negamax::get_actions(int depth) {
  if (depth == 0 || root_game->winner() != 0) {
    return std::vector<Action>({Action{PASS}});
  }

  int best = -10000000;
  const Turn *best_turn;
  std::vector<Turn*> turns = get_turns(root_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 a = -10000000;
  int b = 10000000;
  for (const Turn *turn : turns) {
    int value = -negamax(turn->game, depth - 1, -b, -a);
    if (value > best) {
      best = value;
      best_turn = turn;
    }
    /*
    std::cout << "Value: " << value << std::endl;
    for (const Action& action : turn->actions) {
      std::cout << action << std::endl;
    }
    std::cout << std::endl;
    */
    a = std::max(a, value);
    if (a >= b) {
      break;
    }
  }

  std::vector<Action> actions(best_turn->actions.begin(), best_turn->actions.end());
  for (Turn *turn : turns) {
    delete turn;
  }
  return actions;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x150(%rbp)
movq %rdi, %rax
movq %rax, -0x148(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x140(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x1364c
movq -0x140(%rbp), %rax
movq (%rax), %rdi
callq 0x3c80
cmpl $0x0, %eax
je 0x136c4
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x1, -0x20(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x168c0
movq -0x150(%rbp), %rdi
movq -0x158(%rbp), %rcx
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x168e0
jmp 0x1369c
leaq -0x51(%rbp), %rdi
callq 0x16960
jmp 0x139ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x16960
jmp 0x139ff
movq -0x140(%rbp), %rsi
movl $0xff676980, -0x68(%rbp) # imm = 0xFF676980
movq (%rsi), %rdx
leaq -0x88(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x13a10
movq -0x160(%rbp), %rdi
callq 0x16980
movq -0x160(%rbp), %rdi
movq %rax, -0x90(%rbp)
callq 0x169b0
movq %rax, %rcx
movq -0x140(%rbp), %rax
movq %rcx, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x14030
jmp 0x1373b
movl $0xff676980, -0xa4(%rbp) # imm = 0xFF676980
movl $0x989680, -0xa8(%rbp)  # imm = 0x989680
leaq -0x88(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x16980
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x169b0
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x169e0
testb $0x1, %al
jne 0x1379f
jmp 0x1387a
leaq -0xb8(%rbp), %rdi
callq 0x16a20
movq -0x140(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
decl %edx
movl -0xa8(%rbp), %ecx
negl %ecx
movl -0xa4(%rbp), %r8d
negl %r8d
callq 0x14080
movl %eax, -0x164(%rbp)
jmp 0x137ea
movl -0x164(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0x68(%rbp), %eax
jle 0x1382c
movl -0xcc(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x1382c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x139e1
leaq -0xa4(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
callq 0x16a40
movq %rax, -0x170(%rbp)
jmp 0x13848
movq -0x170(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jl 0x13867
jmp 0x1387a
jmp 0x13869
leaq -0xb8(%rbp), %rdi
callq 0x16a80
jmp 0x13783
movb $0x0, -0xcd(%rbp)
movq -0x70(%rbp), %rsi
addq $0x8, %rsi
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x16aa0
movq -0x70(%rbp), %rsi
addq $0x8, %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x16ad0
leaq -0x111(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x168c0
movq -0x150(%rbp), %rdi
movq -0x188(%rbp), %rsi
movq -0x180(%rbp), %rdx
movq -0x178(%rbp), %rcx
callq 0x16b00
jmp 0x138ed
leaq -0x111(%rbp), %rdi
callq 0x16960
leaq -0x88(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x16980
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x169b0
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x169e0
testb $0x1, %al
jne 0x13946
jmp 0x139b7
leaq -0x128(%rbp), %rdi
callq 0x16a20
movq (%rax), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x190(%rbp)
cmpq $0x0, %rax
je 0x1398d
movq -0x190(%rbp), %rdi
callq 0x13580
movq -0x190(%rbp), %rdi
movl $0x58, %esi
callq 0x3240
jmp 0x1398f
leaq -0x128(%rbp), %rdi
callq 0x16a80
jmp 0x1392d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x111(%rbp), %rdi
callq 0x16960
jmp 0x139e1
movb $0x1, -0xcd(%rbp)
testb $0x1, -0xcd(%rbp)
jne 0x139d3
movq -0x150(%rbp), %rdi
callq 0x16bb0
leaq -0x88(%rbp), %rdi
callq 0x16c10
jmp 0x139ef
leaq -0x88(%rbp), %rdi
callq 0x16c10
jmp 0x139ff
movq -0x148(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x33c0
nopl (%rax,%rax)