// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/hex/agentpns.cpp
// Function: Morat::Hex::AgentPNS::AgentThread::pns(Morat::Hex::Board const&, Morat::Hex::AgentPNS::Node*, int, unsigned int, unsigned int)
bool AgentPNS::AgentThread::pns(const Board & board, Node * node, int depth, uint32_t tp, uint32_t td){
	// no children, create them
	if(node->children.empty()){
		treelen.add(depth);

		if(node->terminal())
			return true;

		if(agent->need_gc())
			return false;

		if(!node->children.lock())
			return false;

		CompactTree<Node>::Children temp;
		temp.alloc(board.moves_avail(), agent->ctmem);

		if(agent->lbdist)
			dists.run(&board);

		unsigned int i = 0;
		for (auto move : board) {
			unsigned int pd;
			Outcome outcome;

			if(agent->ab){
				Board next = board;
				next.move(move);

				pd = 0;
				outcome = (agent->ab == 1 ? solve1ply(next, pd) : solve2ply(next, pd));
			}else{
				pd = 1;
				outcome = board.test_outcome(move);
			}

			if(agent->lbdist && outcome != Outcome::UNKNOWN)
				pd = dists.get(move);

			temp[i] = Node(move).outcome(outcome, board.to_play(), agent->ties, pd);
			i++;
		}
		nodes_seen += i;
		PLUS(agent->nodes_seen, i);
		PLUS(agent->nodes, i);
		temp.shrink(i); //if symmetry, there may be extra moves to ignore
		node->children.swap(temp);
		assert(temp.unlock());

		updatePDnum(node);

		return (agent->nodes_seen >= agent->max_nodes_seen);
	}

	bool mem;
	do{
		Node * child = node->children.begin(), // the best move to explore
		     * child2 = node->children.begin();// second best for thresholds

		uint32_t tpc, tdc; // the thresholds

		if(agent->df){
			for(auto & i : node->children){
				if(i.refdelta() <= child->refdelta()){
					child2 = child;
					child = & i;
				}else if(i.refdelta() < child2->refdelta()){
					child2 = & i;
				}
			}

			tpc = std::min(INF32/2, (td + child->phi - node->delta));
			tdc = std::min(tp, (uint32_t)(child2->delta*(1.0 + agent->epsilon) + 1));
		}else{
			tpc = tdc = 0;
			for(auto & i : node->children)
				if(child->refdelta() > i.refdelta())
					child = & i;
		}

		Board next = board;
		next.move(child->move);

		child->ref();
		uint64_t seen_before = nodes_seen;
		mem = pns(next, child, depth + 1, tpc, tdc);
		child->deref();
		PLUS(child->work, nodes_seen - seen_before);

		if(updatePDnum(node) && !agent->df)
			break;

	}while(!agent->timeout && mem && (!agent->df || (node->phi < tp && node->delta < td)));

	return mem;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, 0x8(%rsp)
movq 0x18(%rdx), %rsi
leaq 0x18(%rdx), %rax
movl %r9d, 0x18(%rsp)
movl %r8d, 0x1c(%rsp)
movl %ecx, 0x10(%rsp)
movq %rdx, %r13
movq %rdi, %rbx
movq %rax, 0x30(%rsp)
cmpq $0x2, %rsi
jb 0x14a9f
cmpw $0x0, 0x6(%rsi)
je 0x14a9f
movq 0x40(%rbx), %rax
movq %rbx, 0x38(%rsp)
incl 0x10(%rsp)
movq 0x38(%rsp), %r14
movb 0x152c(%rax), %dil
leaq 0x10(%rsi), %rdx
cmpq $0x2, %rsi
movl $0x0, %ecx
cmovaeq %rdx, %rcx
testb $0x1, %dil
je 0x148e0
cmpq $0x2, %rsi
jb 0x148f9
movzwl 0x6(%rsi), %esi
shll $0x5, %esi
addq %rsi, %rdx
jmp 0x148fb
cmpq $0x2, %rsi
jb 0x1498e
movzwl 0x6(%rsi), %eax
shll $0x5, %eax
addq %rax, %rdx
jmp 0x14990
xorl %edx, %edx
movq %rcx, %rsi
movq %rcx, %rbp
cmpq %rdx, %rcx
je 0x14940
movzwl 0x10(%rcx), %r8d
movzwl 0x10(%rbp), %edi
addl 0x4(%rcx), %r8d
addl 0x4(%rbp), %edi
cmpl %edi, %r8d
jbe 0x14931
movzwl 0x10(%rsi), %r9d
movq %rcx, %rdi
addl 0x4(%rsi), %r9d
cmpl %r9d, %r8d
jb 0x14937
movq %rsi, %rdi
jmp 0x14937
movq %rbp, %rdi
movq %rcx, %rbp
addq $0x20, %rcx
movq %rdi, %rsi
jmp 0x14901
vcvtusi2sdl 0x4(%rsi), %xmm3, %xmm0
vmovsd 0xfdb9(%rip), %xmm2     # 0x24708
movl (%rbp), %r12d
movl $0x1ffffffe, %ecx       # imm = 0x1FFFFFFE
addl 0x18(%rsp), %r12d
subl 0x4(%r13), %r12d
vcvtss2sd 0x1530(%rax), %xmm3, %xmm1
movl 0x1c(%rsp), %eax
cmpl %ecx, %r12d
cmovael %ecx, %r12d
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ecx
cmpl %eax, %ecx
cmovael %eax, %ecx
movl %ecx, 0x20(%rsp)
jmp 0x149ca
xorl %edx, %edx
movq %rcx, %rax
cmpq %rdx, %rax
je 0x149bc
movzwl 0x10(%rcx), %edi
movzwl 0x10(%rax), %r8d
movq %rax, %rsi
addl 0x4(%rcx), %edi
addl 0x4(%rax), %r8d
cmpl %r8d, %edi
ja 0x149b3
movq %rcx, %rsi
addq $0x20, %rax
movq %rsi, %rcx
jmp 0x14993
movl $0x0, 0x20(%rsp)
xorl %r12d, %r12d
movq %rcx, %rbp
movq 0x8(%rsp), %rsi
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x9908
leaq 0x12(%rbp), %rsi
movq %rbx, %rdi
pushq $0x1
popq %rcx
movl %ecx, %edx
callq 0xe1ea
lock  incw 0x10(%rbp)
movq 0x9cc8(%r14), %r15
movl 0x10(%rsp), %ecx
movl 0x20(%rsp), %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl %r12d, %r8d
callq 0x14852
lock  decw 0x10(%rbp)
movl %eax, %r12d
movq %r13, %rsi
movq 0x9cc8(%r14), %rax
subq %r15, %rax
lock
addq %rax, 0x8(%rbp)
callq 0x14dc6
testb %al, %al
je 0x14a47
movq 0x40(%r14), %rax
cmpb $0x0, 0x152c(%rax)
je 0x14d38
leaq 0x50(%rsp), %rdi
callq 0x83bc
movq 0x40(%r14), %rax
movb 0x8(%rax), %cl
testb %r12b, %r12b
je 0x14d42
testb %cl, %cl
jne 0x14d42
movb 0x152c(%rax), %dil
cmpb $0x1, %dil
jne 0x14a92
movl 0x1c(%rsp), %ecx
cmpl %ecx, (%r13)
jae 0x14d42
movl 0x18(%rsp), %ecx
cmpl %ecx, 0x4(%r13)
jae 0x14d42
movq 0x30(%rsp), %rcx
movq (%rcx), %rsi
jmp 0x148b7
movl 0x10(%rsp), %esi
leaq 0x9ca8(%rbx), %rdi
callq 0x126b2
cmpl $0x0, (%r13)
movb $0x1, %r12b
je 0x14d42
cmpl $0x0, 0x4(%r13)
je 0x14d42
movq 0x40(%rbx), %rdi
callq 0x159a8
xorl %r12d, %r12d
testb %al, %al
jne 0x14d42
pushq $0x1
popq %rcx
movq 0x30(%rsp), %rdx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%rdx)
jne 0x14d42
movq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x123af(%rip), %rcx     # 0x26eb1
andq $0x0, (%rdi)
movb 0x9(%rsi), %al
movswl 0x2(%rsi), %edx
movswl 0x4(%rsi), %esi
subl %esi, %edx
cmpb (%rcx), %al
cmovll %edx, %r12d
movq 0x40(%rbx), %rdx
addq $0x70, %rdx
movl %r12d, %esi
callq 0x159c8
movq 0x40(%rbx), %rax
cmpb $0x1, 0x1535(%rax)
jne 0x14b51
leaq 0x1236f(%rip), %rax     # 0x26eac
leaq 0x48(%rbx), %rdi
movzbl (%rax), %ecx
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rsi
callq 0x126d2
movq 0x8(%rsp), %rdi
movq %r13, 0x10(%rsp)
callq 0xe78e
leaq 0x40(%rsp), %rcx
leaq 0x50(%rsp), %r13
xorl %ebp, %ebp
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
leaq 0x48(%rbx), %rax
movb %dl, 0xc(%rcx)
movq %rax, 0x20(%rsp)
pushq $0x1
popq %r15
cmpw $-0x1, 0x4a(%rsp)
je 0x14cc6
movl 0x48(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1528(%rax)
je 0x14be5
movq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x9908
leaq 0x4(%rsp), %rsi
movq %r13, %rdi
movl %r15d, %edx
movl %r15d, %ecx
callq 0xef38
andl $0x0, (%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1528(%rax)
jne 0x14c00
movq %r13, %rdi
movq %rsp, %rsi
callq 0x15a0e
jmp 0x14c0b
movl $0x1, (%rsp)
movq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x12774
movl %eax, %r12d
jmp 0x14c16
movq %r13, %rdi
movq %rsp, %rsi
callq 0x15aba
movq %r13, %rdi
movl %eax, %r12d
callq 0x83bc
movq 0x40(%rbx), %rcx
cmpb $0x1, 0x1535(%rcx)
jne 0x14c46
leaq 0x12284(%rip), %rax     # 0x26eae
cmpb (%rax), %r12b
je 0x14c46
movl 0x4(%rsp), %esi
movq 0x20(%rsp), %rdi
callq 0x15b9a
movl %eax, (%rsp)
movq 0x40(%rbx), %rcx
jmp 0x14c49
movl (%rsp), %eax
movabsq $0x100000001, %rdx      # imm = 0x100000001
movzwl 0x4(%rsp), %r9d
movq %rdx, 0x50(%rsp)
movq 0x8(%rsp), %rdx
andq $0x0, 0x58(%rsp)
andw $0x0, 0x60(%rsp)
movw %r9w, 0x62(%rsp)
andq $0x0, 0x68(%rsp)
movl 0x8(%rdx), %edx
movl 0x1534(%rcx), %ecx
movzbl %r12b, %esi
movq %r13, %rdi
movl %eax, %r8d
callq 0x15bee
leaq 0x28(%rsp), %rdi
movl %ebp, %esi
movq %rax, %r14
callq 0x15c4a
movq %rax, %rdi
movq %r14, %rsi
callq 0x15ca8
movq %r13, %rdi
callq 0x157b8
incl %ebp
leaq 0x40(%rsp), %rdi
callq 0xe7ae
jmp 0x14b86
movl %ebp, %eax
addq %rax, 0x9cc8(%rbx)
leaq 0x28(%rsp), %r14
movl %ebp, %esi
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1518(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x58(%rcx)
movq (%r14), %rdi
callq 0x1781a
movq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x15cfe
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%r14)
jne 0x14d57
movq 0x10(%rsp), %rsi
callq 0x14dc6
movq 0x40(%rbx), %rax
leaq 0x28(%rsp), %rdi
movq 0x1518(%rax), %rcx
cmpq 0x1520(%rax), %rcx
setae %r12b
callq 0x15d26
jmp 0x14d42
leaq 0x50(%rsp), %rdi
callq 0x83bc
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf8a0(%rip), %rdi      # 0x245fe
leaq 0xfa33(%rip), %rsi      # 0x24798
leaq 0xfa91(%rip), %rcx      # 0x247fd
movl $0x89, %edx
callq 0x51f0
jmp 0x14d91
jmp 0x14d91
jmp 0x14d91
jmp 0x14d91
jmp 0x14d91
jmp 0x14d82
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x83bc
jmp 0x14db4
movq %rax, %rbx
jmp 0x14db4
jmp 0x14d98
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x83bc
jmp 0x14dbe
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x157b8
leaq 0x28(%rsp), %rdi
callq 0x15d26
movq %rbx, %rdi
callq 0x5650