// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/pentago/agentmcts.cpp
// Function: Morat::Pentago::AgentMCTS::return_move(Morat::Pentago::AgentMCTS::Node const*, Morat::Side, int) const
Move AgentMCTS::return_move(const Node * node, Side to_play, int verbose) const {
	if(node->outcome >= Outcome::DRAW)
		return node->bestmove;

	double val, maxval = -1000000000000.0; //1 trillion

	Node * ret = NULL,
		 * child = node->children.begin(),
		 * end = node->children.end();

	for( ; child != end; child++){
		if(child->outcome >= Outcome::DRAW){
			if(child->outcome == to_play)             val =  800000000000.0 - child->exp.num(); //shortest win
			else if(child->outcome == Outcome::DRAW) val = -400000000000.0 + child->exp.num(); //longest tie
			else                                     val = -800000000000.0 + child->exp.num(); //longest loss
		}else{ //not proven
//			val = child->exp.num(); //num simulations
			val = child->exp.sum(); //num wins
		}

		if(maxval < val){
			maxval = val;
			ret = child;
		}
	}

	assert(ret);

	if(verbose)
		logerr("Score:       " + to_str(ret->exp.avg()*100., 2) + "% / " + to_str(ret->exp.num()) + "\n");

	return ret->move;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xc8, %rsp
leaq 0x1732a(%rip), %rax     # 0x24c19
movq %rsi, %rbx
movb (%rax), %al
cmpb %al, 0x12(%rsi)
jge 0xd99b
movq 0x18(%rbx), %r9
xorl %edi, %edi
movl $0x0, %esi
leaq 0x10(%r9), %r8
cmpq $0x2, %r9
cmovaeq %r8, %rsi
jb 0xd924
movzwl 0x6(%r9), %edi
shll $0x5, %edi
addq %rdi, %r8
movq %r8, %rdi
vmovsd 0x12bf4(%rip), %xmm0    # 0x20520
vmovsd 0x12c04(%rip), %xmm1    # 0x20538
vmovsd 0x12bf4(%rip), %xmm2    # 0x20530
vmovsd 0x12be4(%rip), %xmm3    # 0x20528
xorl %ebx, %ebx
cmpq %rdi, %rsi
je 0xd9a4
movb 0x12(%rsi), %r8b
cmpb %al, %r8b
jge 0xd961
movq (%rsi), %r8
shrq %r8
vcvtsi2sd %r8, %xmm5, %xmm4
jmp 0xd989
cmpb %dl, %r8b
jne 0xd973
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
vsubsd %xmm4, %xmm1, %xmm4
jmp 0xd989
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
cmpb %al, %r8b
jne 0xd985
vaddsd %xmm2, %xmm4, %xmm4
jmp 0xd989
vaddsd %xmm3, %xmm4, %xmm4
vucomisd %xmm0, %xmm4
vmaxsd %xmm0, %xmm4, %xmm0
cmovaq %rsi, %rbx
addq $0x20, %rsi
jmp 0xd946
addq $0x16, %rbx
jmp 0xda92
testq %rbx, %rbx
je 0xdaa0
testl %ecx, %ecx
je 0xda8e
movq %rbx, %rdi
callq 0xf23c
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0x12b4f(%rip), %xmm0, %xmm0 # 0x20518
leaq 0x88(%rsp), %r14
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x1df78
leaq 0x12d87(%rip), %rsi     # 0x2076a
leaq 0x28(%rsp), %rdi
movq %r14, %rdx
callq 0xaac0
leaq 0x12d81(%rip), %rdx     # 0x20778
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7b4d
movq 0x8(%rbx), %rsi
leaq 0x8(%rsp), %rdi
callq 0xaba2
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0xab3b
leaq 0x11de9(%rip), %rdx     # 0x1f818
leaq 0xa8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x7b4d
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0xaaae
movq %r14, %rdi
callq 0x5170
leaq 0x68(%rsp), %rdi
callq 0x5170
leaq 0x8(%rsp), %rdi
callq 0x5170
leaq 0x48(%rsp), %rdi
callq 0x5170
leaq 0x28(%rsp), %rdi
callq 0x5170
leaq 0x88(%rsp), %rdi
callq 0x5170
addq $0x14, %rbx
movzwl (%rbx), %eax
addq $0xc8, %rsp
popq %rbx
popq %r14
retq
leaq 0x12c74(%rip), %rdi     # 0x2071b
leaq 0x12c0a(%rip), %rsi     # 0x206b8
leaq 0x12c6a(%rip), %rcx     # 0x2071f
movl $0x11a, %edx            # imm = 0x11A
callq 0x5220
leaq 0x68(%rsp), %rdi
movq %rax, %rbx
callq 0x5170
jmp 0xdad1
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5170
jmp 0xdae0
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5170
jmp 0xdaef
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5170
jmp 0xdafe
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x5170
movq %rbx, %rdi
callq 0x5690
nop