// File: kobanium[P]Ray/src/mcts/ucb/UCBEvaluation.cpp
// Function: SelectBestChildIndexByUCB1(uct_node_t const&, std::mersenne_twister_engine<unsigned long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ul, 29ul, 6148914691236517205ul, 17ul, 8202884508482404352ul, 37ul, 18444473444759240704ul, 43ul, 6364136223846793005ul>&)
int
SelectBestChildIndexByUCB1( const uct_node_t &node, std::mt19937_64 &mt )
{
  const int child_num = node.child_num;
  const int sum = node.move_count + node.virtual_loss.load();
  const double move_score_bonus_weight = bonus_weight * sqrt(bonus_equivalence / (sum + bonus_equivalence));
  const child_node_t *child = node.child;
  int max_child = 0;
  double max_value = -10000.0;

  for (int i = 0; i < child_num; i++) {
    if (child[i].pw || child[i].open) {
      const int move_count = child[i].move_count + child[i].virtual_loss.load();
      double ucb_value = 0.0;
      if (move_count == 0) {
        ucb_value = FPU + 0.0001 * (mt() % 10000);
      } else {
        ucb_value = CalculateUCB1TunedValue(child[i], sum) + CalculateMoveScoreBonus(child[i], move_score_bonus_weight);
      }
      if (ucb_value > max_value) {
        max_value = ucb_value;
        max_child = i;
      }
    }
  }
  
  return max_child;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
callq 0x25780
movl %eax, -0x8c(%rbp)
movq -0x38(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x8(%rbp)
movl $0x5, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x7c(%rbp)
jmp 0x88b30
movl -0x7c(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x90(%rbp)
decl %eax
subl $0x2, %eax
jb 0x88b63
jmp 0x88b48
movl -0x90(%rbp), %eax
subl $0x5, %eax
je 0x88b71
jmp 0x88b55
movq -0x88(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x88b87
movq -0x88(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x88b87
movq -0x88(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x88b87
movq %rax, %rdi
callq 0x25ac0
movl -0x8c(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x48(%rbp)
movsd 0x5ee55(%rip), %xmm0    # 0xe79f0
movsd %xmm0, -0x98(%rbp)
movsd 0x5ee4d(%rip), %xmm0    # 0xe79f8
cvtsi2sdl -0x48(%rbp), %xmm1
addsd 0x5ee40(%rip), %xmm1    # 0xe79f8
divsd %xmm1, %xmm0
callq 0x7400
movaps %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x1c, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movsd 0x26558(%rip), %xmm0    # 0xaf148
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x88ddf
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x18(%rax)
jne 0x88c36
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x19(%rax)
je 0x88dcf
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rdx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
leaq 0x4(%rax,%rcx), %rdi
callq 0x25780
movl %eax, -0xac(%rbp)
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rdx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
leaq 0x8(%rax,%rcx), %rax
movq %rax, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x24(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x9c(%rbp)
jmp 0x88c9c
movl -0x9c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xb0(%rbp)
decl %eax
subl $0x2, %eax
jb 0x88cd2
jmp 0x88cb7
movl -0xb0(%rbp), %eax
subl $0x5, %eax
je 0x88ce0
jmp 0x88cc4
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x88cf6
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x88cf6
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x88cf6
movq %rax, %rdi
callq 0x25ac0
movl -0xac(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
cmpl $0x0, -0x70(%rbp)
jne 0x88d65
movq -0x40(%rbp), %rdi
callq 0x65a00
movq %rax, -0xb8(%rbp)
movabsq $0x346dc5d63886594b, %rcx # imm = 0x346DC5D63886594B
mulq %rcx
movq -0xb8(%rbp), %rax
shrq $0xb, %rdx
movl %edx, %ecx
imull $0x2710, %ecx, %ecx     # imm = 0x2710
subl %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x26402(%rip), %xmm1    # 0xaf150
mulsd %xmm1, %xmm0
movsd 0x251ce(%rip), %xmm1    # 0xadf28
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x88db1
movq -0x58(%rbp), %rdi
movslq -0x6c(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
movl -0x48(%rbp), %esi
callq 0x88930
movsd %xmm0, -0xc0(%rbp)
movq -0x58(%rbp), %rdi
movslq -0x6c(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
movsd -0x50(%rbp), %xmm0
callq 0x887f0
movaps %xmm0, %xmm1
movsd -0xc0(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
ucomisd -0x68(%rbp), %xmm0
jbe 0x88dcd
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x88dcf
jmp 0x88dd1
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x88bfc
movl -0x5c(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)