// File: 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
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x38(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x22db0
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x229e1
leaq -0x90(%rbp), %rdi
callq 0x22dd0
movq -0x38(%rbp), %rax
movslq -0x54(%rbp), %rdx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
leaq 0x1c(%rax,%rcx), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movswl (%rax), %edi
leaq -0xa2(%rbp), %rsi
callq 0x9ef70
jmp 0x226f2
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x79c0
movq -0x160(%rbp), %rdx
leaq -0xd8(%rbp), %rdi
leaq -0xa2(%rbp), %rsi
callq 0x256b0
jmp 0x22721
leaq -0x90(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
movq %rsi, -0x178(%rbp)
callq 0x76b0
movq -0x178(%rbp), %rdi
callq 0x7ae8
leaq -0xd9(%rbp), %rdi
callq 0x75d0
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x170(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x164(%rbp)
jmp 0x22789
movl -0x164(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x17c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x227bf
jmp 0x227a4
movl -0x17c(%rbp), %eax
subl $0x5, %eax
je 0x227cd
jmp 0x227b1
movq -0x170(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x227e3
movq -0x170(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x227e3
movq -0x170(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x227e3
movq %rax, %rdi
callq 0x25ac0
movl -0x14(%rbp), %eax
movl %eax, -0x70(%rbp)
movsd 0x8981f(%rip), %xmm0    # 0xac010
movsd %xmm0, -0xe8(%rbp)
cmpl $0x0, -0x70(%rbp)
jle 0x228e5
movq -0x98(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x188(%rbp)
movl -0x24(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x180(%rbp)
jmp 0x22839
movl -0x180(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x18c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x2286f
jmp 0x22854
movl -0x18c(%rbp), %eax
subl $0x5, %eax
je 0x2287d
jmp 0x22861
movq -0x188(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x22893
movq -0x188(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x22893
movq -0x188(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x22893
movq %rax, %rdi
callq 0x25ac0
cvtsi2sdl -0x2c(%rbp), %xmm0
cvtsi2sdl -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x228e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x229d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x75d0
jmp 0x229d0
movl $0x2710, -0xec(%rbp)    # imm = 0x2710
movsd -0xe8(%rbp), %xmm0
movsd 0x89721(%rip), %xmm1    # 0xac020
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xf0(%rbp)
leaq -0xec(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x25820
movq %rax, -0x198(%rbp)
jmp 0x22929
movq -0x198(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl $0x2710, -0xf4(%rbp)    # imm = 0x2710
movq -0x98(%rbp), %rax
movss 0x14(%rax), %xmm0
movss 0x896ed(%rip), %xmm1    # 0xac040
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xf8(%rbp)
leaq -0xf4(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x25820
movq %rax, -0x1a0(%rbp)
jmp 0x2297d
movq -0x1a0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x98(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x70(%rbp)
jle 0x229b6
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x22df0
jmp 0x229b4
jmp 0x229b6
leaq -0x90(%rbp), %rdi
callq 0x22e70
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2269f
leaq -0x90(%rbp), %rdi
callq 0x22e70
jmp 0x22d91
leaq -0x50(%rbp), %rdi
callq 0x22e90
testb $0x1, %al
jne 0x229f0
jmp 0x229ff
movl $0x1, -0xfc(%rbp)
jmp 0x22d7f
leaq -0x50(%rbp), %rdi
callq 0x22f10
movq %rax, -0x1a8(%rbp)
jmp 0x22a11
movq -0x1a8(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x22f40
movq %rax, -0x1b0(%rbp)
jmp 0x22a31
movq -0x1b0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x22ed0
jmp 0x22a54
movq 0xc44f5(%rip), %rax     # 0xe6f50
movq (%rax), %rax
movq %rax, -0x120(%rbp)
movl $0x0, -0x124(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x1b4(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x22f70
movq %rax, %rcx
movl -0x1b4(%rbp), %eax
cmpl %ecx, %eax
jge 0x22d52
movslq -0x124(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x22fa0
movq %rax, -0x130(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x130(%rbp), %rdi
callq 0x71d0
movq %rax, -0x1d0(%rbp)
movq -0x130(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x1c8(%rbp)
movq -0x130(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x1c4(%rbp)
movq -0x130(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x1c0(%rbp)
movq -0x130(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x1bc(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movq -0x130(%rbp), %rdi
callq 0x71d0
movq -0x1d8(%rbp), %rdi
movq -0x1d0(%rbp), %rdx
movl -0x1c8(%rbp), %ecx
movl -0x1c4(%rbp), %r8d
movl -0x1c0(%rbp), %r9d
movl -0x1bc(%rbp), %r11d
movl -0x1b8(%rbp), %r10d
leaq 0x897b0(%rip), %rsi     # 0xac30c
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x77d0
movq -0x130(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x134(%rbp)
cmpl $-0x1, -0x134(%rbp)
je 0x22d02
leaq 0x1ab29f1b(%rip), %rax  # 0x1ab4cab0
movq (%rax), %rax
movslq -0x134(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
addq $0x1c, %rax
movq %rax, -0x140(%rbp)
leaq 0x1ab29ef5(%rip), %rax  # 0x1ab4cab0
movq (%rax), %rax
movslq -0x134(%rbp), %rcx
imulq $0x4548, %rcx, %rcx     # imm = 0x4548
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, -0x138(%rbp)
movl $0x32, -0x144(%rbp)
movl $0xffffffff, -0x148(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpl -0x138(%rbp), %eax
jge 0x22c8d
movq -0x140(%rbp), %rdi
movslq -0x14c(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
cmpl -0x144(%rbp), %eax
jle 0x22c77
movq -0x140(%rbp), %rdi
movslq -0x14c(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0x4, %rdi
callq 0x25780
movl %eax, -0x144(%rbp)
movl -0x14c(%rbp), %eax
movl %eax, -0x148(%rbp)
jmp 0x22c77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x22d91
jmp 0x22c79
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x22bf6
cmpl $-0x1, -0x148(%rbp)
jne 0x22c98
jmp 0x22d02
movq -0x140(%rbp), %rax
movslq -0x148(%rbp), %rdx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
movswl (%rax,%rcx), %edi
leaq -0x156(%rbp), %rsi
callq 0x9ef70
jmp 0x22cc3
movq -0x120(%rbp), %rdi
leaq -0x156(%rbp), %rdx
leaq 0x89671(%rip), %rsi     # 0xac349
movb $0x0, %al
callq 0x77d0
movq -0x140(%rbp), %rax
movslq -0x148(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x134(%rbp)
jmp 0x22b81
movl -0x124(%rbp), %eax
movl %eax, -0x1dc(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x22f70
movq %rax, %rcx
movl -0x1dc(%rbp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x22d3c
movq -0x120(%rbp), %rdi
leaq 0x89711(%rip), %rsi     # 0xac446
movb $0x0, %al
callq 0x77d0
jmp 0x22d3e
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x22a6f
movq -0x120(%rbp), %rdi
leaq 0x8c9a5(%rip), %rsi     # 0xaf705
xorl %eax, %eax
callq 0x77d0
movq -0x120(%rbp), %rdi
callq 0x7670
jmp 0x22d75
movl $0x0, -0xfc(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x22fc0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x22fc0
movq -0xb0(%rbp), %rdi
callq 0x79b0
nopw %cs:(%rax,%rax)