// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]pentagod/agentmcts.cpp
// Function: AgentMCTS::return_move(AgentMCTS::Node const*, int, int) const
Move AgentMCTS::return_move(const Node * node, int toplay, int verbose) const {
	if(node->outcome >= 0)
		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 >= 0){
			if(child->outcome == toplay) val =  800000000000.0 - child->exp.num(); //shortest win
			else if(child->outcome == 0) 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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
cmpb $0x0, 0x12(%rsi)
js 0x1ddc8
addq $0x18, %rbx
jmp 0x1de7b
movq 0x20(%rbx), %r8
leaq 0x10(%r8), %rdi
xorl %esi, %esi
movl $0x0, %eax
cmpq $0x2, %r8
cmovaeq %rdi, %rax
jb 0x1ddee
movzwl 0x6(%r8), %esi
leaq (%rsi,%rsi,4), %rsi
leaq (%rdi,%rsi,8), %rsi
cmpq %rsi, %rax
je 0x1de68
vmovsd 0xe835(%rip), %xmm0     # 0x2c630
xorl %ebx, %ebx
vmovsd 0xe843(%rip), %xmm1     # 0x2c648
vmovsd 0xe833(%rip), %xmm2     # 0x2c640
vmovsd 0xe823(%rip), %xmm3     # 0x2c638
movsbl 0x12(%rax), %edi
testl %edi, %edi
js 0x1de2e
cmpl %edx, %edi
jne 0x1de3b
vcvtusi2sdq 0x8(%rax), %xmm5, %xmm4
vsubsd %xmm4, %xmm1, %xmm4
jmp 0x1de51
movq (%rax), %rdi
shrq %rdi
vcvtsi2sd %rdi, %xmm5, %xmm4
jmp 0x1de51
vcvtusi2sdq 0x8(%rax), %xmm5, %xmm4
testb %dil, %dil
je 0x1de4d
vaddsd %xmm3, %xmm4, %xmm4
jmp 0x1de51
vaddsd %xmm2, %xmm4, %xmm4
vucomisd %xmm0, %xmm4
vmaxsd %xmm0, %xmm4, %xmm0
cmovaq %rax, %rbx
addq $0x28, %rax
cmpq %rsi, %rax
jne 0x1de15
jmp 0x1de6a
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x1e129
testl %ecx, %ecx
jne 0x1de8c
addq $0x14, %rbx
movl (%rbx), %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
vcvtusi2ssq (%rbx), %xmm5, %xmm0
vmulss 0xe7be(%rip), %xmm0, %xmm0 # 0x2c658
vcvtusi2ssq 0x8(%rbx), %xmm5, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0xc437(%rip), %xmm0, %xmm0 # 0x2a2e8
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
movl $0x2, %esi
callq 0x1579c
leaq 0xeb72(%rip), %rcx      # 0x2ca3f
movl $0xd, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1df02
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r14)
jmp 0x1df0f
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
movq 0x8(%rax), %rdx
leaq 0x68(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0xeb1b(%rip), %rsi      # 0x2ca4d
callq 0x4610
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1df5a
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1df67
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x8(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0xd669
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r15, %rcx
je 0x1dfaf
movq 0x38(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x1dfce
leaq 0x58(%rsp), %rdi
movl $0xf, %esi
cmpq %rdi, -0x10(%rdi)
je 0x1dfc9
movq 0x58(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x1dfdf
movq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdi
callq 0x40b0
jmp 0x1dfed
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x18(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
jne 0x1e00c
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rdx)
jmp 0x1e019
movq %rsi, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0xbaa5(%rip), %rsi      # 0x29ae2
leaq 0x8(%rsp), %rdi
callq 0x4610
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1e06f
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r12)
jmp 0x1e082
movq %rdx, 0x88(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x98(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x18f3c(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0x88(%rsp), %rdi
callq 0x4320
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x1e0c6
callq 0x42b0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1e0d9
callq 0x42b0
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1e0ec
callq 0x42b0
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x1e0fb
callq 0x42b0
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x1e10a
callq 0x42b0
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1de77
callq 0x42b0
jmp 0x1de77
leaq 0xe8d1(%rip), %rdi      # 0x2ca01
leaq 0xe637(%rip), %rsi      # 0x2c76e
leaq 0xe8c7(%rip), %rcx      # 0x2ca05
movl $0x17f, %edx            # imm = 0x17F
callq 0x41b0
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1e163
callq 0x42b0
jmp 0x1e163
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1e17b
callq 0x42b0
jmp 0x1e17b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x1e18f
callq 0x42b0
jmp 0x1e18f
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x1e1a3
callq 0x42b0
jmp 0x1e1a3
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1e1b9
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0
nop