// File: tewalds[P]pentagod/agentmctsthread.cpp
// Function: AgentMCTS::MCTSThread::choose_move(AgentMCTS::Node const*, int) const
AgentMCTS::Node * AgentMCTS::MCTSThread::choose_move(const Node * node, int toplay) const {
	float val, maxval = -1000000000;
	float logvisits = log(node->exp.num());

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

	Node * ret = NULL,
		 * child = node->children.begin(),
		 * end   = node->children.end();

	for(; child != end; child++){
		if(child->outcome >= 0){
			if(child->outcome == toplay) //return a win immediately
				return child;

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

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

	return ret;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
vmovss 0xf30c(%rip), %xmm0     # 0x41010
vmovss %xmm0, 0x34(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2e5d0
movq %rax, %rdi
callq 0x32dd0
movq 0x8(%rsp), %rax
vmovaps %xmm0, %xmm1
vcvtsd2ss %xmm1, %xmm0, %xmm0
vmovss %xmm0, 0x30(%rsp)
movq 0x40(%rax), %rcx
vmovss 0x4c(%rcx), %xmm0
vmovss %xmm0, 0x2c(%rsp)
movq 0x40(%rax), %rax
testb $0x1, 0x49(%rax)
je 0x31d5e
movq 0x40(%rsp), %rdi
callq 0x2e600
vmulss 0x2c(%rsp), %xmm0, %xmm0
vmovss %xmm0, 0x2c(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x40(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc40
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc90
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x31eba
movq 0x18(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x31df1
movq 0x18(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl 0x3c(%rsp), %eax
jne 0x31dc9
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x31ec4
movq 0x18(%rsp), %rax
movsbl 0x12(%rax), %edx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x0, %edx
cmovel %ecx, %eax
vcvtsi2ss %eax, %xmm0, %xmm0
vmovss %xmm0, 0x38(%rsp)
jmp 0x31e7d
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq 0x40(%rcx), %rax
movb 0x50(%rax), %al
movq 0x40(%rcx), %rcx
vmovss 0x58(%rcx), %xmm0
andb $0x1, %al
movzbl %al, %esi
callq 0x32df0
vmovss %xmm0, 0x38(%rsp)
vmovss 0x2c(%rsp), %xmm0
vpxor %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x31e7b
vmovss 0x2c(%rsp), %xmm0
vmovss %xmm0, 0x4(%rsp)
vmovss 0x30(%rsp), %xmm0
vmovss %xmm0, (%rsp)
movq 0x18(%rsp), %rdi
callq 0x2e5d0
vmovss (%rsp), %xmm0
addq $0x1, %rax
vcvtusi2ss %rax, %xmm1, %xmm1
vdivss %xmm1, %xmm0, %xmm0
callq 0x32f10
vmovss 0x4(%rsp), %xmm1
vmovss 0x38(%rsp), %xmm2
vfmadd213ss %xmm2, %xmm1, %xmm0 # xmm0 = (xmm1 * xmm0) + xmm2
vmovss %xmm0, 0x38(%rsp)
jmp 0x31e7d
vmovss 0x34(%rsp), %xmm1
vmovss 0x38(%rsp), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x31ea5
vmovss 0x38(%rsp), %xmm0
vmovss %xmm0, 0x34(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x31ea7
movq 0x18(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x18(%rsp)
jmp 0x31d8d
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rsp
retq
nop