// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
cmpb $0x0, 0x1aaba4fd(%rip)  # 0x1ab04bc9
jne 0x4a742
leaq 0x2b1e3(%rip), %rax     # 0x758b8
cmpl $0x0, (%rax)
jle 0x4a702
leaq 0x1aaba6ef(%rip), %rcx  # 0x1ab04dd0
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
xchgl %edi, (%rcx,%rsi,4)
incq %rsi
cmpq $0x4, %rsi
jne 0x4a6e5
incq %rdx
movslq (%rax), %rsi
addq $0x10, %rcx
cmpq %rsi, %rdx
jl 0x4a6e3
xorl %ecx, %ecx
xchgl %ecx, 0x1aabdb56(%rip)  # 0x1ab08260
movslq (%rax), %r12
testq %r12, %r12
jle 0x4a728
leaq (,%r12,4), %rdx
leaq 0x1aabdb4f(%rip), %rdi  # 0x1ab08270
xorl %esi, %esi
callq 0x6210
testl %r12d, %r12d
jle 0x4a742
shlq $0x3, %r12
leaq 0x1aabe868(%rip), %rdi  # 0x1ab08fa0
xorl %esi, %esi
movq %r12, %rdx
callq 0x6210
callq 0x4d5a9
leaq 0x2b162(%rip), %rax     # 0x758b0
movslq (%rax), %rax
testq %rax, %rax
jle 0x4a79b
leaq 0x1aab79a3(%rip), %rcx  # 0x1ab02100
xorl %edx, %edx
leaq 0x1ab15f8a(%rip), %rsi  # 0x1ab606f0
movabsq $0x4049000000000000, %rdi # imm = 0x4049000000000000
leaq 0x1aab93d9(%rip), %r8   # 0x1ab03b50
leaq 0x1aaba102(%rip), %r9   # 0x1ab04880
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 0x4a77e
cmpb $0x0, 0x1aab739e(%rip)  # 0x1ab01b40
jne 0x4a7a9
callq 0x56126
callq 0x4d511
movq %r14, %rdi
movl %ebx, %esi
callq 0x4aa7a
movl %eax, 0x1aa9017a(%rip)  # 0x1aada938
movq 0x1aa9016b(%rip), %rcx  # 0x1aada930
cltq
imulq $0x4548, %rax, %rax     # imm = 0x4548
movl 0x8(%rcx,%rax), %edx
movq 0x1aa90157(%rip), %rax  # 0x1aada930
movslq 0x1aa90158(%rip), %rcx  # 0x1aada938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
xorl %r12d, %r12d
cmpl $0x2, 0x18(%rax,%rcx)
jl 0x4aa4b
movl %edx, 0x8(%rsp)
callq 0x4d5c0
movl %eax, %edi
callq 0x4d5d6
movl %ebx, 0x1aac01dd(%rip)  # 0x1ab0a9e8
movslq 0x1aa90126(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa90110(%rip), %rsi  # 0x1aada930
movq %r14, %rdi
movl %ebx, %edx
callq 0x5635d
callq 0x4d0ca
movsd %xmm0, (%rsp)
callq 0x4d5b9
movsd (%rsp), %xmm0
movl %eax, %edi
callq 0x1ebe8
movslq 0x2b02c(%rip), %rax     # 0x75878
testq %rax, %rax
jle 0x4a873
leaq 0x1aac01a8(%rip), %rcx  # 0x1ab0aa00
xorl %edx, %edx
movl %edx, -0x8(%rcx)
movq %r14, -0x10(%rcx)
movl %ebx, -0x4(%rcx)
movl %r15d, (%rcx)
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %rax
jne 0x4a85a
movl %ebx, 0xc(%rsp)
xorl %ebx, %ebx
leaq 0x57c(%rip), %r15       # 0x4adfc
leaq 0x18(%rsp), %r12
movq %r14, 0x10(%rsp)
movq %rbx, (%rsp)
leaq 0x157eb(%rip), %rax     # 0x60080
movsd (%rax,%rbx,8), %xmm0
callq 0x4d0e5
cmpl $0x0, 0x2afd2(%rip)     # 0x75878
jle 0x4a8f2
leaq 0x1aac0141(%rip), %rbx  # 0x1ab0a9f0
leaq 0x1aaba31a(%rip), %r14  # 0x1ab04bd0
xorl %ebp, %ebp
movl $0x8, %edi
callq 0x6410
movq %rax, %r13
movq %rbx, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4c976
movq %r13, (%r14)
incq %rbp
movslq 0x2af93(%rip), %rax     # 0x75878
addq $0x8, %r14
addq $0x18, %rbx
cmpq %rax, %rbp
jl 0x4a8b8
cmpl $0x0, 0x2af7f(%rip)     # 0x75878
jle 0x4a93c
leaq 0x1aaba2ce(%rip), %rbx  # 0x1ab04bd0
xorl %r14d, %r14d
movq (%rbx), %rdi
callq 0x6720
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4a929
cmpq $0x0, (%rdi)
jne 0x4aa5d
movl $0x8, %esi
callq 0x6420
incq %r14
movslq 0x2af45(%rip), %rax     # 0x75878
addq $0x8, %rbx
cmpq %rax, %r14
jl 0x4a905
movq (%rsp), %rbx
cmpq $0x2, %rbx
movq 0x10(%rsp), %r14
je 0x4a977
incq %rbx
movslq 0x1aa8ffe3(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8ffcd(%rip), %rdi  # 0x1aada930
movl 0x43b0(%r14), %esi
callq 0x4d6b4
testb %al, %al
jne 0x4a88a
movslq 0x1aa8ffba(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa8ffa4(%rip), %rsi  # 0x1aada930
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
movq %r15, %rcx
callq 0x4cf0e
movl %eax, %r12d
callq 0x4d520
movsd %xmm0, (%rsp)
movslq 0x1aa8ff82(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8ff6c(%rip), %rdi  # 0x1aada930
movl 0x1aabd896(%rip), %ecx  # 0x1ab08260
leaq 0x1aaba3ff(%rip), %rsi  # 0x1ab04dd0
leaq 0x1aab7728(%rip), %r8   # 0x1ab02100
movl %ebx, %edx
callq 0x1de7c
movl 0x2ae93(%rip), %edi     # 0x75878
movsd (%rsp), %xmm0
callq 0x4d4de
cvttsd2si %xmm0, %r13d
movq 0x1aa8ff35(%rip), %rsi  # 0x1aada930
movl 0x1aa8ff37(%rip), %edx  # 0x1aada938
movq %r14, %rdi
movl %ebx, %ecx
callq 0x1e3c6
movslq 0x1aa8ff26(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8ff10(%rip), %rdi  # 0x1aada930
movl %r13d, %esi
movsd (%rsp), %xmm0
movl 0x8(%rsp), %edx
callq 0x1e781
movsd (%r15), %xmm0
movl 0x2ae3c(%rip), %edx     # 0x75878
movq %r14, %rdi
movl %ebx, %esi
movsd (%rsp), %xmm1
callq 0x4d2f5
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 0x6420
movq %rbx, %rdi
callq 0x6770