// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]pentagod/agentpns.cpp
// Function: AgentPNS::PNSThread::pns(Board const&, AgentPNS::Node*, int, unsigned int, unsigned int)
bool AgentPNS::PNSThread::pns(const Board & board, Node * node, int depth, uint32_t tp, uint32_t td){
	iters++;
	if(agent->maxdepth < depth)
		agent->maxdepth = depth;

	if(node->children.empty()){
		if(node->terminal())
			return true;

		if(agent->ctmem.memalloced() >= agent->memlimit)
			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;
		unsigned int seen = 0;
		for(MoveIterator move(board); !move.done(); ++move){
			int outcome = solve1ply(move.board(), seen);
			unsigned int pd = 1;
			temp[i] = Node(*move).outcome(outcome, board.toplay(), agent->ties, pd);
			i++;
		}
		PLUS(agent->nodes, i);
		temp.shrink(i); //if symmetry, there may be extra moves to ignore
		node->children.swap(temp);
		assert(temp.unlock());

		PLUS(agent->nodes_seen, seen);

		updatePDnum(node);

		return true;
	}

	bool mem;
	do{
		Node * child = node->children.begin(),
		     * child2 = node->children.begin(),
		     * childend = node->children.end();

		uint32_t tpc, tdc;

		if(agent->df){
			for(Node * i = node->children.begin(); i != childend; i++){
				if(i->refdelta() <= child->refdelta()){
					child2 = child;
					child = i;
				}else if(i->refdelta() < child2->refdelta()){
					child2 = i;
				}
			}

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

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

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

		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
movl %r9d, 0x1c(%rsp)
movl %r8d, 0x20(%rsp)
movq %rdx, %rbx
movq %rdi, %r14
incq 0x40(%rdi)
movq 0x38(%rdi), %rax
movl %ecx, 0x10(%rsp)
cmpl %ecx, 0x9e0(%rax)
jge 0x22ce1
movl 0x10(%rsp), %ecx
movl %ecx, 0x9e0(%rax)
leaq 0x18(%rbx), %r12
movq 0x18(%rbx), %rcx
cmpq $0x2, %rcx
movq %rsi, 0x28(%rsp)
jb 0x22ee7
cmpw $0x0, 0x6(%rcx)
je 0x22ee7
incl 0x10(%rsp)
movq %r14, 0x58(%rsp)
movq %r12, 0x50(%rsp)
movq (%r12), %rdx
leaq 0x10(%rdx), %rcx
movl $0x0, %ebp
movl $0x0, %eax
cmpq $0x2, %rdx
cmovaeq %rcx, %rbp
jb 0x22d3a
movzwl 0x6(%rdx), %eax
shll $0x5, %eax
addq %rax, %rcx
movq %rcx, %rax
movq 0x38(%r14), %rcx
cmpb $0x0, 0xad0(%rcx)
je 0x22de5
movq %rbp, %rdx
movq %rbp, %rsi
cmpq %rax, %rbp
je 0x22d99
movzwl 0x10(%rsi), %edi
addl 0x4(%rsi), %edi
movzwl 0x10(%rbp), %r8d
addl 0x4(%rbp), %r8d
cmpl %r8d, %edi
jbe 0x22d87
movzwl 0x10(%rdx), %r9d
addl 0x4(%rdx), %r9d
movq %rsi, %r8
cmpl %r9d, %edi
jb 0x22d7f
movq %rdx, %r8
movq %rbp, %rdi
movq %r8, %rbp
jmp 0x22d8a
movq %rsi, %rdi
movq %rbp, %rdx
addq $0x20, %rsi
movq %rdi, %rbp
cmpq %rax, %rsi
jne 0x22d56
movl (%rbp), %r15d
addl 0x1c(%rsp), %r15d
subl 0x4(%rbx), %r15d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
cmpl %eax, %r15d
cmovael %eax, %r15d
vcvtusi2sdl 0x4(%rdx), %xmm3, %xmm0
vcvtss2sd 0xad4(%rcx), %xmm3, %xmm1
vmovsd 0xa5df(%rip), %xmm2     # 0x2d3a8
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %r12d
movl 0x20(%rsp), %eax
cmpl %eax, %r12d
cmovael %eax, %r12d
jmp 0x22e1a
xorl %r15d, %r15d
movq %rbp, %rcx
cmpq %rax, %rbp
je 0x22e17
movzwl 0x10(%rcx), %esi
addl 0x4(%rcx), %esi
movzwl 0x10(%rbp), %edi
addl 0x4(%rbp), %edi
movq %rbp, %rdx
cmpl %edi, %esi
ja 0x22e08
movq %rcx, %rdx
addq $0x20, %rbp
movq %rdx, %rcx
cmpq %rax, %rbp
jne 0x22df0
movq %rdx, %rbp
xorl %r12d, %r12d
movq 0x28(%rsp), %rcx
movq 0x20(%rcx), %rax
movq %rax, 0x80(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x60(%rsp)
movl 0x14(%rbp), %esi
leaq 0x60(%rsp), %r14
movq %r14, %rdi
vzeroupper
callq 0x9240
lock  incw 0x10(%rbp)
movq 0x58(%rsp), %rdi
movq 0x40(%rdi), %r13
movq %r14, %rsi
movq %rdi, %r14
movq %rbp, %rdx
movl 0x10(%rsp), %ecx
movl %r15d, %r8d
movl %r12d, %r9d
callq 0x22ca2
movl %eax, %r15d
lock  decw 0x10(%rbp)
movq 0x40(%r14), %rax
subq %r13, %rax
lock
addq %rax, 0x8(%rbp)
movq %rbx, %rsi
callq 0x234ee
testb %al, %al
je 0x22e9f
movq 0x38(%r14), %rax
cmpb $0x1, 0xad0(%rax)
jne 0x230f4
movq 0x38(%r14), %rax
movb 0x8(%rax), %cl
testb %r15b, %r15b
je 0x230f4
testb %cl, %cl
jne 0x230f4
cmpb $0x1, 0xad0(%rax)
movq 0x50(%rsp), %r12
jne 0x22d11
movl 0x20(%rsp), %eax
cmpl %eax, (%rbx)
jae 0x230f4
movl 0x1c(%rsp), %eax
cmpl %eax, 0x4(%rbx)
jb 0x22d11
jmp 0x230f4
cmpl $0x0, (%rbx)
movb $0x1, %r15b
je 0x230f4
cmpl $0x0, 0x4(%rbx)
je 0x230f4
movq 0x58(%rax), %rcx
movq 0x60(%rax), %rdx
movl 0x8(%rdx), %edx
shlq $0x18, %rdx
movl 0x10(%rcx), %ecx
addq %rdx, %rcx
cmpq 0x40(%rax), %rcx
jae 0x23061
xorl %r15d, %r15d
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r12)
jne 0x230f4
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0xda1e
leaq 0x8(%rsp), %rdi
movq $0x0, (%rdi)
movq 0x38(%r14), %rdx
addq $0x50, %rdx
movl %eax, %esi
callq 0x24160
movq %rbx, 0x10(%rsp)
movq %r12, %rbx
movl $0x0, 0x24(%rsp)
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xd316
xorl %r15d, %r15d
leaq 0x30(%rsp), %r13
leaq 0x60(%rsp), %r12
xorl %ebp, %ebp
cmpb $0x24, 0x90(%rsp)
jge 0x23069
leaq 0x68(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0x241a6
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movw $0x0, 0x40(%rsp)
movl 0x90(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movq %r15, 0x48(%rsp)
movq 0x38(%r14), %rcx
movl 0xad8(%rcx), %ecx
testl %ecx, %ecx
cmovel %eax, %ecx
testl %eax, %eax
cmovnel %eax, %ecx
cmpl $-0x3, %ecx
jne 0x22ffb
movl $0x1, 0x30(%rsp)
movl $0x1, %eax
jmp 0x2302f
movq 0x28(%rsp), %rax
movzbl 0x19(%rax), %eax
cmpl %eax, %ecx
jne 0x23014
movl $0x3fffffff, 0x30(%rsp) # imm = 0x3FFFFFFF
xorl %eax, %eax
jmp 0x2302f
movl $0x3, %edx
subl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
sete %al
movl $0x0, 0x30(%rsp)
orl $0x3ffffffe, %eax       # imm = 0x3FFFFFFE
movl %eax, 0x34(%rsp)
leaq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x24270
movq %rax, %rdi
movq %r13, %rsi
callq 0x242ce
movq %r13, %rdi
callq 0xd77e
incl %ebp
movq %r12, %rdi
callq 0xd402
jmp 0x22f8b
xorl %r15d, %r15d
jmp 0x230f4
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2307b
callq 0x43d0
movq 0x38(%r14), %rax
movl %ebp, %ecx
lock
addq %rcx, 0x38(%rax)
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x25c06
movq (%rbx), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rbx)
movq %rax, 0x8(%rsp)
cmpq $0x1, %rcx
jbe 0x230ac
movq %rbx, 0x8(%rcx)
cmpq $0x2, %rax
movq 0x10(%rsp), %rsi
jb 0x230c0
leaq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x8(%rsp)
jne 0x23109
movq 0x38(%r14), %rax
movl 0x24(%rsp), %ecx
lock
addq %rcx, 0x9e8(%rax)
callq 0x234ee
cmpq $0x0, 0x8(%rsp)
jne 0x23171
movb $0x1, %r15b
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa08d(%rip), %rdi      # 0x2d19d
leaq 0xa3c9(%rip), %rsi      # 0x2d4e0
leaq 0xa47f(%rip), %rcx      # 0x2d59d
movl $0xab, %edx
callq 0x41b0
jmp 0x2312a
movq %rax, %rbx
jmp 0x23161
jmp 0x23140
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xd77e
jmp 0x23143
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x23155
callq 0x43d0
movq $0x0, 0xa8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x23171
movq %rbx, %rdi
callq 0x45b0
leaq 0x7013(%rip), %rdi      # 0x2a18b
leaq 0x6f76(%rip), %rsi      # 0x2a0f5
leaq 0x7012(%rip), %rcx      # 0x2a198
movl $0x73, %edx
callq 0x41b0