// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/gomoku/agentpns.cpp
// Function: Morat::Gomoku::AgentPNS::AgentThread::pns(Morat::Gomoku::Board const&, Morat::Gomoku::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;

		CompactTree<Node>::Children temp;
		temp.alloc(board.moves_avail(), agent->ctmem);

		unsigned int i = 0;
		for (auto move : board) {
			unsigned int pd;
			Outcome outcome;

			if(agent->ab){
				Board next = board;
				next.move(move);

				pd = 0;
				outcome = (agent->ab == 1 ? solve1ply(next, pd) : solve2ply(next, pd));
			}else{
				pd = 1;
				outcome = board.test_outcome(move);
			}

			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 $0x88, %rsp
movq %rsi, 0x18(%rsp)
movq 0x18(%rdx), %rsi
leaq 0x18(%rdx), %rax
movl %r9d, 0xc(%rsp)
movl %r8d, 0x10(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, %r14
movq %rdi, %rbx
movq %rax, 0x28(%rsp)
cmpq $0x2, %rsi
jb 0x13b58
cmpw $0x0, 0x6(%rsi)
je 0x13b58
movq 0x40(%rbx), %rax
movq %rbx, 0x30(%rsp)
incl 0x4(%rsp)
movq 0x30(%rsp), %r15
movb 0x1524(%rax), %dil
leaq 0x10(%rsi), %rdx
cmpq $0x2, %rsi
movl $0x0, %ecx
cmovaeq %rdx, %rcx
testb $0x1, %dil
je 0x1399a
cmpq $0x2, %rsi
jb 0x139b3
movzwl 0x6(%rsi), %esi
shll $0x5, %esi
addq %rsi, %rdx
jmp 0x139b5
cmpq $0x2, %rsi
jb 0x13a48
movzwl 0x6(%rsi), %eax
shll $0x5, %eax
addq %rax, %rdx
jmp 0x13a4a
xorl %edx, %edx
movq %rcx, %rsi
movq %rcx, %rbp
cmpq %rdx, %rcx
je 0x139fa
movzwl 0x10(%rcx), %r8d
movzwl 0x10(%rbp), %edi
addl 0x4(%rcx), %r8d
addl 0x4(%rbp), %edi
cmpl %edi, %r8d
jbe 0x139eb
movzwl 0x10(%rsi), %r9d
movq %rcx, %rdi
addl 0x4(%rsi), %r9d
cmpl %r9d, %r8d
jb 0x139f1
movq %rsi, %rdi
jmp 0x139f1
movq %rbp, %rdi
movq %rcx, %rbp
addq $0x20, %rcx
movq %rdi, %rsi
jmp 0x139bb
vcvtusi2sdl 0x4(%rsi), %xmm3, %xmm0
vmovsd 0xed7f(%rip), %xmm2     # 0x22788
movl (%rbp), %r12d
movl $0x1ffffffe, %ecx       # imm = 0x1FFFFFFE
addl 0xc(%rsp), %r12d
subl 0x4(%r14), %r12d
vcvtss2sd 0x1528(%rax), %xmm3, %xmm1
movl 0x10(%rsp), %eax
cmpl %ecx, %r12d
cmovael %ecx, %r12d
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ecx
cmpl %eax, %ecx
cmovael %eax, %ecx
movl %ecx, 0x14(%rsp)
jmp 0x13a84
xorl %edx, %edx
movq %rcx, %rax
cmpq %rdx, %rax
je 0x13a76
movzwl 0x10(%rcx), %edi
movzwl 0x10(%rax), %r8d
movq %rax, %rsi
addl 0x4(%rcx), %edi
addl 0x4(%rax), %r8d
cmpl %r8d, %edi
ja 0x13a6d
movq %rcx, %rsi
addq $0x20, %rax
movq %rsi, %rcx
jmp 0x13a4d
movl $0x0, 0x14(%rsp)
xorl %r12d, %r12d
movq %rcx, %rbp
movq 0x18(%rsp), %rsi
leaq 0x48(%rsp), %rbx
movq %r14, %r13
movq %rbx, %rdi
callq 0x9812
leaq 0x12(%rbp), %rsi
movq %rbx, %rdi
pushq $0x1
popq %rcx
movl %ecx, %edx
callq 0xe0ba
lock  incw 0x10(%rbp)
movq 0x68(%r15), %r14
movl 0x4(%rsp), %ecx
movl 0x14(%rsp), %r9d
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movl %r12d, %r8d
callq 0x1390c
lock  decw 0x10(%rbp)
movl %eax, %r12d
movq %r13, %rsi
movq 0x68(%r15), %rax
subq %r14, %rax
movq %r13, %r14
lock
addq %rax, 0x8(%rbp)
callq 0x13e0e
testb %al, %al
je 0x13b01
movq 0x40(%r15), %rax
cmpb $0x0, 0x1524(%rax)
je 0x13d81
leaq 0x48(%rsp), %rdi
callq 0x82f6
movq 0x40(%r15), %rax
movb 0x8(%rax), %cl
testb %r12b, %r12b
je 0x13d8b
testb %cl, %cl
jne 0x13d8b
movb 0x1524(%rax), %dil
cmpb $0x1, %dil
jne 0x13b4b
movl 0x10(%rsp), %ecx
cmpl %ecx, (%r14)
jae 0x13d8b
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4(%r14)
jae 0x13d8b
movq 0x28(%rsp), %rcx
movq (%rcx), %rsi
jmp 0x13971
movl 0x4(%rsp), %esi
leaq 0x48(%rbx), %rdi
callq 0x121da
cmpl $0x0, (%r14)
movb $0x1, %r12b
je 0x13d8b
cmpl $0x0, 0x4(%r14)
je 0x13d8b
movq 0x40(%rbx), %rdi
callq 0x149ea
xorl %r12d, %r12d
testb %al, %al
jne 0x13d8b
pushq $0x1
popq %rcx
movq 0x28(%rsp), %rdx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%rdx)
jne 0x13d8b
movq 0x18(%rsp), %r15
leaq 0x20(%rsp), %rdi
leaq 0x10f72(%rip), %rcx     # 0x24b29
andq $0x0, (%rdi)
movswl 0x2(%r15), %edx
movswl 0x4(%r15), %esi
movb 0x9(%r15), %al
subl %esi, %edx
cmpb (%rcx), %al
cmovll %edx, %r12d
movq 0x40(%rbx), %rdx
addq $0x68, %rdx
movl %r12d, %esi
callq 0x14a08
movq %r15, %rdi
callq 0xe65e
leaq 0x38(%rsp), %rcx
leaq 0x48(%rsp), %r13
xorl %ebp, %ebp
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
pushq $0x1
popq %r15
cmpw $-0x1, 0x42(%rsp)
movq 0x18(%rsp), %rsi
je 0x13d14
movl 0x40(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1520(%rax)
je 0x13c65
movq %r13, %rdi
callq 0x9812
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
movl %r15d, %edx
movl %r15d, %ecx
callq 0xee08
andl $0x0, (%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1520(%rax)
jne 0x13c7e
movq %r13, %rdi
movq %rsp, %rsi
callq 0x14a4e
jmp 0x13c89
movl $0x1, (%rsp)
movq %rsi, %rdi
leaq 0x8(%rsp), %rsi
callq 0x121fa
movl %eax, %r12d
jmp 0x13c94
movq %r13, %rdi
movq %rsp, %rsi
callq 0x14afa
movq %r13, %rdi
movl %eax, %r12d
callq 0x82f6
movzwl 0x8(%rsp), %ecx
movabsq $0x100000001, %rax      # imm = 0x100000001
movl (%rsp), %r8d
movq %rax, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
andw $0x0, 0x58(%rsp)
movw %cx, 0x5a(%rsp)
andq $0x0, 0x60(%rsp)
movq 0x18(%rsp), %rcx
movq 0x40(%rbx), %rax
movl 0x8(%rcx), %edx
movl 0x152c(%rax), %ecx
movzbl %r12b, %esi
movq %r13, %rdi
callq 0x14bda
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rax, %r12
callq 0x14c36
movq %rax, %rdi
movq %r12, %rsi
callq 0x14c94
movq %r13, %rdi
callq 0x147fa
incl %ebp
leaq 0x38(%rsp), %rdi
callq 0xe67e
jmp 0x13c06
movl %ebp, %eax
addq %rax, 0x68(%rbx)
leaq 0x20(%rsp), %r15
movl %ebp, %esi
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1510(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x50(%rcx)
movq (%r15), %rdi
callq 0x167d4
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x14cea
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%r15)
jne 0x13da0
movq %r14, %rsi
callq 0x13e0e
movq 0x40(%rbx), %rax
leaq 0x20(%rsp), %rdi
movq 0x1510(%rax), %rcx
cmpq 0x1518(%rax), %rcx
setae %r12b
callq 0x14d12
jmp 0x13d8b
leaq 0x48(%rsp), %rdi
callq 0x82f6
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe8d8(%rip), %rdi      # 0x2267f
leaq 0xea6a(%rip), %rsi      # 0x22818
leaq 0xeace(%rip), %rcx      # 0x22883
movl $0x83, %edx
callq 0x51f0
jmp 0x13dd8
jmp 0x13dd8
jmp 0x13dd8
jmp 0x13dc9
jmp 0x13dd8
leaq 0x48(%rsp), %rdi
movq %rax, %rbx
callq 0x82f6
jmp 0x13dfb
movq %rax, %rbx
jmp 0x13dfb
jmp 0x13ddf
leaq 0x48(%rsp), %rdi
movq %rax, %rbx
callq 0x82f6
jmp 0x13e05
leaq 0x48(%rsp), %rdi
movq %rax, %rbx
callq 0x147fa
leaq 0x20(%rsp), %rdi
callq 0x14d12
movq %rbx, %rdi
callq 0x5650
nop