// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %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, %rbp
movq %rdi, %rbx
movq %rsi, 0x30(%rsp)
cmpq $0x2, %rdx
jb 0x199ef
cmpw $0x0, 0x6(%rdx)
je 0x199ef
leaq 0x10(%rbp), %rax
incl 0x18(%rsp)
movq %r15, 0x88(%rsp)
movq %rax, 0x90(%rsp)
movq 0x40(%rbx), %rax
movb 0x1524(%rax), %sil
leaq 0x10(%rdx), %rcx
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rcx, %r12
testb $0x1, %sil
je 0x19779
cmpq $0x2, %rdx
jb 0x19792
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rcx
jmp 0x19794
cmpq $0x2, %rdx
jb 0x1982c
movzwl 0x6(%rdx), %eax
shll $0x5, %eax
addq %rax, %rcx
jmp 0x1982e
xorl %ecx, %ecx
movq %r12, %rdx
movq %r12, %rsi
cmpq %rcx, %r12
je 0x197de
movzwl 0x10(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rsi), %edi
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x197cf
movzwl 0x10(%r12), %r9d
movq %rsi, %r8
addl 0x4(%r12), %r9d
cmpl %r9d, %edi
jb 0x197ca
movq %r12, %r8
movq %r8, %r12
jmp 0x197d5
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rcx, %rsi
jne 0x1979f
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vmovsd 0x16142(%rip), %xmm2    # 0x2f930
movl (%rdx), %r13d
movl $0x1ffffffe, %ecx       # imm = 0x1FFFFFFE
addl 0x20(%rsp), %r13d
subl 0x4(%r14), %r13d
vcvtss2sd 0x1528(%rax), %xmm3, %xmm1
movl 0x24(%rsp), %eax
cmpl %ecx, %r13d
cmovael %ecx, %r13d
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ecx
cmpl %eax, %ecx
cmovael %eax, %ecx
movl %ecx, 0x28(%rsp)
jmp 0x1986d
xorl %ecx, %ecx
movl $0x0, 0x28(%rsp)
cmpq %rcx, %r12
je 0x199e7
movq %r12, %rax
movzwl 0x10(%rax), %esi
movzwl 0x10(%r12), %edi
movq %r12, %rdx
addl 0x4(%rax), %esi
addl 0x4(%r12), %edi
cmpl %edi, %esi
ja 0x1985e
movq %rax, %rdx
addq $0x20, %r12
movq %rdx, %rax
cmpq %rcx, %r12
jne 0x19842
xorl %r13d, %r13d
movq %rdx, %r12
movzwl 0x8(%rbp), %eax
movq 0x90(%rsp), %rsi
leaq 0x48(%rsp), %rdi
movw %ax, 0x40(%rsp)
movq (%rbp), %rax
movq %rax, 0x38(%rsp)
callq 0xb1c0
movq 0x28(%rbp), %rax
leaq 0x38(%rsp), %rdi
movq %rax, 0x60(%rsp)
vmovups 0x30(%rbp), %xmm0
movq 0x38(%rbp), %rax
vmovups %xmm0, 0x68(%rsp)
testq %rax, %rax
je 0x198cb
movq 0x2b70b(%rip), %rcx     # 0x44fc8
cmpb $0x0, (%rcx)
je 0x198c7
incl 0x8(%rax)
jmp 0x198cb
lock
incl 0x8(%rax)
movsbl 0x12(%r12), %eax
movsbl 0x38(%rsp), %ecx
movq %r14, %r15
imull %eax, %ecx
movsbl 0x13(%r12), %eax
addl %ecx, %eax
movzwl 0x12(%r12), %ecx
movw %cx, 0x78(%rsp)
movw %ax, 0x7a(%rsp)
leaq 0x78(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
movq %rdi, %rbp
callq 0x1359e
lock  incw 0x10(%r12)
movq 0x68(%rbx), %r14
movl 0x18(%rsp), %ecx
movl 0x28(%rsp), %r9d
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %r13d, %r8d
callq 0x196e2
lock  decw 0x10(%r12)
movl %eax, %r13d
movq %r15, %rsi
movq 0x68(%rbx), %rax
subq %r14, %rax
movq %r15, %r14
lock
addq %rax, 0x8(%r12)
callq 0x19da4
testb %al, %al
je 0x19963
movq 0x40(%rbx), %rax
cmpb $0x0, 0x1524(%rax)
je 0x19968
movb $0x1, %r12b
jmp 0x1996b
xorl %r12d, %r12d
movq 0x88(%rsp), %r15
movq 0x70(%rsp), %rdi
movq 0x30(%rsp), %rbp
testq %rdi, %rdi
je 0x19987
callq 0xafe4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x19996
callq 0x52e0
testb %r12b, %r12b
je 0x19d07
movq 0x40(%rbx), %rax
movb 0x8(%rax), %cl
testb %r13b, %r13b
je 0x19d07
testb %cl, %cl
jne 0x19d07
movb 0x1524(%rax), %sil
cmpb $0x1, %sil
jne 0x199df
movl 0x24(%rsp), %ecx
cmpl %ecx, (%r14)
jae 0x19d07
movl 0x20(%rsp), %ecx
cmpl %ecx, 0x4(%r14)
jae 0x19d07
movq (%r15), %rdx
jmp 0x1974f
xorl %r13d, %r13d
jmp 0x19870
movl 0x18(%rsp), %esi
leaq 0x48(%rbx), %rdi
callq 0x17302
cmpl $0x0, (%r14)
movb $0x1, %r13b
je 0x19d07
cmpl $0x0, 0x4(%r14)
je 0x19d07
movq 0x40(%rbx), %rax
movq 0x78(%rax), %rdx
movq 0x70(%rax), %rcx
movl 0x8(%rdx), %edx
movl 0x10(%rcx), %ecx
shlq $0x18, %rdx
addq %rdx, %rcx
cmpq 0x58(%rax), %rcx
jae 0x19c79
movl $0x1, %ecx
xorl %r13d, %r13d
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r15)
jne 0x19d07
leaq 0x10(%rsp), %rdi
leaq 0x18471(%rip), %rsi     # 0x31ec9
movq $0x0, (%rdi)
movswl 0x2(%rbp), %ecx
movswl 0x4(%rbp), %edx
movb 0x9(%rbp), %al
subl %edx, %ecx
cmpb (%rsi), %al
movq 0x40(%rbx), %rdx
cmovll %ecx, %r13d
addq $0x68, %rdx
movl %r13d, %esi
callq 0x1ae5c
movq %rbp, %rdi
callq 0x12e26
leaq 0x78(%rsp), %rcx
leaq 0x38(%rsp), %r13
movq %rax, (%rcx)
leaq 0x10(%rbp), %rax
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
xorl %ebp, %ebp
movb %dl, 0xc(%rcx)
movq %rax, 0x28(%rsp)
cmpw $-0x1, 0x82(%rsp)
je 0x19c81
movl 0x80(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1520(%rax)
je 0x19b2d
movq 0x30(%rsp), %r12
movzwl 0x8(%r12), %eax
movw %ax, 0x40(%rsp)
movq (%r12), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0xb1c0
movq 0x28(%r12), %rax
movq %rax, 0x60(%rsp)
vmovups 0x30(%r12), %xmm0
movq 0x38(%r12), %rax
vmovups %xmm0, 0x68(%rsp)
testq %rax, %rax
je 0x19b50
movq 0x2b4a5(%rip), %rcx     # 0x44fc8
cmpb $0x0, (%rcx)
je 0x19b4c
incl 0x8(%rax)
jmp 0x19b50
movq 0x30(%rsp), %rdi
movl $0x1, 0xc(%rsp)
movl 0x8(%rdi), %edx
leaq 0x1c(%rsp), %rsi
callq 0x136aa
movl %eax, %r12d
jmp 0x19bb9
lock
incl 0x8(%rax)
leaq 0x1c(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
movq %r13, %rdi
callq 0x1359e
movl $0x0, 0xc(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1520(%rax)
jne 0x19b8b
leaq 0xc(%rsp), %rsi
movq %r13, %rdi
callq 0x1aea2
jmp 0x19b98
leaq 0xc(%rsp), %rsi
movq %r13, %rdi
callq 0x1af50
movq 0x70(%rsp), %rdi
movl %eax, %r12d
testq %rdi, %rdi
je 0x19baa
callq 0xafe4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x19bb9
callq 0x52e0
movzwl 0x1c(%rsp), %eax
xorl %ecx, %ecx
movq %rcx, 0x40(%rsp)
movw $0x0, 0x48(%rsp)
leaq 0x182ee(%rip), %rsi     # 0x31ec1
movzbl %r12b, %edx
movw %ax, 0x4a(%rsp)
movq %rcx, 0x50(%rsp)
movq 0x40(%rbx), %rax
movl 0x152c(%rax), %ecx
leaq 0x182d7(%rip), %rax     # 0x31ec9
cmpb %r12b, (%rax)
movl %ecx, %eax
cmovnel %edx, %eax
cmpb %cl, (%rsi)
leaq 0x182c3(%rip), %rcx     # 0x31ec6
cmovel %edx, %eax
cmpb (%rcx), %al
jne 0x19c14
movl 0xc(%rsp), %ecx
movl %ecx, 0x38(%rsp)
jmp 0x19c45
movq 0x30(%rsp), %rcx
movb 0x8(%rcx), %cl
cmpb %cl, %al
jne 0x19c2c
movl $0x3fffffff, 0x38(%rsp) # imm = 0x3FFFFFFF
xorl %ecx, %ecx
jmp 0x19c45
movb $0x3, %dl
movl $0x0, 0x38(%rsp)
subb %cl, %dl
xorl %ecx, %ecx
cmpb %dl, %al
sete %cl
orl $0x3ffffffe, %ecx       # imm = 0x3FFFFFFE
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
movl %ecx, 0x3c(%rsp)
callq 0x1b032
movq %rax, %rdi
movq %r13, %rsi
callq 0x1b090
movq %r13, %rdi
callq 0x1ac2a
incl %ebp
leaq 0x78(%rsp), %rdi
callq 0x12e70
jmp 0x19aac
xorl %r13d, %r13d
jmp 0x19d07
movl %ebp, %eax
addq %rax, 0x68(%rbx)
movl %ebp, %esi
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1510(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x50(%rcx)
movq 0x10(%rsp), %rdi
callq 0x1d404
movq (%r15), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r15)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x19cc2
movq %r15, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x19cd1
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x19d1c
movq %r14, %rsi
callq 0x19da4
cmpq $0x0, 0x10(%rsp)
jne 0x19d6e
movq 0x40(%rbx), %rax
movq 0x1510(%rax), %rcx
cmpq 0x1518(%rax), %rcx
setae %r13b
movl %r13d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15b00(%rip), %rdi     # 0x2f823
leaq 0x15d82(%rip), %rsi     # 0x2faac
leaq 0x15de6(%rip), %rcx     # 0x2fb17
movl $0x83, %edx
callq 0x51d0
jmp 0x19d63
jmp 0x19d63
jmp 0x19d63
jmp 0x19d45
jmp 0x19d63
leaq 0x38(%rsp), %rdi
movq %rax, %rbx
callq 0x9b56
jmp 0x19d66
leaq 0x38(%rsp), %rdi
movq %rax, %rbx
callq 0x1ac2a
jmp 0x19d66
movq %rax, %rbx
cmpq $0x0, 0x10(%rsp)
je 0x19d9c
leaq 0x14abb(%rip), %rdi     # 0x2e830
leaq 0x14ac1(%rip), %rsi     # 0x2e83d
leaq 0x16346(%rip), %rcx     # 0x300c9
movl $0x7a, %edx
callq 0x51d0
jmp 0x19d8f
leaq 0x38(%rsp), %rdi
movq %rax, %rbx
callq 0x9b56
movq %rbx, %rdi
callq 0x5670