// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %r9d, 0x18(%rsp)
movl %r8d, 0x1c(%rsp)
movl %ecx, 0x10(%rsp)
movq %rdx, %r15
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
leaq 0x18(%rdx), %r14
movq 0x18(%rdx), %rax
cmpq $0x2, %rax
movq %r14, 0x28(%rsp)
jb 0x19b6e
cmpw $0x0, 0x6(%rax)
je 0x19b6e
incl 0x10(%rsp)
movq %rbx, 0x58(%rsp)
movq (%r14), %rdx
leaq 0x10(%rdx), %rax
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rax, %r12
movq 0x40(%rbx), %rcx
cmpb $0x1, 0x150c(%rcx)
jne 0x199a3
cmpq $0x2, %rdx
jb 0x199bc
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rax
jmp 0x199be
cmpq $0x2, %rdx
jb 0x19a52
movzwl 0x6(%rdx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
jmp 0x19a54
xorl %eax, %eax
movq %r12, %rdx
movq %r12, %rsi
cmpq %rax, %r12
je 0x19a08
movzwl 0x10(%rsi), %edi
addl 0x4(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x199f9
movzwl 0x10(%r12), %r9d
addl 0x4(%r12), %r9d
movq %rsi, %r8
cmpl %r9d, %edi
jb 0x199f4
movq %r12, %r8
movq %r8, %r12
jmp 0x199ff
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rax, %rsi
jne 0x199c9
movl (%rdx), %r14d
addl 0x18(%rsp), %r14d
subl 0x4(%r15), %r14d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
cmpl %eax, %r14d
cmovael %eax, %r14d
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vcvtss2sd 0x1510(%rcx), %xmm3, %xmm1
vmovsd 0x15110(%rip), %xmm2    # 0x2eb48
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ebp
movl 0x1c(%rsp), %eax
cmpl %eax, %ebp
cmovael %eax, %ebp
jmp 0x19a8d
xorl %eax, %eax
xorl %ebp, %ebp
cmpq %rax, %r12
je 0x19b66
movq %r12, %rcx
movzwl 0x10(%r12), %esi
addl 0x4(%r12), %esi
movzwl 0x10(%rcx), %edi
addl 0x4(%rcx), %edi
movq %rcx, %rdx
cmpl %edi, %esi
ja 0x19a7e
movq %r12, %rdx
addq $0x20, %rcx
movq %rdx, %r12
cmpq %rax, %rcx
jne 0x19a62
xorl %r14d, %r14d
movq %rdx, %r12
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
movq %rax, 0x80(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x60(%rsp)
movzwl 0x12(%r12), %esi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
vzeroupper
callq 0xd74e
lock  incw 0x10(%r12)
movq 0x58(%rsp), %rdi
movq 0x68(%rdi), %r13
movq %rbx, %rsi
movq %rdi, %rbx
movq %r12, %rdx
movl 0x10(%rsp), %ecx
movl %r14d, %r8d
movl %ebp, %r9d
callq 0x1991a
movl %eax, %ebp
lock  decw 0x10(%r12)
movq 0x68(%rbx), %rax
subq %r13, %rax
lock
addq %rax, 0x8(%r12)
movq %r15, %rsi
callq 0x19ebc
testb %al, %al
je 0x19b1c
movq 0x40(%rbx), %rax
cmpb $0x1, 0x150c(%rax)
jne 0x19e1c
movq 0x40(%rbx), %rax
movb 0x8(%rax), %cl
testb %bpl, %bpl
je 0x19e1c
testb %cl, %cl
jne 0x19e1c
cmpb $0x1, 0x150c(%rax)
movq 0x28(%rsp), %r14
jne 0x1996f
movl 0x1c(%rsp), %eax
cmpl %eax, (%r15)
jae 0x19e1c
movl 0x18(%rsp), %eax
cmpl %eax, 0x4(%r15)
jb 0x1996f
jmp 0x19e1c
xorl %r14d, %r14d
jmp 0x19a90
leaq 0x48(%rbx), %rdi
movl 0x10(%rsp), %esi
callq 0x17d98
cmpl $0x0, (%r15)
movb $0x1, %bpl
je 0x19e1c
cmpl $0x0, 0x4(%r15)
je 0x19e1c
movq 0x40(%rbx), %rax
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 0x19d74
xorl %ebp, %ebp
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r14)
jne 0x19e1c
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xe726
leal (,%rax,8), %esi
leaq 0x8(%rsp), %rdi
movq $0x0, (%rdi)
movq 0x40(%rbx), %rdx
addq $0x50, %rdx
callq 0x1af0a
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xe0ba
xorl %r14d, %r14d
leaq 0x30(%rsp), %r13
leaq 0x60(%rsp), %r12
xorl %ebp, %ebp
cmpb $0x24, 0x90(%rsp)
jge 0x19d7b
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1508(%rax)
je 0x19c76
movq 0x20(%rsp), %rcx
movq 0x20(%rcx), %rax
movq %rax, 0x50(%rsp)
vmovups (%rcx), %ymm0
vmovups %ymm0, 0x30(%rsp)
movl 0x90(%rsp), %esi
movq %r13, %rdi
vzeroupper
callq 0xd74e
movl $0x0, 0x14(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x1af50
jmp 0x19ca8
movl $0x1, 0x14(%rsp)
movb 0x82(%rsp), %al
leaq 0x18119(%rip), %rcx     # 0x31da5
cmpb (%rcx), %al
jne 0x19ca1
leaq 0x68(%rsp), %rdi
callq 0xe358
movb %al, 0x82(%rsp)
movb 0x82(%rsp), %al
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x30(%rsp)
movq %r14, 0x38(%rsp)
movw $0x0, 0x40(%rsp)
movzwl 0x90(%rsp), %ecx
movw %cx, 0x42(%rsp)
movq %r14, 0x48(%rsp)
movq 0x40(%rbx), %rcx
movl 0x1514(%rcx), %ecx
leaq 0x180c3(%rip), %rdx     # 0x31da9
cmpb %al, (%rdx)
movzbl %al, %edx
movl %ecx, %eax
cmovnel %edx, %eax
leaq 0x180aa(%rip), %rsi     # 0x31da1
cmpb %cl, (%rsi)
cmovel %edx, %eax
leaq 0x180a3(%rip), %rcx     # 0x31da6
cmpb (%rcx), %al
jne 0x19d11
movl 0x14(%rsp), %ecx
movl %ecx, 0x30(%rsp)
jmp 0x19d42
movq 0x20(%rsp), %rcx
movb 0x19(%rcx), %cl
cmpb %cl, %al
jne 0x19d29
movl $0x3fffffff, 0x30(%rsp) # imm = 0x3FFFFFFF
xorl %ecx, %ecx
jmp 0x19d42
movb $0x3, %dl
subb %cl, %dl
xorl %ecx, %ecx
cmpb %dl, %al
sete %cl
movl $0x0, 0x30(%rsp)
orl $0x3ffffffe, %ecx       # imm = 0x3FFFFFFE
movl %ecx, 0x34(%rsp)
leaq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x1b03e
movq %rax, %rdi
movq %r13, %rsi
callq 0x1b09c
movq %r13, %rdi
callq 0x1acd8
incl %ebp
movq %r12, %rdi
callq 0xe1b8
jmp 0x19c18
xorl %ebp, %ebp
jmp 0x19e1c
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x19d8d
callq 0x5490
movl %ebp, %eax
addq %rax, 0x68(%rbx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x14f8(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x38(%rcx)
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x1d3e6
movq 0x28(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rdx)
movq %rax, 0x8(%rsp)
cmpq $0x1, %rcx
jbe 0x19dd3
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x19de2
leaq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x8(%rsp)
jne 0x19e30
movq %r15, %rsi
callq 0x19ebc
cmpq $0x0, 0x8(%rsp)
jne 0x19e9c
movq 0x40(%rbx), %rax
movq 0x14f8(%rax), %rcx
cmpq 0x1500(%rax), %rcx
setae %bpl
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14c43(%rip), %rdi     # 0x2ea7a
leaq 0x14ece(%rip), %rsi     # 0x2ed0c
leaq 0x14f2a(%rip), %rcx     # 0x2ed6f
movl $0x85, %edx
callq 0x5200
jmp 0x19e51
movq %rax, %rbx
jmp 0x19e8c
jmp 0x19e6b
jmp 0x19e6b
jmp 0x19e6b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1acd8
jmp 0x19e6e
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x19e80
callq 0x5490
movq $0x0, 0xa8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x19e9c
movq %rbx, %rdi
callq 0x56c0
leaq 0x13b25(%rip), %rdi     # 0x2d9c8
leaq 0x13b2b(%rip), %rsi     # 0x2d9d5
leaq 0x15488(%rip), %rcx     # 0x2f339
movl $0x7a, %edx
callq 0x5200
nop