// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: ExpandRoot(game_info_t*, int)
static int
ExpandRoot( game_info_t *game, int color )
{
  const int moves = game->moves;
  const unsigned long long hash = game->move_hash;
  unsigned int index = FindSameHashIndex(hash, color, moves);
  int pm1 = PASS, pm2 = PASS;
  bool ladder[BOARD_MAX] = { false };

  // 直前の着手の座標を取り出す
  pm1 = game->record[moves - 1].pos;
  // 2手前の着手の座標を取り出す
  if (moves > 1) pm2 = game->record[moves - 2].pos;

  // 9路盤でなければシチョウを調べる  
  if (pure_board_size != 9) {
    LadderExtension(game, color, ladder);
  }

  // 既に展開されていた時は, 探索結果を再利用する
  if (index != uct_hash_size) {
    std::vector<int> indexes;

    // 現局面の子ノード以外を削除する
    CorrectDescendentNodes(indexes, index);
    std::sort(indexes.begin(), indexes.end());
    ClearNotDescendentNodes(indexes);
    
    // 直前と2手前の着手を更新
    uct_node[index].previous_move1 = pm1;
    uct_node[index].previous_move2 = pm2;

    ReuseRootCandidateWithoutLadderMove(uct_node[index], ladder);

    // 展開されたノード数を1に初期化
    uct_node[index].width = 1;

    // 候補手のレーティング
    RatingNode(game, color, index);

    PrintReuseCount(uct_node[index].move_count);

    return index;
  } else {
    // 全ノードのクリア
    ClearUctHash();
    
    // 空のインデックスを探す
    index = SearchEmptyIndex(hash, color, moves);

    assert(index != uct_hash_size);    
    
    // ルートノードの初期化
    InitializeNode(uct_node[index], pm1, pm2);

    child_node_t *uct_child = uct_node[index].child;
    int child_num = 0;

    // パスノードの展開
    InitializeCandidate(uct_child[PASS_INDEX], child_num, PASS, ladder[PASS]);
    
    // 候補手の展開
    if (moves == 1) {
      for (int i = 0; i < first_move_candidates; i++) {
        const int pos = first_move_candidate[i];
        // 探索候補かつ合法手であれば探索対象にする
        if (candidates[pos] && IsLegal(game, pos, color)) {
          InitializeCandidate(uct_child[child_num], child_num, pos, ladder[pos]);
        }
      }
    } else {
      for (int i = 0; i < pure_board_max; i++) {
        const int pos = onboard_pos[i];
        // 探索候補かつ合法手であれば探索対象にする
        if (candidates[pos] &&
            IsLegal(game, pos, color) &&
            IsMeaningfulSelfAtari(game, color, pos)) {
          InitializeCandidate(uct_child[child_num], child_num, pos, ladder[pos]);
        }
      }
    }
    
    // 子ノード個数の設定
    uct_node[index].child_num = child_num;
    
    // 候補手のレーティング
    RatingNode(game, color, index);

    // セキの確認
    CheckSeki(game, uct_node[index].seki);
    
    uct_node[index].width++;
  }

  return index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movq %rdi, %r14
movslq 0x43b0(%rdi), %r12
movq 0x43f0(%rdi), %r13
movq %r13, %rdi
movl %esi, 0x4(%rsp)
movl %r12d, %edx
callq 0x3d24a
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x61f0
movq %r12, %rax
shlq $0x4, %rax
cmpq $0x2, %r12
jl 0x3337e
leal -0x2(%r12), %ecx
shlq $0x4, %rcx
movl 0x4(%r14,%rcx), %r15d
movl -0xc(%rax,%r14), %eax
movl %eax, 0x8(%rsp)
leaq 0x2e526(%rip), %rax     # 0x618b4
cmpl $0x9, (%rax)
je 0x333a4
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x42c50
leaq 0x2e525(%rip), %rbx     # 0x618d0
cmpl (%rbx), %ebp
jne 0x33492
callq 0x3d0d6
movq %r13, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
callq 0x3d1d5
cmpl (%rbx), %eax
je 0x335ff
movl %eax, 0x34(%rsp)
movl %eax, %eax
imulq $0x4548, %rax, %rbx     # imm = 0x4548
movq 0x1aa9354d(%rip), %rdi  # 0x1aac6930
addq %rbx, %rdi
movl 0x8(%rsp), %esi
movl %r15d, %edx
callq 0x327d0
movq 0x1aa93537(%rip), %rax  # 0x1aac6930
movq %rbx, 0x38(%rsp)
leaq (%rax,%rbx), %rdi
addq $0x1c, %rdi
leaq 0x10(%rsp), %rsi
andl $0x0, (%rsi)
movzbl 0x40(%rsp), %ecx
movq %rdi, 0x8(%rsp)
xorl %edx, %edx
callq 0x3281e
cmpl $0x1, %r12d
jne 0x33528
xorl %ebx, %ebx
leaq 0x1aac4ace(%rip), %r15  # 0x1aaf7f00
leaq 0x1ab19867(%rip), %rbp  # 0x1ab4cca0
leaq 0x1aabd440(%rip), %r12  # 0x1aaf0880
movslq (%r15), %rax
cmpq %rax, %rbx
jge 0x335a0
movslq (%rbp,%rbx,4), %r13
cmpb $0x1, (%r13,%r12)
jne 0x3348d
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x3bc4c
testb %al, %al
je 0x3348d
movslq 0x10(%rsp), %rax
imulq $0x1c, %rax, %rdi
addq 0x8(%rsp), %rdi
movzbl 0x40(%rsp,%r13), %ecx
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x3281e
incq %rbx
jmp 0x33440
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl %ebp, %esi
callq 0x3422f
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x351d9
leaq 0x10(%rsp), %rdi
callq 0x3d10c
movq 0x1aa93467(%rip), %rax  # 0x1aac6930
movl %ebp, %ecx
imulq $0x4548, %rcx, %rbx     # imm = 0x4548
leaq (%rax,%rbx), %rdi
movl 0x8(%rsp), %ecx
movl %ecx, (%rax,%rbx)
movl %r15d, 0x4(%rax,%rbx)
leaq 0x40(%rsp), %rsi
callq 0x32866
movq 0x1aa9343d(%rip), %rax  # 0x1aac6930
movl $0x1, 0x14(%rax,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebp, %edx
callq 0x3429c
movq 0x1aa93420(%rip), %rax  # 0x1aac6930
movl 0x8(%rax,%rbx), %edi
callq 0x106f6
leaq 0x10(%rsp), %rdi
callq 0x149b8
jmp 0x335eb
xorl %r12d, %r12d
leaq 0x2e37e(%rip), %rbx     # 0x618b0
leaq 0x1ab191b7(%rip), %r15  # 0x1ab4c6f0
leaq 0x1aabd340(%rip), %rbp  # 0x1aaf0880
movslq (%rbx), %rax
cmpq %rax, %r12
jge 0x335a0
movslq (%r15,%r12,4), %r13
cmpb $0x1, (%r13,%rbp)
jne 0x3359b
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x3bc4c
testb %al, %al
je 0x3359b
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
callq 0x4273b
testb %al, %al
je 0x3359b
movslq 0x10(%rsp), %rax
imulq $0x1c, %rax, %rdi
addq 0x8(%rsp), %rdi
movzbl 0x40(%rsp,%r13), %ecx
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x3281e
incq %r12
jmp 0x33540
movl 0x10(%rsp), %eax
movq 0x1aa93385(%rip), %rcx  # 0x1aac6930
movq 0x38(%rsp), %rbx
movl %eax, 0x18(%rcx,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl 0x34(%rsp), %ebp
movl %ebp, %edx
callq 0x3429c
movq 0x1aa93363(%rip), %rax  # 0x1aac6930
leaq (%rax,%rbx), %rsi
addq $0x27b4, %rsi           # imm = 0x27B4
movq %r14, %rdi
callq 0x3d368
movq 0x1aa93349(%rip), %rax  # 0x1aac6930
incl 0x14(%rax,%rbx)
movl %ebp, %eax
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13a77(%rip), %rdi     # 0x4707d
leaq 0x13a87(%rip), %rsi     # 0x47094
leaq 0x13a97(%rip), %rcx     # 0x470ab
movl $0x2da, %edx            # imm = 0x2DA
callq 0x6230
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x149b8
movq %rbx, %rdi
callq 0x6740