// File: 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
movq %rsp, %rbp
subq $0x5c70, %rsp           # imm = 0x5C70
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq 0x1aac17d4(%rip), %rax  # 0x1ab4cab0
movslq -0x10(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl 0x43b0(%rax), %eax
movl %eax, -0x18(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq 0x1aac179c(%rip), %rax  # 0x1ab4cab0
movslq -0x10(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
addq $0x1c, %rax
movq %rax, -0x48(%rbp)
leaq -0x5c50(%rbp), %rdi
xorl %esi, %esi
movl $0x5bfc, %edx           # imm = 0x5BFC
callq 0x72b0
movl $0x0, -0x5c54(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x5c50(%rbp), %rdx
movl -0x5c54(%rbp), %ecx
xorl %esi, %esi
callq 0x85f40
cvtsd2ss %xmm0, %xmm0
movq -0x48(%rbp), %rax
movss %xmm0, 0x14(%rax)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x5c50(%rbp), %rdx
callq 0xa5cf0
movl %eax, -0x5c54(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x5c50(%rbp), %rdx
callq 0xa7be0
movq -0x8(%rbp), %rax
movl 0x43c8(%rax), %eax
movl -0x18(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jne 0x8b3d1
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x5c50(%rbp), %rdx
callq 0xa7320
movq -0x8(%rbp), %rdi
leaq -0x5c50(%rbp), %rsi
callq 0xa7ab0
jmp 0x8b3fa
movq -0x8(%rbp), %rax
movl 0x43c8(%rax), %eax
movl -0x18(%rbp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jne 0x8b3f8
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
leaq -0x5c50(%rbp), %rdx
callq 0xa7b00
jmp 0x8b3fa
movl $0x0, -0x1c(%rbp)
movq -0x48(%rbp), %rax
movss 0x14(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x1, -0x5c58(%rbp)
movl -0x5c58(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8b584
movq -0x48(%rbp), %rax
movslq -0x5c58(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x5c5c(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c5c(%rbp), %edx
leaq -0x5c50(%rbp), %rcx
callq 0xa7480
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c5c(%rbp), %edx
leaq -0x5c50(%rbp), %rcx
callq 0xa7870
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x5c5c(%rbp), %edx
leaq -0x5c50(%rbp), %rcx
callq 0xa79e0
movq -0x48(%rbp), %rax
movslq -0x5c58(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x1a(%rax)
je 0x8b4b4
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x8b4d5
movq -0x8(%rbp), %rdi
movl -0x5c5c(%rbp), %esi
leaq -0x5c50(%rbp), %rdx
movl -0x5c54(%rbp), %ecx
callq 0x85f40
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x48(%rbp), %rax
movslq -0x5c58(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x14(%rax)
movsd -0x28(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movslq -0x5c5c(%rbp), %rcx
leaq 0x1aaea7be(%rip), %rax  # 0x1ab75cd0
movslq (%rax,%rcx,4), %rcx
leaq 0x80a1303(%rip), %rax   # 0x812c820
movsd (%rax,%rcx,8), %xmm0
movslq -0x5c5c(%rbp), %rcx
leaq 0x1aaeeec0(%rip), %rax  # 0x1ab7a3f0
movslq (%rax,%rcx,4), %rcx
leaq 0x80a1345(%rip), %rax   # 0x812c880
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x28(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x8b56e
movl -0x5c58(%rbp), %eax
movl %eax, -0x1c(%rbp)
movsd -0x28(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0x8b570
movl -0x5c58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c58(%rbp)
jmp 0x8b41d
movsd 0x20a7c(%rip), %xmm0    # 0xac008
divsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x5c68(%rbp)
movl $0x0, -0x5c6c(%rbp)
movl -0x5c6c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8b5ef
movsd -0x5c68(%rbp), %xmm1
movq -0x48(%rbp), %rax
movslq -0x5c6c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x14(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rax)
movl -0x5c6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c6c(%rbp)
jmp 0x8b5a3
movq -0x48(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movb $0x1, 0x18(%rax)
addq $0x5c70, %rsp           # imm = 0x5C70
popq %rbp
retq
nopl (%rax,%rax)