// File: /workspace/github_cpp_makefile_star2_O3/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 0x2b1d6(%rip), %rax     # 0x768b8
movslq (%rax), %r15
leaq 0x1aaba6e4(%rip), %rbx  # 0x1ab05dd0
testq %r15, %r15
jle 0x4b747
xorl %eax, %eax
movq %rbx, %rcx
xorl %edx, %edx
xorl %esi, %esi
xchgl %esi, (%rcx,%rdx,4)
incq %rdx
cmpq $0x4, %rdx
jne 0x4b6f8
incq %rax
addq $0x10, %rcx
cmpq %r15, %rax
jb 0x4b6f6
xorl %eax, %eax
xchgl %eax, 0x1aabdb46(%rip)  # 0x1ab09260
leaq (,%r15,4), %rdx
leaq 0x1aabdb47(%rip), %rdi  # 0x1ab09270
xorl %esi, %esi
callq 0x6200
shlq $0x3, %r15
leaq 0x1aabe865(%rip), %rdi  # 0x1ab09fa0
xorl %esi, %esi
movq %r15, %rdx
callq 0x6200
jmp 0x4b74f
xorl %eax, %eax
xchgl %eax, 0x1aabdb11(%rip)  # 0x1ab09260
callq 0x4d8d9
callq 0x56ace
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x4addf
movl %eax, 0x1aa901cb(%rip)  # 0x1aadb938
movl $0x2710, %edi           # imm = 0x2710
callq 0x4d906
cmpl $0x0, 0x2b0fa(%rip)     # 0x76878
jle 0x4b827
leaq 0x1aac0265(%rip), %rbp  # 0x1ab0b9f0
xorl %r14d, %r14d
leaq -0x634(%rip), %r15      # 0x4b161
leaq 0x18(%rsp), %r12
movl %r14d, 0x8(%rbp)
movq 0x10(%rsp), %rax
movq %rax, (%rbp)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rbp)
movl $0xffffffff, 0x10(%rbp) # imm = 0xFFFFFFFF
movl $0x8, %edi
callq 0x6400
movq %rax, %r13
movq %rbp, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4ccd8
movq %r13, 0x20(%rsp,%r14,8)
incq %r14
movslq 0x2b094(%rip), %rax     # 0x76878
addq $0x18, %rbp
cmpq %rax, %r14
jl 0x4b79a
testl %eax, %eax
jle 0x4b827
xorl %r15d, %r15d
movq 0x20(%rsp,%r15,8), %r14
movq %r14, %rdi
callq 0x6710
cmpq $0x0, (%r14)
jne 0x4b923
movl $0x8, %esi
movq %r14, %rdi
callq 0x6410
incq %r15
movslq 0x2b056(%rip), %rax     # 0x76878
cmpq %rax, %r15
jl 0x4b7f4
movl 0x1aabda33(%rip), %edi  # 0x1ab09260
leaq 0x2b08c(%rip), %rax     # 0x768c0
movl (%rax), %edx
leaq 0x2b087(%rip), %rcx     # 0x768c4
movl (%rcx), %r8d
xorl %ebp, %ebp
movl $0x0, %esi
cmpl %r8d, %edx
jg 0x4b8db
cvtsi2sd %edi, %xmm0
movsd 0x117ac(%rip), %xmm1    # 0x5d008
divsd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
xorl %esi, %esi
leaq 0x2b04b(%rip), %rdi     # 0x768bc
movsd 0x11797(%rip), %xmm1    # 0x5d010
xorl %ebp, %ebp
movslq (%rax), %r9
cmpl %r8d, %r9d
jg 0x4b8cf
leal -0x1(%r9), %r10d
movslq %edx, %r11
movslq (%rdi), %r8
imulq %r11, %r8
addq %r9, %r8
shlq $0x4, %r8
movl 0x4(%r8,%rbx), %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
mulsd %xmm0, %xmm2
xorl %r8d, %r8d
xorl %r14d, %r14d
ucomisd %xmm1, %xmm2
setbe %r8b
seta %r14b
addl %r14d, %ebp
addl %r8d, %esi
movl (%rcx), %r8d
incl %r10d
incq %r9
cmpl %r8d, %r10d
jl 0x4b88a
leal 0x1(%rdx), %r9d
cmpl %r8d, %edx
movl %r9d, %edx
jl 0x4b87b
subl %esi, %ebp
movslq 0x1aa90054(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa9003e(%rip), %rdi  # 0x1aadb930
movl 0x1aabd968(%rip), %ecx  # 0x1ab09260
leaq 0x1aaba4d1(%rip), %rsi  # 0x1ab05dd0
leaq 0x1aab77fa(%rip), %r8   # 0x1ab03100
movl 0xc(%rsp), %edx
callq 0x1dc6c
movl %ebp, %eax
addq $0x228, %rsp            # imm = 0x228
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