// File: tewalds[P]morat/hex/agentmcts.cpp
// Function: Morat::Hex::AgentMCTS::return_move(Morat::Hex::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;

	assert(!node->children.empty());

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

	const Node * ret = NULL;
	for(const auto& child : node->children) {
		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
			if(msrave == -1) //num simulations
				val = child.exp.num();
			else if(msrave == -2) //num wins
				val = child.exp.sum();
			else
				val = child.value(msrave, 0, 0) - msexplore*sqrt(log(node->exp.num())/(child.exp.num() + 1));
		}

		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 $0x158, %rsp            # imm = 0x158
movb %dl, %al
movb %al, 0x155(%rsp)
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movl %ecx, 0x13c(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x140(%rsp), %rdi
addq $0x22, %rdi
leaq 0x549cc(%rip), %rsi     # 0x6d4c3
callq 0x1a8c0
testb $0x1, %al
jne 0x18b02
jmp 0x18b1b
movq 0x140(%rsp), %rax
movw 0x26(%rax), %ax
movw %ax, 0x156(%rsp)
jmp 0x1907e
movq 0x140(%rsp), %rdi
addq $0x28, %rdi
callq 0x1b500
xorb $-0x1, %al
testb $0x1, %al
jne 0x18b34
jmp 0x18b36
jmp 0x18b55
leaq 0x3490c(%rip), %rdi     # 0x4d449
leaq 0x348a3(%rip), %rsi     # 0x4d3e7
movl $0x140, %edx            # imm = 0x140
leaq 0x34911(%rip), %rcx     # 0x4d461
callq 0x52a0
vmovsd 0x34683(%rip), %xmm0    # 0x4d1e0
vmovsd %xmm0, 0x128(%rsp)
movq $0x0, 0x120(%rsp)
movq 0x140(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x1c340
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rdi
callq 0x1c390
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
cmpq 0x108(%rsp), %rax
je 0x18e56
movq 0x110(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rdi
addq $0x22, %rdi
leaq 0x548da(%rip), %rsi     # 0x6d4c3
callq 0x1a8c0
testb $0x1, %al
jne 0x18bf7
jmp 0x18cc3
movq 0x100(%rsp), %rdi
addq $0x22, %rdi
leaq 0x155(%rsp), %rsi
callq 0x1c7c0
testb $0x1, %al
jne 0x18c16
jmp 0x18c44
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x345c3(%rip), %xmm0    # 0x4d1f8
vsubsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18cbe
movq 0x100(%rsp), %rdi
addq $0x22, %rdi
leaq 0x5486c(%rip), %rsi     # 0x6d4c3
callq 0x1c810
testb $0x1, %al
jne 0x18c62
jmp 0x18c90
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x3456f(%rip), %xmm0    # 0x4d1f0
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18cbc
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vcvtusi2sd %rax, %xmm0, %xmm1
vmovsd 0x34539(%rip), %xmm0    # 0x4d1e8
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18cbe
jmp 0x18e01
movq 0x28(%rsp), %rax
vmovss 0x6c(%rax), %xmm0
vmovss 0x34547(%rip), %xmm1    # 0x4d21c
vucomiss %xmm1, %xmm0
jne 0x18d02
jp 0x18d02
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vcvtusi2sd %rax, %xmm0, %xmm0
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18dff
movq 0x28(%rsp), %rax
vmovss 0x6c(%rax), %xmm0
vmovss 0x34504(%rip), %xmm1    # 0x4d218
vucomiss %xmm1, %xmm0
jne 0x18d41
jp 0x18d41
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c850
vcvtusi2sd %rax, %xmm0, %xmm0
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18dfd
movq 0x28(%rsp), %rax
movq 0x100(%rsp), %rdi
vmovss 0x6c(%rax), %xmm0
xorl %esi, %esi
vpxor %xmm1, %xmm1, %xmm1
callq 0x1c870
movq 0x28(%rsp), %rax
vmovaps %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x20(%rsp)
vmovss 0x70(%rax), %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x18(%rsp)
movq 0x140(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vcvtusi2sd %rax, %xmm0, %xmm0
callq 0x5330
vmovsd %xmm0, 0x10(%rsp)
movq 0x100(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
vmovsd 0x10(%rsp), %xmm0
addq $0x1, %rax
vcvtusi2sd %rax, %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
callq 0x5320
vmovsd 0x18(%rsp), %xmm1
vmovsd 0x20(%rsp), %xmm2
vmovq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
vmovq %rax, %xmm1
vfmadd213sd %xmm2, %xmm1, %xmm0 # xmm0 = (xmm1 * xmm0) + xmm2
vmovsd %xmm0, 0x130(%rsp)
jmp 0x18dff
jmp 0x18e01
vmovsd 0x128(%rsp), %xmm1
vmovsd 0x130(%rsp), %xmm0
vucomisd %xmm1, %xmm0
jbe 0x18e3b
vmovsd 0x130(%rsp), %xmm0
vmovsd %xmm0, 0x128(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0x18e3d
movq 0x110(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x110(%rsp)
jmp 0x18bb0
cmpq $0x0, 0x120(%rsp)
je 0x18e63
jmp 0x18e82
leaq 0x3463e(%rip), %rdi     # 0x4d4a8
leaq 0x34576(%rip), %rsi     # 0x4d3e7
movl $0x159, %edx            # imm = 0x159
leaq 0x345e4(%rip), %rcx     # 0x4d461
callq 0x52a0
cmpl $0x0, 0x13c(%rsp)
je 0x1906a
movq 0x120(%rsp), %rdi
addq $0x10, %rdi
callq 0x1cab0
vmovaps %xmm0, %xmm1
vcvtss2sd %xmm1, %xmm0, %xmm0
vmovsd 0x34327(%rip), %xmm1    # 0x4d1d8
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x2, %esi
callq 0x48780
movq 0x8(%rsp), %rdx
leaq 0x345d7(%rip), %rsi     # 0x4d4ac
leaq 0x80(%rsp), %rdi
callq 0x19f30
jmp 0x18ee4
leaq 0x345cf(%rip), %rdx     # 0x4d4ba
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xb910
jmp 0x18f02
movq 0x120(%rsp), %rdi
addq $0x10, %rdi
callq 0x1c800
movq %rax, (%rsp)
jmp 0x18f19
movq (%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x1a9f0
jmp 0x18f29
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x19e40
jmp 0x18f45
leaq 0x3399c(%rip), %rdx     # 0x4c8e8
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xb910
jmp 0x18f63
leaq 0xe0(%rsp), %rdi
callq 0x1a9b0
jmp 0x18f72
leaq 0xe0(%rsp), %rdi
callq 0x5260
leaq 0xc0(%rsp), %rdi
callq 0x5260
leaq 0x30(%rsp), %rdi
callq 0x5260
leaq 0xa0(%rsp), %rdi
callq 0x5260
leaq 0x80(%rsp), %rdi
callq 0x5260
leaq 0x60(%rsp), %rdi
callq 0x5260
jmp 0x1906a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x1905e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x19051
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x19044
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x1903a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x1902d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x5260
leaq 0xc0(%rsp), %rdi
callq 0x5260
leaq 0x30(%rsp), %rdi
callq 0x5260
leaq 0xa0(%rsp), %rdi
callq 0x5260
leaq 0x80(%rsp), %rdi
callq 0x5260
leaq 0x60(%rsp), %rdi
callq 0x5260
jmp 0x1908e
movq 0x120(%rsp), %rax
movw 0x24(%rax), %ax
movw %ax, 0x156(%rsp)
movw 0x156(%rsp), %ax
addq $0x158, %rsp            # imm = 0x158
retq
movq 0x58(%rsp), %rdi
callq 0x5760
nopl (%rax,%rax)