// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/pentago/agentpns.cpp
// Function: Morat::Pentago::AgentPNS::AgentThread::pns(Morat::Pentago::Board const&, Morat::Pentago::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;

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

		unsigned int i = 0;
		for(MoveIterator move(board); !move.done(); ++move){
			unsigned int pd;
			Outcome outcome;

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

				pd = 0;
				outcome = solve1ply(move.board(), pd);
			}else{
				pd = 1;
				outcome = move.board().outcome();
			}

			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 $0xb8, %rsp
movq %rsi, %r12
movq 0x18(%rdx), %rsi
leaq 0x18(%rdx), %rax
movl %r9d, 0x8(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, (%rsp)
movq %rdx, %r14
movq %rdi, %rbx
movq %rax, 0x18(%rsp)
cmpq $0x2, %rsi
jb 0x13313
cmpw $0x0, 0x6(%rsi)
je 0x13313
movq 0x40(%rbx), %rcx
incl (%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movb 0x150c(%rcx), %dil
leaq 0x10(%rsi), %rdx
cmpq $0x2, %rsi
movl $0x0, %eax
cmovaeq %rdx, %rax
testb $0x1, %dil
je 0x13160
cmpq $0x2, %rsi
jb 0x13179
movzwl 0x6(%rsi), %esi
shll $0x5, %esi
addq %rsi, %rdx
jmp 0x1317b
cmpq $0x2, %rsi
jb 0x1320b
movzwl 0x6(%rsi), %ecx
shll $0x5, %ecx
addq %rcx, %rdx
jmp 0x1320d
xorl %edx, %edx
movq %rax, %rsi
movq %rax, %r13
cmpq %rdx, %rax
je 0x131c2
movzwl 0x10(%rax), %r8d
movzwl 0x10(%r13), %edi
addl 0x4(%rax), %r8d
addl 0x4(%r13), %edi
cmpl %edi, %r8d
jbe 0x131b3
movzwl 0x10(%rsi), %r9d
movq %rax, %rdi
addl 0x4(%rsi), %r9d
cmpl %r9d, %r8d
jb 0x131b9
movq %rsi, %rdi
jmp 0x131b9
movq %r13, %rdi
movq %rax, %r13
addq $0x20, %rax
movq %rdi, %rsi
jmp 0x13181
vcvtusi2sdl 0x4(%rsi), %xmm3, %xmm0
vmovsd 0xe8d7(%rip), %xmm2     # 0x21aa8
movl (%r13), %ebp
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
addl 0x8(%rsp), %ebp
subl 0x4(%r14), %ebp
vcvtss2sd 0x1510(%rcx), %xmm3, %xmm1
cmpl %eax, %ebp
cmovael %eax, %ebp
movl 0xc(%rsp), %eax
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %r15d
cmpl %eax, %r15d
cmovael %eax, %r15d
jmp 0x1323d
xorl %edx, %edx
movq %rax, %r13
cmpq %rdx, %rax
je 0x13238
movzwl 0x10(%r13), %esi
movzwl 0x10(%rax), %edi
movq %rax, %rcx
addl 0x4(%r13), %esi
addl 0x4(%rax), %edi
cmpl %edi, %esi
ja 0x1322f
movq %r13, %rcx
addq $0x20, %rax
movq %rcx, %r13
jmp 0x13210
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x28(%rsp), %rcx
leaq 0x60(%rsp), %rbx
movq %r14, %r12
movq %rbx, %rdi
movq 0x20(%rcx), %rax
movq %rax, 0x80(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x60(%rsp)
movzwl 0x12(%r13), %esi
vzeroupper
callq 0xad6a
lock  incw 0x10(%r13)
movq %rbx, %rsi
movq %r13, %rdx
movl %ebp, %r8d
movl %r15d, %r9d
movq 0x20(%rsp), %rdi
movl (%rsp), %ecx
movq 0x68(%rdi), %r14
movq %rdi, %rbx
callq 0x130d6
lock  decw 0x10(%r13)
movl %eax, %ebp
movq %r12, %rsi
movq 0x68(%rbx), %rax
subq %r14, %rax
lock
addq %rax, 0x8(%r13)
callq 0x1356c
movq 0x40(%rbx), %rcx
movq %r12, %r14
testb %al, %al
je 0x132ca
cmpb $0x0, 0x150c(%rcx)
je 0x134fa
movb 0x8(%rcx), %al
testb %bpl, %bpl
je 0x134fa
testb %al, %al
jne 0x134fa
movb 0x150c(%rcx), %dil
cmpb $0x1, %dil
jne 0x13306
movl 0xc(%rsp), %eax
cmpl %eax, (%r14)
jae 0x134fa
movl 0x8(%rsp), %eax
cmpl %eax, 0x4(%r14)
jae 0x134fa
movq 0x18(%rsp), %rax
movq (%rax), %rsi
jmp 0x13137
movl (%rsp), %esi
leaq 0x48(%rbx), %rdi
callq 0x12258
cmpl $0x0, (%r14)
movb $0x1, %bpl
je 0x134fa
cmpl $0x0, 0x4(%r14)
je 0x134fa
movq 0x40(%rbx), %rdi
callq 0x140e6
xorl %ebp, %ebp
testb %al, %al
jne 0x134fa
pushq $0x1
popq %rcx
movq 0x18(%rsp), %rdx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%rdx)
jne 0x134fa
movq %r12, %rdi
callq 0xb90a
leaq 0x10(%rsp), %rdi
andq $0x0, (%rdi)
movq 0x40(%rbx), %rdx
addq $0x50, %rdx
movl %eax, %esi
callq 0x14104
leaq 0x60(%rsp), %rdi
pushq $-0x1
popq %rdx
movq %r12, %rsi
callq 0xb2ac
leaq 0x30(%rsp), %r13
xorl %ebp, %ebp
cmpb $0x23, 0x90(%rsp)
jg 0x13482
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1508(%rax)
je 0x133ef
movq 0x20(%r12), %rax
movl 0x90(%rsp), %esi
movq %rax, 0x50(%rsp)
vmovups (%r12), %ymm0
vmovups %ymm0, 0x30(%rsp)
movq %r13, %rdi
vzeroupper
callq 0xad6a
andl $0x0, 0x4(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x1414a
jmp 0x13401
movl $0x1, 0x4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xa9d0
movzwl 0x90(%rsp), %edx
movabsq $0x100000001, %rcx      # imm = 0x100000001
movl 0x4(%rsp), %r8d
movq %rcx, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
andw $0x0, 0x40(%rsp)
movw %dx, 0x42(%rsp)
andq $0x0, 0x48(%rsp)
movq 0x40(%rbx), %rcx
movzbl 0x19(%r12), %edx
movl 0x1514(%rcx), %ecx
movzbl %al, %esi
movq %r13, %rdi
callq 0x14210
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
movq %rax, %r15
callq 0x1426c
movq %rax, %rdi
movq %r15, %rsi
callq 0x142ca
movq %r13, %rdi
callq 0x13ef6
incl %ebp
leaq 0x60(%rsp), %rdi
callq 0xb372
jmp 0x13396
leaq 0x98(%rsp), %rdi
callq 0xb916
movl %ebp, %eax
addq %rax, 0x68(%rbx)
leaq 0x10(%rsp), %r15
movl %ebp, %esi
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x14f8(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x38(%rcx)
movq (%r15), %rdi
callq 0x15dea
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x14320
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%r15)
jne 0x1350e
movq %r14, %rsi
callq 0x1356c
movq 0x40(%rbx), %rax
leaq 0x10(%rsp), %rdi
movq 0x14f8(%rax), %rcx
cmpq 0x1500(%rax), %rcx
setae %bpl
callq 0x14348
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe4c5(%rip), %rdi      # 0x219da
leaq 0xe61c(%rip), %rsi      # 0x21b38
leaq 0xe678(%rip), %rcx      # 0x21b9b
movl $0x85, %edx
callq 0x5220
jmp 0x1352f
movq %rax, %rbx
jmp 0x13559
jmp 0x1353a
jmp 0x1353a
jmp 0x1353a
movq %rax, %rbx
jmp 0x1354c
leaq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0x13ef6
leaq 0x98(%rsp), %rdi
callq 0xb916
leaq 0x10(%rsp), %rdi
callq 0x14348
movq %rbx, %rdi
callq 0x5690
nop