// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq %rdx, %r14
leaq 0x18(%rdx), %r15
movq 0x18(%rdx), %rdx
movl %r9d, 0x20(%rsp)
movl %r8d, 0x24(%rsp)
movl %ecx, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x8(%rsp)
cmpq $0x2, %rdx
jb 0x19940
cmpw $0x0, 0x6(%rdx)
je 0x19940
movq 0x8(%rsp), %rax
incl 0x18(%rsp)
movq 0x8(%rsp), %rbp
movq %r15, 0x60(%rsp)
movq %r14, 0x68(%rsp)
movq 0x40(%rax), %rcx
movb 0x150c(%rcx), %sil
leaq 0x10(%rdx), %rax
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rax, %r12
testb $0x1, %sil
je 0x19778
cmpq $0x2, %rdx
jb 0x19791
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rax
jmp 0x19793
cmpq $0x2, %rdx
jb 0x1982c
movzwl 0x6(%rdx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
jmp 0x1982e
xorl %eax, %eax
movq %r12, %rdx
movq %r12, %rsi
cmpq %rax, %r12
je 0x197dd
movzwl 0x10(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rsi), %edi
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x197ce
movzwl 0x10(%r12), %r9d
movq %rsi, %r8
addl 0x4(%r12), %r9d
cmpl %r9d, %edi
jb 0x197c9
movq %r12, %r8
movq %r8, %r12
jmp 0x197d4
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rax, %rsi
jne 0x1979e
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vmovsd 0x1535b(%rip), %xmm2    # 0x2eb48
movl (%rdx), %r13d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
movq %rdx, %r12
addl 0x20(%rsp), %r13d
subl 0x4(%r14), %r13d
vcvtss2sd 0x1510(%rcx), %xmm3, %xmm1
cmpl %eax, %r13d
cmovael %eax, %r13d
movl 0x24(%rsp), %eax
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %r15d
cmpl %eax, %r15d
cmovael %eax, %r15d
jmp 0x1986c
xorl %eax, %eax
cmpq %rax, %r12
je 0x19866
movq %r12, %rcx
movzwl 0x10(%r12), %esi
movzwl 0x10(%rcx), %edi
movq %rcx, %rdx
addl 0x4(%r12), %esi
addl 0x4(%rcx), %edi
cmpl %edi, %esi
ja 0x19852
movq %r12, %rdx
addq $0x20, %rcx
movq %rdx, %r12
cmpq %rax, %rcx
jne 0x19836
xorl %r13d, %r13d
movq %rdx, %r12
jmp 0x19869
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x28(%rsp), %rcx
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rcx), %rax
movq %rax, 0x90(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x70(%rsp)
movzwl 0x12(%r12), %esi
vzeroupper
callq 0xd6ec
lock  incw 0x10(%r12)
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movl %r13d, %r8d
movl %r15d, %r9d
movl 0x18(%rsp), %ecx
movq 0x68(%rbp), %r14
callq 0x196e2
lock  decw 0x10(%r12)
movl %eax, %ebx
movq 0x68(%rbp), %rax
subq %r14, %rax
lock
addq %rax, 0x8(%r12)
movq 0x68(%rsp), %r14
movq %r14, %rsi
callq 0x19c82
movq 0x40(%rbp), %rcx
testb %al, %al
je 0x198f8
cmpb $0x0, 0x150c(%rcx)
je 0x19bef
movb 0x8(%rcx), %al
testb %bl, %bl
je 0x19bef
testb %al, %al
jne 0x19bef
movb 0x150c(%rcx), %sil
movq 0x60(%rsp), %rax
cmpb $0x1, %sil
jne 0x19938
movl 0x24(%rsp), %edx
cmpl %edx, (%r14)
jae 0x19bef
movl 0x20(%rsp), %edx
cmpl %edx, 0x4(%r14)
jae 0x19bef
movq (%rax), %rdx
jmp 0x1974e
movq 0x8(%rsp), %rax
movl 0x18(%rsp), %esi
leaq 0x48(%rax), %rdi
callq 0x17b9e
cmpl $0x0, (%r14)
movb $0x1, %bl
je 0x19bef
cmpl $0x0, 0x4(%r14)
je 0x19bef
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x60(%rax), %rdx
movq 0x58(%rax), %rcx
movl 0x8(%rdx), %edx
movl 0x10(%rcx), %ecx
shlq $0x18, %rdx
addq %rdx, %rcx
cmpq 0x40(%rax), %rcx
jae 0x19b48
movl $0x1, %ecx
xorl %ebx, %ebx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r15)
jne 0x19bef
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0xe682
leal (,%rax,8), %esi
movq 0x8(%rsp), %rax
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movq 0x40(%rax), %rdx
addq $0x50, %rdx
callq 0x1ac70
leaq 0x70(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r12, %rsi
callq 0xe03e
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %r13
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpb $0x24, 0xa0(%rsp)
jge 0x19b4f
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0x1508(%rax)
je 0x19a5b
movq 0x28(%rsp), %rcx
movl 0xa0(%rsp), %esi
movq 0x20(%rcx), %rax
movq %rax, 0x50(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x30(%rsp)
movq %r13, %rdi
vzeroupper
callq 0xd6ec
movl $0x0, 0x1c(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
callq 0x1acb6
jmp 0x19a86
movb 0x92(%rsp), %al
leaq 0x1833c(%rip), %rcx     # 0x31da5
movl $0x1, 0x1c(%rsp)
cmpb (%rcx), %al
jne 0x19a86
leaq 0x78(%rsp), %rdi
callq 0xe2a8
movb %al, 0x92(%rsp)
movzwl 0xa0(%rsp), %ecx
movq 0x8(%rsp), %rdx
movq %r12, 0x38(%rsp)
movw $0x0, 0x40(%rsp)
leaq 0x182fb(%rip), %rsi     # 0x31da1
movw %cx, 0x42(%rsp)
movq %r12, 0x48(%rsp)
movq 0x40(%rdx), %rcx
leaq 0x182ee(%rip), %rdx     # 0x31da9
cmpb %al, (%rdx)
movl 0x1514(%rcx), %ecx
movzbl %al, %edx
movl %ecx, %eax
cmovnel %edx, %eax
cmpb %cl, (%rsi)
leaq 0x182d2(%rip), %rcx     # 0x31da6
cmovel %edx, %eax
cmpb (%rcx), %al
jne 0x19ae5
movl 0x1c(%rsp), %ecx
movl %ecx, 0x30(%rsp)
jmp 0x19b16
movq 0x28(%rsp), %rcx
movb 0x19(%rcx), %cl
cmpb %cl, %al
jne 0x19afd
movl $0x3fffffff, 0x30(%rsp) # imm = 0x3FFFFFFF
xorl %ecx, %ecx
jmp 0x19b16
movb $0x3, %dl
movl $0x0, 0x30(%rsp)
subb %cl, %dl
xorl %ecx, %ecx
cmpb %dl, %al
sete %cl
orl $0x3ffffffe, %ecx       # imm = 0x3FFFFFFE
movl %ecx, 0x34(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ad98
movq %rax, %rdi
movq %r13, %rsi
callq 0x1adf6
movq %r13, %rdi
callq 0x1aa3e
incl %ebp
leaq 0x70(%rsp), %rdi
callq 0xe122
jmp 0x199f8
xorl %ebx, %ebx
jmp 0x19bef
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x19b61
callq 0x5490
movq 0x8(%rsp), %rdx
movl %ebp, %eax
movl %ebp, %esi
addq %rax, 0x68(%rdx)
movq 0x40(%rdx), %rcx
lock
addq %rax, 0x14f8(%rcx)
movq 0x40(%rdx), %rcx
lock
addq %rax, 0x38(%rcx)
movq 0x10(%rsp), %rdi
callq 0x1d0de
movq (%r15), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r15)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x19ba7
movq %r15, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x19bb1
movq %rbx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x19c03
movq %r14, %rsi
callq 0x19c82
cmpq $0x0, 0x10(%rsp)
jne 0x19c63
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rax
movq 0x14f8(%rax), %rcx
cmpq 0x1500(%rax), %rcx
setae %bl
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14e70(%rip), %rdi     # 0x2ea7a
leaq 0x150fb(%rip), %rsi     # 0x2ed0c
leaq 0x15157(%rip), %rcx     # 0x2ed6f
movl $0x85, %edx
callq 0x5200
jmp 0x19c24
movq %rax, %rbx
jmp 0x19c53
jmp 0x19c3e
jmp 0x19c3e
jmp 0x19c3e
leaq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0x1aa3e
jmp 0x19c41
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x19c53
callq 0x5490
cmpq $0x0, 0x10(%rsp)
jne 0x19c63
movq %rbx, %rdi
callq 0x56c0
leaq 0x13d5e(%rip), %rdi     # 0x2d9c8
leaq 0x13d64(%rip), %rsi     # 0x2d9d5
leaq 0x156c1(%rip), %rcx     # 0x2f339
movl $0x7a, %edx
callq 0x5200