// File: tewalds[P]pentagod/agentmcts.cpp
// Function: AgentMCTS::return_move(AgentMCTS::Node const*, int, int) const
Move AgentMCTS::return_move(const Node * node, int toplay, int verbose) const {
	if(node->outcome >= 0)
		return node->bestmove;

	double val, maxval = -1000000000000.0; //1 trillion

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

	for( ; child != end; child++){
		if(child->outcome >= 0){
			if(child->outcome == toplay) val =  800000000000.0 - child->exp.num(); //shortest win
			else if(child->outcome == 0) val = -400000000000.0 + child->exp.num(); //longest tie
			else                         val = -800000000000.0 + child->exp.num(); //longest loss
		}else{ //not proven
//			val = child->exp.num(); //num simulations
			val = child->exp.sum(); //num wins
		}

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

	assert(ret);

	if(verbose)
		logerr("Score:       " + to_str(ret->exp.avg()*100., 2) + "% / " + to_str(ret->exp.num()) + "\n");

	return ret->move;
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movl %edx, 0x10c(%rsp)
movl %ecx, 0x108(%rsp)
movq 0x110(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x2be4d
movq 0x110(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x124(%rsp)
jmp 0x2c1f0
vmovsd 0x14753(%rip), %xmm0    # 0x405a8
vmovsd %xmm0, 0xf8(%rsp)
movq $0x0, 0xf0(%rsp)
movq 0x110(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc40
movq %rax, 0xe8(%rsp)
movq 0x110(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc90
movq %rax, 0xe0(%rsp)
movq 0xe8(%rsp), %rax
cmpq 0xe0(%rsp), %rax
je 0x2bfde
movq 0xe8(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x2bf6d
movq 0xe8(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl 0x10c(%rsp), %eax
jne 0x2bf06
movq 0xe8(%rsp), %rdi
callq 0x2e5d0
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x146c9(%rip), %xmm0    # 0x405c0
vsubsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x2bf6b
movq 0xe8(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jne 0x2bf41
movq 0xe8(%rsp), %rdi
callq 0x2e5d0
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x14686(%rip), %xmm0    # 0x405b8
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x2bf69
movq 0xe8(%rsp), %rdi
callq 0x2e5d0
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x14654(%rip), %xmm0    # 0x405b0
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x2bf6b
jmp 0x2bf89
movq 0xe8(%rsp), %rdi
callq 0x2e5e0
vcvtusi2sd %rax, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
vmovsd 0xf8(%rsp), %xmm1
vmovsd 0x100(%rsp), %xmm0
vucomisd %xmm1, %xmm0
jbe 0x2bfc3
vmovsd 0x100(%rsp), %xmm0
vmovsd %xmm0, 0xf8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0xf0(%rsp)
jmp 0x2bfc5
movq 0xe8(%rsp), %rax
addq $0x28, %rax
movq %rax, 0xe8(%rsp)
jmp 0x2be9c
cmpq $0x0, 0xf0(%rsp)
je 0x2bfeb
jmp 0x2c00a
leaq 0x14973(%rip), %rdi     # 0x40965
leaq 0x146d9(%rip), %rsi     # 0x406d2
movl $0x17f, %edx            # imm = 0x17F
leaq 0x14964(%rip), %rcx     # 0x40969
callq 0x4250
cmpl $0x0, 0x108(%rsp)
je 0x2c1de
movq 0xf0(%rsp), %rdi
callq 0x2e600
vmovaps %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd 0x12263(%rip), %xmm1    # 0x3e298
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x2, %esi
callq 0x1e870
movq 0x8(%rsp), %rdx
leaq 0x1494a(%rip), %rsi     # 0x409a3
leaq 0x60(%rsp), %rdi
callq 0xe5f0
jmp 0x2c065
leaq 0x14945(%rip), %rdx     # 0x409b1
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x93b0
jmp 0x2c080
movq 0xf0(%rsp), %rdi
callq 0x2e5d0
movq %rax, (%rsp)
jmp 0x2c093
movq (%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x161a0
jmp 0x2c0a3
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0xe470
jmp 0x2c0bf
leaq 0x119ea(%rip), %rdx     # 0x3dab0
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x93b0
jmp 0x2c0dd
leaq 0xc0(%rsp), %rdi
callq 0x15ad0
jmp 0x2c0ec
leaq 0xc0(%rsp), %rdi
callq 0x4210
leaq 0xa0(%rsp), %rdi
callq 0x4210
leaq 0x10(%rsp), %rdi
callq 0x4210
leaq 0x80(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
jmp 0x2c1de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2c1d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2c1c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2c1bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2c1b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2c1a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x4210
leaq 0xa0(%rsp), %rdi
callq 0x4210
leaq 0x10(%rsp), %rdi
callq 0x4210
leaq 0x80(%rsp), %rdi
callq 0x4210
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
jmp 0x2c1ff
movq 0xf0(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x124(%rsp)
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0x38(%rsp), %rdi
callq 0x4660
nopl (%rax)