// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]morat/pentago/agentmctsthread.cpp
// Function: Morat::Pentago::AgentMCTS::AgentThread::choose_move(Morat::Pentago::AgentMCTS::Node const*, Morat::Side) const
AgentMCTS::Node * AgentMCTS::AgentThread::choose_move(const Node * node, Side to_play) const {
	float val, maxval = -1000000000;
	float logvisits = log(node->exp.num());

	float explore = agent->explore;
	if(agent->parentexplore)
		explore *= node->exp.avg();

	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) //return a win immediately
				return child;

			val = (child->outcome == Outcome::DRAW ? -1 : -2); //-1 for tie so any unknown is better, -2 for loss so it's even worse
		}else{
			val = child->value(agent->knowledge, agent->fpurgency);
			if(explore > 0)
				val += explore*sqrt(logvisits/(child->exp.num() + 1));
		}

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

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
vcvtusi2sdq 0x8(%rsi), %xmm0, %xmm0
callq 0x5290
movq 0x40(%r14), %rax
vmovss 0x4c(%rax), %xmm1
vmovss %xmm1, (%rsp)
cmpb $0x1, 0x49(%rax)
jne 0x17513
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x17501
vcvtusi2ssq (%r15), %xmm2, %xmm1
vmulss 0x16047(%rip), %xmm1, %xmm1 # 0x2d53c
vcvtusi2ss %rax, %xmm2, %xmm2
vdivss %xmm2, %xmm1, %xmm1
jmp 0x17505
vxorps %xmm1, %xmm1, %xmm1
vmovss (%rsp), %xmm2
vmulss %xmm1, %xmm2, %xmm2
vmovss %xmm2, (%rsp)
movq 0x18(%r15), %rcx
leaq 0x10(%rcx), %rax
xorl %r13d, %r13d
movl $0x0, %r15d
cmpq $0x2, %rcx
cmovaeq %rax, %r15
jb 0x1753b
movzwl 0x6(%rcx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
movq %rax, %r13
cmpq %r13, %r15
je 0x1762f
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0xc(%rsp)
vcvtss2sd (%rsp), %xmm3, %xmm0
vmovsd %xmm0, 0x10(%rsp)
vmovss 0x1740f(%rip), %xmm2    # 0x2e970
xorl %r12d, %r12d
leaq 0x1a83e(%rip), %rbp     # 0x31da9
movb 0x12(%r15), %al
movb (%rbp), %cl
cmpb %cl, %al
jge 0x175d5
movq 0x40(%r14), %rax
movzbl 0x50(%rax), %esi
vmovss 0x58(%rax), %xmm0
movq %r15, %rdi
vmovss %xmm2, 0x4(%rsp)
callq 0x17db8
vmovss 0x4(%rsp), %xmm2
vmovaps %xmm0, %xmm3
vmovss (%rsp), %xmm0
vucomiss 0x173cc(%rip), %xmm0    # 0x2e974
jbe 0x17614
movq 0x8(%r15), %rax
incq %rax
vcvtusi2ss %rax, %xmm4, %xmm0
vmovss 0xc(%rsp), %xmm1
vdivss %xmm0, %xmm1, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vucomisd 0x15b7b(%rip), %xmm0    # 0x2d148
jb 0x175ee
vsqrtsd %xmm0, %xmm0, %xmm0
jmp 0x17605
cmpb %bl, %al
je 0x17634
xorl %edx, %edx
cmpb %cl, %al
sete %dl
leaq 0x17381(%rip), %rax     # 0x2e968
vmovss (%rax,%rdx,4), %xmm3
jmp 0x17614
vmovss %xmm3, 0x8(%rsp)
callq 0x5280
vmovss 0x8(%rsp), %xmm3
vmovss 0x4(%rsp), %xmm2
vcvtss2sd %xmm3, %xmm3, %xmm1
vfmadd231sd 0x10(%rsp), %xmm0, %xmm1 # xmm1 = (xmm0 * mem) + xmm1
vcvtsd2ss %xmm1, %xmm1, %xmm3
vucomiss %xmm2, %xmm3
vmaxss %xmm2, %xmm3, %xmm2
cmovaq %r15, %r12
addq $0x20, %r15
cmpq %r13, %r15
jne 0x1756b
jmp 0x17637
xorl %r12d, %r12d
jmp 0x17637
movq %r15, %r12
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop