// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d28d
leaq 0x3c(%rbx), %rcx
movl $0x1, %edx
movl (%rcx), %esi
cmpl %eax, %esi
jle 0x4d27a
movl (%rcx), %eax
movl %edx, %ebp
incq %rdx
movslq 0x18(%rbx), %rsi
addq $0x1c, %rcx
cmpq %rsi, %rdx
jl 0x4d270
movslq %ebp, %rbp
movq %rbx, %rdi
callq 0x4a137
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x4b9fc
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 0x295a3(%rip), %xmm1    # 0x76880
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
ja 0x4d3cf
movslq 0x43b0(%r14), %rcx
xorl %eax, %eax
cmpq $0x439, %rcx            # imm = 0x439
jg 0x4d3cf
addq %r12, %r13
cmpb $0x1, 0x1aabed34(%rip)  # 0x1ab0c040
jne 0x4d380
movzwl (%r13), %eax
testw %ax, %ax
jne 0x4d3ce
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x58ef4
testb %al, %al
je 0x4d3c9
movl 0x3c(%rbx), %eax
movl $0x1, %ecx
cmpl $0x3, 0x18(%rbx)
jl 0x4d36e
leaq 0x58(%rbx), %rcx
movl $0x1, %esi
movl $0x2, %edx
movl (%rcx), %edi
cmpl %eax, %edi
jle 0x4d35b
movl (%rcx), %eax
movl %edx, %esi
incq %rdx
movslq 0x18(%rbx), %rdi
addq $0x1c, %rcx
cmpq %rdi, %rdx
jl 0x4d351
movslq %esi, %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movswl (%r12,%rax), %eax
jmp 0x4d3cf
movsd 0x8(%rsp), %xmm0
ucomisd 0x11b32(%rip), %xmm0    # 0x5eec0
jb 0x4d39f
movq %rcx, %rdx
shlq $0x4, %rdx
cmpl $0x0, -0xc(%rdx,%r14)
je 0x4d3cf
cmpl $0x4, %ecx
jl 0x4d3c2
leal -0x1(%rcx), %edx
shlq $0x4, %rdx
cmpl $0x0, 0x4(%r14,%rdx)
jne 0x4d3c2
addl $-0x3, %ecx
shlq $0x4, %rcx
cmpl $0x0, 0x4(%r14,%rcx)
je 0x4d3cf
movswl (%r13), %eax
jmp 0x4d3cf
movzwl (%r13), %eax
cwtl
addq $0x34a8, %rsp           # imm = 0x34A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)