// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: RatingNode(game_info_t*, int, int)
static void
RatingNode( game_info_t *game, int color, int index )
{
  const int child_num = uct_node[index].child_num;
  const int moves = game->moves;
  int max_index;
  double score = 0.0, max_score, dynamic_parameter, total_score = 0.0;
  child_node_t *uct_child = uct_node[index].child;
  unsigned int tactical_features[BOARD_MAX * UCT_INDEX_MAX] = {0};
  int distance_index = 0;

  // パスのレーティング
  uct_child[PASS_INDEX].rate = CalculateMoveScoreWithBTFM(game, PASS, tactical_features, distance_index);
  // 直前の着手で発生した特徴の確認
  distance_index = CheckFeaturesForTree(game, color, tactical_features);
  // 直前の着手で石を2つ取られたか確認
  CheckRemove2StonesForTree(game, color, tactical_features);
  // 2手前で劫が発生していたら, 劫を解消するトリの確認
  if (game->ko_move == moves - 2) {
    CheckCaptureAfterKoForTree(game, color, tactical_features);
    CheckKoConnectionForTree(game, tactical_features);
  } else if (game->ko_move == moves - 3) {
    CheckKoRecaptureForTree(game, color, tactical_features);
  }

  max_index = 0;
  max_score = uct_child[0].rate;

  for (int i = 1; i < child_num; i++) {
    const int pos = uct_child[i].pos;
  
    // 自己アタリの確認
    CheckSelfAtariForTree(game, color, pos, tactical_features);
    // トリの確認
    CheckCaptureForTree(game, color, pos, tactical_features);
    // アタリの確認
    CheckAtariForTree(game, color, pos, tactical_features);

    // 自己アタリが無意味だったらスコアを0.0にする
    // 逃げられないシチョウならスコアを-1.0にする
    if (uct_child[i].ladder) {
      score = 0.0;
    } else {
      score = CalculateMoveScoreWithBTFM(game, pos, tactical_features, distance_index);
    }

    // その手のγを記録
    uct_child[i].rate = score;
    total_score += score;

    // 現在見ている箇所のOwnerとCriticalityの補正値を求める
    //dynamic_parameter = 1.0;
    dynamic_parameter = uct_owner[owner_index[pos]] * uct_criticality[criticality_index[pos]];

    // 最もγが大きい着手を記録する
    if (score * dynamic_parameter > max_score) {
      max_index = i;
      max_score = score * dynamic_parameter;
    }
  }

  const double inv_total = 1.0 / total_score;

  for (int i = 0; i < child_num; i++) {
    uct_child[i].rate *= inv_total;
  }
  
  // 最もγが大きい着手を探索できるようにする
  uct_child[max_index].pw = true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c48, %rsp           # imm = 0x5C48
movl %esi, %ebx
movq %rdi, %r14
movq 0x1aa92677(%rip), %rbp  # 0x1aac6930
movslq %edx, %rax
imulq $0x4548, %rax, %r12     # imm = 0x4548
leaq (%r12,%rbp), %rax
movq %rax, (%rsp)
movslq 0x18(%rbp,%r12), %rax
movq %rax, 0x20(%rsp)
movl 0x43b0(%rdi), %r13d
leaq 0x40(%rsp), %r15
movl $0x5bfc, %edx           # imm = 0x5BFC
movq %r15, %rdi
xorl %esi, %esi
callq 0x61f0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x31b6b
cvtsd2ss %xmm0, %xmm0
movq %r12, 0x18(%rsp)
movss %xmm0, 0x30(%rbp,%r12)
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x404a0
movl %eax, 0xc(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x4187b
movl 0x43c8(%r14), %eax
leal -0x2(%r13), %ecx
cmpl %ecx, %eax
jne 0x3435b
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x41413
movq %r14, %rdi
movq %r15, %rsi
callq 0x417f6
jmp 0x34373
addl $-0x3, %r13d
cmpl %r13d, %eax
jne 0x34373
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x41823
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %rcx
addq $0x1c, %rcx
movq %rcx, 0x28(%rsp)
movq (%rsp), %rcx
cvtss2sd 0x30(%rcx), %xmm1
movq %rbp, 0x30(%rsp)
leaq (%rax,%rbp), %r13
addq $0x52, %r13
xorl %edi, %edi
xorpd %xmm2, %xmm2
pushq $0x1
popq %rbp
movl %ebx, 0x10(%rsp)
cmpq 0x20(%rsp), %rbp
jge 0x34492
movsd %xmm1, 0x38(%rsp)
movsd %xmm2, (%rsp)
movl %edi, 0x14(%rsp)
movswl -0x1a(%r13), %r12d
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
movq %r14, %r15
movl %ebx, %r14d
leaq 0x40(%rsp), %rbx
movq %rbx, %rcx
callq 0x4149f
movq %r15, %rdi
movl %r14d, %esi
movl %r12d, %edx
movq %rbx, %rcx
callq 0x416b8
movq %r15, %rdi
movl %r14d, %esi
movq %r15, %r14
movl %r12d, %edx
movq %rbx, %rcx
callq 0x41774
xorps %xmm0, %xmm0
cmpb $0x0, (%r13)
jne 0x34425
movq %r14, %rdi
movl %r12d, %esi
leaq 0x40(%rsp), %rdx
movl 0xc(%rsp), %ecx
callq 0x31b6b
cvtsd2ss %xmm0, %xmm1
movss %xmm1, -0x6(%r13)
movslq %r12d, %rax
leaq 0x1aabb717(%rip), %rcx  # 0x1aaefb50
movslq (%rcx,%rax,4), %rcx
leaq 0x807225c(%rip), %rdx   # 0x80a66a0
movsd (%rdx,%rcx,8), %xmm1
leaq 0x1aabfe20(%rip), %rcx  # 0x1aaf4270
movslq (%rcx,%rax,4), %rax
leaq 0x80722a5(%rip), %rcx   # 0x80a6700
mulsd (%rcx,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd 0x38(%rsp), %xmm3
ucomisd %xmm3, %xmm1
movsd (%rsp), %xmm2
addsd %xmm0, %xmm2
movl 0x14(%rsp), %edi
cmoval %ebp, %edi
maxsd %xmm3, %xmm1
incq %rbp
addq $0x1c, %r13
movl 0x10(%rsp), %ebx
jmp 0x343a8
movsd 0xfb6e(%rip), %xmm0     # 0x44008
divsd %xmm2, %xmm0
xorl %eax, %eax
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
cmovlel %eax, %ecx
imulq $0x1c, %rcx, %rcx
movq 0x30(%rsp), %rdx
movq 0x18(%rsp), %rsi
addq %rsi, %rdx
addq $0x30, %rdx
cmpq %rax, %rcx
je 0x344dc
cvtss2sd (%rdx,%rax), %xmm1
mulsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rdx,%rax)
addq $0x1c, %rax
jmp 0x344bf
movslq %edi, %rax
imulq $0x1c, %rax, %rax
movq 0x28(%rsp), %rcx
movb $0x1, 0x18(%rcx,%rax)
addq $0x5c48, %rsp           # imm = 0x5C48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq