// File: /workspace/github_cpp_makefile_star2_O2/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 $0x238, %rsp            # imm = 0x238
movl %esi, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x1aabcf2e(%rip), %r15  # 0x1aaf0dd0
xorl %r14d, %r14d
leaq 0x2da0c(%rip), %rbx     # 0x618b8
movslq (%rbx), %rsi
cmpq %rsi, %r14
jge 0x33ec5
movq %r15, %rdi
callq 0x35010
incq %r14
addq $0x10, %r15
jmp 0x33eac
xorl %r14d, %r14d
xorl %eax, %eax
xchgl %eax, 0x1aac0390(%rip)  # 0x1aaf4260
leaq 0x20(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x1aac0391(%rip), %rdi  # 0x1aaf4270
callq 0x355fe
movl (%rbx), %eax
testl %eax, %eax
cmovlel %r14d, %eax
shlq $0x3, %rax
leaq 0x1aac10a9(%rip), %rcx  # 0x1aaf4fa0
cmpq %r14, %rax
je 0x33f07
andq $0x0, (%r14,%rcx)
addq $0x8, %r14
jmp 0x33ef7
callq 0x35c02
callq 0x35c40
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x3331c
movl %eax, 0x1aa92a13(%rip)  # 0x1aac6938
movq 0x1aa92a04(%rip), %rcx  # 0x1aac6930
cltq
imulq $0x4548, %rax, %rdx     # imm = 0x4548
xorl %eax, %eax
cmpl $0x2, 0x18(%rcx,%rdx)
jl 0x34207
leaq 0x2d967(%rip), %rax     # 0x618b0
movl (%rax), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
leaq 0x1ab18797(%rip), %rdi  # 0x1ab4c6f0
leaq 0x1aaba1a0(%rip), %r8   # 0x1aaee100
movabsq $0x4049000000000000, %rdx # imm = 0x4049000000000000
cmpq %rcx, %rax
je 0x33f7c
movslq (%rdi,%rcx,4), %rsi
movq %rdx, (%r8,%rsi,8)
incq %rcx
jmp 0x33f6a
callq 0x35c57
movl %eax, %edi
callq 0x35c6d
movslq 0x1aa929a9(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rsi     # imm = 0x4548
addq 0x1aa92993(%rip), %rsi  # 0x1aac6930
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
callq 0x3d2f5
leaq 0x1aac2a3e(%rip), %r14  # 0x1aaf69f0
xorl %ebx, %ebx
pushq $0x8
popq %r12
leaq -0x98c(%rip), %r13      # 0x33633
leaq 0x20(%rsp), %rbp
movslq 0x2d8ad(%rip), %rax     # 0x61878
cmpq %rax, %rbx
jge 0x34015
movl %ebx, 0x8(%r14)
movq 0x10(%rsp), %rax
movq %rax, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%r14)
orl $-0x1, 0x10(%r14)
movq %r12, %rdi
callq 0x63e0
movq %rax, %r15
movq %r14, 0x20(%rsp)
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x35024
movq %r15, 0x30(%rsp,%rbx,8)
incq %rbx
addq $0x18, %r14
jmp 0x33fc4
xorl %ebx, %ebx
pushq $0x8
popq %r12
leaq 0x1aaba0de(%rip), %r13  # 0x1aaee100
cltq
cmpq %rax, %rbx
jge 0x34054
movq 0x30(%rsp,%rbx,8), %r15
movq %r15, %rdi
callq 0x66e0
movq %r15, %rdi
callq 0x2695c
movq %r15, %rdi
movq %r12, %rsi
callq 0x63f0
incq %rbx
movl 0x2d826(%rip), %eax     # 0x61878
jmp 0x34022
movslq 0x1aa928dd(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %r12     # imm = 0x4548
addq 0x1aa928c7(%rip), %r12  # 0x1aac6930
movq %r12, %rdi
callq 0x35684
movl %eax, %ebp
callq 0x35c11
movsd %xmm0, 0x18(%rsp)
movq 0x1aa928ab(%rip), %rax  # 0x1aac6930
movslq 0x1aa928ac(%rip), %rcx  # 0x1aac6938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0xc(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm2
movq 0x1aa9288e(%rip), %rax  # 0x1aac6930
movslq 0x1aa9288f(%rip), %rcx  # 0x1aac6938
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
movl 0x8(%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x28(%rsp)
movl 0x2d7b0(%rip), %edi     # 0x61878
callq 0x35bd5
cvttsd2si %xmm0, %esi
movslq 0x1aa92860(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa9284a(%rip), %rdi  # 0x1aac6930
xorl %r15d, %r15d
movsd 0x18(%rsp), %xmm0
xorl %edx, %edx
callq 0x103ad
movslq 0x1aa9283b(%rip), %rax  # 0x1aac6938
imulq $0x4548, %rax, %rdi     # imm = 0x4548
addq 0x1aa92825(%rip), %rdi  # 0x1aac6930
movl 0x1aac014f(%rip), %ecx  # 0x1aaf4260
leaq 0x1aabccb8(%rip), %rsi  # 0x1aaf0dd0
movl 0xc(%rsp), %ebx
movl %ebx, %edx
movq %r13, %r8
callq 0xfca5
movq 0x1aa92803(%rip), %rsi  # 0x1aac6930
movl 0x1aa92805(%rip), %edx  # 0x1aac6938
movq 0x10(%rsp), %r14
movq %r14, %rdi
movl %ebx, %ecx
callq 0x100d9
movl 0x2d730(%rip), %edx     # 0x61878
movq %r14, %rdi
movl %ebx, %esi
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
callq 0x359f1
leaq 0x2d74b(%rip), %rax     # 0x618b0
movl (%rax), %eax
testl %eax, %eax
cmovlel %r15d, %eax
movsd 0x12ea3(%rip), %xmm0    # 0x47018
movsd 0x11d2b(%rip), %xmm1    # 0x45ea8
leaq 0x1aabc6fc(%rip), %rdx  # 0x1aaf0880
xorl %ecx, %ecx
leaq 0x1ab18563(%rip), %r8   # 0x1ab4c6f0
cmpq %r15, %rax
je 0x341c3
movslq (%r8,%r15,4), %rsi
movsd (%r13,%rsi,8), %xmm2
movapd %xmm1, %xmm3
cmplepd %xmm2, %xmm3
cmplepd %xmm0, %xmm2
andpd %xmm3, %xmm2
movq %xmm2, %rdi
subl %edi, %ecx
andb $0x1, %dil
movb %dil, (%rsi,%rdx)
incq %r15
jmp 0x3418d
addq $0x1c, %r12
xorl %edx, %edx
movslq %ebp, %rsi
movl $0x0, %eax
testl %ecx, %ecx
je 0x341de
imulq $0x1c, %rsi, %rax
movswl (%r12,%rax), %eax
imulq $0x1c, %rsi, %rcx
movl 0xc(%r12,%rcx), %esi
cvtsi2sd %esi, %xmm0
movl 0x4(%r12,%rcx), %ecx
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
movsd 0x11cf0(%rip), %xmm1    # 0x45ef0
ucomisd %xmm0, %xmm1
cmoval %edx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x63f0
movq %rbx, %rdi
callq 0x6740