// File: /workspace/github_cpp_makefile_star2_O3/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), %r13
movq 0x43f0(%rdi), %r12
movq %r12, %rdi
movl %esi, 0x4(%rsp)
movl %r13d, %edx
callq 0x56c69
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x6200
movq %r13, %rax
shlq $0x4, %rax
cmpq $0x2, %r13
jl 0x4ae40
leal -0x2(%r13), %ecx
shlq $0x4, %rcx
movl 0x4(%r14,%rcx), %r15d
movl -0xc(%rax,%r14), %eax
movl %eax, 0x8(%rsp)
leaq 0x2ba64(%rip), %rax     # 0x768b4
cmpl $0x9, (%rax)
je 0x4ae66
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x5c8b0
leaq 0x2ba63(%rip), %rbx     # 0x768d0
cmpl (%rbx), %ebp
jne 0x4af66
callq 0x56ace
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
callq 0x56bf4
cmpl (%rbx), %eax
je 0x4b120
movl %eax, 0x34(%rsp)
movl %eax, %eax
imulq $0x4548, %rax, %rbx     # imm = 0x4548
movq 0x1aa90a8b(%rip), %rdi  # 0x1aadb930
addq %rbx, %rdi
movl 0x8(%rsp), %esi
movl %r15d, %edx
callq 0x4a038
movq 0x1aa90a75(%rip), %rax  # 0x1aadb930
movq %rbx, 0x38(%rsp)
leaq (%rax,%rbx), %rdi
addq $0x1c, %rdi
leaq 0x10(%rsp), %rsi
movl $0x0, (%rsi)
movzbl 0x40(%rsp), %ecx
movq %rdi, 0x8(%rsp)
xorl %edx, %edx
callq 0x4a082
cmpl $0x1, %r13d
jne 0x4b03d
leaq 0x1aac200b(%rip), %rbx  # 0x1ab0cf00
cmpl $0x0, (%rbx)
jle 0x4b0c1
xorl %r15d, %r15d
leaq 0x1ab16d98(%rip), %r12  # 0x1ab61ca0
leaq 0x1aaba971(%rip), %rbp  # 0x1ab05880
movslq (%r12,%r15,4), %r13
cmpb $0x1, (%r13,%rbp)
jne 0x4af56
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x55478
testb %al, %al
je 0x4af56
movslq 0x10(%rsp), %rdi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq 0x8(%rsp), %rdi
addq %rax, %rdi
movzbl 0x40(%rsp,%r13), %ecx
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x4a082
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x4af0f
jmp 0x4b0c1
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %ebp, %esi
callq 0x4be06
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
je 0x4afbc
movq %r13, %rax
subq %r12, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4ce77
movq %r12, %rdi
movq %r13, %rsi
callq 0x4cf5a
leaq 0x10(%rsp), %rdi
callq 0x56b0e
movq 0x1aa90963(%rip), %rax  # 0x1aadb930
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 0x4a0cb
movq 0x1aa90939(%rip), %rax  # 0x1aadb930
movl $0x1, 0x14(%rax,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebp, %edx
callq 0x4be91
movq 0x1aa9091c(%rip), %rax  # 0x1aadb930
movl 0x8(%rax,%rbx), %edi
callq 0x1eade
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4b10c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x6410
jmp 0x4b10c
leaq 0x2b86c(%rip), %rbp     # 0x768b0
cmpl $0x0, (%rbp)
jle 0x4b0c1
xorl %r15d, %r15d
leaq 0x1ab1669c(%rip), %rbx  # 0x1ab616f0
leaq 0x1aaba825(%rip), %r12  # 0x1ab05880
movslq (%rbx,%r15,4), %r13
cmpb $0x1, (%r13,%r12)
jne 0x4b0b5
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x55478
testb %al, %al
je 0x4b0b5
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
callq 0x5c3e4
testb %al, %al
je 0x4b0b5
movslq 0x10(%rsp), %rdi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq 0x8(%rsp), %rdi
addq %rax, %rdi
movzbl 0x40(%rsp,%r13), %ecx
leaq 0x10(%rsp), %rsi
movl %r13d, %edx
callq 0x4a082
incq %r15
movslq (%rbp), %rax
cmpq %rax, %r15
jl 0x4b05b
movl 0x10(%rsp), %eax
movq 0x1aa90864(%rip), %rcx  # 0x1aadb930
movq 0x38(%rsp), %rbx
movl %eax, 0x18(%rcx,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl 0x34(%rsp), %ebp
movl %ebp, %edx
callq 0x4be91
movq 0x1aa90842(%rip), %rax  # 0x1aadb930
leaq (%rax,%rbx), %rsi
addq $0x27b4, %rsi           # imm = 0x27B4
movq %r14, %rdi
callq 0x56d88
movq 0x1aa90828(%rip), %rax  # 0x1aadb930
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 0x14fbe(%rip), %rdi     # 0x600e5
leaq 0x14fce(%rip), %rsi     # 0x600fc
leaq 0x14fde(%rip), %rcx     # 0x60113
movl $0x2da, %edx            # imm = 0x2DA
callq 0x6240
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4b159
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x6410
movq %rbx, %rdi
callq 0x6760