// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rsi, %rbx
leaq 0x1fd35(%rip), %rax     # 0x31da9
movb (%rax), %al
cmpb %al, 0x12(%rsi)
jge 0x12122
movq 0x18(%rbx), %r9
leaq 0x10(%r9), %r8
xorl %edi, %edi
movl $0x0, %esi
cmpq $0x2, %r9
cmovaeq %r8, %rsi
jb 0x120a6
movzwl 0x6(%r9), %edi
shll $0x5, %edi
addq %rdi, %r8
movq %r8, %rdi
cmpq %rdi, %rsi
je 0x12128
vmovsd 0x1b4a5(%rip), %xmm0    # 0x2d558
xorl %ebx, %ebx
vmovsd 0x1b4b3(%rip), %xmm1    # 0x2d570
vmovsd 0x1b4a3(%rip), %xmm2    # 0x2d568
vmovsd 0x1b493(%rip), %xmm3    # 0x2d560
movb 0x12(%rsi), %r8b
cmpb %al, %r8b
jge 0x120e3
movq (%rsi), %r8
shrq %r8
vcvtsi2sd %r8, %xmm5, %xmm4
jmp 0x1210b
cmpb %dl, %r8b
jne 0x120f5
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
vsubsd %xmm4, %xmm1, %xmm4
jmp 0x1210b
vcvtusi2sdq 0x8(%rsi), %xmm5, %xmm4
cmpb %al, %r8b
jne 0x12107
vaddsd %xmm2, %xmm4, %xmm4
jmp 0x1210b
vaddsd %xmm3, %xmm4, %xmm4
vucomisd %xmm0, %xmm4
vmaxsd %xmm0, %xmm4, %xmm0
cmovaq %rsi, %rbx
addq $0x20, %rsi
cmpq %rdi, %rsi
jne 0x120cd
jmp 0x1212a
addq $0x16, %rbx
jmp 0x1213b
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x123f6
testl %ecx, %ecx
jne 0x1214d
addq $0x14, %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 0x1217e
vcvtusi2ssq (%rbx), %xmm5, %xmm0
vmulss 0x1b3d4(%rip), %xmm0, %xmm0 # 0x2d53c
vcvtusi2ss %rax, %xmm5, %xmm1
vdivss %xmm1, %xmm0, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vmulsd 0x1b3ca(%rip), %xmm0, %xmm0 # 0x2d548
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
movl $0x2, %esi
callq 0x2a99c
leaq 0x1b670(%rip), %rcx     # 0x2d80a
movl $0xd, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x53c0
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x121cf
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r14)
jmp 0x121dc
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 0x1b619(%rip), %rsi     # 0x2d818
callq 0x5720
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x12227
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r15)
jmp 0x12234
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 0xd392
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r15, %rcx
je 0x1227c
movq 0x38(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x1229b
leaq 0x58(%rsp), %rdi
movl $0xf, %esi
cmpq %rdi, -0x10(%rdi)
je 0x12296
movq 0x58(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x122ac
movq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdi
callq 0x50e0
jmp 0x122ba
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x53c0
leaq 0x18(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
jne 0x122d9
vmovups (%rcx), %xmm0
vmovups %xmm0, (%rdx)
jmp 0x122e6
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 0x1a538(%rip), %rsi     # 0x2c842
leaq 0x8(%rsp), %rdi
callq 0x5720
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1233c
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r12)
jmp 0x1234f
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 0x2ac67(%rip), %rax     # 0x3cfd8
movq (%rax), %rsi
movq 0x88(%rsp), %rdi
callq 0x53b0
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x12393
callq 0x5310
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x123a6
callq 0x5310
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x123b9
callq 0x5310
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x123c8
callq 0x5310
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x123d7
callq 0x5310
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12137
callq 0x5310
jmp 0x12137
leaq 0x1b3be(%rip), %rdi     # 0x2d7bb
leaq 0x1b354(%rip), %rsi     # 0x2d758
leaq 0x1b3b4(%rip), %rcx     # 0x2d7bf
movl $0x11a, %edx            # imm = 0x11A
callq 0x5200
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12430
callq 0x5310
jmp 0x12430
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12448
callq 0x5310
jmp 0x12448
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x1245c
callq 0x5310
jmp 0x1245c
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x12470
callq 0x5310
jmp 0x12470
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12486
callq 0x5310
movq %rbx, %rdi
callq 0x56c0