// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movl %edx, -0x28(%rbp)
testb $0x1, 0x1aaed9bd(%rip)  # 0x1ab76d49
jne 0x8942c
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
leaq 0x5e6a5(%rip), %rcx     # 0xe7a48
cmpl (%rcx), %eax
jge 0x893c9
movslq -0x34(%rbp), %rax
leaq 0x1aaedb9e(%rip), %rdi  # 0x1ab76f50
shlq $0x4, %rax
addq %rax, %rdi
callq 0x8cf10
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x89399
leaq 0x1aaf1010(%rip), %rdi  # 0x1ab7a3e0
xorl %esi, %esi
callq 0x88450
leaq 0x5e66a(%rip), %rax     # 0xe7a48
movl (%rax), %esi
movl $0x0, -0x38(%rbp)
leaq 0x1aaf1002(%rip), %rdi  # 0x1ab7a3f0
leaq -0x38(%rbp), %rdx
callq 0x80790
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
leaq 0x5e640(%rip), %rcx     # 0xe7a48
cmpl (%rcx), %eax
jge 0x8942a
movslq -0x3c(%rbp), %rcx
leaq 0x1aaf1d09(%rip), %rax  # 0x1ab7b120
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x893fe
jmp 0x8942c
callq 0x8f880
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
leaq 0x5e5fe(%rip), %rcx     # 0xe7a40
cmpl (%rcx), %eax
jge 0x8949b
movslq -0x40(%rbp), %rcx
leaq 0x1ab4941f(%rip), %rax  # 0x1abd2870
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movslq -0x44(%rbp), %rcx
leaq 0x1aaeae1e(%rip), %rax  # 0x1ab74280
movsd 0x25cfe(%rip), %xmm0    # 0xaf168
movsd %xmm0, (%rax,%rcx,8)
movslq -0x44(%rbp), %rcx
leaq 0x1aaec856(%rip), %rax  # 0x1ab75cd0
movl $0x5, (%rax,%rcx,4)
movslq -0x44(%rbp), %rcx
leaq 0x1aaed574(%rip), %rax  # 0x1ab76a00
movb $0x1, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x89438
testb $0x1, 0x1aaea81e(%rip)  # 0x1ab73cc0
jne 0x894a9
callq 0x9f8d0
callq 0x8f7d0
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x89930
movl %eax, 0x1aac35f8(%rip)  # 0x1ab4cab8
movq 0x1aac35e9(%rip), %rdi  # 0x1ab4cab0
movslq 0x1aac35ea(%rip), %rax  # 0x1ab4cab8
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
addq $0x8, %rdi
callq 0x25780
movl %eax, -0x48(%rbp)
movq 0x1aac35c5(%rip), %rax  # 0x1ab4cab0
movslq 0x1aac35c6(%rip), %rcx  # 0x1ab4cab8
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
cmpl $0x1, 0x18(%rax)
jg 0x8950e
movl $0x0, -0x18(%rbp)
jmp 0x89912
callq 0x8f8d0
movl %eax, %edi
callq 0x8f910
movl -0x24(%rbp), %eax
movl %eax, 0x1aaf3645(%rip)  # 0x1ab7cb68
movq -0x20(%rbp), %rdi
movq 0x1aac3582(%rip), %rsi  # 0x1ab4cab0
movslq 0x1aac3583(%rip), %rax  # 0x1ab4cab8
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rsi
movl -0x24(%rbp), %edx
callq 0x9fe50
callq 0x8f0a0
movsd %xmm0, -0xb0(%rbp)
callq 0x8f8c0
movsd -0xb0(%rbp), %xmm0
movl %eax, %edi
callq 0x222e0
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x5e488(%rip), %eax     # 0xe7a00
jge 0x895e6
movl -0x4c(%rbp), %ecx
movslq -0x4c(%rbp), %rdx
leaq 0x1aaf35e8(%rip), %rax  # 0x1ab7cb70
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
leaq 0x1aaf35cf(%rip), %rax  # 0x1ab7cb70
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x24(%rbp), %ecx
movslq -0x4c(%rbp), %rdx
leaq 0x1aaf35b7(%rip), %rax  # 0x1ab7cb70
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x28(%rbp), %ecx
movslq -0x4c(%rbp), %rdx
leaq 0x1aaf359f(%rip), %rax  # 0x1ab7cb70
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8956f
movq 0x25c73(%rip), %rax     # 0xaf260
movq %rax, -0x70(%rbp)
movq 0x25c70(%rip), %rax     # 0xaf268
movq %rax, -0x68(%rbp)
movq 0x25c6d(%rip), %rax     # 0xaf270
movq %rax, -0x60(%rbp)
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movsd -0x70(%rbp,%rax,8), %xmm0
callq 0x8f0e0
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl 0x5e3d3(%rip), %eax     # 0xe7a00
jge 0x896bc
movl $0x8, %edi
callq 0x7550
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
movslq -0x78(%rbp), %rax
leaq (%rax,%rax,2), %rcx
leaq 0x1aaf3517(%rip), %rax  # 0x1ab7cb70
leaq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
leaq 0x7a8(%rip), %rsi       # 0x89e10
leaq -0x80(%rbp), %rdx
callq 0x8cf60
jmp 0x89673
movq -0xb8(%rbp), %rdx
movslq -0x78(%rbp), %rcx
leaq 0x1aaed6cb(%rip), %rax  # 0x1ab76d50
movq %rdx, (%rax,%rcx,8)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x89624
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
movl $0x8, %esi
callq 0x7560
jmp 0x8991e
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl 0x5e32e(%rip), %eax     # 0xe7a00
jge 0x8973a
movslq -0x90(%rbp), %rcx
leaq 0x1aaed66e(%rip), %rax  # 0x1ab76d50
movq (%rax,%rcx,8), %rdi
callq 0x7940
movslq -0x90(%rbp), %rcx
leaq 0x1aaed657(%rip), %rax  # 0x1ab76d50
movq (%rax,%rcx,8), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x89727
movq -0xc0(%rbp), %rdi
callq 0x66080
movq -0xc0(%rbp), %rdi
movl $0x8, %esi
callq 0x7560
jmp 0x89729
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x896c6
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
xorl %eax, %eax
cmpl $0x3, -0x74(%rbp)
movb %al, -0xc1(%rbp)
jge 0x8977e
movq 0x1aac3358(%rip), %rdi  # 0x1ab4cab0
movslq 0x1aac3359(%rip), %rax  # 0x1ab4cab8
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
movq -0x20(%rbp), %rax
movl 0x43b0(%rax), %esi
callq 0x8fa90
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x8960e
movq -0x20(%rbp), %rdi
movq 0x1aac3319(%rip), %rsi  # 0x1ab4cab0
movslq 0x1aac331a(%rip), %rax  # 0x1ab4cab8
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rsi
movl -0x24(%rbp), %edx
leaq -0x30(%rbp), %rcx
callq 0x8ed80
movl %eax, -0x94(%rbp)
callq 0x8f800
movsd %xmm0, -0xa0(%rbp)
movq 0x1aac32e2(%rip), %rax  # 0x1ab4cab0
movslq 0x1aac32e3(%rip), %rcx  # 0x1ab4cab8
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq 0x1aaf0bea(%rip), %rax  # 0x1ab7a3e0
movq %rax, -0x8(%rbp)
movl $0x5, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0xc8(%rbp)
decl %eax
subl $0x2, %eax
jb 0x89849
jmp 0x8982e
movl -0xc8(%rbp), %eax
subl $0x5, %eax
je 0x89857
jmp 0x8983b
movq -0xd0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x89863
movq -0xd0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x89863
movq -0xd0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
movl -0xd4(%rbp), %edx
movq -0xe0(%rbp), %rdi
movl -0x14(%rbp), %ecx
leaq 0x1aaed6d6(%rip), %rsi  # 0x1ab76f50
leaq 0x1aaea9ff(%rip), %r8   # 0x1ab74280
callq 0x211f0
movsd -0xa0(%rbp), %xmm0
movl 0x5e16c(%rip), %edi     # 0xe7a00
callq 0x8f770
cvttsd2si %xmm0, %eax
movl %eax, -0xa4(%rbp)
movq -0x20(%rbp), %rdi
movq 0x1aac3202(%rip), %rsi  # 0x1ab4cab0
movl 0x1aac3204(%rip), %edx  # 0x1ab4cab8
movl -0x24(%rbp), %ecx
callq 0x21960
movq 0x1aac31ed(%rip), %rdi  # 0x1ab4cab0
movslq 0x1aac31ee(%rip), %rax  # 0x1ab4cab8
imulq $0x4548, %rax, %rax     # imm = 0x4548
addq %rax, %rdi
movl -0xa4(%rbp), %esi
movsd -0xa0(%rbp), %xmm0
movl -0x48(%rbp), %edx
callq 0x21ea0
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
movsd -0x30(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
movl 0x5e0fc(%rip), %edx     # 0xe7a00
callq 0x8f420
movl -0x94(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x79b0
nopw (%rax,%rax)