// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x34a8, %rsp           # imm = 0x34A8
movq %rcx, %r15
movl %edx, 0x4(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1c(%rsi), %r12
movl 0x20(%rsi), %eax
xorl %ebp, %ebp
cmpl $0x2, 0x18(%rsi)
jl 0x4cf61
leaq 0x3c(%rbx), %rcx
movl $0x1, %edx
movl (%rcx), %esi
cmpl %eax, %esi
jle 0x4cf4e
movl (%rcx), %eax
movl %edx, %ebp
incq %rdx
movslq 0x18(%rbx), %rsi
addq $0x1c, %rcx
cmpq %rsi, %rdx
jl 0x4cf44
movslq %ebp, %rbp
movq %rbx, %rdi
callq 0x49eb9
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x4b6a8
leaq (,%rbp,8), %rax
addq %rbp, %rax
leaq (%rax,%rax,2), %r13
addq %rbp, %r13
movl 0xc(%r12,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movl 0x4(%r12,%r13), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%r15)
movsd 0x288cf(%rip), %xmm1    # 0x75880
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
ja 0x4d09a
movslq 0x43b0(%r14), %rcx
xorl %eax, %eax
cmpq $0x439, %rcx            # imm = 0x439
jg 0x4d09a
addq %r12, %r13
cmpb $0x1, 0x1aabe060(%rip)  # 0x1ab0b040
jne 0x4d053
cmpw $0x0, (%r13)
jne 0x4d095
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x585d0
testb %al, %al
je 0x4d095
movl 0x3c(%rbx), %eax
movl $0x1, %ecx
cmpl $0x3, 0x18(%rbx)
jl 0x4d040
leaq 0x58(%rbx), %rcx
movl $0x1, %esi
movl $0x2, %edx
movl (%rcx), %edi
cmpl %eax, %edi
jle 0x4d02d
movl (%rcx), %eax
movl %edx, %esi
incq %rdx
movslq 0x18(%rbx), %rdi
addq $0x1c, %rcx
cmpq %rdi, %rdx
jl 0x4d023
movslq %esi, %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %r12
addq %rax, %r12
movq %r12, %r13
jmp 0x4d095
movsd 0x8(%rsp), %xmm0
ucomisd 0x11e37(%rip), %xmm0    # 0x5ee98
jb 0x4d072
movq %rcx, %rdx
shlq $0x4, %rdx
cmpl $0x0, -0xc(%rdx,%r14)
je 0x4d09a
cmpl $0x4, %ecx
jl 0x4d095
leal -0x1(%rcx), %edx
shlq $0x4, %rdx
cmpl $0x0, 0x4(%r14,%rdx)
jne 0x4d095
addl $-0x3, %ecx
shlq $0x4, %rcx
cmpl $0x0, 0x4(%r14,%rcx)
je 0x4d09a
movswl (%r13), %eax
addq $0x34a8, %rsp           # imm = 0x34A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq