// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::choose_move(Morat::Hex::AgentMCTS::Node const*, Morat::Side, int) const
AgentMCTS::Node * AgentMCTS::AgentThread::choose_move(const Node * node, Side to_play, int remain) const {
	float val, maxval = -1000000000;
	float logvisits = log(node->exp.num());
	int dynwidenlim = (agent->dynwiden > 0 ? (int)(logvisits/agent->logdynwiden)+2 : Board::max_vec_size);

	float raveval = use_rave * (agent->ravefactor + agent->decrrave*remain);
	float explore = use_explore * agent->explore;
	if(agent->parentexplore)
		explore *= node->exp.avg();

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

	for(; child != end && dynwidenlim >= 0; 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(raveval, agent->knowledge, agent->fpurgency);
			if(explore > 0)
				val += explore*sqrt(logvisits/(child->exp.num() + 1));
			dynwidenlim--;
		}

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

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movl %edx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
vcvtusi2sdq 0x18(%rsi), %xmm0, %xmm0
callq 0x5260
vcvtsd2ss %xmm0, %xmm0, %xmm0
vmovss %xmm0, 0x4(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x40(%rbx), %rax
vmovss 0x98(%rax), %xmm0
movl $0x271, %r13d           # imm = 0x271
vxorps %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x16572
vmovss 0x4(%rsp), %xmm0
vdivss 0x9c(%rax), %xmm0, %xmm0
vcvttss2si %xmm0, %r13d
addl $0x2, %r13d
movq 0x8(%rsp), %rcx
movsbl 0x2f15(%rcx), %ecx
vcvtsi2ss %ecx, %xmm2, %xmm0
vmulss 0x78(%rax), %xmm0, %xmm0
vmovss %xmm0, (%rsp)
cmpb $0x1, 0x74(%rax)
jne 0x165c8
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x165b6
vcvtusi2ssq 0x10(%r15), %xmm2, %xmm0
vmulss 0x1bc4e(%rip), %xmm0, %xmm0 # 0x321f8
vcvtusi2ss %rcx, %xmm2, %xmm1
vdivss %xmm1, %xmm0, %xmm0
jmp 0x165ba
vxorps %xmm0, %xmm0, %xmm0
vmovss (%rsp), %xmm1
vmulss %xmm0, %xmm1, %xmm1
vmovss %xmm1, (%rsp)
movq 0x28(%r15), %rdx
leaq 0x10(%rdx), %rcx
xorl %ebx, %ebx
movl $0x0, %ebp
cmpq $0x2, %rdx
cmovaeq %rcx, %rbp
jb 0x165f2
movzwl 0x6(%rdx), %edx
leaq (%rdx,%rdx,2), %rdx
shll $0x4, %edx
addq %rdx, %rcx
movq %rcx, %rbx
xorl %r15d, %r15d
cmpq %rbx, %rbp
je 0x16734
testl %r13d, %r13d
js 0x16734
movq 0x8(%rsp), %rcx
movsbl 0x2f14(%rcx), %ecx
vmovss 0x80(%rax), %xmm0
vcvtsi2ss %r12d, %xmm2, %xmm1
vcvtsi2ss %ecx, %xmm2, %xmm2
vfmadd213ss 0x7c(%rax), %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + mem
vmulss %xmm2, %xmm1, %xmm0
vmovss %xmm0, 0x14(%rsp)
vcvtss2sd (%rsp), %xmm3, %xmm0
vmovsd %xmm0, 0x20(%rsp)
addq $0x30, %rbp
vmovss 0x1d06d(%rip), %xmm0    # 0x336b8
xorl %r15d, %r15d
leaq 0x1fd5c(%rip), %r14     # 0x363b1
leaq -0x30(%rbp), %r12
movb -0xe(%rbp), %al
movb (%r14), %cl
cmpb %cl, %al
jge 0x166cd
vmovss %xmm0, 0x1c(%rsp)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movzbl 0x84(%rax), %esi
vmovss 0x90(%rax), %xmm1
movq %r12, %rdi
vmovss 0x14(%rsp), %xmm0
callq 0x1290a
vmovaps %xmm0, %xmm2
vmovss (%rsp), %xmm0
vucomiss 0x1d01c(%rip), %xmm0    # 0x336bc
jbe 0x16708
movq -0x18(%rbp), %rax
incq %rax
vcvtusi2ss %rax, %xmm3, %xmm0
vmovss 0x4(%rsp), %xmm1
vdivss %xmm0, %xmm1, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vucomisd 0x1bb8b(%rip), %xmm0    # 0x32250
jb 0x166e8
vsqrtsd %xmm0, %xmm0, %xmm0
jmp 0x166f9
cmpb 0x18(%rsp), %al
je 0x16731
xorl %edx, %edx
cmpb %cl, %al
sete %dl
leaq 0x1cfc7(%rip), %rax     # 0x336a8
vmovss (%rax,%rdx,4), %xmm2
jmp 0x16711
vmovss %xmm2, 0x10(%rsp)
callq 0x5250
vmovss 0x10(%rsp), %xmm2
vcvtss2sd %xmm2, %xmm2, %xmm1
vfmadd231sd 0x20(%rsp), %xmm0, %xmm1 # xmm1 = (xmm0 * mem) + xmm1
vcvtsd2ss %xmm1, %xmm1, %xmm2
decl %r13d
vmovss 0x1c(%rsp), %xmm0
vucomiss %xmm0, %xmm2
cmovaq %r12, %r15
cmpq %rbx, %rbp
je 0x16734
vmaxss %xmm0, %xmm2, %xmm0
addq $0x30, %rbp
testl %r13d, %r13d
jns 0x16655
jmp 0x16734
movq %r12, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq