// File: /workspace/github_cpp_makefile_star2_O3/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 0x1aa8f058(%rip), %rax  # 0x1aadb930
movslq %edi, %rcx
imulq $0x4548, %rcx, %r14     # imm = 0x4548
movl 0x18(%rax,%r14), %r12d
movl 0x8(%rax,%r14), %ebp
movq 0x1aa8f03d(%rip), %r13  # 0x1aadb930
leaq (%r14,%r13), %r15
addq $0x1c, %r15
testb $0x7f, %bpl
sete %al
testl %ebp, %ebp
setne %cl
testb %al, %cl
je 0x4ca42
movq %rdx, 0x8(%rsp)
leaq (%r14,%r13), %rcx
movl 0x1aabc942(%rip), %eax  # 0x1ab09260
movsd 0x106e2(%rip), %xmm0    # 0x5d008
movapd %xmm0, %xmm1
testl %eax, %eax
jle 0x4c944
movl 0x1aabc92c(%rip), %eax  # 0x1ab09260
cvtsi2sd %eax, %xmm2
movsd 0x106c8(%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 0x4ca12
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 0x1aab9440(%rip), %r9   # 0x1ab05dd0
movsd 0x10680(%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 0x4c99e
movq 0x1aa8ef17(%rip), %rdx  # 0x1aadb930
leaq (%rdx,%r14), %rax
movl 0x1aabc83d(%rip), %ecx  # 0x1ab09260
testl %ecx, %ecx
jle 0x4ca4f
movl 0x1aabc833(%rip), %ecx  # 0x1ab09260
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x105cc(%rip), %xmm0    # 0x5d008
divsd %xmm1, %xmm0
jmp 0x4ca57
movl 0x14(%r13,%r14), %eax
movq %r13, %rdi
jmp 0x4cbc1
movsd 0x105b1(%rip), %xmm0    # 0x5d008
movslq 0x18(%rax), %rax
movl $0x0, 0x5c0(%rsp)
cmpq $0x2, %rax
jl 0x4cae6
movslq %esi, %rcx
leaq 0x1aab935a(%rip), %rsi  # 0x1ab05dd0
leaq (%rsi,%rcx,4), %rcx
addq %r14, %rdx
addq $0x38, %rdx
movl $0x1, %esi
movsd 0x135e2(%rip), %xmm1    # 0x60070
movsd 0x1057a(%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 0x4ca9e
movslq %r12d, %rsi
testl %r12d, %r12d
jle 0x4cb60
leaq 0xb78(%rsp), %rax
leaq (%r14,%r13), %rcx
addq $0x34, %rcx
xorl %edx, %edx
movsd 0x10500(%rip), %xmm0    # 0x5d008
leaq 0x806eb91(%rip), %rdi   # 0x80bb6a0
leaq 0x806ebea(%rip), %r8    # 0x80bb700
cmpw $0x0, -0x18(%rcx)
movapd %xmm0, %xmm1
je 0x4cb3a
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 0x4cb16
leaq 0x14f(%rip), %rcx       # 0x4ccb6
leaq 0xb70(%rsp), %rdi
movl $0x10, %edx
callq 0x67f0
movq 0x1aa8edb0(%rip), %rdi  # 0x1aadb930
movl 0x14(%rdi,%r14), %eax
cmpl %r12d, %eax
movl %r12d, %ecx
cmovll %eax, %ecx
testl %ecx, %ecx
jle 0x4cbbc
movl %ecx, %ecx
shlq $0x4, %rcx
xorl %edx, %edx
movslq 0xb70(%rsp,%rdx), %rsi
leaq (%rsi,%rsi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rsi, %r8
movb $0x1, 0x18(%r15,%r8)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x4cb9a
movq 0x8(%rsp), %rdx
movslq %eax, %rcx
leaq 0x1aab5f85(%rip), %rsi  # 0x1ab02b50
cmpl (%rsi,%rcx,4), %ebp
jle 0x4cc99
leaq (%rdi,%r14), %r8
addq $0x14, %r8
testl %r12d, %r12d
jle 0x4cc94
movq %r8, 0x8(%rsp)
leaq (%r14,%r13), %rsi
addq $0x34, %rsi
movsd 0x1344e(%rip), %xmm1    # 0x60048
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movsd 0x103fe(%rip), %xmm0    # 0x5d008
leaq 0x1aab7f3f(%rip), %r9   # 0x1ab04b50
leaq 0x806ea88(%rip), %r10   # 0x80bb6a0
leaq 0x1aabc651(%rip), %r11  # 0x1ab09270
leaq 0x806eada(%rip), %r13   # 0x80bb700
cmpb $0x0, (%rsi)
jne 0x4cc6a
movswq -0x18(%rsi), %rbp
movapd %xmm0, %xmm2
testq %rbp, %rbp
je 0x4cc4e
movslq (%r9,%rbp,4), %rbx
movsd (%r10,%rbx,8), %xmm2
movslq (%r11,%rbp,4), %rbx
mulsd (%r13,%rbx,8), %xmm2
xorps %xmm3, %xmm3
cvtss2sd -0x4(%rsi), %xmm3
mulsd %xmm2, %xmm3
ucomisd %xmm1, %xmm3
maxsd %xmm1, %xmm3
cmoval %r8d, %ecx
movapd %xmm3, %xmm1
incq %r8
addq $0x1c, %rsi
cmpq %r8, %r12
jne 0x4cc26
cmpl $-0x1, %ecx
movq 0x8(%rsp), %r8
je 0x4cc94
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, (%r8)
addq %r14, %rdi
movq %rdx, %rsi
callq 0x4a36a
addq $0x2218, %rsp           # imm = 0x2218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq