// File: tewalds[P]morat/pentago/agentmcts.cpp
// Function: Morat::Pentago::AgentMCTS::return_move(Morat::Pentago::AgentMCTS::Node const*, Morat::Side, int) const
Move AgentMCTS::return_move(const Node * node, Side to_play, int verbose) const {
	if(node->outcome >= Outcome::DRAW)
		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 >= Outcome::DRAW){
			if(child->outcome == to_play)             val =  800000000000.0 - child->exp.num(); //shortest win
			else if(child->outcome == Outcome::DRAW) 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
movb %dl, %al
movb %al, 0x125(%rsp)
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movl %ecx, 0x10c(%rsp)
movq 0x110(%rsp), %rdi
addq $0x12, %rdi
leaq 0x40e41(%rip), %rsi     # 0x5b81b
callq 0x140c0
testb $0x1, %al
jne 0x1a9e5
jmp 0x1a9fe
movq 0x110(%rsp), %rax
movw 0x16(%rax), %ax
movw %ax, 0x126(%rsp)
jmp 0x1adc6
vmovsd 0x29b0a(%rip), %xmm0    # 0x44510
vmovsd %xmm0, 0xf8(%rsp)
movq $0x0, 0xf0(%rsp)
movq 0x110(%rsp), %rdi
addq $0x18, %rdi
callq 0x1d7b0
movq %rax, 0xe8(%rsp)
movq 0x110(%rsp), %rdi
addq $0x18, %rdi
callq 0x1d800
movq %rax, 0xe0(%rsp)
movq 0xe8(%rsp), %rax
cmpq 0xe0(%rsp), %rax
je 0x1abb2
movq 0xe8(%rsp), %rdi
addq $0x12, %rdi
leaq 0x40da5(%rip), %rsi     # 0x5b81b
callq 0x140c0
testb $0x1, %al
jne 0x1aa84
jmp 0x1ab41
movq 0xe8(%rsp), %rdi
addq $0x12, %rdi
leaq 0x125(%rsp), %rsi
callq 0x1d9c0
testb $0x1, %al
jne 0x1aaa3
jmp 0x1aacd
movq 0xe8(%rsp), %rdi
callq 0x1da00
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x29a6a(%rip), %xmm0    # 0x44528
vsubsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x1ab3f
movq 0xe8(%rsp), %rdi
addq $0x12, %rdi
leaq 0x40d3b(%rip), %rsi     # 0x5b81b
callq 0x14ae0
testb $0x1, %al
jne 0x1aaeb
jmp 0x1ab15
movq 0xe8(%rsp), %rdi
callq 0x1da00
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x29a1a(%rip), %xmm0    # 0x44520
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x1ab3d
movq 0xe8(%rsp), %rdi
callq 0x1da00
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x299e8(%rip), %xmm0    # 0x44518
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
jmp 0x1ab3f
jmp 0x1ab5d
movq 0xe8(%rsp), %rdi
callq 0x1da10
vcvtusi2sd %rax, %xmm0, %xmm0
vmovsd %xmm0, 0x100(%rsp)
vmovsd 0xf8(%rsp), %xmm1
vmovsd 0x100(%rsp), %xmm0
vucomisd %xmm1, %xmm0
jbe 0x1ab97
vmovsd 0x100(%rsp), %xmm0
vmovsd %xmm0, 0xf8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0xf0(%rsp)
jmp 0x1ab99
movq 0xe8(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xe8(%rsp)
jmp 0x1aa4d
cmpq $0x0, 0xf0(%rsp)
je 0x1abbf
jmp 0x1abde
leaq 0x29b41(%rip), %rdi     # 0x44707
leaq 0x29ad7(%rip), %rsi     # 0x446a4
movl $0x11a, %edx            # imm = 0x11A
leaq 0x29b32(%rip), %rcx     # 0x4470b
callq 0x52d0
cmpl $0x0, 0x10c(%rsp)
je 0x1adb2
movq 0xf0(%rsp), %rdi
callq 0x1da30
vmovaps %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd 0x298ff(%rip), %xmm1    # 0x44508
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x2, %esi
callq 0x3fed0
movq 0x8(%rsp), %rdx
leaq 0x29b29(%rip), %rsi     # 0x44756
leaq 0x60(%rsp), %rdi
callq 0x14410
jmp 0x1ac39
leaq 0x29b24(%rip), %rdx     # 0x44764
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xaa40
jmp 0x1ac54
movq 0xf0(%rsp), %rdi
callq 0x1da00
movq %rax, (%rsp)
jmp 0x1ac67
movq (%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x14600
jmp 0x1ac77
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x14510
jmp 0x1ac93
leaq 0x28b8a(%rip), %rdx     # 0x43824
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xaa40
jmp 0x1acb1
leaq 0xc0(%rsp), %rdi
callq 0x143d0
jmp 0x1acc0
leaq 0xc0(%rsp), %rdi
callq 0x5290
leaq 0xa0(%rsp), %rdi
callq 0x5290
leaq 0x10(%rsp), %rdi
callq 0x5290
leaq 0x80(%rsp), %rdi
callq 0x5290
leaq 0x60(%rsp), %rdi
callq 0x5290
leaq 0x40(%rsp), %rdi
callq 0x5290
jmp 0x1adb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x1ada6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x1ad9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x1ad8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x1ad85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x1ad78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x38(%rsp)
movl %eax, 0x34(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x5290
leaq 0xa0(%rsp), %rdi
callq 0x5290
leaq 0x10(%rsp), %rdi
callq 0x5290
leaq 0x80(%rsp), %rdi
callq 0x5290
leaq 0x60(%rsp), %rdi
callq 0x5290
leaq 0x40(%rsp), %rdi
callq 0x5290
jmp 0x1add6
movq 0xf0(%rsp), %rax
movw 0x14(%rax), %ax
movw %ax, 0x126(%rsp)
movw 0x126(%rsp), %ax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0x38(%rsp), %rdi
callq 0x57a0