// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
movq %rdi, %r13
cmpb $0x0, 0x1aabdc53(%rip)  # 0x1aaf0bc9
je 0x330af
callq 0x35c40
leaq 0x2e928(%rip), %rax     # 0x618b0
movl (%rax), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x1ab19758(%rip), %rdx  # 0x1ab4c6f0
leaq 0x1aabb161(%rip), %rsi  # 0x1aaee100
movabsq $0x4049000000000000, %rdi # imm = 0x4049000000000000
leaq 0x1aabcba0(%rip), %r8   # 0x1aaefb50
leaq 0x1aabd8c9(%rip), %r9   # 0x1aaf0880
cmpq %rcx, %rax
je 0x32fd6
movslq (%rdx,%rcx,4), %r10
movq %rdi, (%rsi,%r10,8)
movl $0x5, (%r8,%r10,4)
movb $0x1, (%r10,%r9)
incq %rcx
jmp 0x32fb7
cmpb $0x0, 0x1aabab63(%rip)  # 0x1aaedb40
jne 0x32fe4
callq 0x3d0d6
callq 0x35c02
movq %r13, %rdi
movl %ebp, %esi
callq 0x3331c
movl %eax, 0x1aa9393f(%rip)  # 0x1aac6938
movq 0x1aa93930(%rip), %rcx  # 0x1aac6930
cltq
imulq $0x4548, %rax, %rax     # imm = 0x4548
movl 0x8(%rcx,%rax), %edx
movq 0x1aa9391c(%rip), %rax  # 0x1aac6930
movslq 0x1aa9391d(%rip), %rcx  # 0x1aac6938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
xorl %r12d, %r12d
cmpl $0x2, 0x18(%rax,%rcx)
jl 0x332ae
movl %edx, 0x8(%rsp)
callq 0x35c57
movl %eax, %edi
callq 0x35c6d
movl %ebp, 0x1aac39a2(%rip)  # 0x1aaf69e8
movslq 0x1aa938eb(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa938d5(%rip), %rsi  # 0x1aac6930
movq %r13, %rdi
movl %ebp, %edx
callq 0x3d2f5
callq 0x35822
movsd %xmm0, (%rsp)
callq 0x35c50
movsd (%rsp), %xmm0
movl %eax, %edi
callq 0x10670
movl 0x2e7f2(%rip), %eax     # 0x61878
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x1aac396c(%rip), %rdx  # 0x1aaf6a00
cmpq %rcx, %rax
je 0x330dd
movl %ecx, -0x8(%rdx)
movq %r13, -0x10(%rdx)
movl %ebp, -0x4(%rdx)
movl %r15d, (%rdx)
incq %rcx
addq $0x18, %rdx
jmp 0x33094
leaq 0x1aabdd1a(%rip), %r12  # 0x1aaf0dd0
xorl %r14d, %r14d
leaq 0x2e7f8(%rip), %rbx     # 0x618b8
movslq (%rbx), %rsi
cmpq %rsi, %r14
jge 0x332c0
movq %r12, %rdi
callq 0x35010
incq %r14
addq $0x10, %r12
jmp 0x330c0
movq %r13, 0x10(%rsp)
movl %ebp, 0xc(%rsp)
xorl %ebx, %ebx
leaq 0x1aabdae1(%rip), %r12  # 0x1aaf0bd0
pushq $0x8
popq %r15
leaq 0x18(%rsp), %r13
movq %rbx, (%rsp)
leaq 0x13ffd(%rip), %rax     # 0x47100
movsd (%rax,%rbx,8), %xmm0
callq 0x3583d
leaq 0x1aac38dc(%rip), %r14  # 0x1aaf69f0
movq %r12, %rbx
xorl %r12d, %r12d
movslq 0x2e757(%rip), %rax     # 0x61878
cmpq %rax, %r12
jge 0x33158
movq %r15, %rdi
callq 0x63e0
movq %rax, %rbp
movq %r14, 0x18(%rsp)
movq %rax, %rdi
leaq 0x4f3(%rip), %rsi       # 0x33633
movq %r13, %rdx
callq 0x35024
movq %rbp, (%rbx)
incq %r12
addq $0x8, %rbx
addq $0x18, %r14
jmp 0x3311a
leaq 0x1aabda71(%rip), %r12  # 0x1aaf0bd0
movq %r12, %r14
xorl %ebx, %ebx
cltq
cmpq %rax, %rbx
jge 0x3319d
movq (%r14), %rdi
callq 0x66e0
movq (%r14), %rbp
testq %rbp, %rbp
je 0x33183
movq %rbp, %rdi
callq 0x2695c
movq %rbp, %rdi
movq %r15, %rsi
callq 0x63f0
incq %rbx
movl 0x2e6e1(%rip), %eax     # 0x61878
addq $0x8, %r14
jmp 0x33164
movq (%rsp), %rbx
cmpq $0x2, %rbx
je 0x331d7
incq %rbx
movslq 0x1aa93787(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa93771(%rip), %rdi  # 0x1aac6930
movq 0x10(%rsp), %rax
movl 0x43b0(%rax), %esi
callq 0x35d15
testb %al, %al
jne 0x330f8
movslq 0x1aa9375a(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa93744(%rip), %rsi  # 0x1aac6930
leaq 0x20(%rsp), %r15
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
movq %r15, %rcx
callq 0x356af
movl %eax, %r12d
callq 0x35c11
movsd %xmm0, (%rsp)
movslq 0x1aa9371d(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa93707(%rip), %rdi  # 0x1aac6930
movl 0x1aac1031(%rip), %ecx  # 0x1aaf4260
leaq 0x1aabdb9a(%rip), %rsi  # 0x1aaf0dd0
leaq 0x1aabaec3(%rip), %r8   # 0x1aaee100
movl %ebx, %edx
callq 0xfca5
movl 0x2e62e(%rip), %edi     # 0x61878
movsd (%rsp), %xmm0
callq 0x35bd5
cvttsd2si %xmm0, %ebp
movq 0x1aa936d1(%rip), %rsi  # 0x1aac6930
movl 0x1aa936d3(%rip), %edx  # 0x1aac6938
movq %r14, %rdi
movl %ebx, %ecx
callq 0x100d9
movslq 0x1aa936c2(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa936ac(%rip), %rdi  # 0x1aac6930
movl %ebp, %esi
movsd (%rsp), %xmm0
movl 0x8(%rsp), %edx
callq 0x103ad
movsd (%r15), %xmm0
movl 0x2e5d9(%rip), %edx     # 0x61878
movq %r14, %rdi
movl %ebx, %esi
movsd (%rsp), %xmm1
callq 0x359f1
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
xorl %eax, %eax
xchgl %eax, 0x1aac0f95(%rip)  # 0x1aaf4260
leaq 0x18(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x1aac0f96(%rip), %rdi  # 0x1aaf4270
callq 0x355fe
movl (%rbx), %eax
testl %eax, %eax
cmovlel %r14d, %eax
shlq $0x3, %rax
leaq 0x1aac1cae(%rip), %rcx  # 0x1aaf4fa0
cmpq %r14, %rax
je 0x32f7c
andq $0x0, (%r14,%rcx)
addq $0x8, %r14
jmp 0x332f2
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x63f0
movq %rbx, %rdi
callq 0x6740