// File: /workspace/github_cpp_makefile_star2_O3/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
vcvtusi2sdq 0x8(%rsi), %xmm0, %xmm0
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
callq 0x5290
movq 0x40(%r14), %rax
vmovss 0x4c(%rax), %xmm1
cmpb $0x1, 0x49(%rax)
vmovss %xmm1, (%rsp)
jne 0x1733f
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1732d
vcvtusi2ssq (%r15), %xmm2, %xmm1
vcvtusi2ss %rax, %xmm2, %xmm2
vmulss 0x16215(%rip), %xmm1, %xmm1 # 0x2d53c
vdivss %xmm2, %xmm1, %xmm1
jmp 0x17331
vxorps %xmm1, %xmm1, %xmm1
vmovss (%rsp), %xmm2
vmulss %xmm1, %xmm2, %xmm2
vmovss %xmm2, (%rsp)
movq 0x18(%r15), %rcx
xorl %r13d, %r13d
movl $0x0, %r15d
leaq 0x10(%rcx), %rax
cmpq $0x2, %rcx
cmovaeq %rax, %r15
jb 0x17367
movzwl 0x6(%rcx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
movq %rax, %r13
cmpq %r13, %r15
je 0x1745b
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss 0x175f4(%rip), %xmm2    # 0x2e970
leaq 0x1aa26(%rip), %rbp     # 0x31da9
xorl %r12d, %r12d
vmovss %xmm0, 0xc(%rsp)
vcvtss2sd (%rsp), %xmm3, %xmm0
vmovsd %xmm0, 0x10(%rsp)
movb 0x12(%r15), %al
movb (%rbp), %cl
cmpb %cl, %al
jge 0x17401
movq 0x40(%r14), %rax
movq %r15, %rdi
vmovss %xmm2, 0x4(%rsp)
vmovss 0x58(%rax), %xmm0
movzbl 0x50(%rax), %esi
callq 0x17bbe
vmovss (%rsp), %xmm1
vmovss 0x4(%rsp), %xmm2
vmovaps %xmm0, %xmm3
vucomiss 0x175a0(%rip), %xmm1    # 0x2e974
jbe 0x17440
movq 0x8(%r15), %rax
vmovss 0xc(%rsp), %xmm1
incq %rax
vcvtusi2ss %rax, %xmm4, %xmm0
vdivss %xmm0, %xmm1, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vucomisd 0x15d4f(%rip), %xmm0    # 0x2d148
jb 0x1741a
vsqrtsd %xmm0, %xmm0, %xmm0
jmp 0x17431
cmpb %bl, %al
je 0x17460
xorl %edx, %edx
cmpb %cl, %al
leaq 0x17558(%rip), %rax     # 0x2e968
sete %dl
vmovss (%rax,%rdx,4), %xmm3
jmp 0x17440
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 0x17397
jmp 0x17463
xorl %r12d, %r12d
jmp 0x17463
movq %r15, %r12
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop