// File: /workspace/github_cpp_makefile_star2_O1/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 0x2a526(%rip), %rax     # 0x758b8
cmpl $0x0, (%rax)
leaq 0x1aab9a34(%rip), %rbx  # 0x1ab04dd0
jle 0x4b3c2
xorl %ecx, %ecx
movq %rbx, %rdx
xorl %esi, %esi
xorl %edi, %edi
xchgl %edi, (%rdx,%rsi,4)
incq %rsi
cmpq $0x4, %rsi
jne 0x4b3a5
incq %rcx
movslq (%rax), %rsi
addq $0x10, %rdx
cmpq %rsi, %rcx
jl 0x4b3a3
xorl %ecx, %ecx
xchgl %ecx, 0x1aabce96(%rip)  # 0x1ab08260
movslq (%rax), %r15
testq %r15, %r15
jle 0x4b3e8
leaq (,%r15,4), %rdx
leaq 0x1aabce8f(%rip), %rdi  # 0x1ab08270
xorl %esi, %esi
callq 0x6210
testl %r15d, %r15d
jle 0x4b402
shlq $0x3, %r15
leaq 0x1aabdba8(%rip), %rdi  # 0x1ab08fa0
xorl %esi, %esi
movq %r15, %rdx
callq 0x6210
callq 0x4d5a9
callq 0x56126
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x4aa7a
movl %eax, 0x1aa8f518(%rip)  # 0x1aada938
movl $0x2710, %edi           # imm = 0x2710
callq 0x4d5d6
cmpl $0x0, 0x2a447(%rip)     # 0x75878
jle 0x4b49c
leaq 0x1aabf5b6(%rip), %rbp  # 0x1ab0a9f0
xorl %r14d, %r14d
leaq -0x648(%rip), %r15      # 0x4adfc
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 0x6410
movq %rax, %r13
movq %rbp, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4c976
movq %r13, 0x20(%rsp,%r14,8)
incq %r14
movslq 0x2a3e5(%rip), %rax     # 0x75878
addq $0x18, %rbp
cmpq %rax, %r14
jl 0x4b449
cmpl $0x0, 0x2a3d5(%rip)     # 0x75878
jle 0x4b4db
xorl %r15d, %r15d
movq 0x20(%rsp,%r15,8), %r14
movq %r14, %rdi
callq 0x6720
cmpq $0x0, (%r14)
jne 0x4b5cf
movl $0x8, %esi
movq %r14, %rdi
callq 0x6420
incq %r15
movslq 0x2a3a2(%rip), %rax     # 0x75878
cmpq %rax, %r15
jl 0x4b4a8
movl 0x1aabcd7f(%rip), %edi  # 0x1ab08260
leaq 0x2a3d8(%rip), %rax     # 0x758c0
movl (%rax), %edx
leaq 0x2a3d3(%rip), %rcx     # 0x758c4
xorl %ebp, %ebp
movl $0x0, %esi
cmpl (%rcx), %edx
jg 0x4b587
cvtsi2sd %edi, %xmm0
movsd 0x11afc(%rip), %xmm1    # 0x5d008
divsd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
xorl %esi, %esi
leaq 0x2a39b(%rip), %rdi     # 0x758bc
movsd 0x11ae7(%rip), %xmm1    # 0x5d010
xorl %ebp, %ebp
movslq (%rax), %r8
cmpl (%rcx), %r8d
jg 0x4b57c
leal -0x1(%r8), %r9d
movslq %edx, %r10
movslq (%rdi), %r11
imulq %r10, %r11
addq %r8, %r11
shlq $0x4, %r11
movl 0x4(%r11,%rbx), %r11d
xorps %xmm2, %xmm2
cvtsi2sd %r11d, %xmm2
mulsd %xmm0, %xmm2
xorl %r11d, %r11d
xorl %r14d, %r14d
ucomisd %xmm1, %xmm2
setbe %r11b
seta %r14b
addl %r14d, %ebp
addl %r11d, %esi
incl %r9d
incq %r8
cmpl (%rcx), %r9d
jl 0x4b53a
leal 0x1(%rdx), %r8d
cmpl (%rcx), %edx
movl %r8d, %edx
jl 0x4b52b
subl %esi, %ebp
movslq 0x1aa8f3a8(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8f392(%rip), %rdi  # 0x1aada930
movl 0x1aabccbc(%rip), %ecx  # 0x1ab08260
leaq 0x1aab9825(%rip), %rsi  # 0x1ab04dd0
leaq 0x1aab6b4e(%rip), %r8   # 0x1ab02100
movl 0xc(%rsp), %edx
callq 0x1de7c
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 0x6420
movq %rbx, %rdi
callq 0x6770