// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
incq 0x40(%rdi)
movl %r9d, 0x4(%rsp)
movl %r8d, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movl %ecx, (%rsp)
movq 0x38(%rdi), %r10
cmpl %ecx, 0x9e0(%r10)
jge 0x1812e
movl (%rsp), %eax
movl %eax, 0x9e0(%r10)
movq 0x18(%rbx), %rdx
leaq 0x18(%rbx), %rax
movq %r15, 0x20(%rsp)
movq %rax, 0x18(%rsp)
cmpq $0x2, %rdx
jb 0x18320
cmpw $0x0, 0x6(%rdx)
je 0x18320
incl (%rsp)
movq %r14, 0x48(%rsp)
leaq 0x10(%rdx), %rsi
movl $0x0, %eax
movl $0x0, %ebp
cmpq $0x2, %rdx
cmovaeq %rsi, %rbp
jb 0x18182
movzwl 0x6(%rdx), %eax
shll $0x5, %eax
addq %rax, %rsi
movq %rsi, %rax
cmpb $0x0, 0xad0(%r10)
je 0x181d7
movq %rbp, %rdx
movq %rbp, %rsi
cmpq %rax, %rsi
je 0x18200
movzwl 0x10(%rsi), %edi
movzwl 0x10(%rbp), %r8d
addl 0x4(%rsi), %edi
addl 0x4(%rbp), %r8d
cmpl %r8d, %edi
jbe 0x181c8
movzwl 0x10(%rdx), %r9d
movq %rsi, %r8
addl 0x4(%rdx), %r9d
cmpl %r9d, %edi
jb 0x181c0
movq %rdx, %r8
movq %rbp, %rdi
movq %r8, %rbp
jmp 0x181cb
movq %rsi, %rdi
movq %rbp, %rdx
addq $0x20, %rsi
movq %rdi, %rbp
jmp 0x18192
movq %rbp, %rcx
cmpq %rax, %rcx
je 0x1824d
movzwl 0x10(%rbp), %esi
movzwl 0x10(%rcx), %edi
movq %rcx, %rdx
addl 0x4(%rbp), %esi
addl 0x4(%rcx), %edi
cmpl %edi, %esi
ja 0x181f7
movq %rbp, %rdx
addq $0x20, %rcx
movq %rdx, %rbp
jmp 0x181da
vcvtusi2sdl 0x4(%rdx), %xmm3, %xmm0
vmovsd 0x80f9(%rip), %xmm2     # 0x20308
movl (%rbp), %r12d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
addl 0x4(%rsp), %r12d
subl 0x4(%rbx), %r12d
vcvtss2sd 0xad4(%r10), %xmm3, %xmm1
cmpl %eax, %r12d
cmovael %eax, %r12d
movl 0x8(%rsp), %eax
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %r13d
cmpl %eax, %r13d
cmovael %eax, %r13d
jmp 0x18253
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%r15), %rax
leaq 0x50(%rsp), %rdi
movq %rbx, %r14
movq %rax, 0x70(%rsp)
vmovups (%r15), %ymm0
vmovups %ymm0, 0x50(%rsp)
movl 0x14(%rbp), %esi
vzeroupper
callq 0x7ada
lock  incw 0x10(%rbp)
leaq 0x50(%rsp), %rsi
movq %rbp, %rdx
movl %r12d, %r8d
movl %r13d, %r9d
movq 0x48(%rsp), %rbx
movl (%rsp), %ecx
movq 0x40(%rbx), %r15
movq %rbx, %rdi
callq 0x180ec
lock  decw 0x10(%rbp)
movl %eax, %r12d
movq %r14, %rsi
movq 0x40(%rbx), %rax
subq %r15, %rax
lock
addq %rax, 0x8(%rbp)
callq 0x18848
movq 0x38(%rbx), %r10
movq %r14, %rbx
testb %al, %al
je 0x182d6
cmpb $0x0, 0xad0(%r10)
je 0x184dd
movb 0x8(%r10), %al
testb %r12b, %r12b
je 0x184dd
testb %al, %al
jne 0x184dd
cmpb $0x1, 0xad0(%r10)
movq 0x20(%rsp), %r15
jne 0x18313
movl 0x8(%rsp), %eax
cmpl %eax, (%rbx)
jae 0x184dd
movl 0x4(%rsp), %eax
cmpl %eax, 0x4(%rbx)
jae 0x184dd
movq 0x18(%rsp), %rax
movq (%rax), %rdx
jmp 0x1815d
cmpl $0x0, (%rbx)
movb $0x1, %r12b
je 0x184dd
cmpl $0x0, 0x4(%rbx)
je 0x184dd
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 0x18473
xorl %r12d, %r12d
pushq $0x1
popq %rcx
movq 0x18(%rsp), %rdx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%rdx)
jne 0x184dd
movq %r15, %rdi
callq 0xb35c
leaq 0x10(%rsp), %rdi
andq $0x0, (%rdi)
movq 0x38(%r14), %rdx
addq $0x50, %rdx
movl %eax, %esi
callq 0x1913e
andl $0x0, 0xc(%rsp)
leaq 0x50(%rsp), %rdi
pushq $-0x1
popq %rdx
movq %r15, %rsi
callq 0xac78
xorl %ebp, %ebp
pushq $0x1
popq %r12
leaq 0x28(%rsp), %r13
leaq 0x50(%rsp), %r15
cmpb $0x23, 0x80(%rsp)
jg 0x18478
leaq 0x58(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x19182
movl 0x80(%rsp), %ecx
andq $0x0, 0x30(%rsp)
andw $0x0, 0x38(%rsp)
movl %ecx, 0x3c(%rsp)
andq $0x0, 0x40(%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 0x18412
movl $0x1, 0x28(%rsp)
movl %r12d, %eax
jmp 0x18441
movq 0x20(%rsp), %rax
movzbl 0x19(%rax), %eax
cmpl %eax, %ecx
jne 0x1842b
movl $0x3fffffff, 0x28(%rsp) # imm = 0x3FFFFFFF
xorl %eax, %eax
jmp 0x18441
pushq $0x3
popq %rdx
subl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
sete %al
andl $0x0, 0x28(%rsp)
orl $0x3ffffffe, %eax       # imm = 0x3FFFFFFE
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %eax, 0x2c(%rsp)
callq 0x1922c
movq %rax, %rdi
movq %r13, %rsi
callq 0x1928a
movq %r13, %rdi
callq 0xb06a
incl %ebp
movq %r15, %rdi
callq 0xad32
jmp 0x183b2
xorl %r12d, %r12d
jmp 0x184dd
leaq 0x88(%rsp), %rdi
callq 0xb368
movq 0x38(%r14), %rax
movl %ebp, %ecx
leaq 0x10(%rsp), %r15
movl %ebp, %esi
lock
addq %rcx, 0x38(%rax)
movq (%r15), %rdi
callq 0x1a396
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x192d8
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%r15)
jne 0x184f2
movq 0x38(%r14), %rax
movl 0xc(%rsp), %ecx
movq %rbx, %rsi
lock
addq %rcx, 0x9e8(%rax)
callq 0x18848
leaq 0x10(%rsp), %rdi
callq 0xb200
movb $0x1, %r12b
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7c04(%rip), %rdi      # 0x200fd
leaq 0x7e74(%rip), %rsi      # 0x20374
leaq 0x7f2a(%rip), %rcx      # 0x20431
movl $0xab, %edx
callq 0x41c0
jmp 0x18513
movq %rax, %rbx
jmp 0x18539
jmp 0x18529
leaq 0x28(%rsp), %rdi
movq %rax, %rbx
callq 0xb06a
jmp 0x1852c
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0xb368
leaq 0x10(%rsp), %rdi
callq 0xb200
movq %rbx, %rdi
callq 0x4590
nop