// File: /workspace/github_cpp_makefile_star2_O2/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 0x1aa91cdb(%rip), %rax  # 0x1aac6930
movslq %edi, %rcx
imulq $0x4548, %rcx, %r15     # imm = 0x4548
movslq 0x18(%rax,%r15), %r14
movl 0x8(%rax,%r15), %ebp
movq 0x1aa91cc0(%rip), %r13  # 0x1aac6930
leaq (%r15,%r13), %r12
addq $0x1c, %r12
testb $0x7f, %bpl
sete %al
testl %ebp, %ebp
setne %cl
testb %al, %cl
je 0x34da8
leaq (%r15,%r13), %rax
movl %esi, %ecx
xorl $0x3, %ecx
movl 0x1aabf5c5(%rip), %edx  # 0x1aaf4260
movsd 0xf365(%rip), %xmm0     # 0x44008
movapd %xmm0, %xmm1
testl %edx, %edx
jle 0x34cc1
movl 0x1aabf5af(%rip), %edx  # 0x1aaf4260
cvtsi2sd %edx, %xmm2
movsd 0xf34b(%rip), %xmm1     # 0x44008
divsd %xmm2, %xmm1
movslq 0x18(%rax), %rdx
movl 0xc(%rax), %edi
cvtsi2sd %edi, %xmm2
movl 0x8(%rax), %eax
cvtsi2sd %eax, %xmm3
divsd %xmm3, %xmm2
andl $0x0, 0x10(%rsp)
subsd %xmm2, %xmm0
movslq %esi, %rax
movslq %ecx, %rsi
leaq (%r15,%r13), %rdi
addq $0x38, %rdi
pushq $0x1
popq %r8
leaq 0x1aabc0d7(%rip), %rcx  # 0x1aaf0dd0
movsd 0xf317(%rip), %xmm3     # 0x44018
pushq $0x18
popq %r9
cmpq %rdx, %r8
jge 0x34d74
movswq (%rdi), %r10
shlq $0x4, %r10
movl 0xc(%r10,%rcx), %r11d
cvtsi2sd %r11d, %xmm4
addq %rcx, %r10
movl (%r10,%rax,4), %r11d
cvtsi2sd %r11d, %xmm5
movl (%r10,%rsi,4), %r10d
cvtsi2sd %r10d, %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, %r10d
cmpl $0x18, %r10d
cmovgel %r9d, %r10d
movl %r10d, 0x10(%rsp,%r8,4)
incq %r8
addq $0x1c, %rdi
jmp 0x34d05
movq 0x1aa91bb5(%rip), %rsi  # 0x1aac6930
leaq (%rsi,%r15), %rdx
movl 0x1aabf4db(%rip), %edi  # 0x1aaf4260
testl %edi, %edi
jle 0x34e8d
movl 0x1aabf4cd(%rip), %edi  # 0x1aaf4260
cvtsi2sd %edi, %xmm1
movsd 0xf269(%rip), %xmm0     # 0x44008
divsd %xmm1, %xmm0
jmp 0x34e95
movl 0x14(%r13,%r15), %eax
movq %r13, %rdi
movslq %eax, %rcx
leaq 0x1aab8d96(%rip), %rdx  # 0x1aaedb50
cmpl (%rdx,%rcx,4), %ebp
jle 0x34e6e
leaq (%rdi,%r15), %rcx
addq $0x14, %rcx
xorl %edx, %edx
testl %r14d, %r14d
cmovlel %edx, %r14d
leaq 0x34(%r15,%r13), %r8
pushq $-0x1
popq %rsi
movsd 0x12214(%rip), %xmm1    # 0x46ff8
movsd 0xf21c(%rip), %xmm0     # 0x44008
leaq 0x1aabad5d(%rip), %r9   # 0x1aaefb50
leaq 0x80718a6(%rip), %r10   # 0x80a66a0
leaq 0x1aabf46f(%rip), %r11  # 0x1aaf4270
leaq 0x80718f8(%rip), %r13   # 0x80a6700
cmpq %rdx, %r14
je 0x34e58
cmpb $0x0, (%r8)
jne 0x34e4f
movswq -0x18(%r8), %rbp
movapd %xmm0, %xmm2
testq %rbp, %rbp
je 0x34e36
movslq (%r9,%rbp,4), %rbx
movsd (%r10,%rbx,8), %xmm2
movslq (%r11,%rbp,4), %rbx
mulsd (%r13,%rbx,8), %xmm2
cvtss2sd -0x4(%r8), %xmm3
mulsd %xmm2, %xmm3
ucomisd %xmm1, %xmm3
maxsd %xmm1, %xmm3
cmoval %edx, %esi
movapd %xmm3, %xmm1
incq %rdx
addq $0x1c, %r8
jmp 0x34e08
cmpl $-0x1, %esi
je 0x34e6a
movslq %esi, %rdx
imulq $0x1c, %rdx, %rdx
movb $0x1, 0x18(%r12,%rdx)
incl %eax
movl %eax, (%rcx)
addq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x32ae1
addq $0x2218, %rsp           # imm = 0x2218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xf173(%rip), %xmm0     # 0x44008
movslq 0x18(%rdx), %rdx
andl $0x0, 0x5c0(%rsp)
leaq (%rcx,%rax,4), %rax
leaq (%r15,%rsi), %rcx
addq $0x38, %rcx
pushq $0x1
popq %rsi
movsd 0x12168(%rip), %xmm1    # 0x47020
movsd 0xf150(%rip), %xmm2     # 0x44010
pushq $0xa
popq %rdi
xorl %r8d, %r8d
cmpq %rdx, %rsi
jge 0x34f0d
movswq (%rcx), %r9
shlq $0x4, %r9
movl (%rax,%r9), %r9d
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, %rcx
jmp 0x34ec6
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
leaq 0xb78(%rsp), %rdx
leaq (%r15,%r13), %rsi
addq $0x34, %rsi
movsd 0xf0d5(%rip), %xmm0     # 0x44008
leaq 0x8071766(%rip), %rdi   # 0x80a66a0
leaq 0x80717bf(%rip), %r8    # 0x80a6700
cmpq %rax, %rcx
je 0x34f8a
cmpw $0x0, -0x18(%rsi)
movapd %xmm0, %xmm1
je 0x34f6a
movslq 0x5c0(%rsp,%rax,4), %r9
movsd (%rdi,%r9,8), %xmm1
movslq 0x10(%rsp,%rax,4), %r9
mulsd (%r8,%r9,8), %xmm1
cvtss2sd -0x4(%rsi), %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, (%rdx)
movl %eax, -0x8(%rdx)
movb $0x0, (%rsi)
incq %rax
addq $0x10, %rdx
addq $0x1c, %rsi
jmp 0x34f41
leaq 0x54(%rip), %rcx        # 0x34fe5
leaq 0xb70(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x67d0
movq 0x1aa91985(%rip), %rdi  # 0x1aac6930
movl 0x14(%rdi,%r15), %eax
cmpl %r14d, %eax
movl %r14d, %ecx
cmovll %eax, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
shlq $0x4, %rcx
cmpq %rdx, %rcx
je 0x34db0
movslq 0xb70(%rsp,%rdx), %rsi
imulq $0x1c, %rsi, %rsi
movb $0x1, 0x18(%r12,%rsi)
addq $0x10, %rdx
jmp 0x34fc4