// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xc8, %rsp
leaq 0x1fe7c(%rip), %rax     # 0x31da9
movq %rsi, %rbx
movb (%rax), %al
cmpb %al, 0x12(%rsi)
jge 0x11ff3
movq 0x18(%rbx), %r9
xorl %edi, %edi
movl $0x0, %esi
leaq 0x10(%r9), %r8
cmpq $0x2, %r9
cmovaeq %r8, %rsi
jb 0x11f62
movzwl 0x6(%r9), %edi
shll $0x5, %edi
addq %rdi, %r8
movq %r8, %rdi
cmpq %rdi, %rsi
je 0x122ac
vmovsd 0x1b5e5(%rip), %xmm0    # 0x2d558
vmovsd 0x1b5f5(%rip), %xmm1    # 0x2d570
vmovsd 0x1b5e5(%rip), %xmm2    # 0x2d568
vmovsd 0x1b5d5(%rip), %xmm3    # 0x2d560
xorl %ebx, %ebx
movb 0x12(%rsi), %r8b
cmpb %al, %r8b
jge 0x11fa3
movq (%rsi), %r8
shrq %r8
vcvtsi2sd %r8, %xmm5, %xmm4
jmp 0x11fcb
cmpb %dl, %r8b
jne 0x11fb5
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
vsubsd %xmm4, %xmm1, %xmm4
jmp 0x11fcb
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
cmpb %al, %r8b
jne 0x11fc7
vaddsd %xmm2, %xmm4, %xmm4
jmp 0x11fcb
vaddsd %xmm3, %xmm4, %xmm4
vucomisd %xmm0, %xmm4
vmaxsd %xmm0, %xmm4, %xmm0
cmovaq %rsi, %rbx
addq $0x20, %rsi
cmpq %rdi, %rsi
jne 0x11f8d
testq %rbx, %rbx
je 0x122ac
testl %ecx, %ecx
jne 0x12009
addq $0x14, %rbx
jmp 0x11ff7
addq $0x16, %rbx
movzwl (%rbx), %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x8(%rbx), %rax
vxorpd %xmm0, %xmm0, %xmm0
testq %rax, %rax
je 0x1203a
vcvtusi2ssq (%rbx), %xmm5, %xmm0
vmulss 0x1b518(%rip), %xmm0, %xmm0 # 0x2d53c
vcvtusi2ss %rax, %xmm5, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0x1b50e(%rip), %xmm0, %xmm0 # 0x2d548
leaq 0xa8(%rsp), %r14
movl $0x2, %esi
movq %r14, %rdi
callq 0x2a510
leaq 0x1b7b4(%rip), %rcx     # 0x2d80a
movl $0xd, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x53c0
leaq 0x78(%rsp), %r14
movq %rax, %rcx
addq $0x10, %rcx
movq %r14, -0x10(%r14)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1208b
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r14)
jmp 0x12098
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 0x1b75d(%rip), %rsi     # 0x2d818
callq 0x5720
leaq 0x38(%rsp), %r15
movq %rax, %rcx
addq $0x10, %rcx
movq %r15, -0x10(%r15)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x120e3
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r15)
jmp 0x120f0
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 0xd33e
movq 0x30(%rsp), %r8
movq 0x50(%rsp), %rdx
movq 0x28(%rsp), %rcx
movl $0xf, %edi
leaq (%rdx,%r8), %rax
cmpq %r15, %rcx
je 0x12138
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x1215b
leaq 0x58(%rsp), %r9
movl $0xf, %edi
cmpq %r9, %rsi
je 0x12156
movq 0x58(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x12167
leaq 0x28(%rsp), %rdi
callq 0x50e0
jmp 0x12175
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x53c0
leaq 0x18(%rsp), %rdx
leaq 0x10(%rax), %rcx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
cmpq %rcx, %rsi
jne 0x12194
vmovups (%rcx), %xmm0
vmovups %xmm0, (%rdx)
jmp 0x121a1
movq %rsi, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rax), %rsi
movq %rax, %rdx
addq $0x8, %rdx
movq %rsi, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x1a67d(%rip), %rsi     # 0x2c842
leaq 0x8(%rsp), %rdi
callq 0x5720
leaq 0x98(%rsp), %r12
movq %rax, %rcx
addq $0x10, %rcx
movq %r12, -0x10(%r12)
movq (%rax), %rdi
cmpq %rcx, %rdi
jne 0x121fa
vmovups (%rcx), %xmm0
movq %r12, %rdi
vmovups %xmm0, (%r12)
jmp 0x1220d
movq %rdi, 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 0x2ada9(%rip), %rax     # 0x3cfd8
movq (%rax), %rsi
callq 0x53b0
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x12249
callq 0x5310
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1225c
callq 0x5310
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1226f
callq 0x5310
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x1227e
callq 0x5310
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x1228d
callq 0x5310
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11fed
callq 0x5310
jmp 0x11fed
leaq 0x1b508(%rip), %rdi     # 0x2d7bb
leaq 0x1b49e(%rip), %rsi     # 0x2d758
leaq 0x1b4fe(%rip), %rcx     # 0x2d7bf
movl $0x11a, %edx            # imm = 0x11A
callq 0x5200
leaq 0x18(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x122e6
callq 0x5310
jmp 0x122e6
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x122fe
callq 0x5310
jmp 0x122fe
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x12312
callq 0x5310
jmp 0x12312
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x12326
callq 0x5310
jmp 0x12326
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1233c
callq 0x5310
movq %rbx, %rdi
callq 0x56c0