// File: 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
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x38(%rbp)
movl %esi, -0x3c(%rbp)
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
leaq 0x5d52f(%rip), %rcx     # 0xe7a48
cmpl (%rcx), %eax
jge 0x8a548
movslq -0x244(%rbp), %rax
leaq 0x1aaeca25(%rip), %rdi  # 0x1ab76f50
shlq $0x4, %rax
addq %rax, %rdi
callq 0x8cf10
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x8a50c
leaq 0x1aaefe91(%rip), %rdi  # 0x1ab7a3e0
xorl %esi, %esi
callq 0x88450
leaq 0x5d4eb(%rip), %rax     # 0xe7a48
movl (%rax), %esi
movl $0x0, -0x248(%rbp)
leaq 0x1aaefe80(%rip), %rdi  # 0x1ab7a3f0
leaq -0x248(%rbp), %rdx
callq 0x80790
movl $0x0, -0x24c(%rbp)
movl -0x24c(%rbp), %eax
leaq 0x5d4b5(%rip), %rcx     # 0xe7a48
cmpl (%rcx), %eax
jge 0x8a5be
movslq -0x24c(%rbp), %rcx
leaq 0x1aaf0b7b(%rip), %rax  # 0x1ab7b120
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x24c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24c(%rbp)
jmp 0x8a586
callq 0x8f880
callq 0x9f8d0
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x89930
movl %eax, 0x1aac24de(%rip)  # 0x1ab4cab8
movl $0x2710, %edi           # imm = 0x2710
callq 0x8f910
movl $0x0, -0x250(%rbp)
movl -0x250(%rbp), %eax
cmpl 0x5d406(%rip), %eax     # 0xe7a00
jge 0x8a6f2
movslq -0x250(%rbp), %rax
movl %eax, %edx
leaq (%rax,%rax,2), %rcx
leaq 0x1aaf255c(%rip), %rax  # 0x1ab7cb70
movq %rax, -0x298(%rbp)
movl %edx, 0x8(%rax,%rcx,8)
movq -0x38(%rbp), %rdx
movslq -0x250(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x3c(%rbp), %edx
movslq -0x250(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl %edx, 0xc(%rax,%rcx,8)
movslq -0x250(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl $0xffffffff, 0x10(%rax,%rcx,8) # imm = 0xFFFFFFFF
movl $0x8, %edi
callq 0x7550
movq %rax, %rdi
movq -0x298(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x290(%rbp)
movslq -0x250(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x258(%rbp)
leaq -0x882(%rip), %rsi      # 0x89e10
leaq -0x258(%rbp), %rdx
callq 0x8cf60
jmp 0x8a6a0
movq -0x290(%rbp), %rcx
movslq -0x250(%rbp), %rax
movq %rcx, -0x240(%rbp,%rax,8)
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x8a5ee
movq -0x290(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
movl $0x8, %esi
callq 0x7560
jmp 0x8a9df
movl $0x0, -0x268(%rbp)
movl -0x268(%rbp), %eax
cmpl 0x5d2f8(%rip), %eax     # 0xe7a00
jge 0x8a76a
movslq -0x268(%rbp), %rax
movq -0x240(%rbp,%rax,8), %rdi
callq 0x7940
movslq -0x268(%rbp), %rax
movq -0x240(%rbp,%rax,8), %rax
movq %rax, -0x2a0(%rbp)
cmpq $0x0, %rax
je 0x8a757
movq -0x2a0(%rbp), %rdi
callq 0x66080
movq -0x2a0(%rbp), %rdi
movl $0x8, %esi
callq 0x7560
jmp 0x8a759
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x8a6fc
movl $0x0, -0x26c(%rbp)
movl $0x0, -0x270(%rbp)
leaq 0x1aaefc5b(%rip), %rax  # 0x1ab7a3e0
movq %rax, -0x8(%rbp)
movl $0x5, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x2a4(%rbp)
decl %eax
subl $0x2, %eax
jb 0x8a7d8
jmp 0x8a7bd
movl -0x2a4(%rbp), %eax
subl $0x5, %eax
je 0x8a7e6
jmp 0x8a7ca
movq -0x2b0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x8a7f2
movq -0x2b0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x8a7f2
movq -0x2b0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
cvtsi2sdl -0x14(%rbp), %xmm1
movsd 0x21809(%rip), %xmm0    # 0xac008
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x274(%rbp)
leaq 0x5d23c(%rip), %rax     # 0xe7a50
movl (%rax), %eax
movl %eax, -0x278(%rbp)
movl -0x278(%rbp), %eax
leaq 0x5d22b(%rip), %rcx     # 0xe7a54
cmpl (%rcx), %eax
jg 0x8a90b
leaq 0x5d218(%rip), %rax     # 0xe7a50
movl (%rax), %eax
movl %eax, -0x27c(%rbp)
movl -0x27c(%rbp), %eax
leaq 0x5d207(%rip), %rcx     # 0xe7a54
cmpl (%rcx), %eax
jg 0x8a8f5
movl -0x27c(%rbp), %eax
movl -0x278(%rbp), %ecx
leaq 0x5d1e4(%rip), %rdx     # 0xe7a4c
imull (%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x280(%rbp)
movslq -0x280(%rbp), %rax
leaq 0x1aaec6cf(%rip), %rdi  # 0x1ab76f50
shlq $0x4, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x274(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x288(%rbp)
movsd -0x288(%rbp), %xmm0
movsd 0x21757(%rip), %xmm1    # 0xac010
ucomisd %xmm1, %xmm0
jbe 0x8a8d0
movl -0x26c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x26c(%rbp)
jmp 0x8a8df
movl -0x270(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x270(%rbp)
jmp 0x8a8e1
movl -0x27c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x27c(%rbp)
jmp 0x8a840
jmp 0x8a8f7
movl -0x278(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x278(%rbp)
jmp 0x8a81c
movq 0x1aac219e(%rip), %rax  # 0x1ab4cab0
movslq 0x1aac219f(%rip), %rcx  # 0x1ab4cab8
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movq %rax, -0x2d0(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x2c4(%rbp)
leaq 0x1aaefaa6(%rip), %rax  # 0x1ab7a3e0
movq %rax, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movl -0x24(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x2b4(%rbp)
decl %eax
subl $0x2, %eax
jb 0x8a98d
jmp 0x8a972
movl -0x2b4(%rbp), %eax
subl $0x5, %eax
je 0x8a99b
jmp 0x8a97f
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x8a9a7
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x8a9a7
movq -0x2c0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c4(%rbp), %edx
movq -0x2d0(%rbp), %rdi
movl -0x2c(%rbp), %ecx
leaq 0x1aaec592(%rip), %rsi  # 0x1ab76f50
leaq 0x1aae98bb(%rip), %r8   # 0x1ab74280
callq 0x211f0
movl -0x26c(%rbp), %eax
subl -0x270(%rbp), %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x260(%rbp), %rdi
callq 0x79b0
nopl (%rax,%rax)