// File: kobanium[P]Ray/src/mcts/MoveSelection.cpp
// Function: SelectMove(game_info_t const*, uct_node_t const&, int, double&)
int
SelectMove( const game_info_t *game, const uct_node_t &root, const int color, double &best_wp )
{
  const child_node_t *child = root.child;
  const int select_index = SelectMaxVisitChild(root);
  const double pass_wp = CalculatePassWinningPercentage(root);
  statistic_t statistic[BOARD_MAX];

  CopyStatistic(statistic);

  best_wp = static_cast<double>(child[select_index].win) / child[select_index].move_count;

  if (best_wp < resign_threshold) {
    return RESIGN;
  } else if (game->moves >= MAX_MOVES) {
    return PASS;
  } else if (capture_all_mode) {
    if (child[select_index].pos == PASS &&
        IsRemainingDeadStone(game, color, statistic)) {
      int index = 1, max_count = child[index].move_count;
      for (int i = 2; i < root.child_num; i++) {
        if (child[i].move_count > max_count) {
          index = i;
          max_count = child[i].move_count;
        }
      }
      return child[index].pos;
    } else {
      return child[select_index].pos;
    }
  } else {
    if (pass_wp >= PASS_THRESHOLD &&
        (game->record[game->moves - 1].pos == PASS)) {
      return PASS;
    } else if (game->moves > 3 &&
               game->record[game->moves - 1].pos == PASS &&
               game->record[game->moves - 3].pos == PASS) {
      return PASS;
    } else {
      return child[select_index].pos;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x34f0, %rsp           # imm = 0x34F0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
addq $0x1c, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8ece0
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
callq 0x88260
movsd %xmm0, -0x40(%rbp)
leaq -0x34d0(%rbp), %rdi
callq 0x8ab20
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0xc, %rdi
callq 0x25780
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x34e8(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
movsd -0x34e8(%rbp), %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x28(%rbp), %rax
movsd (%rax), %xmm1
movsd 0x58be0(%rip), %xmm0    # 0xe7a10
ucomisd %xmm1, %xmm0
jbe 0x8ee42
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8f02a
movq -0x10(%rbp), %rax
cmpl $0x43a, 0x43b0(%rax)    # imm = 0x43A
jl 0x8ee5e
movl $0x0, -0x4(%rbp)
jmp 0x8f02a
testb $0x1, 0x1aaee35b(%rip)  # 0x1ab7d1c0
je 0x8ef80
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
cmpl $0x0, %eax
jne 0x8ef66
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq -0x34d0(%rbp), %rdx
callq 0xa3630
testb $0x1, %al
jne 0x8eea2
jmp 0x8ef66
movl $0x1, -0x34d4(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x34d4(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
movl %eax, -0x34d8(%rbp)
movl $0x2, -0x34dc(%rbp)
movl -0x34dc(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x8ef49
movq -0x30(%rbp), %rdi
movslq -0x34dc(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
cmpl -0x34d8(%rbp), %eax
jle 0x8ef36
movl -0x34dc(%rbp), %eax
movl %eax, -0x34d4(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x34dc(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
movl %eax, -0x34d8(%rbp)
jmp 0x8ef38
movl -0x34dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34dc(%rbp)
jmp 0x8eed7
movq -0x30(%rbp), %rax
movslq -0x34d4(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x8f02a
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x8f02a
movsd -0x40(%rbp), %xmm0
movsd 0x1efdb(%rip), %xmm1    # 0xadf68
ucomisd %xmm1, %xmm0
jb 0x8efbd
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x43b0(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x8efbd
movl $0x0, -0x4(%rbp)
jmp 0x8f02a
movq -0x10(%rbp), %rax
cmpl $0x3, 0x43b0(%rax)
jle 0x8f015
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x43b0(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x8f015
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x43b0(%rcx), %ecx
subl $0x3, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x8f015
movl $0x0, -0x4(%rbp)
jmp 0x8f02a
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x34f0, %rsp           # imm = 0x34F0
popq %rbp
retq
nopw %cs:(%rax,%rax)