// File: /workspace/github_cpp_makefile_star2_O1/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: SelectMaxUcbChild(int, int, std::mersenne_twister_engine<unsigned long, 64ul, 312ul, 156ul, 31ul, 13043109905998158313ul, 29ul, 6148914691236517205ul, 17ul, 8202884508482404352ul, 37ul, 18444473444759240704ul, 43ul, 6364136223846793005ul>&)
static int
SelectMaxUcbChild( int current, int color, std::mt19937_64 &mt )
{
  const int child_num = uct_node[current].child_num;
  const int sum = uct_node[current].move_count;
  child_node_t *uct_child = uct_node[current].child;
  rate_order_t order[PURE_BOARD_MAX + 1];  
  
  // 128回ごとにOwnerとCriticalityでソートし直す  
  if ((sum & 0x7f) == 0 && sum != 0) {
    int o_index[UCT_CHILD_MAX], c_index[UCT_CHILD_MAX];
    CalculateCriticalityIndex(&uct_node[current], statistic, color, c_index);
    CalculateOwnerIndex(&uct_node[current], statistic, color, o_index);
    for (int i = 0; i < child_num; i++) {
      const int pos = uct_child[i].pos;
      const double dynamic_parameter = (pos == PASS) ? 1.0 : uct_owner[o_index[i]] * uct_criticality[c_index[i]];

      order[i].rate = uct_child[i].rate * dynamic_parameter;
      order[i].index = i;
      uct_child[i].pw = false;
    }
    qsort(order, child_num, sizeof(rate_order_t), RateComp);

    // 子ノードの数と探索幅の最小値を取る
    const int width = ((uct_node[current].width > child_num) ? child_num : uct_node[current].width);

    // 探索候補の手を展開し直す
    for (int i = 0; i < width; i++) {
      uct_child[order[i].index].pw = true;
    }
  }

  // Progressive Wideningの閾値を超えたら, 
  // レートが最大の手を読む候補を1手追加
  if (sum > pw[uct_node[current].width]) {
    int max_index = -1;
    double max_rate = -10000.0;
    for (int i = 0; i < child_num; i++) {
      if (!uct_child[i].pw) {
        const int pos = uct_child[i].pos;
        const double dynamic_parameter = (pos == PASS) ? 1.0 : uct_owner[owner_index[pos]] * uct_criticality[criticality_index[pos]];
        const double rate = uct_child[i].rate * dynamic_parameter;

        if (rate > max_rate) {
          max_index = i;
          max_rate = rate;
        }
      }
    }
    if (max_index != -1) {
      uct_child[max_index].pw = true;
    }
    uct_node[current].width++;  
  }

  return SelectBestChildIndexByUCB1(uct_node[current], mt);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2218, %rsp           # imm = 0x2218
movq %rdx, 0x8(%rsp)
movq 0x1aa8e3a6(%rip), %rax  # 0x1aada930
movslq %edi, %rcx
imulq $0x4548, %rcx, %r14     # imm = 0x4548
movl 0x18(%rax,%r14), %r12d
movl 0x8(%rax,%r14), %ebp
movq 0x1aa8e38b(%rip), %r13  # 0x1aada930
leaq (%r14,%r13), %r15
addq $0x1c, %r15
testb $0x7f, %bpl
setne %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x4c85a
leaq (%r14,%r13), %rcx
movl 0x1aabbc95(%rip), %eax  # 0x1ab08260
movsd 0x10a35(%rip), %xmm0    # 0x5d008
movapd %xmm0, %xmm1
testl %eax, %eax
jle 0x4c5f1
movl 0x1aabbc7f(%rip), %eax  # 0x1ab08260
cvtsi2sd %eax, %xmm2
movsd 0x10a1b(%rip), %xmm1    # 0x5d008
divsd %xmm2, %xmm1
movslq 0x18(%rcx), %rax
movl 0xc(%rcx), %edx
movl 0x8(%rcx), %ecx
movl $0x0, 0x10(%rsp)
cmpq $0x2, %rax
jl 0x4c6bf
movl %esi, %edi
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
xorl $0x3, %edi
cvtsi2sd %ecx, %xmm3
divsd %xmm3, %xmm2
subsd %xmm2, %xmm0
movslq %esi, %rcx
movslq %edi, %rdx
leaq 0x38(%r14,%r13), %rdi
movl $0x1, %r8d
leaq 0x1aab8793(%rip), %r9   # 0x1ab04dd0
movsd 0x109d3(%rip), %xmm3    # 0x5d018
movl $0x18, %r10d
movswq (%rdi), %r11
shlq $0x4, %r11
movl 0xc(%r11,%r9), %ebx
xorps %xmm4, %xmm4
cvtsi2sd %ebx, %xmm4
addq %r9, %r11
movl (%r11,%rcx,4), %ebx
xorps %xmm5, %xmm5
cvtsi2sd %ebx, %xmm5
movl (%r11,%rdx,4), %r11d
xorps %xmm6, %xmm6
cvtsi2sd %r11d, %xmm6
mulsd %xmm1, %xmm5
mulsd %xmm1, %xmm6
mulsd %xmm0, %xmm6
mulsd %xmm2, %xmm5
addsd %xmm6, %xmm5
mulsd %xmm1, %xmm4
subsd %xmm5, %xmm4
xorpd %xmm5, %xmm5
maxsd %xmm4, %xmm5
mulsd %xmm3, %xmm5
cvttsd2si %xmm5, %r11d
cmpl $0x18, %r11d
cmovgel %r10d, %r11d
movl %r11d, 0x10(%rsp,%r8,4)
incq %r8
addq $0x1c, %rdi
cmpq %r8, %rax
jne 0x4c64b
movq 0x1aa8e26a(%rip), %rdx  # 0x1aada930
leaq (%rdx,%r14), %rax
movl 0x1aabbb90(%rip), %ecx  # 0x1ab08260
testl %ecx, %ecx
jle 0x4c6ef
movl 0x1aabbb86(%rip), %ecx  # 0x1ab08260
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x1091f(%rip), %xmm0    # 0x5d008
divsd %xmm1, %xmm0
jmp 0x4c6f7
movsd 0x10911(%rip), %xmm0    # 0x5d008
movslq 0x18(%rax), %rax
movl $0x0, 0x5c0(%rsp)
cmpq $0x2, %rax
jl 0x4c786
movslq %esi, %rcx
leaq 0x1aab86ba(%rip), %rsi  # 0x1ab04dd0
leaq (%rsi,%rcx,4), %rcx
addq %r14, %rdx
addq $0x38, %rdx
movl $0x1, %esi
movsd 0x13872(%rip), %xmm1    # 0x5ffa0
movsd 0x108da(%rip), %xmm2    # 0x5d010
movl $0xa, %edi
xorl %r8d, %r8d
movswq (%rdx), %r9
shlq $0x4, %r9
movl (%rcx,%r9), %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r9d
cmpl $0xa, %r9d
cmovgel %edi, %r9d
testl %r9d, %r9d
cmovlel %r8d, %r9d
movl %r9d, 0x5c0(%rsp,%rsi,4)
incq %rsi
addq $0x1c, %rdx
cmpq %rsi, %rax
jne 0x4c73e
movslq %r12d, %rsi
testl %r12d, %r12d
jle 0x4c800
leaq 0xb78(%rsp), %rax
leaq (%r14,%r13), %rcx
addq $0x34, %rcx
xorl %edx, %edx
movsd 0x10860(%rip), %xmm0    # 0x5d008
leaq 0x806def1(%rip), %rdi   # 0x80ba6a0
leaq 0x806df4a(%rip), %r8    # 0x80ba700
cmpw $0x0, -0x18(%rcx)
movapd %xmm0, %xmm1
je 0x4c7da
movslq 0x5c0(%rsp,%rdx,4), %r9
movsd (%rdi,%r9,8), %xmm1
movslq 0x10(%rsp,%rdx,4), %r9
mulsd (%r8,%r9,8), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x4(%rcx), %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, (%rax)
movl %edx, -0x8(%rax)
movb $0x0, (%rcx)
incq %rdx
addq $0x10, %rax
addq $0x1c, %rcx
cmpq %rdx, %r12
jne 0x4c7b6
leaq 0x14c(%rip), %rcx       # 0x4c953
leaq 0xb70(%rsp), %rdi
movl $0x10, %edx
callq 0x6800
movq 0x1aa8e110(%rip), %rax  # 0x1aada930
movl 0x14(%rax,%r14), %eax
cmpl %r12d, %eax
cmovgel %r12d, %eax
testl %eax, %eax
jle 0x4c85a
movl %eax, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
movslq 0xb70(%rsp,%rcx), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
movb $0x1, 0x18(%r15,%rsi)
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x4c838
movq 0x1aa8e0cf(%rip), %rdi  # 0x1aada930
movslq 0x14(%rdi,%r14), %rax
leaq 0x1aab52e3(%rip), %rcx  # 0x1ab01b50
cmpl (%rcx,%rax,4), %ebp
jle 0x4c934
testl %r12d, %r12d
jle 0x4c90a
leaq (%r14,%r13), %rdx
addq $0x34, %rdx
movsd 0x136e9(%rip), %xmm1    # 0x5ff78
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %esi, %esi
movsd 0x1076a(%rip), %xmm0    # 0x5d008
leaq 0x1aab72ab(%rip), %r8   # 0x1ab03b50
leaq 0x806ddf4(%rip), %r9    # 0x80ba6a0
leaq 0x1aabb9bd(%rip), %r10  # 0x1ab08270
leaq 0x806de46(%rip), %r11   # 0x80ba700
cmpb $0x0, (%rdx)
jne 0x4c8fc
movswq -0x18(%rdx), %r13
movapd %xmm0, %xmm2
testq %r13, %r13
je 0x4c8e1
movslq (%r8,%r13,4), %rbx
movsd (%r9,%rbx,8), %xmm2
movslq (%r10,%r13,4), %rbx
mulsd (%r11,%rbx,8), %xmm2
xorps %xmm3, %xmm3
cvtss2sd -0x4(%rdx), %xmm3
mulsd %xmm2, %xmm3
ucomisd %xmm1, %xmm3
maxsd %xmm1, %xmm3
cmoval %esi, %ecx
movapd %xmm3, %xmm1
incq %rsi
addq $0x1c, %rdx
cmpq %rsi, %r12
jne 0x4c8ba
jmp 0x4c90f
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq (%rdi,%r14), %rdx
addq $0x14, %rdx
cmpl $-0x1, %ecx
je 0x4c930
movslq %ecx, %rcx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movb $0x1, 0x18(%r15,%rsi)
incl %eax
movl %eax, (%rdx)
addq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x4a0ee
addq $0x2218, %rsp           # imm = 0x2218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq