// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: UctSearchGenmove(game_info_t*, int, int)
int
UctSearchGenmove( game_info_t *game, int color, int lz_analysis_cs )
{
  double best_wp;

  // 探索情報をクリア
  if (!pondered) {
    for (int i = 0; i < board_max; i++) {
      statistic[i].clear();
    }
    statistic_count = 0;
    std::fill_n(criticality_index, board_max, 0);
    for (int i = 0; i < board_max; i++) {
      criticality[i] = 0.0;
    }
  }

  ResetPoCount();

  for (int i = 0; i < pure_board_max; i++) {
    const int pos = onboard_pos[i];
    owner[pos] = 50;
    owner_index[pos] = 5;
    candidates[pos] = true;
  }

  if (!reuse_subtree) {
    ClearUctHash();
  }
  
  // 探索開始時刻の記録
  StartTimer();
  
  // UCTの初期化
  current_root = ExpandRoot(game, color);

  // 前回から持ち込んだ探索回数を記録
  const int pre_simulated = uct_node[current_root].move_count;

  // 子ノードが1つ(パスのみ)ならPASSを返す
  if (uct_node[current_root].child_num <= 1) {
    return PASS;
  }

  // 探索回数の閾値を設定
  SetPoHalt(GetPoNum());

  // 自分の手番を設定
  my_color = color;

  // Dynamic Komiの算出(置碁のときのみ)
  DynamicKomi(game, &uct_node[current_root], color);

  // 探索時間とプレイアウト回数の予定値を出力
  PrintPlayoutLimits(GetTimeLimit(), GetPoHalt());

  for (int i = 0; i < threads; i++) {
    t_arg[i].thread_id = i;
    t_arg[i].game = game;
    t_arg[i].color = color;
    t_arg[i].lz_analysis_cs = lz_analysis_cs;
  }

  const double mag[3] = { 1.0, 1.5, 2.0 };
  int mag_count = 0;

  // 着手が41手以降で, 
  // 時間延長を行う設定になっていて,
  // 探索時間延長をすべきときは
  // 探索回数を1.5倍, 2.0倍に増やす

  do {
    ExtendSearchTime(mag[mag_count]);
    for (int i = 0; i < threads; i++) {
      worker[i] = new std::thread(ParallelUctSearch, &t_arg[i]);
    }
    for (int i = 0; i < threads; i++) {
      worker[i]->join();
      delete worker[i];
    }
    mag_count++;
  } while (mag_count < 3 && ExtendTime(uct_node[current_root], game->moves));

  const int pos = SelectMove(game, uct_node[current_root], color, best_wp);
  
  // 探索にかかった時間を求める
  const double finish_time = CalculateElapsedTime();

  // 各地点の領地になる確率の出力
  PrintOwner(&uct_node[current_root], statistic, color, statistic_count.load(), owner);
  
  const int po_speed = static_cast<int>(CalculatePlayoutSpeed(finish_time, threads));

  // 最善応手列を出力
  PrintBestSequence(game, uct_node, current_root, color);
  // 探索の情報を出力(探索回数, 勝敗, 思考時間, 勝率, 探索速度)
  PrintPlayoutInformation(&uct_node[current_root], po_speed, finish_time, pre_simulated);
  // 次の探索でのプレイアウト回数の算出
  CalculateNextPlayouts(game, color, best_wp, finish_time, threads);

  return pos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r15d
movl %esi, %ebx
movq %rdi, %rbp
cmpb $0x0, 0x1aabb195(%rip)  # 0x1ab05bc9
jne 0x4aaa7
leaq 0x2be7b(%rip), %rax     # 0x768b8
movslq (%rax), %r12
testq %r12, %r12
jle 0x4aa9f
leaq 0x1aabb384(%rip), %rax  # 0x1ab05dd0
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
xchgl %esi, (%rax,%rdx,4)
incq %rdx
cmpq $0x4, %rdx
jne 0x4aa50
incq %rcx
addq $0x10, %rax
cmpq %r12, %rcx
jb 0x4aa4e
xorl %eax, %eax
xchgl %eax, 0x1aabe7ee(%rip)  # 0x1ab09260
leaq (,%r12,4), %rdx
leaq 0x1aabe7ef(%rip), %rdi  # 0x1ab09270
xorl %esi, %esi
callq 0x6200
shlq $0x3, %r12
leaq 0x1aabf50d(%rip), %rdi  # 0x1ab09fa0
xorl %esi, %esi
movq %r12, %rdx
callq 0x6200
jmp 0x4aaa7
xorl %eax, %eax
xchgl %eax, 0x1aabe7b9(%rip)  # 0x1ab09260
callq 0x4d8d9
leaq 0x2bdfd(%rip), %rax     # 0x768b0
movslq (%rax), %rax
testq %rax, %rax
jle 0x4ab00
leaq 0x1aab863e(%rip), %rcx  # 0x1ab03100
xorl %edx, %edx
leaq 0x1ab16c25(%rip), %rsi  # 0x1ab616f0
movabsq $0x4049000000000000, %rdi # imm = 0x4049000000000000
leaq 0x1aaba074(%rip), %r8   # 0x1ab04b50
leaq 0x1aabad9d(%rip), %r9   # 0x1ab05880
movslq (%rsi,%rdx,4), %r10
movq %rdi, (%rcx,%r10,8)
movl $0x5, (%r8,%r10,4)
movb $0x1, (%r10,%r9)
incq %rdx
cmpq %rdx, %rax
jne 0x4aae3
cmpb $0x0, 0x1aab8039(%rip)  # 0x1ab02b40
jne 0x4ab0e
callq 0x56ace
callq 0x4d841
movq %rbp, %rdi
movl %ebx, %esi
callq 0x4addf
movl %eax, 0x1aa90e15(%rip)  # 0x1aadb938
movq 0x1aa90e06(%rip), %rcx  # 0x1aadb930
cltq
imulq $0x4548, %rax, %rax     # imm = 0x4548
movl 0x8(%rcx,%rax), %edx
movq 0x1aa90df2(%rip), %rax  # 0x1aadb930
movslq 0x1aa90df3(%rip), %rcx  # 0x1aadb938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
xorl %r12d, %r12d
cmpl $0x2, 0x18(%rax,%rcx)
jl 0x4adb0
movl %edx, 0x8(%rsp)
callq 0x4d8f0
movl %eax, %edi
callq 0x4d906
movl %ebx, 0x1aac0e78(%rip)  # 0x1ab0b9e8
movslq 0x1aa90dc1(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa90dab(%rip), %rsi  # 0x1aadb930
movq %rbp, %rdi
movl %ebx, %edx
callq 0x56d15
callq 0x4d402
movsd %xmm0, (%rsp)
callq 0x4d8e9
movsd (%rsp), %xmm0
movl %eax, %edi
callq 0x1e9f3
movslq 0x2bcc7(%rip), %rax     # 0x76878
testq %rax, %rax
jle 0x4abd8
leaq 0x1aac0e43(%rip), %rcx  # 0x1ab0ba00
xorl %edx, %edx
movl %edx, -0x8(%rcx)
movq %rbp, -0x10(%rcx)
movl %ebx, -0x4(%rcx)
movl %r15d, (%rcx)
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %rax
jne 0x4abbf
movl %ebx, 0xc(%rsp)
xorl %r13d, %r13d
leaq 0x57b(%rip), %r15       # 0x4b161
leaq 0x18(%rsp), %r12
movq %rbp, 0x10(%rsp)
leaq 0x15569(%rip), %rax     # 0x60160
movsd (%rax,%r13,8), %xmm0
callq 0x4d41d
cmpl $0x0, 0x2bc6f(%rip)     # 0x76878
jle 0x4acab
movq %r13, (%rsp)
leaq 0x1aac0dd6(%rip), %rbx  # 0x1ab0b9f0
leaq 0x1aabafaf(%rip), %r14  # 0x1ab05bd0
xorl %ebp, %ebp
movl $0x8, %edi
callq 0x6400
movq %rax, %r13
movq %rbx, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4ccd8
movq %r13, (%r14)
incq %rbp
movslq 0x2bc28(%rip), %rax     # 0x76878
addq $0x8, %r14
addq $0x18, %rbx
cmpq %rax, %rbp
jl 0x4ac23
testl %eax, %eax
movq 0x10(%rsp), %rbp
movq (%rsp), %r13
jle 0x4acab
leaq 0x1aabaf5f(%rip), %rbx  # 0x1ab05bd0
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x6710
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4ac8e
cmpq $0x0, (%rdi)
jne 0x4adc2
movl $0x8, %esi
callq 0x6410
incq %r14
movslq 0x2bbd6(%rip), %rax     # 0x76878
addq $0x8, %rbx
cmpq %rax, %r14
jl 0x4ac74
cmpq $0x2, %r13
je 0x4acdc
incq %r13
movslq 0x1aa90c7d(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa90c67(%rip), %rdi  # 0x1aadb930
movl 0x43b0(%rbp), %esi
callq 0x4d9e4
testb %al, %al
jne 0x4abf0
movslq 0x1aa90c55(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa90c3f(%rip), %rsi  # 0x1aadb930
leaq 0x20(%rsp), %r15
movq %rbp, %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
movq %r15, %rcx
callq 0x4d23a
movl %eax, %r12d
callq 0x4d850
movsd %xmm0, (%rsp)
movslq 0x1aa90c1d(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa90c07(%rip), %rdi  # 0x1aadb930
movl 0x1aabe531(%rip), %ecx  # 0x1ab09260
leaq 0x1aabb09a(%rip), %rsi  # 0x1ab05dd0
leaq 0x1aab83c3(%rip), %r8   # 0x1ab03100
movl %ebx, %edx
callq 0x1dc6c
movl 0x2bb2e(%rip), %edi     # 0x76878
movsd (%rsp), %xmm0
callq 0x4d80e
cvttsd2si %xmm0, %r13d
movq 0x1aa90bd0(%rip), %rsi  # 0x1aadb930
movl 0x1aa90bd2(%rip), %edx  # 0x1aadb938
movq %rbp, %rdi
movl %ebx, %ecx
callq 0x1e1b6
movslq 0x1aa90bc1(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa90bab(%rip), %rdi  # 0x1aadb930
movl %r13d, %esi
movsd (%rsp), %xmm0
movl 0x8(%rsp), %edx
callq 0x1e560
movsd (%r15), %xmm0
movl 0x2bad7(%rip), %edx     # 0x76878
movq %rbp, %rdi
movl %ebx, %esi
movsd (%rsp), %xmm1
callq 0x4d629
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x60b0
movq %rax, %rbx
movl $0x8, %esi
movq %r13, %rdi
callq 0x6410
movq %rbx, %rdi
callq 0x6760