// File: 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:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1a8(%rsp)
movq %rsi, 0x1a0(%rsp)
movq %rdx, 0x198(%rsp)
movl %ecx, 0x194(%rsp)
movl %r8d, 0x190(%rsp)
movl %r9d, 0x18c(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x40(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rax)
movq 0x38(%rax), %rax
movl 0x9e0(%rax), %eax
cmpl 0x194(%rsp), %eax
jge 0x342f8
movq 0x60(%rsp), %rax
movl 0x194(%rsp), %ecx
movq 0x38(%rax), %rax
movl %ecx, 0x9e0(%rax)
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x16910
testb $0x1, %al
jne 0x34312
jmp 0x3469b
movq 0x198(%rsp), %rdi
callq 0x35c90
testb $0x1, %al
jne 0x34325
jmp 0x34332
movb $0x1, 0x1b7(%rsp)
jmp 0x34aab
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x50, %rdi
callq 0x35cd0
movq 0x60(%rsp), %rcx
movq 0x38(%rcx), %rcx
cmpq 0x40(%rcx), %rax
jb 0x34360
movb $0x0, 0x1b7(%rsp)
jmp 0x34aab
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x364b0
testb $0x1, %al
jne 0x34382
movb $0x0, 0x1b7(%rsp)
jmp 0x34aab
movq 0x1a0(%rsp), %rdi
callq 0x16f50
movl %eax, 0x188(%rsp)
leaq 0x180(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x168f0
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %rdi
movl 0x188(%rsp), %esi
movq 0x38(%rax), %rdx
addq $0x50, %rdx
callq 0x364f0
jmp 0x343c8
movl $0x0, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
movq 0x1a0(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x15b10
jmp 0x343fa
jmp 0x343fc
leaq 0x118(%rsp), %rdi
callq 0x15c80
movb %al, 0x57(%rsp)
jmp 0x3440f
movb 0x57(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3445d
leaq 0x118(%rsp), %rdi
callq 0x15e30
jmp 0x345b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0x34689
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
jmp 0x3459f
leaq 0x118(%rsp), %rdi
callq 0x32d50
movq %rax, 0x48(%rsp)
jmp 0x34471
movq 0x48(%rsp), %rdi
leaq 0x16c(%rsp), %rsi
callq 0x36560
movl %eax, 0x44(%rsp)
jmp 0x34489
movl 0x44(%rsp), %eax
movl %eax, 0x114(%rsp)
movl $0x1, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x32cd0
movq %rax, 0x38(%rsp)
jmp 0x344b3
movq 0x38(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
movl $0x1, %edx
callq 0x366c0
jmp 0x344cc
movl 0x114(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x17190
movb %al, 0x37(%rsp)
jmp 0x344ea
movl 0x30(%rsp), %esi
movq 0x60(%rsp), %rax
movb 0x37(%rsp), %cl
movzbl %cl, %edx
movq 0x38(%rax), %rax
movl 0xad8(%rax), %ecx
movl 0x110(%rsp), %r8d
leaq 0xf0(%rsp), %rdi
callq 0x36710
movq %rax, 0x28(%rsp)
jmp 0x34520
movl 0x170(%rsp), %esi
leaq 0x180(%rsp), %rdi
callq 0x367c0
movq %rax, 0x20(%rsp)
jmp 0x3453b
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x36870
jmp 0x3454c
leaq 0xf0(%rsp), %rdi
callq 0x163b0
movl 0x170(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x170(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x15cb0
jmp 0x34579
jmp 0x343fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x163b0
leaq 0x118(%rsp), %rdi
callq 0x15e30
jmp 0x34689
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x38, %rdi
leaq 0x170(%rsp), %rsi
callq 0x2edb0
jmp 0x345cd
movl 0x170(%rsp), %esi
leaq 0x180(%rsp), %rdi
callq 0x36900
jmp 0x345e3
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
leaq 0x180(%rsp), %rsi
callq 0x36930
jmp 0x345fe
leaq 0x180(%rsp), %rdi
callq 0x369b0
movb %al, 0x1f(%rsp)
jmp 0x34611
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x3461b
jmp 0x3461d
jmp 0x3463c
leaq 0xcad1(%rip), %rdi      # 0x410f5
leaq 0xcd41(%rip), %rsi      # 0x4136c
movl $0xab, %edx
leaq 0xcdf2(%rip), %rcx      # 0x41429
callq 0x4250
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x9e8, %rdi            # imm = 0x9E8
leaq 0x16c(%rsp), %rsi
callq 0x2edb0
jmp 0x3465b
movq 0x60(%rsp), %rdi
movq 0x198(%rsp), %rsi
callq 0x351c0
jmp 0x3466f
movb $0x1, 0x1b7(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x16930
jmp 0x34aab
leaq 0x180(%rsp), %rdi
callq 0x16930
jmp 0x34abc
jmp 0x3469d
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x369f0
movq %rax, 0xe0(%rsp)
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x369f0
movq %rax, 0xd8(%rsp)
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x36a40
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0xd0(%rsp)
movq 0x38(%rax), %rax
testb $0x1, 0xad0(%rax)
je 0x34887
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x369f0
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
je 0x347cf
movq 0xc0(%rsp), %rdi
callq 0x36a90
movl %eax, 0x18(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x36a90
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
ja 0x3477a
movq 0xe0(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0xe0(%rsp)
jmp 0x347b4
movq 0xc0(%rsp), %rdi
callq 0x36a90
movl %eax, 0x14(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x36a90
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jae 0x347b2
movq 0xc0(%rsp), %rax
movq %rax, 0xd8(%rsp)
jmp 0x347b4
jmp 0x347b6
movq 0xc0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xc0(%rsp)
jmp 0x3471a
movl $0x1ffffffe, 0xbc(%rsp) # imm = 0x1FFFFFFE
movl 0x18c(%rsp), %eax
movq 0xe0(%rsp), %rcx
movl (%rcx), %ecx
addl %ecx, %eax
movq 0x198(%rsp), %rcx
movl 0x4(%rcx), %ecx
subl %ecx, %eax
movl %eax, 0xb8(%rsp)
leaq 0xbc(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x36ab0
movq %rax, %rcx
movq 0x60(%rsp), %rax
movl (%rcx), %ecx
movl %ecx, 0xcc(%rsp)
movq 0xd8(%rsp), %rcx
vcvtusi2sdl 0x4(%rcx), %xmm0, %xmm1
movq 0x38(%rax), %rax
vmovss 0xad4(%rax), %xmm2
vcvtss2sd %xmm2, %xmm0, %xmm0
vmovsd 0xcab2(%rip), %xmm2     # 0x41300
vaddsd %xmm2, %xmm0, %xmm0
vfmadd213sd %xmm2, %xmm1, %xmm0 # xmm0 = (xmm1 * xmm0) + xmm2
vcvttsd2usi %xmm0, %eax
movl %eax, 0xb4(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x36ab0
movl (%rax), %eax
movl %eax, 0xc8(%rsp)
jmp 0x3491a
movl $0x0, 0xc8(%rsp)
movl $0x0, 0xcc(%rsp)
movq 0x198(%rsp), %rdi
addq $0x18, %rdi
callq 0x369f0
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
cmpq 0xd0(%rsp), %rax
je 0x34918
movq 0xe0(%rsp), %rdi
callq 0x36a90
movl %eax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x36a90
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jbe 0x34900
movq 0xa8(%rsp), %rax
movq %rax, 0xe0(%rsp)
jmp 0x34902
movq 0xa8(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xa8(%rsp)
jmp 0x348b6
jmp 0x3491a
movq 0x1a0(%rsp), %rsi
leaq 0x80(%rsp), %rdi
movl $0x28, %edx
callq 0x4300
movq 0xe0(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %esi
leaq 0x80(%rsp), %rdi
callq 0xd040
movq 0xe0(%rsp), %rdi
callq 0x36af0
movq 0x60(%rsp), %rdi
movq 0x40(%rdi), %rax
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %rdx
movl 0x194(%rsp), %ecx
addl $0x1, %ecx
movl 0xcc(%rsp), %r8d
movl 0xc8(%rsp), %r9d
leaq 0x80(%rsp), %rsi
callq 0x34280
andb $0x1, %al
movb %al, 0xef(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x36b20
movq 0x60(%rsp), %rax
movq 0xe0(%rsp), %rdi
addq $0x8, %rdi
movq 0x40(%rax), %rax
subq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rsi
callq 0x33230
movq 0x60(%rsp), %rdi
movq 0x198(%rsp), %rsi
callq 0x351c0
testb $0x1, %al
jne 0x349f5
jmp 0x34a0c
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rax
testb $0x1, 0xad0(%rax)
jne 0x34a0c
jmp 0x34a9b
jmp 0x34a0e
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rax
movb 0x8(%rax), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0xf(%rsp)
jne 0x34a8f
xorl %eax, %eax
testb $0x1, 0xef(%rsp)
movb %al, 0xf(%rsp)
je 0x34a8f
movq 0x60(%rsp), %rax
movq 0x38(%rax), %rcx
movb $0x1, %al
testb $0x1, 0xad0(%rcx)
movb %al, 0xe(%rsp)
je 0x34a87
movq 0x198(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl 0x190(%rsp), %ecx
movb %al, 0xd(%rsp)
jae 0x34a7f
movq 0x198(%rsp), %rax
movl 0x4(%rax), %eax
cmpl 0x18c(%rsp), %eax
setb %al
movb %al, 0xd(%rsp)
movb 0xd(%rsp), %al
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x3469d
movb 0xef(%rsp), %al
andb $0x1, %al
movb %al, 0x1b7(%rsp)
movb 0x1b7(%rsp), %al
andb $0x1, %al
addq $0x1b8, %rsp            # imm = 0x1B8
retq
movq 0x178(%rsp), %rdi
callq 0x4660
nopl (%rax)