// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x34a0, %rsp           # imm = 0x34A0
movq %rcx, %r15
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0x35684
movl %eax, %r12d
movq %rbx, %rdi
callq 0x328c8
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x33e38
movslq %r12d, %rax
imulq $0x1c, %rax, %rdx
movl 0x28(%rbx,%rdx), %eax
cvtsi2sd %eax, %xmm0
movl 0x20(%rbx,%rdx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%r15)
movsd 0x2c16c(%rip), %xmm1    # 0x61880
ucomisd %xmm0, %xmm1
jbe 0x35722
pushq $-0x1
popq %rax
jmp 0x357e5
movslq 0x43b0(%r14), %rcx
xorl %eax, %eax
cmpq $0x439, %rcx            # imm = 0x439
jg 0x357e5
leaq (%rbx,%rdx), %r15
addq $0x1c, %r15
cmpb $0x1, 0x1aac18f9(%rip)  # 0x1aaf7040
jne 0x35798
movzwl (%r15), %eax
testw %ax, %ax
jne 0x357e4
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl %ebp, %esi
callq 0x3f440
testb %al, %al
je 0x357e0
leaq 0x1c(%rbx), %rax
movl 0x3c(%rbx), %edx
leaq 0x58(%rbx), %rcx
pushq $0x1
popq %rdi
pushq $0x2
popq %rsi
movslq 0x18(%rbx), %r8
cmpq %r8, %rsi
jge 0x357f5
movl (%rcx), %r8d
cmpl %edx, %r8d
jle 0x3578f
movl (%rcx), %edx
movl %esi, %edi
incq %rsi
addq $0x1c, %rcx
jmp 0x3577a
movsd 0x8(%rsp), %xmm0
ucomisd 0x10732(%rip), %xmm0    # 0x45ed8
jb 0x357b7
movq %rcx, %rdx
shlq $0x4, %rdx
cmpl $0x0, -0xc(%rdx,%r14)
je 0x357e5
cmpl $0x4, %ecx
jl 0x357da
leal -0x1(%rcx), %edx
shlq $0x4, %rdx
cmpl $0x0, 0x4(%r14,%rdx)
jne 0x357da
addl $-0x3, %ecx
shlq $0x4, %rcx
cmpl $0x0, 0x4(%r14,%rcx)
je 0x357e5
movswl (%r15), %eax
jmp 0x357e5
movzwl (%r15), %eax
cwtl
addq $0x34a0, %rsp           # imm = 0x34A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movslq %edi, %rcx
imulq $0x1c, %rcx, %rcx
movswl (%rax,%rcx), %eax
jmp 0x357e5
nop