// File: /workspace/github_cpp_makefile_star2_O3/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 0x1aa8fa82(%rip), %rbp  # 0x1aadb930
movslq %edx, %rax
imulq $0x4548, %rax, %r13     # imm = 0x4548
movl 0x18(%rbp,%r13), %eax
movq %rax, 0x18(%rsp)
movl 0x43b0(%rdi), %r12d
leaq 0x40(%rsp), %r15
movl $0x5bfc, %edx           # imm = 0x5BFC
movq %r15, %rdi
xorl %esi, %esi
callq 0x6200
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x49374
cvtsd2ss %xmm0, %xmm0
movq %rbp, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movss %xmm0, 0x30(%rbp,%r13)
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x59fc0
movl %eax, 0x20(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x5b56e
movl 0x43c8(%r14), %eax
leal -0x2(%r12), %ecx
cmpl %ecx, %eax
jne 0x4bf4e
leaq 0x40(%rsp), %r15
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x5b0d4
movq %r14, %rdi
movq %r15, %rsi
callq 0x5b4db
jmp 0x4bf66
addl $-0x3, %r12d
cmpl %r12d, %eax
jne 0x4bf66
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x5b50f
xorl %edi, %edi
movq 0x18(%rsp), %rsi
cmpl $0x2, %esi
jl 0x4c095
movl %ebx, 0x24(%rsp)
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx), %rax
cvtss2sd 0x30(%rax), %xmm1
leaq (%rdx,%rcx), %r13
addq $0x52, %r13
xorpd %xmm2, %xmm2
movl $0x1, %r15d
movsd %xmm1, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
movsd %xmm2, 0x38(%rsp)
movswl -0x1a(%r13), %r12d
movq %r14, %rdi
movq %r14, %rbp
movl 0x24(%rsp), %r14d
movl %r14d, %esi
movl %r12d, %edx
leaq 0x40(%rsp), %rbx
movq %rbx, %rcx
callq 0x5b165
movq %rbp, %rdi
movl %r14d, %esi
movl %r12d, %edx
movq %rbx, %rcx
callq 0x5b391
movq %rbp, %rdi
movl %r14d, %esi
movq %rbp, %r14
movl %r12d, %edx
movq %rbx, %rcx
callq 0x5b454
cmpb $0x0, (%r13)
xorps %xmm0, %xmm0
jne 0x4c016
movq %r14, %rdi
movl %r12d, %esi
leaq 0x40(%rsp), %rdx
movl 0x20(%rsp), %ecx
callq 0x49374
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss %xmm1, -0x6(%r13)
movslq %r12d, %rax
leaq 0x1aab8b23(%rip), %rcx  # 0x1ab04b50
movslq (%rcx,%rax,4), %rcx
leaq 0x806f668(%rip), %rdx   # 0x80bb6a0
movsd (%rdx,%rcx,8), %xmm1
leaq 0x1aabd22c(%rip), %rcx  # 0x1ab09270
movslq (%rcx,%rax,4), %rax
leaq 0x806f6b1(%rip), %rcx   # 0x80bb700
mulsd (%rcx,%rax,8), %xmm1
movsd 0x38(%rsp), %xmm2
addsd %xmm0, %xmm2
mulsd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
ucomisd %xmm0, %xmm1
maxsd %xmm0, %xmm1
movq 0x30(%rsp), %rdi
cmoval %r15d, %edi
incq %r15
addq $0x1c, %r13
cmpq %r15, 0x18(%rsp)
jne 0x4bf9f
movslq %edi, %rdi
movq 0x18(%rsp), %rsi
jmp 0x4c099
xorpd %xmm2, %xmm2
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx), %rax
addq $0x1c, %rax
testl %esi, %esi
jle 0x4c0ed
movsd 0x10f51(%rip), %xmm0    # 0x5d008
divsd %xmm2, %xmm0
addq %rdx, %rcx
addq $0x30, %rcx
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
xorl %esi, %esi
xorps %xmm1, %xmm1
cvtss2sd (%rcx,%rsi), %xmm1
mulsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%rcx,%rsi)
addq $0x1c, %rsi
cmpq %rsi, %rdx
jne 0x4c0cf
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
movb $0x1, 0x18(%rax,%rcx)
addq $0x5c48, %rsp           # imm = 0x5C48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq