// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: UctAnalyze(game_info_t*, int)
int
UctAnalyze( game_info_t *game, int color )
{
  std::thread *worker[THREAD_MAX];

  // 探索情報をクリア
  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();

  ClearUctHash();

  current_root = ExpandRoot(game, color);

  SetPoHalt(10000);

  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 = -1;
    worker[i] = new std::thread(ParallelUctSearch, &t_arg[i]);
  }

  for (int i = 0; i < threads; i++) {
    worker[i]->join();
    delete worker[i];
  }

  int black = 0, white = 0;

  const int inv_count = 1.0 / statistic_count.load();
  for (int y = board_start; y <= board_end; y++) {
    for (int x = board_start; x <= board_end; x++) {
      const int pos = POS(x, y);
      const double ownership_value = static_cast<double>(statistic[pos].colors[S_BLACK]) * inv_count;
      if (ownership_value > 0.5) {
        black++;
      } else {
        white++;
      }
    }
  }

  PrintOwner(&uct_node[current_root], statistic, color, statistic_count.load(), owner);

  return black - white;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %esi, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x1aabd271(%rip), %r15  # 0x1aaf0dd0
xorl %r14d, %r14d
leaq 0x2dd4f(%rip), %rbx     # 0x618b8
movslq (%rbx), %rsi
cmpq %rsi, %r14
jge 0x33b82
movq %r15, %rdi
callq 0x35010
incq %r14
addq $0x10, %r15
jmp 0x33b69
xorl %r14d, %r14d
xorl %eax, %eax
xchgl %eax, 0x1aac06d3(%rip)  # 0x1aaf4260
leaq 0x18(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x1aac06d4(%rip), %rdi  # 0x1aaf4270
callq 0x355fe
movl (%rbx), %eax
testl %eax, %eax
cmovlel %r14d, %eax
shlq $0x3, %rax
leaq 0x1aac13ec(%rip), %rcx  # 0x1aaf4fa0
cmpq %r14, %rax
je 0x33bc4
andq $0x0, (%r14,%rcx)
addq $0x8, %r14
jmp 0x33bb4
callq 0x35c40
callq 0x3d0d6
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x3331c
movl %eax, 0x1aa92d56(%rip)  # 0x1aac6938
movl $0x2710, %edi           # imm = 0x2710
callq 0x35c6d
leaq 0x1aac2dfd(%rip), %rbx  # 0x1aaf69f0
xorl %r14d, %r14d
pushq $0x8
popq %r15
leaq -0x5ce(%rip), %r12      # 0x33633
leaq 0x18(%rsp), %r13
movslq 0x2dc6b(%rip), %rax     # 0x61878
cmpq %rax, %r14
jge 0x33c55
movl %r14d, 0x8(%rbx)
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rbx)
orl $-0x1, 0x10(%rbx)
movq %r15, %rdi
callq 0x63e0
movq %rax, %rbp
movq %rbx, 0x18(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x35024
movq %rbp, 0x20(%rsp,%r14,8)
incq %r14
addq $0x18, %rbx
jmp 0x33c06
xorl %ebx, %ebx
pushq $0x8
popq %r14
cltq
cmpq %rax, %rbx
jge 0x33c8d
movq 0x20(%rsp,%rbx,8), %r15
movq %r15, %rdi
callq 0x66e0
movq %r15, %rdi
callq 0x2695c
movq %r15, %rdi
movq %r14, %rsi
callq 0x63f0
incq %rbx
movl 0x2dbed(%rip), %eax     # 0x61878
jmp 0x33c5b
movl 0x1aac05cd(%rip), %eax  # 0x1aaf4260
cvtsi2sd %eax, %xmm0
movsd 0x10369(%rip), %xmm1    # 0x44008
divsd %xmm0, %xmm1
leaq 0x2dc16(%rip), %rax     # 0x618c0
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
movl (%rax), %ecx
leaq 0x2dc09(%rip), %rdx     # 0x618c4
movl (%rdx), %r8d
xorl %ebp, %ebp
leaq 0x2dbf5(%rip), %rsi     # 0x618bc
leaq 0x1aabd102(%rip), %rdi  # 0x1aaf0dd0
movsd 0x1033a(%rip), %xmm1    # 0x44010
xorl %r14d, %r14d
cmpl %r8d, %ecx
jg 0x33d29
movslq (%rax), %r9
movslq %ecx, %r10
cmpl %r8d, %r9d
jg 0x33d25
movslq (%rsi), %r8
imulq %r10, %r8
addq %r9, %r8
shlq $0x4, %r8
movl 0x4(%r8,%rdi), %r8d
cvtsi2sd %r8d, %xmm2
mulsd %xmm0, %xmm2
xorl %r8d, %r8d
xorl %r11d, %r11d
ucomisd %xmm1, %xmm2
setbe %r8b
seta %r11b
addl %r11d, %ebp
addl %r8d, %r14d
movl (%rdx), %r8d
incq %r9
jmp 0x33ce4
incl %ecx
jmp 0x33cd9
movslq 0x1aa92c08(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa92bf2(%rip), %rdi  # 0x1aac6930
movl 0x1aac051c(%rip), %ecx  # 0x1aaf4260
leaq 0x1aabd085(%rip), %rsi  # 0x1aaf0dd0
leaq 0x1aaba3ae(%rip), %r8   # 0x1aaee100
movl 0xc(%rsp), %edx
callq 0xfca5
subl %r14d, %ebp
movl %ebp, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x63f0
movq %rbx, %rdi
callq 0x6740