// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/common/Message.cpp
// Function: PrintLeelaZeroAnalyze(uct_node_t const*)
void
PrintLeelaZeroAnalyze( const uct_node_t *root )
{
  struct verbose_t {
    std::string lz_pos;
    int visits, winrate, prior, lcb, index;
  };

  std::vector<verbose_t> child_verbose;

  for ( int i = 0; i < root->child_num; ++i ) {
    verbose_t v;
    const child_node_t *c = &root->child[i];

    char cpos[10];
    IntegerToString(c->pos, cpos);

    v.lz_pos = std::string{cpos};
    v.visits = c->move_count.load(std::memory_order_relaxed);

    double winrate = 0.5f;
    if (v.visits > 0) {
      winrate = static_cast<double>(c->win.load(std::memory_order_relaxed)) / v.visits;
    }

    v.winrate = std::min(10000, static_cast<int>(10000 * winrate));
    v.prior = std::min(10000, static_cast<int>(10000 * c->rate));
    v.lcb = v.winrate;
    v.index = c->index;

    if (v.visits > 0) {
      child_verbose.emplace_back(v);
    }
  }

  if ( child_verbose.empty() ) return;

  std::sort(std::begin(child_verbose), std::end(child_verbose),
              []( verbose_t &a, verbose_t &b ){
                return a.winrate > b.winrate;
              }
           );

  FILE *STD_STREAM = stdout;

  for ( int i = 0; i < static_cast<int>(child_verbose.size()); i++ ) {
    verbose_t *v = &child_verbose[i];
    fprintf(STD_STREAM, "info move %s visits %d winrate %d prior %d lcb %d order %d pv %s",
              v->lz_pos.c_str(),
              v->visits,
              v->winrate,
              v->prior,
              v->lcb,
              i,
              v->lz_pos.c_str()
           );

    int current = v->index;
    int child_num;
    const child_node_t *uct_child;

    while (current != NOT_EXPANDED) {
      uct_child = uct_node[current].child;
      child_num = uct_node[current].child_num;

      int max = 50;
      int index = -1;

      for (int j = 0; j < child_num; j++) {
        if (uct_child[j].move_count > max) {
          max = uct_child[j].move_count;
          index = j;
        }
      }

      if (index == -1) break;

      char cpos[10];
      IntegerToString(uct_child[index].pos, cpos);
      fprintf(STD_STREAM, " %s", cpos);

      current = uct_child[index].index;
    }

    if (i != static_cast<int>(child_verbose.size()) - 1) {
      fprintf(STD_STREAM, " ");
    }
  }
  fprintf(STD_STREAM, "\n");
  fflush(STD_STREAM);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x30(%rdi), %r13
movq $0x0, (%rsp)
leaq 0x66(%rsp), %r12
leaq 0x78(%rsp), %rbp
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %rdi, 0x70(%rsp)
movslq 0x18(%r14), %rax
cmpq %rax, %rbx
jge 0x10b33
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movswl -0x14(%r13), %edi
movq %r12, %rsi
callq 0x3cc92
movq %rbp, %rdi
movq %r12, %rsi
leaq 0xf(%rsp), %rdx
callq 0x114b2
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x64c0
movq %rbp, %rdi
callq 0x6838
movl -0x10(%r13), %eax
movl %eax, 0x48(%rsp)
testl %eax, %eax
jle 0x109a8
movl -0x8(%r13), %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x33688(%rip), %xmm0    # 0x44020
cvttsd2si %xmm0, %ecx
movl $0x2710, %esi           # imm = 0x2710
cmpl %esi, %ecx
cmovgel %esi, %ecx
jmp 0x109b2
movl $0x1388, %ecx           # imm = 0x1388
movl $0x2710, %esi           # imm = 0x2710
movl %ecx, 0x4c(%rsp)
movss (%r13), %xmm0
mulss 0x3367c(%rip), %xmm0    # 0x44040
cvttss2si %xmm0, %edx
cmpl %esi, %edx
cmovgel %esi, %edx
movl %edx, 0x50(%rsp)
movl %ecx, 0x54(%rsp)
movl -0x4(%r13), %ecx
movl %ecx, 0x58(%rsp)
testl %eax, %eax
jle 0x10b1d
movq (%rsp), %r12
cmpq 0x20(%rsp), %r12
je 0x10a26
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0x6540
leaq 0x38(%rsp), %rcx
movl 0x20(%rcx), %eax
movl %eax, 0x30(%r12)
movups 0x10(%rcx), %xmm0
movups %xmm0, 0x20(%r12)
addq $0x38, %r12
movq %r12, (%rsp)
movq %r12, 0x18(%rsp)
jmp 0x10b13
movq 0x10(%rsp), %r15
movq %r12, %rax
subq %r15, %rax
movabsq $0x7ffffffffffffff8, %rcx # imm = 0x7FFFFFFFFFFFFFF8
cmpq %rcx, %rax
je 0x10d23
cqto
pushq $0x38
popq %rcx
idivq %rcx
movq %rax, %rbp
cmpq %r15, %r12
pushq $0x1
popq %rcx
cmoveq %rcx, %rax
leaq (%rax,%rbp), %r12
movabsq $0x249249249249249, %rcx # imm = 0x249249249249249
cmpq %rcx, %r12
cmovaeq %rcx, %r12
addq %rbp, %rax
cmovbq %rcx, %r12
testq %r12, %r12
je 0x10a88
imulq $0x38, %r12, %rdi
callq 0x63e0
movq %rax, %r14
jmp 0x10a8b
xorl %r14d, %r14d
imulq $0x38, %rbp, %rbp
addq %r14, %rbp
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x6540
leaq 0x38(%rsp), %rcx
movl 0x20(%rcx), %eax
movl %eax, 0x30(%rbp)
movups 0x10(%rcx), %xmm0
movups %xmm0, 0x20(%rbp)
movq %r15, %rdi
movq (%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10fc4
leaq 0x38(%rax), %rdx
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x10fc4
movq %rax, %rbp
movq 0x20(%rsp), %rax
subq %r15, %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
movq %r15, %rdi
movq %rax, %rsi
callq 0x10fb4
movq %r14, 0x10(%rsp)
movq %rbp, (%rsp)
movq %rbp, 0x18(%rsp)
imulq $0x38, %r12, %rax
movq %r14, %r15
addq %r14, %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %r14
leaq 0x66(%rsp), %r12
leaq 0x78(%rsp), %rbp
leaq 0x28(%rsp), %rdi
callq 0x6838
incq %rbx
addq $0x1c, %r13
jmp 0x10920
movq (%rsp), %rbx
cmpq %rbx, %r15
je 0x10d07
movq %r15, %r14
movq %rbx, %r15
subq %r14, %r15
movq %r15, %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x11025
cmpq $0x381, %r15            # imm = 0x381
jl 0x10b9a
leaq 0x380(%r14), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x113c1
cmpq %rbx, %r15
je 0x10ba5
movq %r15, %rdi
callq 0x11461
addq $0x38, %r15
jmp 0x10b87
movq %r14, %rdi
movq %rbx, %rsi
callq 0x113c1
movq 0x503a4(%rip), %rax     # 0x60f50
movq (%rax), %rbx
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
pushq $-0x1
popq %rbp
pushq $0x32
popq %r12
leaq 0x28(%rsp), %r15
xorl %esi, %esi
subq %r14, %r13
movq %r13, %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
cltq
cmpq %rax, %rsi
jge 0x10cf4
imulq $0x38, %rsi, %r13
movq (%r14,%r13), %rdx
movl 0x20(%r14,%r13), %ecx
movl 0x24(%r14,%r13), %r8d
movl 0x28(%r14,%r13), %r9d
movl 0x2c(%r14,%r13), %r10d
subq $0x8, %rsp
movq %rbx, %rdi
movq %rsi, %r11
leaq 0x336ff(%rip), %rsi     # 0x4430c
xorl %eax, %eax
pushq %rdx
movq %r11, 0x10(%rsp)
pushq %r11
pushq %r10
callq 0x6590
addq $0x20, %rsp
movl 0x30(%r14,%r13), %eax
cmpl $-0x1, %eax
je 0x10cba
leaq 0x1aab5cf9(%rip), %rcx  # 0x1aac6930
movq (%rcx), %rcx
cltq
imulq $0x4548, %rax, %rdx     # imm = 0x4548
leaq (%rcx,%rdx), %r14
addq $0x1c, %r14
movl -0x4(%r14), %eax
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
addq %rdx, %rcx
addq $0x20, %rcx
xorl %edx, %edx
movl %r12d, %edi
movl %ebp, %esi
cmpq %rdx, %rax
je 0x10c81
movl (%rcx), %r8d
cmpl %edi, %r8d
jle 0x10c78
movl (%rcx), %edi
movl %edx, %esi
incq %rdx
addq $0x1c, %rcx
jmp 0x10c67
cmpl $-0x1, %esi
je 0x10cba
movslq %esi, %rax
imulq $0x1c, %rax, %r13
movswl (%r14,%r13), %edi
movq %r15, %rsi
callq 0x3cc92
addq %r13, %r14
movq %rbx, %rdi
leaq 0x336a2(%rip), %rsi     # 0x44349
movq %r15, %rdx
xorl %eax, %eax
callq 0x6590
movl 0x10(%r14), %eax
jmp 0x10c27
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
movq %r13, %rax
subq %r14, %rax
cqto
pushq $0x38
popq %rcx
idivq %rcx
decl %eax
movq (%rsp), %rsi
cmpq %rax, %rsi
je 0x10cec
pushq $0x20
popq %rdi
movq %rbx, %rsi
callq 0x64e0
movq (%rsp), %rsi
incq %rsi
jmp 0x10bc7
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x64e0
movq %rbx, %rdi
callq 0x6490
leaq 0x10(%rsp), %rdi
callq 0x10da0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x33658(%rip), %rdi     # 0x44382
callq 0x61d0
jmp 0x10d7c
movq %rax, %rdi
callq 0x6130
movq %r14, %rbx
testq %r14, %r14
jne 0x10d4b
movq %rbp, %rdi
callq 0x6838
jmp 0x10d56
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x10f8f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10fb4
callq 0x65b0
movq %rax, %rbx
callq 0x66a0
jmp 0x10d7f
movq %rax, %rdi
callq 0x114ed
jmp 0x10d7c
jmp 0x10d7c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6838
jmp 0x10d8e
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x10da0
movq %rbx, %rdi
callq 0x6740