// File: kobanium[P]Ray/src/mcts/UctSearch.cpp
// Function: CalculateCriticalityIndex(uct_node_t*, statistic_t*, int, int*)
static void
CalculateCriticalityIndex( uct_node_t *node, statistic_t *node_statistic, int color, int *index )
{
  const int other = GetOppositeColor(color);
  const double inv_count = (statistic_count.load() > 0) ? 1.0 / statistic_count.load() : 1.0;
  const int child_num = node->child_num;
  const double win = static_cast<double>(node->win) / node->move_count;
  const double lose = 1.0 - win;
  double tmp;

  index[0] = 0;

  for (int i = 1; i < child_num; i++) {
    const int pos = node->child[i].pos;

    tmp = node_statistic[pos].colors[static_cast<int>(StatisticInformation::Win)] * inv_count
      - ((node_statistic[pos].colors[color] * inv_count) * win
         + (node_statistic[pos].colors[other] * inv_count) * lose);
    if (tmp < 0) tmp = 0;
    index[i] = static_cast<int>(tmp * CRITICALITY_TERM);
    if (index[i] > criticality_max - 1) index[i] = criticality_max - 1;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movl %edx, -0x44(%rbp)
movq %rcx, -0x50(%rbp)
movl -0x44(%rbp), %edi
callq 0x25810
movl %eax, -0x54(%rbp)
leaq 0x1aaedae4(%rip), %rax  # 0x1ab7a3e0
movq %rax, -0x8(%rbp)
movl $0x5, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x8c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x8c94f
jmp 0x8c934
movl -0x8c(%rbp), %eax
subl $0x5, %eax
je 0x8c95d
jmp 0x8c941
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x8c969
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
jmp 0x8c969
movq -0x98(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x8ca02
leaq 0x1aaeda66(%rip), %rax  # 0x1ab7a3e0
movq %rax, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x24(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x25b00
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x9c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x8c9cd
jmp 0x8c9b2
movl -0x9c(%rbp), %eax
subl $0x5, %eax
je 0x8c9db
jmp 0x8c9bf
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x8c9e7
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
jmp 0x8c9e7
movq -0xa8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
cvtsi2sdl -0x2c(%rbp), %xmm1
movsd 0x1f614(%rip), %xmm0    # 0xac008
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x8ca14
movsd 0x1f5fe(%rip), %xmm0    # 0xac008
movsd %xmm0, -0xb0(%rbp)
jmp 0x8ca14
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rdi
addq $0xc, %rdi
callq 0x25780
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
callq 0x25780
movsd -0xb8(%rbp), %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd 0x1f59a(%rip), %xmm0    # 0xac008
subsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x50(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x8cc23
movq -0x38(%rbp), %rax
addq $0x1c, %rax
movslq -0x84(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movswl (%rax), %eax
movl %eax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x88(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
addq $0xc, %rdi
callq 0x25780
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x88(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movslq -0x44(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x25780
cvtsi2sd %eax, %xmm0
mulsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x88(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movslq -0x54(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x25780
movsd -0xd8(%rbp), %xmm1
movsd -0xd0(%rbp), %xmm4
movsd -0xc8(%rbp), %xmm0
movsd -0xc0(%rbp), %xmm2
cvtsi2sd %eax, %xmm3
mulsd -0x60(%rbp), %xmm3
mulsd -0x78(%rbp), %xmm3
mulsd %xmm4, %xmm1
addsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x80(%rbp), %xmm0
jbe 0x8cbbc
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd 0x1f454(%rip), %xmm0    # 0xac018
mulsd -0x80(%rbp), %xmm0
cvttsd2si %xmm0, %edx
movq -0x50(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x5ae19(%rip), %ecx     # 0xe7a08
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x8cc0d
movl 0x5ae0c(%rip), %edx     # 0xe7a08
subl $0x1, %edx
movq -0x50(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x8cc0f
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x8ca8c
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)