// File: tewalds[P]morat/hex/agentpns.cpp
// Function: Morat::Hex::AgentPNS::return_move(Morat::Hex::AgentPNS::Node const*, Morat::Side, int) const
Move AgentPNS::return_move(const Node * node, Side to_play, int verbose) const {
	double val, maxval = -1000000000000.0; //1 trillion

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

	for( ; child != end; child++){
		Outcome outcome = child->to_outcome(to_play);
		if(outcome >= Outcome::DRAW){
			if(     outcome == +to_play)       val =  800000000000.0 - (double)child->work; //shortest win
			else if(outcome == Outcome::DRAW) val = -400000000000.0 + (double)child->work; //longest tie
			else                              val = -800000000000.0 + (double)child->work; //longest loss
		}else{ //not proven
			val = child->work;
		}

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

	assert(ret);

	if(verbose)
		logerr(ret->to_s() + "\n");

	return ret->move;
}

// Assembly:
subq $0xa8, %rsp
movb %dl, %al
movb %al, 0xa5(%rsp)
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movl %ecx, 0x8c(%rsp)
vmovsd 0x1e201(%rip), %xmm0    # 0x4d1e0
vmovsd %xmm0, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movq 0x90(%rsp), %rdi
addq $0x18, %rdi
callq 0x30e90
movq %rax, 0x68(%rsp)
movq 0x90(%rsp), %rdi
addq $0x18, %rdi
callq 0x30ee0
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
cmpq 0x60(%rsp), %rax
je 0x2f16a
movq 0x68(%rsp), %rdi
movb 0xa5(%rsp), %al
movb %al, 0x5e(%rsp)
movzbl 0x5e(%rsp), %esi
callq 0x30510
movb %al, 0x5f(%rsp)
leaq 0x5f(%rsp), %rdi
leaq 0x3e46f(%rip), %rsi     # 0x6d4c3
callq 0x1a8c0
testb $0x1, %al
jne 0x2f062
jmp 0x2f112
movb 0xa5(%rsp), %al
movb %al, 0x5c(%rsp)
movzbl 0x5c(%rsp), %edi
callq 0x1fba0
movb %al, 0x5d(%rsp)
leaq 0x5f(%rsp), %rdi
leaq 0x5d(%rsp), %rsi
callq 0x1c810
testb $0x1, %al
jne 0x2f090
jmp 0x2f0b3
movq 0x68(%rsp), %rax
vcvtusi2sdq 0x8(%rax), %xmm0, %xmm1
vmovsd 0x1e154(%rip), %xmm0    # 0x4d1f8
vsubsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x80(%rsp)
jmp 0x2f110
leaq 0x5f(%rsp), %rdi
leaq 0x3e404(%rip), %rsi     # 0x6d4c3
callq 0x1c810
testb $0x1, %al
jne 0x2f0ca
jmp 0x2f0ed
movq 0x68(%rsp), %rax
vcvtusi2sdq 0x8(%rax), %xmm0, %xmm1
vmovsd 0x1e112(%rip), %xmm0    # 0x4d1f0
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x80(%rsp)
jmp 0x2f10e
movq 0x68(%rsp), %rax
vcvtusi2sdq 0x8(%rax), %xmm0, %xmm1
vmovsd 0x1e0e7(%rip), %xmm0    # 0x4d1e8
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x80(%rsp)
jmp 0x2f110
jmp 0x2f127
movq 0x68(%rsp), %rax
vcvtusi2sdq 0x8(%rax), %xmm0, %xmm0
vmovsd %xmm0, 0x80(%rsp)
vmovsd 0x78(%rsp), %xmm1
vmovsd 0x80(%rsp), %xmm0
vucomisd %xmm1, %xmm0
jbe 0x2f155
vmovsd 0x80(%rsp), %xmm0
vmovsd %xmm0, 0x78(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2f157
movq 0x68(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x68(%rsp)
jmp 0x2f01a
cmpq $0x0, 0x70(%rsp)
je 0x2f174
jmp 0x2f193
leaq 0x1e32d(%rip), %rdi     # 0x4d4a8
leaq 0x1f6d6(%rip), %rsi     # 0x4e858
movl $0x123, %edx            # imm = 0x123
leaq 0x1f78b(%rip), %rcx     # 0x4e919
callq 0x52a0
cmpl $0x0, 0x8c(%rsp)
je 0x2f221
movq 0x70(%rsp), %rsi
leaq 0x18(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x2c960
movq (%rsp), %rsi
leaq 0x1d729(%rip), %rdx     # 0x4c8e8
leaq 0x38(%rsp), %rdi
callq 0xb910
jmp 0x2f1cb
leaq 0x38(%rsp), %rdi
callq 0x1a9b0
jmp 0x2f1d7
leaq 0x38(%rsp), %rdi
callq 0x5260
leaq 0x18(%rsp), %rdi
callq 0x5260
jmp 0x2f221
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10(%rsp)
movl %eax, 0xc(%rsp)
jmp 0x2f215
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10(%rsp)
movl %eax, 0xc(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x5260
leaq 0x18(%rsp), %rdi
callq 0x5260
jmp 0x2f242
movq 0x70(%rsp), %rax
movw 0x12(%rax), %ax
movw %ax, 0xa6(%rsp)
movw 0xa6(%rsp), %ax
addq $0xa8, %rsp
retq
movq 0x10(%rsp), %rdi
callq 0x5760
nopl (%rax)