// File: /workspace/github_cpp_makefile_star2_O1/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 0x562af
movl %eax, %ebp
leaq 0x40(%rsp), %rdi
xorl %r15d, %r15d
movl $0x349, %edx            # imm = 0x349
xorl %esi, %esi
callq 0x6210
movq %r13, %rax
shlq $0x4, %rax
cmpq $0x2, %r13
jl 0x4aadb
leal -0x2(%r13), %ecx
shlq $0x4, %rcx
movl 0x4(%r14,%rcx), %r15d
movl -0xc(%rax,%r14), %eax
movl %eax, 0x8(%rsp)
leaq 0x2adc9(%rip), %rax     # 0x758b4
cmpl $0x9, (%rax)
je 0x4ab01
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x5c01c
leaq 0x2adc8(%rip), %rbx     # 0x758d0
cmpl (%rbx), %ebp
jne 0x4ac01
callq 0x56126
movq %r12, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
callq 0x5623a
cmpl (%rbx), %eax
je 0x4adbb
movl %eax, 0x34(%rsp)
movl %eax, %eax
imulq $0x4548, %rax, %rbx     # imm = 0x4548
movq 0x1aa8fdf0(%rip), %rdi  # 0x1aada930
addq %rbx, %rdi
movl 0x8(%rsp), %esi
movl %r15d, %edx
callq 0x49dc0
movq 0x1aa8fdda(%rip), %rax  # 0x1aada930
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 0x49e0a
cmpl $0x1, %r13d
jne 0x4acd8
leaq 0x1aac1370(%rip), %rbx  # 0x1ab0bf00
cmpl $0x0, (%rbx)
jle 0x4ad5c
xorl %r15d, %r15d
leaq 0x1ab160fd(%rip), %r12  # 0x1ab60ca0
leaq 0x1aab9cd6(%rip), %rbp  # 0x1ab04880
movslq (%r12,%r15,4), %r13
cmpb $0x1, (%r13,%rbp)
jne 0x4abf1
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x54a9c
testb %al, %al
je 0x4abf1
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 0x49e0a
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x4abaa
jmp 0x4ad5c
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %ebp, %esi
callq 0x4bab3
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
cmpq %r13, %r12
je 0x4ac57
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 0x4cb27
movq %r12, %rdi
movq %r13, %rsi
callq 0x4cc0a
leaq 0x10(%rsp), %rdi
callq 0x56166
movq 0x1aa8fcc8(%rip), %rax  # 0x1aada930
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 0x49e53
movq 0x1aa8fc9e(%rip), %rax  # 0x1aada930
movl $0x1, 0x14(%rax,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebp, %edx
callq 0x4bb3e
movq 0x1aa8fc81(%rip), %rax  # 0x1aada930
movl 0x8(%rax,%rbx), %edi
callq 0x1ecd3
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4ada7
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x6420
jmp 0x4ada7
leaq 0x2abd1(%rip), %rbp     # 0x758b0
cmpl $0x0, (%rbp)
jle 0x4ad5c
xorl %r15d, %r15d
leaq 0x1ab15a01(%rip), %rbx  # 0x1ab606f0
leaq 0x1aab9b8a(%rip), %r12  # 0x1ab04880
movslq (%rbx,%r15,4), %r13
cmpb $0x1, (%r13,%r12)
jne 0x4ad50
movq %r14, %rdi
movl %r13d, %esi
movl 0x4(%rsp), %edx
callq 0x54a9c
testb %al, %al
je 0x4ad50
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %r13d, %edx
callq 0x5bb5d
testb %al, %al
je 0x4ad50
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 0x49e0a
incq %r15
movslq (%rbp), %rax
cmpq %rax, %r15
jl 0x4acf6
movl 0x10(%rsp), %eax
movq 0x1aa8fbc9(%rip), %rcx  # 0x1aada930
movq 0x38(%rsp), %rbx
movl %eax, 0x18(%rcx,%rbx)
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl 0x34(%rsp), %ebp
movl %ebp, %edx
callq 0x4bb3e
movq 0x1aa8fba7(%rip), %rax  # 0x1aada930
leaq (%rax,%rbx), %rsi
addq $0x27b4, %rsi           # imm = 0x27B4
movq %r14, %rdi
callq 0x563dc
movq 0x1aa8fb8d(%rip), %rax  # 0x1aada930
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 0x1523b(%rip), %rdi     # 0x5fffd
leaq 0x1524b(%rip), %rsi     # 0x60014
leaq 0x1525b(%rip), %rcx     # 0x6002b
movl $0x2da, %edx            # imm = 0x2DA
callq 0x6250
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4adf4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x6420
movq %rbx, %rdi
callq 0x6770