// File: /workspace/github_cpp_makefile_star2_O3/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 0x2ae62(%rip), %rax     # 0x768b8
movslq (%rax), %r15
testq %r15, %r15
jle 0x4bab8
leaq 0x1aaba36b(%rip), %rax  # 0x1ab05dd0
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
xchgl %esi, (%rax,%rdx,4)
incq %rdx
cmpq $0x4, %rdx
jne 0x4ba69
incq %rcx
addq $0x10, %rax
cmpq %r15, %rcx
jb 0x4ba67
xorl %eax, %eax
xchgl %eax, 0x1aabd7d5(%rip)  # 0x1ab09260
leaq (,%r15,4), %rdx
leaq 0x1aabd7d6(%rip), %rdi  # 0x1ab09270
xorl %esi, %esi
callq 0x6200
shlq $0x3, %r15
leaq 0x1aabe4f4(%rip), %rdi  # 0x1ab09fa0
xorl %esi, %esi
movq %r15, %rdx
callq 0x6200
jmp 0x4bac0
xorl %eax, %eax
xchgl %eax, 0x1aabd7a0(%rip)  # 0x1ab09260
callq 0x4d841
callq 0x4d8d9
movq (%rsp), %rdi
movl %ebx, %esi
callq 0x4addf
movl %eax, 0x1aa8fe5d(%rip)  # 0x1aadb938
movq 0x1aa8fe4e(%rip), %rcx  # 0x1aadb930
cltq
imulq $0x4548, %rax, %rdx     # imm = 0x4548
xorl %eax, %eax
cmpl $0x2, 0x18(%rcx,%rdx)
jl 0x4bdd7
leaq 0x2adb1(%rip), %rax     # 0x768b0
movslq (%rax), %rax
testq %rax, %rax
jle 0x4bb31
leaq 0x1aab75f2(%rip), %rcx  # 0x1ab03100
xorl %edx, %edx
leaq 0x1ab15bd9(%rip), %rsi  # 0x1ab616f0
movabsq $0x4049000000000000, %rdi # imm = 0x4049000000000000
movslq (%rsi,%rdx,4), %r8
movq %rdi, (%rcx,%r8,8)
incq %rdx
cmpq %rdx, %rax
jne 0x4bb21
callq 0x4d8f0
movl %eax, %edi
callq 0x4d906
movslq 0x1aa8fdf4(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa8fdde(%rip), %rsi  # 0x1aadb930
movq (%rsp), %rdi
movl %ebx, %edx
callq 0x56d15
cmpl $0x0, 0x2ad14(%rip)     # 0x76878
jle 0x4bc08
leaq 0x1aabfe7f(%rip), %rbp  # 0x1ab0b9f0
xorl %r14d, %r14d
leaq -0xa1a(%rip), %r15      # 0x4b161
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 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 0x2acb3(%rip), %rax     # 0x76878
addq $0x18, %rbp
cmpq %rax, %r14
jl 0x4bb80
testl %eax, %eax
jle 0x4bc08
xorl %r14d, %r14d
movq 0x20(%rsp,%r14,8), %r15
movq %r15, %rdi
callq 0x6710
cmpq $0x0, (%r15)
jne 0x4bde9
movl $0x8, %esi
movq %r15, %rdi
callq 0x6410
incq %r14
movslq 0x2ac75(%rip), %rax     # 0x76878
cmpq %rax, %r14
jl 0x4bbd5
movq 0x1aa8fd21(%rip), %rax  # 0x1aadb930
movslq 0x1aa8fd22(%rip), %rcx  # 0x1aadb938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
leaq (%rax,%rcx), %rdi
leaq (%rax,%rcx), %r12
addq $0x1c, %r12
callq 0x4d207
movl %eax, %ebp
callq 0x4d850
movsd %xmm0, 0x8(%rsp)
movq 0x1aa8fcee(%rip), %rax  # 0x1aadb930
movslq 0x1aa8fcef(%rip), %rcx  # 0x1aadb938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0xc(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm2
movq 0x1aa8fcd1(%rip), %rax  # 0x1aadb930
movslq 0x1aa8fcd2(%rip), %rcx  # 0x1aadb938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0x8(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movl 0x2abf3(%rip), %edi     # 0x76878
callq 0x4d80e
cvttsd2si %xmm0, %esi
movslq 0x1aa8fca3(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8fc8d(%rip), %rdi  # 0x1aadb930
xorl %r13d, %r13d
movsd 0x8(%rsp), %xmm0
xorl %edx, %edx
callq 0x1e560
movslq 0x1aa8fc7e(%rip), %rax  # 0x1aadb938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa8fc68(%rip), %rdi  # 0x1aadb930
movl 0x1aabd592(%rip), %ecx  # 0x1ab09260
leaq 0x1aaba0fb(%rip), %rsi  # 0x1ab05dd0
leaq 0x1aab7424(%rip), %r15  # 0x1ab03100
movl %ebx, %edx
movq %r15, %r8
callq 0x1dc6c
movq 0x1aa8fc43(%rip), %rsi  # 0x1aadb930
movl 0x1aa8fc45(%rip), %edx  # 0x1aadb938
movq (%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
callq 0x1e1b6
movl 0x2ab71(%rip), %edx     # 0x76878
movq %r14, %rdi
movl %ebx, %esi
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x4d629
leaq 0x2ab8c(%rip), %rax     # 0x768b0
movslq (%rax), %rax
testq %rax, %rax
jle 0x4bd9b
xorl %ecx, %ecx
leaq 0x1ab159bb(%rip), %rdx  # 0x1ab616f0
movsd 0x1432b(%rip), %xmm0    # 0x60068
movsd 0x1314b(%rip), %xmm1    # 0x5ee90
leaq 0x1aab9b34(%rip), %rsi  # 0x1ab05880
xorl %edi, %edi
movslq (%rdx,%rcx,4), %r8
movsd (%r15,%r8,8), %xmm2
movapd %xmm1, %xmm3
cmplepd %xmm2, %xmm3
cmplepd %xmm0, %xmm2
andpd %xmm3, %xmm2
movq %xmm2, %r9
subl %r9d, %edi
andb $0x1, %r9b
movb %r9b, (%r8,%rsi)
incq %rcx
cmpq %rcx, %rax
jne 0x4bd4e
testl %edi, %edi
je 0x4bd9b
movslq %ebp, %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movswl (%r12,%rcx), %r13d
jmp 0x4bd9e
movslq %ebp, %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movl 0xc(%r12,%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movl 0x4(%r12,%rcx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
xorl %eax, %eax
movsd 0x130b9(%rip), %xmm1    # 0x5ee88
ucomisd %xmm0, %xmm1
cmovbel %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 0x6410
movq %rbx, %rdi
callq 0x6760