// File: /workspace/github_cpp_makefile_star2_O3/tyhsu[P]Ewn/uct.cpp
// Function: UCT::select_children_list_index(Tree_node*)
int UCT::select_children_list_index(Tree_node* cur_node)
{
	float child_value_list[18] = {0};
	float best_value = 0.0;

	// calculate every UCT value of the child nodes to find the max
	for (int i=0; i<18; i++) {
		if (cur_node->get_child_ptr(i) == NULL) break;

		Tree_node* node_ptr = cur_node->get_child_ptr(i);
		float exploitation = (float)node_ptr->get_win_count() / ((float)node_ptr->get_visit_count() + this->eps);
		float exploration = this->exploration_param * sqrt( log((float)cur_node->get_visit_count() + this->t_param) / ((float)node_ptr->get_visit_count() + this->eps));
		child_value_list[i] = exploitation + exploration;
		if (child_value_list[i] > best_value)
			best_value = child_value_list[i];
	}

	// gather the nodes whose UCT value approaches best_value
	int best_children_num = 0;
	float child_index_list[18];
	for (int i=0; i<18; i++) {
		if (cur_node->get_child_ptr(i) == NULL) break;
		if (best_value - child_value_list[i] < this->eps)
			child_index_list[ best_children_num++ ] = i;
	}
	
	// select one of best child node randomly and return its index
	return child_index_list[ rand()%best_children_num ];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0xe0(%rbp), %r15
movl $0x0, (%r15)
xorps %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6926
testq %rax, %rax
je 0x6569
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6926
movq %rax, %r13
movq %rax, %rdi
callq 0x68fc
movl %eax, %r12d
movq %r13, %rdi
callq 0x6904
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss 0x8(%r14), %xmm0
movss %xmm0, -0x3c(%rbp)
movss 0xc(%r14), %xmm0
movss %xmm0, -0x38(%rbp)
movq %rbx, %rdi
callq 0x6904
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss 0x10(%r14), %xmm0
callq 0x2220
movss %xmm0, -0x34(%rbp)
movq %r13, %rdi
callq 0x6904
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss 0x8(%r14), %xmm0
movss -0x34(%rbp), %xmm1
divss %xmm0, %xmm1
movaps %xmm1, %xmm0
ucomiss 0x31ef(%rip), %xmm1     # 0x9704
jb 0x651d
sqrtss %xmm0, %xmm0
jmp 0x6522
callq 0x2210
movss -0x38(%rbp), %xmm1
mulss %xmm0, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r12d, %xmm2
divss -0x3c(%rbp), %xmm2
addss %xmm1, %xmm2
movslq -0xe0(%rbp), %rax
leal 0x1(%rax), %ecx
movss %xmm2, -0x90(%rbp,%rax,4)
maxss -0x2c(%rbp), %xmm2
movl %ecx, -0xe0(%rbp)
movaps %xmm2, %xmm0
cmpq $0x11, %rax
jl 0x6477
jmp 0x656e
movss -0x2c(%rbp), %xmm2
leaq -0x30(%rbp), %r15
movl $0x0, (%r15)
xorl %r12d, %r12d
movss %xmm2, -0x2c(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6926
testq %rax, %rax
je 0x65cf
movslq -0x30(%rbp), %rax
movss -0x2c(%rbp), %xmm0
subss -0x90(%rbp,%rax,4), %xmm0
movss 0x8(%r14), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x65c4
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %r12d, %rcx
incl %r12d
movss %xmm0, -0xe0(%rbp,%rcx,4)
leal 0x1(%rax), %ecx
movl %ecx, -0x30(%rbp)
cmpl $0x11, %eax
jl 0x6581
callq 0x2060
cltd
idivl %r12d
movslq %edx, %rax
cvttss2si -0xe0(%rbp,%rax,4), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop