// File: /workspace/github_cpp_makefile_star2_O1/kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: UctSearchGenmoveCleanUp(game_info_t*, int)
int
UctSearchGenmoveCleanUp( game_info_t *game, int color )
{
  int pos;
  double wp;
  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;
  }

  StartTimer();

  ResetPoCount();

  current_root = ExpandRoot(game, color);

  if (uct_node[current_root].child_num <= 1) {
    return PASS;
  }

  for (int i = 0; i < pure_board_max; i++) {
    pos = onboard_pos[i];
    owner[pos] = 50.0;
  }

  SetPoHalt(GetPoNum());

  DynamicKomi(game, &uct_node[current_root], color);

  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];
  }

  child_node_t *uct_child = uct_node[current_root].child;

  const int select_index = SelectMaxVisitChild(uct_node[current_root]);

  const double finish_time = CalculateElapsedTime();

  wp = (double)uct_node[current_root].win / uct_node[current_root].move_count;

  const int po_speed = static_cast<int>(CalculatePlayoutSpeed(finish_time, threads));

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

  PrintBestSequence(game, uct_node, current_root, color);

  CalculateNextPlayouts(game, color, wp, finish_time, threads);

  int count = 0;

  for (int i = 0; i < pure_board_max; i++) {
    pos = onboard_pos[i];

    if (owner[pos] >= 5 && owner[pos] <= 95) {
      candidates[pos] = true;
      count++;
    } else {
      candidates[pos] = false;
    }
  }

  if (count == 0) {
    pos = PASS;
  } else {
    pos = uct_child[select_index].pos;
  }
  
  if (static_cast<double>(uct_child[select_index].win) / uct_child[select_index].move_count < RESIGN_THRESHOLD) {
    return PASS;
  } else {
    return pos;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %esi, %ebx
movq %rdi, (%rsp)
leaq 0x2a1b6(%rip), %rax     # 0x758b8
cmpl $0x0, (%rax)
jle 0x4b72f
leaq 0x1aab96c2(%rip), %rcx  # 0x1ab04dd0
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
xchgl %edi, (%rcx,%rsi,4)
incq %rsi
cmpq $0x4, %rsi
jne 0x4b712
incq %rdx
movslq (%rax), %rsi
addq $0x10, %rcx
cmpq %rsi, %rdx
jl 0x4b710
xorl %ecx, %ecx
xchgl %ecx, 0x1aabcb29(%rip)  # 0x1ab08260
movslq (%rax), %r15
testq %r15, %r15
jle 0x4b755
leaq (,%r15,4), %rdx
leaq 0x1aabcb22(%rip), %rdi  # 0x1ab08270
xorl %esi, %esi
callq 0x6210
testl %r15d, %r15d
jle 0x4b76f
shlq $0x3, %r15
leaq 0x1aabd83b(%rip), %rdi  # 0x1ab08fa0
xorl %esi, %esi
movq %r15, %rdx
callq 0x6210
callq 0x4d511
callq 0x4d5a9
movq (%rsp), %rdi
movl %ebx, %esi
callq 0x4aa7a
movl %eax, 0x1aa8f1ae(%rip)  # 0x1aada938
movq 0x1aa8f19f(%rip), %rcx  # 0x1aada930
cltq
imulq $0x4548, %rax, %rdx     # imm = 0x4548
xorl %eax, %eax
cmpl $0x2, 0x18(%rcx,%rdx)
jl 0x4ba84
leaq 0x2a102(%rip), %rax     # 0x758b0
movslq (%rax), %rax
testq %rax, %rax
jle 0x4b7e0
leaq 0x1aab6943(%rip), %rcx  # 0x1ab02100
xorl %edx, %edx
leaq 0x1ab14f2a(%rip), %rsi  # 0x1ab606f0
movabsq $0x4049000000000000, %rdi # imm = 0x4049000000000000
movslq (%rsi,%rdx,4), %r8
movq %rdi, (%rcx,%r8,8)
incq %rdx
cmpq %rdx, %rax
jne 0x4b7d0
callq 0x4d5c0
movl %eax, %edi
callq 0x4d5d6
movslq 0x1aa8f145(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa8f12f(%rip), %rsi  # 0x1aada930
movq (%rsp), %rdi
movl %ebx, %edx
callq 0x5635d
cmpl $0x0, 0x2a065(%rip)     # 0x75878
jle 0x4b879
leaq 0x1aabf1d4(%rip), %rbp  # 0x1ab0a9f0
xorl %r14d, %r14d
leaq -0xa2a(%rip), %r15      # 0x4adfc
leaq 0x18(%rsp), %r12
movl %r14d, 0x8(%rbp)
movq (%rsp), %rax
movq %rax, (%rbp)
movl %ebx, 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 0x2a008(%rip), %rax     # 0x75878
addq $0x18, %rbp
cmpq %rax, %r14
jl 0x4b82b
cmpl $0x0, 0x29ff8(%rip)     # 0x75878
jle 0x4b8b8
xorl %r14d, %r14d
movq 0x20(%rsp,%r14,8), %r15
movq %r15, %rdi
callq 0x6720
cmpq $0x0, (%r15)
jne 0x4ba96
movl $0x8, %esi
movq %r15, %rdi
callq 0x6420
incq %r14
movslq 0x29fc5(%rip), %rax     # 0x75878
cmpq %rax, %r14
jl 0x4b885
movq 0x1aa8f071(%rip), %rax  # 0x1aada930
movslq 0x1aa8f072(%rip), %rcx  # 0x1aada938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
leaq (%rax,%rcx), %rdi
leaq (%rax,%rcx), %r12
addq $0x1c, %r12
callq 0x4cedb
movl %eax, %ebp
callq 0x4d520
movsd %xmm0, 0x8(%rsp)
movq 0x1aa8f03e(%rip), %rax  # 0x1aada930
movslq 0x1aa8f03f(%rip), %rcx  # 0x1aada938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0xc(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm2
movq 0x1aa8f021(%rip), %rax  # 0x1aada930
movslq 0x1aa8f022(%rip), %rcx  # 0x1aada938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0x8(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movl 0x29f43(%rip), %edi     # 0x75878
callq 0x4d4de
cvttsd2si %xmm0, %esi
movslq 0x1aa8eff3(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8efdd(%rip), %rdi  # 0x1aada930
xorl %r13d, %r13d
movsd 0x8(%rsp), %xmm0
xorl %edx, %edx
callq 0x1e781
movslq 0x1aa8efce(%rip), %rax  # 0x1aada938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8efb8(%rip), %rdi  # 0x1aada930
movl 0x1aabc8e2(%rip), %ecx  # 0x1ab08260
leaq 0x1aab944b(%rip), %rsi  # 0x1ab04dd0
leaq 0x1aab6774(%rip), %r15  # 0x1ab02100
movl %ebx, %edx
movq %r15, %r8
callq 0x1de7c
movq 0x1aa8ef93(%rip), %rsi  # 0x1aada930
movl 0x1aa8ef95(%rip), %edx  # 0x1aada938
movq (%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
callq 0x1e3c6
movl 0x29ec1(%rip), %edx     # 0x75878
movq %r14, %rdi
movl %ebx, %esi
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x4d2f5
leaq 0x29edc(%rip), %rax     # 0x758b0
movslq (%rax), %rdx
movslq %ebp, %rcx
movl $0x0, %eax
testq %rdx, %rdx
jle 0x4ba4d
xorl %eax, %eax
leaq 0x1ab14d03(%rip), %rsi  # 0x1ab606f0
movsd 0x13493(%rip), %xmm0    # 0x5ee88
leaq 0x1aab8e84(%rip), %rdi  # 0x1ab04880
movsd 0x14594(%rip), %xmm1    # 0x5ff98
xorl %r8d, %r8d
movslq (%rsi,%rax,4), %r9
movsd (%r15,%r9,8), %xmm2
ucomisd %xmm0, %xmm2
jb 0x4ba27
ucomisd %xmm2, %xmm1
jb 0x4ba27
movb $0x1, (%r9,%rdi)
incl %r8d
jmp 0x4ba2c
movb $0x0, (%r9,%rdi)
incq %rax
cmpq %rax, %rdx
jne 0x4ba07
testl %r8d, %r8d
je 0x4ba4b
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movswl (%r12,%rax), %eax
jmp 0x4ba4d
xorl %eax, %eax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%r12,%rdx), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movl 0x4(%r12,%rdx), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd 0x13404(%rip), %xmm1    # 0x5ee80
ucomisd %xmm0, %xmm1
cmoval %r13d, %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