// File: /workspace/github_cpp_makefile_star2_O3/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
vcvtusi2sdq 0x18(%rsi), %xmm0, %xmm0
movl %ecx, %r12d
movl %edx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
callq 0x5260
movq 0x40(%rbx), %rax
vcvtsd2ss %xmm0, %xmm0, %xmm0
vxorps %xmm1, %xmm1, %xmm1
movl $0x271, %r13d           # imm = 0x271
movq %rbx, 0x8(%rsp)
vmovss %xmm0, 0x4(%rsp)
vmovss 0x98(%rax), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x16670
vmovss 0x4(%rsp), %xmm0
vdivss 0x9c(%rax), %xmm0, %xmm0
vcvttss2si %xmm0, %r13d
addl $0x2, %r13d
movq 0x8(%rsp), %rcx
cmpb $0x1, 0x74(%rax)
movsbl 0x2f15(%rcx), %ecx
vcvtsi2ss %ecx, %xmm2, %xmm0
vmulss 0x78(%rax), %xmm0, %xmm0
vmovss %xmm0, (%rsp)
jne 0x166c6
movq 0x18(%r15), %rcx
testq %rcx, %rcx
je 0x166b4
vcvtusi2ssq 0x10(%r15), %xmm2, %xmm0
vmulss 0x1bb50(%rip), %xmm0, %xmm0 # 0x321f8
vcvtusi2ss %rcx, %xmm2, %xmm1
vdivss %xmm1, %xmm0, %xmm0
jmp 0x166b8
vxorps %xmm0, %xmm0, %xmm0
vmovss (%rsp), %xmm1
vmulss %xmm0, %xmm1, %xmm1
vmovss %xmm1, (%rsp)
movq 0x28(%r15), %rdx
xorl %ebx, %ebx
movl $0x0, %ebp
leaq 0x10(%rdx), %rcx
cmpq $0x2, %rdx
cmovaeq %rcx, %rbp
jb 0x166f0
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 0x16832
testl %r13d, %r13d
js 0x16832
vmovss 0x80(%rax), %xmm0
movq 0x8(%rsp), %rcx
leaq 0x1fc98(%rip), %r14     # 0x363b1
addq $0x30, %rbp
xorl %r15d, %r15d
movsbl 0x2f14(%rcx), %ecx
vcvtsi2ss %r12d, %xmm2, %xmm1
vfmadd213ss 0x7c(%rax), %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + mem
vcvtsi2ss %ecx, %xmm2, %xmm2
vmulss %xmm2, %xmm1, %xmm0
vmovss %xmm0, 0x14(%rsp)
vcvtss2sd (%rsp), %xmm3, %xmm0
vmovsd %xmm0, 0x20(%rsp)
vmovss 0x1cf65(%rip), %xmm0    # 0x336b8
movb -0xe(%rbp), %al
movb (%r14), %cl
leaq -0x30(%rbp), %r12
cmpb %cl, %al
jge 0x167cb
movq 0x8(%rsp), %rax
vmovss %xmm0, 0x1c(%rsp)
vmovss 0x14(%rsp), %xmm0
movq %r12, %rdi
movq 0x40(%rax), %rax
vmovss 0x90(%rax), %xmm1
movzbl 0x84(%rax), %esi
callq 0x12a46
vmovss (%rsp), %xmm1
vmovaps %xmm0, %xmm2
vucomiss 0x1cf1e(%rip), %xmm1    # 0x336bc
jbe 0x16806
movq -0x18(%rbp), %rax
vmovss 0x4(%rsp), %xmm1
incq %rax
vcvtusi2ss %rax, %xmm3, %xmm0
vdivss %xmm0, %xmm1, %xmm0
vcvtss2sd %xmm0, %xmm0, %xmm0
vucomisd 0x1ba8d(%rip), %xmm0    # 0x32250
jb 0x167e6
vsqrtsd %xmm0, %xmm0, %xmm0
jmp 0x167f7
cmpb 0x18(%rsp), %al
je 0x1682f
xorl %edx, %edx
cmpb %cl, %al
leaq 0x1cecc(%rip), %rax     # 0x336a8
sete %dl
vmovss (%rax,%rdx,4), %xmm2
jmp 0x1680f
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
vmovss 0x1c(%rsp), %xmm0
decl %r13d
vucomiss %xmm0, %xmm2
cmovaq %r12, %r15
cmpq %rbx, %rbp
je 0x16832
vmaxss %xmm0, %xmm2, %xmm0
addq $0x30, %rbp
testl %r13d, %r13d
jns 0x16753
jmp 0x16832
movq %r12, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq