// File: 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
movq %rsp, %rbp
subq $0x2280, %rsp           # imm = 0x2280
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq 0x1aac0b94(%rip), %rax  # 0x1ab4cab0
movslq -0x4(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, -0x14(%rbp)
movq 0x1aac0b79(%rip), %rdi  # 0x1ab4cab0
movslq -0x4(%rbp), %rax
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
addq $0x8, %rdi
callq 0x25780
movl %eax, -0x18(%rbp)
movq 0x1aac0b58(%rip), %rax  # 0x1ab4cab0
movslq -0x4(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
addq $0x1c, %rax
movq %rax, -0x20(%rbp)
movl -0x18(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x8c1be
cmpl $0x0, -0x18(%rbp)
je 0x8c1be
movq 0x1aac0b22(%rip), %rdi  # 0x1ab4cab0
movslq -0x4(%rbp), %rax
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
movl -0x8(%rbp), %edx
leaq -0x2220(%rbp), %rcx
leaq 0x1aaeafa3(%rip), %rsi  # 0x1ab76f50
callq 0x8c8d0
movq 0x1aac0af7(%rip), %rdi  # 0x1ab4cab0
movslq -0x4(%rbp), %rax
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
movl -0x8(%rbp), %edx
leaq -0x1c70(%rbp), %rcx
leaq 0x1aaeaf78(%rip), %rsi  # 0x1ab76f50
callq 0x8cc30
movl $0x0, -0x2224(%rbp)
movl -0x2224(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8c0fe
movq -0x20(%rbp), %rax
movslq -0x2224(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x2228(%rbp)
cmpl $0x0, -0x2228(%rbp)
jne 0x8c02c
movsd 0x1ffe6(%rip), %xmm0    # 0xac008
movsd %xmm0, -0x2268(%rbp)
jmp 0x8c06a
movslq -0x2224(%rbp), %rax
movslq -0x1c70(%rbp,%rax,4), %rcx
leaq 0x80a07de(%rip), %rax   # 0x812c820
movsd (%rax,%rcx,8), %xmm0
movslq -0x2224(%rbp), %rax
movslq -0x2220(%rbp,%rax,4), %rcx
leaq 0x80a0823(%rip), %rax   # 0x812c880
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x2268(%rbp)
movsd -0x2268(%rbp), %xmm0
movsd %xmm0, -0x2230(%rbp)
movq -0x20(%rbp), %rax
movslq -0x2224(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd -0x2230(%rbp), %xmm0
movslq -0x2224(%rbp), %rcx
leaq -0x16c0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
movl -0x2224(%rbp), %ecx
movslq -0x2224(%rbp), %rdx
leaq -0x16c0(%rbp), %rax
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movslq -0x2224(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x18(%rax)
movl -0x2224(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2224(%rbp)
jmp 0x8bfe7
leaq -0x16c0(%rbp), %rdi
movslq -0x14(%rbp), %rsi
movl $0x10, %edx
leaq 0xd2b(%rip), %rcx       # 0x8ce40
callq 0x7a70
movq 0x1aac098f(%rip), %rax  # 0x1ab4cab0
movslq -0x4(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x14(%rax), %eax
cmpl -0x14(%rbp), %eax
jle 0x8c142
movl -0x14(%rbp), %eax
movl %eax, -0x226c(%rbp)
jmp 0x8c160
movq 0x1aac0967(%rip), %rax  # 0x1ab4cab0
movslq -0x4(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0x226c(%rbp)
movl -0x226c(%rbp), %eax
movl %eax, -0x2234(%rbp)
movl $0x0, -0x2238(%rbp)
movl -0x2238(%rbp), %eax
cmpl -0x2234(%rbp), %eax
jge 0x8c1bc
movq -0x20(%rbp), %rax
movslq -0x2238(%rbp), %rdx
leaq -0x16c0(%rbp), %rcx
shlq $0x4, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movb $0x1, 0x18(%rax)
movl -0x2238(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2238(%rbp)
jmp 0x8c176
jmp 0x8c1be
movl -0x18(%rbp), %eax
movq 0x1aac08e8(%rip), %rcx  # 0x1ab4cab0
movslq -0x4(%rbp), %rdx
imulq $0x4548, %rdx, %rdx     # imm = 0x4548
addq %rdx, %rcx
movslq 0x14(%rcx), %rdx
leaq 0x1aae7aef(%rip), %rcx  # 0x1ab73cd0
cmpl (%rcx,%rdx,4), %eax
jle 0x8c371
movl $0xffffffff, -0x223c(%rbp) # imm = 0xFFFFFFFF
movsd 0x22f4c(%rip), %xmm0    # 0xaf148
movsd %xmm0, -0x2248(%rbp)
movl $0x0, -0x224c(%rbp)
movl -0x224c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8c334
movq -0x20(%rbp), %rax
movslq -0x224c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x18(%rax)
jne 0x8c31e
movq -0x20(%rbp), %rax
movslq -0x224c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x2250(%rbp)
cmpl $0x0, -0x2250(%rbp)
jne 0x8c26f
movsd 0x1fda3(%rip), %xmm0    # 0xac008
movsd %xmm0, -0x2278(%rbp)
jmp 0x8c2b3
movslq -0x2250(%rbp), %rcx
leaq 0x1aae9a53(%rip), %rax  # 0x1ab75cd0
movslq (%rax,%rcx,4), %rcx
leaq 0x80a0598(%rip), %rax   # 0x812c820
movsd (%rax,%rcx,8), %xmm0
movslq -0x2250(%rbp), %rcx
leaq 0x1aaee155(%rip), %rax  # 0x1ab7a3f0
movslq (%rax,%rcx,4), %rcx
leaq 0x80a05da(%rip), %rax   # 0x812c880
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x2278(%rbp)
movsd -0x2278(%rbp), %xmm0
movsd %xmm0, -0x2258(%rbp)
movq -0x20(%rbp), %rax
movslq -0x224c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd -0x2258(%rbp), %xmm0
movsd %xmm0, -0x2260(%rbp)
movsd -0x2260(%rbp), %xmm0
ucomisd -0x2248(%rbp), %xmm0
jbe 0x8c31c
movl -0x224c(%rbp), %eax
movl %eax, -0x223c(%rbp)
movsd -0x2260(%rbp), %xmm0
movsd %xmm0, -0x2248(%rbp)
jmp 0x8c31e
jmp 0x8c320
movl -0x224c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x224c(%rbp)
jmp 0x8c20e
cmpl $-0x1, -0x223c(%rbp)
je 0x8c353
movq -0x20(%rbp), %rax
movslq -0x223c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movb $0x1, 0x18(%rax)
movq 0x1aac0756(%rip), %rax  # 0x1ab4cab0
movslq -0x4(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x14(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x14(%rax)
movq 0x1aac0738(%rip), %rdi  # 0x1ab4cab0
movslq -0x4(%rbp), %rax
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x88ad0
addq $0x2280, %rsp           # imm = 0x2280
popq %rbp
retq
nopl (%rax,%rax)