// File: /workspace/github_cpp_makefile_star2_O3/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
incq 0x40(%rdi)
movl %r9d, 0x24(%rsp)
movl %r8d, 0x28(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movq 0x38(%rdi), %r10
cmpl %ecx, 0x9e0(%r10)
jge 0x22634
movl 0x14(%rsp), %eax
movl %eax, 0x9e0(%r10)
movq 0x18(%rbx), %rdx
leaq 0x18(%rbx), %r12
movq %r13, 0x38(%rsp)
cmpq $0x2, %rdx
jb 0x22834
cmpw $0x0, 0x6(%rdx)
je 0x22834
incl 0x14(%rsp)
movq %r12, 0x30(%rsp)
leaq 0x10(%rdx), %rsi
movl $0x0, %eax
movl $0x0, %ebp
cmpq $0x2, %rdx
cmovaeq %rsi, %rbp
jb 0x22684
movzwl 0x6(%rdx), %eax
shll $0x5, %eax
addq %rax, %rsi
movq %rsi, %rax
cmpb $0x0, 0xad0(%r10)
je 0x2272d
movq %rbp, %rdx
movq %rbp, %rsi
cmpq %rax, %rbp
je 0x226e0
movzwl 0x10(%rsi), %edi
movzwl 0x10(%rbp), %r8d
addl 0x4(%rsi), %edi
addl 0x4(%rbp), %r8d
cmpl %r8d, %edi
jbe 0x226ce
movzwl 0x10(%rdx), %r9d
movq %rsi, %r8
addl 0x4(%rdx), %r9d
cmpl %r9d, %edi
jb 0x226c6
movq %rdx, %r8
movq %rbp, %rdi
movq %r8, %rbp
jmp 0x226d1
movq %rsi, %rdi
addq $0x20, %rsi
movq %rbp, %rdx
movq %rdi, %rbp
cmpq %rax, %rsi
jne 0x2269d
vcvtusi2sdl 0x4(%rdx), %xmm3, %xmm0
vmovsd 0x9cb9(%rip), %xmm2     # 0x2c3a8
movl (%rbp), %r15d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
addl 0x24(%rsp), %r15d
subl 0x4(%rbx), %r15d
vcvtss2sd 0xad4(%r10), %xmm3, %xmm1
cmpl %eax, %r15d
cmovael %eax, %r15d
movl 0x28(%rsp), %eax
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %r12d
cmpl %eax, %r12d
cmovael %eax, %r12d
jmp 0x22762
xorl %r15d, %r15d
movq %rbp, %rcx
cmpq %rax, %rbp
je 0x2275f
movzwl 0x10(%rcx), %esi
movzwl 0x10(%rbp), %edi
movq %rbp, %rdx
addl 0x4(%rcx), %esi
addl 0x4(%rbp), %edi
cmpl %edi, %esi
ja 0x22750
movq %rcx, %rdx
addq $0x20, %rbp
movq %rdx, %rcx
cmpq %rax, %rbp
jne 0x22738
movq %rdx, %rbp
xorl %r12d, %r12d
movq 0x20(%r13), %rax
leaq 0x60(%rsp), %rdi
movq %rbx, %r14
movq %rax, 0x80(%rsp)
vmovups (%r13), %ymm0
vmovups %ymm0, 0x60(%rsp)
movl 0x14(%rbp), %esi
vzeroupper
callq 0x9282
lock  incw 0x10(%rbp)
leaq 0x60(%rsp), %rsi
movq %rbp, %rdx
movl %r15d, %r8d
movl %r12d, %r9d
movq 0x18(%rsp), %rbx
movl 0x14(%rsp), %ecx
movq 0x40(%rbx), %r13
movq %rbx, %rdi
callq 0x225ee
lock  decw 0x10(%rbp)
movl %eax, %r15d
movq %r14, %rsi
movq 0x40(%rbx), %rax
subq %r13, %rax
lock
addq %rax, 0x8(%rbp)
callq 0x22e4a
movq 0x38(%rbx), %r10
movq %r14, %rbx
testb %al, %al
je 0x227ea
cmpb $0x0, 0xad0(%r10)
je 0x22a5b
movb 0x8(%r10), %al
testb %r15b, %r15b
je 0x22a5b
testb %al, %al
jne 0x22a5b
cmpb $0x1, 0xad0(%r10)
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %r13
jne 0x2282c
movl 0x28(%rsp), %edx
cmpl %edx, (%rbx)
jae 0x22a5b
movl 0x24(%rsp), %edx
cmpl %edx, 0x4(%rbx)
jae 0x22a5b
movq (%rax), %rdx
jmp 0x2265f
cmpl $0x0, (%rbx)
movb $0x1, %r15b
je 0x22a5b
cmpl $0x0, 0x4(%rbx)
je 0x22a5b
movq 0x60(%r10), %rdx
movq 0x58(%r10), %rax
movl 0x8(%rdx), %edx
movl 0x10(%rax), %eax
shlq $0x18, %rdx
addq %rdx, %rax
cmpq 0x40(%r10), %rax
jae 0x229c4
movl $0x1, %ecx
xorl %r15d, %r15d
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r12)
jne 0x22a5b
movb 0x1a(%r13), %al
cmpb $-0x4, %al
jne 0x22893
movq %r13, %rdi
callq 0xd9e4
movb %al, 0x1a(%r13)
movzbl 0x18(%r13), %ecx
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
leaq 0x8(%rsp), %rdi
movq $0x0, (%rdi)
shll $0x3, %ecx
subl %ecx, %edx
testb %al, %al
movq 0x18(%rsp), %rax
cmovsl %edx, %esi
movq 0x38(%rax), %rdx
addq $0x50, %rdx
callq 0x23a76
movq %r12, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
leaq 0x60(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r13, %rsi
callq 0xd282
leaq 0x8(%rsp), %r14
leaq 0x40(%rsp), %r13
leaq 0x60(%rsp), %r12
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpb $0x24, 0x90(%rsp)
jge 0x229cc
leaq 0x68(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x23abc
movl 0x90(%rsp), %ecx
movq %r15, 0x48(%rsp)
movw $0x0, 0x50(%rsp)
movl %ecx, 0x54(%rsp)
movq 0x18(%rsp), %rcx
movq %r15, 0x58(%rsp)
movq 0x38(%rcx), %rcx
movl 0xad8(%rcx), %ecx
testl %ecx, %ecx
cmovel %eax, %ecx
testl %eax, %eax
cmovnel %eax, %ecx
cmpl $-0x3, %ecx
jne 0x22960
movl $0x1, 0x40(%rsp)
movl $0x1, %eax
jmp 0x22994
movq 0x38(%rsp), %rax
movzbl 0x19(%rax), %eax
cmpl %eax, %ecx
jne 0x22979
movl $0x3fffffff, 0x40(%rsp) # imm = 0x3FFFFFFF
xorl %eax, %eax
jmp 0x22994
movl $0x3, %edx
movl $0x0, 0x40(%rsp)
subl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
sete %al
orl $0x3ffffffe, %eax       # imm = 0x3FFFFFFE
movl %eax, 0x44(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x23b80
movq %rax, %rdi
movq %r13, %rsi
callq 0x23bde
movq %r13, %rdi
callq 0xd6fe
incl %ebp
movq %r12, %rdi
callq 0xd39c
jmp 0x228fa
xorl %r15d, %r15d
jmp 0x22a5b
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x229de
callq 0x43d0
movq 0x18(%rsp), %rax
movl %ebp, %ecx
movl %ebp, %esi
movq 0x38(%rax), %rax
lock
addq %rcx, 0x38(%rax)
movq 0x8(%rsp), %rdi
callq 0x254b8
movq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rdx), %rax
movq %rcx, (%rdx)
movq %rax, 0x8(%rsp)
cmpq $0x2, %rcx
jb 0x22a19
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x22a23
movq %r14, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x8(%rsp)
jne 0x22a70
movq 0x18(%rsp), %rax
movl 0x2c(%rsp), %ecx
movq %rbx, %rsi
movq 0x38(%rax), %rax
lock
addq %rcx, 0x9e8(%rax)
callq 0x22e4a
cmpq $0x0, 0x8(%rsp)
jne 0x22acc
movb $0x1, %r15b
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9726(%rip), %rdi      # 0x2c19d
leaq 0x9a62(%rip), %rsi      # 0x2c4e0
leaq 0x9b18(%rip), %rcx      # 0x2c59d
movl $0xab, %edx
callq 0x41b0
jmp 0x22a91
movq %rax, %rbx
jmp 0x22abc
jmp 0x22aa7
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0xd6fe
jmp 0x22aaa
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x22abc
callq 0x43d0
cmpq $0x0, 0x8(%rsp)
jne 0x22acc
movq %rbx, %rdi
callq 0x45b0
leaq 0x66a8(%rip), %rdi      # 0x2917b
leaq 0x660b(%rip), %rsi      # 0x290e5
leaq 0x66a7(%rip), %rcx      # 0x29188
movl $0x73, %edx
callq 0x41b0
nop