// File: /workspace/github_cpp_makefile_star2_O1/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
movl %r9d, 0x28(%rsp)
movl %r8d, 0x2c(%rsp)
movl %ecx, 0x18(%rsp)
movq %rdx, %r14
movq %rdi, %rbx
leaq 0x18(%rdx), %r15
movq 0x18(%rdx), %rax
cmpq $0x2, %rax
movq %rsi, 0x20(%rsp)
jb 0x19afb
cmpw $0x0, 0x6(%rax)
je 0x19afb
leaq 0x10(%rsi), %rax
movq %rax, 0x90(%rsp)
incl 0x18(%rsp)
movq %r15, 0x38(%rsp)
movq (%r15), %rdx
leaq 0x10(%rdx), %rax
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rax, %r12
movq 0x40(%rbx), %rcx
cmpb $0x1, 0x1524(%rcx)
jne 0x19882
cmpq $0x2, %rdx
jb 0x1989b
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rax
jmp 0x1989d
cmpq $0x2, %rdx
jb 0x19935
movzwl 0x6(%rdx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
jmp 0x19937
xorl %eax, %eax
movq %r12, %rdx
movq %r12, %rsi
cmpq %rax, %r12
je 0x198e7
movzwl 0x10(%rsi), %edi
addl 0x4(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x198d8
movzwl 0x10(%r12), %r9d
addl 0x4(%r12), %r9d
movq %rsi, %r8
cmpl %r9d, %edi
jb 0x198d3
movq %r12, %r8
movq %r8, %r12
jmp 0x198de
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rax, %rsi
jne 0x198a8
movl (%rdx), %r13d
addl 0x28(%rsp), %r13d
subl 0x4(%r14), %r13d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
cmpl %eax, %r13d
cmovael %eax, %r13d
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vcvtss2sd 0x1528(%rcx), %xmm3, %xmm1
vmovsd 0x17019(%rip), %xmm2    # 0x30930
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ecx
movl 0x2c(%rsp), %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movl %ecx, 0x30(%rsp)
jmp 0x19976
xorl %eax, %eax
movl $0x0, 0x30(%rsp)
cmpq %rax, %r12
je 0x19af3
movq %r12, %rcx
movzwl 0x10(%rcx), %esi
addl 0x4(%rcx), %esi
movzwl 0x10(%r12), %edi
addl 0x4(%r12), %edi
movq %r12, %rdx
cmpl %edi, %esi
ja 0x19967
movq %rcx, %rdx
addq $0x20, %r12
movq %rdx, %rcx
cmpq %rax, %r12
jne 0x1994b
xorl %r13d, %r13d
movq %rdx, %r12
movq 0x20(%rsp), %r15
movzwl 0x8(%r15), %eax
movw %ax, 0x48(%rsp)
movq (%r15), %rax
movq %rax, 0x40(%rsp)
leaq 0x50(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0xb1f8
movq 0x28(%r15), %rax
movq %rax, 0x68(%rsp)
movq 0x38(%r15), %rax
vmovups 0x30(%r15), %xmm0
vmovups %xmm0, 0x70(%rsp)
testq %rax, %rax
leaq 0x40(%rsp), %rbp
je 0x199da
movq 0x2c5fc(%rip), %rcx     # 0x45fc8
cmpb $0x0, (%rcx)
je 0x199d6
incl 0x8(%rax)
jmp 0x199da
lock
incl 0x8(%rax)
movq %r14, %r15
movsbl 0x12(%r12), %eax
movsbl 0x40(%rsp), %ecx
imull %eax, %ecx
movsbl 0x13(%r12), %eax
addl %ecx, %eax
movzwl 0x12(%r12), %ecx
movw %cx, 0x80(%rsp)
movw %ax, 0x82(%rsp)
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x135c0
lock  incw 0x10(%r12)
movq 0x68(%rbx), %r14
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl 0x18(%rsp), %ecx
movl %r13d, %r8d
movl 0x30(%rsp), %r9d
callq 0x197f2
movl %eax, %ebp
lock  decw 0x10(%r12)
movq 0x68(%rbx), %rax
subq %r14, %rax
lock
addq %rax, 0x8(%r12)
movq %r15, %r14
movq %r15, %rsi
callq 0x19ed2
testb %al, %al
je 0x19a7a
movq 0x40(%rbx), %rax
cmpb $0x0, 0x1524(%rax)
je 0x19a7f
movb $0x1, %r12b
jmp 0x19a82
xorl %r12d, %r12d
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x19a91
callq 0xb01a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x19aa0
callq 0x52e0
testb %r12b, %r12b
je 0x19e35
movq 0x40(%rbx), %rax
movb 0x8(%rax), %cl
testb %bpl, %bpl
je 0x19e35
testb %cl, %cl
jne 0x19e35
cmpb $0x1, 0x1524(%rax)
movq 0x38(%rsp), %r15
jne 0x1984e
movl 0x2c(%rsp), %eax
cmpl %eax, (%r14)
jae 0x19e35
movl 0x28(%rsp), %eax
cmpl %eax, 0x4(%r14)
jb 0x1984e
jmp 0x19e35
xorl %r13d, %r13d
jmp 0x19979
leaq 0x48(%rbx), %rdi
movl 0x18(%rsp), %esi
callq 0x173f0
cmpl $0x0, (%r14)
movb $0x1, %bpl
je 0x19e35
cmpl $0x0, 0x4(%r14)
je 0x19e35
movq 0x40(%rbx), %rax
movq 0x70(%rax), %rcx
movq 0x78(%rax), %rdx
movl 0x8(%rdx), %edx
shlq $0x18, %rdx
movl 0x10(%rcx), %ecx
addq %rdx, %rcx
cmpq 0x58(%rax), %rcx
jae 0x19da1
xorl %ebp, %ebp
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r15)
jne 0x19e35
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movq 0x20(%rsp), %r12
movb 0x9(%r12), %al
leaq 0x19355(%rip), %rsi     # 0x32ec9
movswl 0x2(%r12), %ecx
movswl 0x4(%r12), %edx
subl %edx, %ecx
cmpb (%rsi), %al
cmovll %ecx, %ebp
movq 0x40(%rbx), %rdx
addq $0x68, %rdx
movl %ebp, %esi
callq 0x1afdc
movq %r15, 0x38(%rsp)
movq %r12, %rdi
callq 0x12e58
leaq 0x80(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movq %rcx, %r15
movb %dl, 0xc(%rcx)
leaq 0x10(%r12), %rax
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %r13
xorl %r12d, %r12d
cmpw $-0x1, 0x8a(%rsp)
je 0x19da8
movl 0x88(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1520(%rax)
je 0x19c48
movq 0x20(%rsp), %rbp
movzwl 0x8(%rbp), %eax
movw %ax, 0x48(%rsp)
movq (%rbp), %rax
movq %rax, 0x40(%rsp)
leaq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0xb1f8
movq 0x28(%rbp), %rax
movq %rax, 0x68(%rsp)
movq 0x38(%rbp), %rax
vmovups 0x30(%rbp), %xmm0
vmovups %xmm0, 0x70(%rsp)
testq %rax, %rax
je 0x19c6a
movq 0x2c38a(%rip), %rcx     # 0x45fc8
cmpb $0x0, (%rcx)
je 0x19c66
incl 0x8(%rax)
jmp 0x19c6a
movl $0x1, 0xc(%rsp)
movq 0x20(%rsp), %rdi
movl 0x8(%rdi), %edx
leaq 0x1c(%rsp), %rsi
callq 0x136d8
movl %eax, %ebp
jmp 0x19cd2
lock
incl 0x8(%rax)
movq %r13, %rdi
leaq 0x1c(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x135c0
movl $0x0, 0xc(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1520(%rax)
jne 0x19ca5
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x1b022
jmp 0x19cb2
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x1b0c6
movl %eax, %ebp
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x19cc3
callq 0xb01a
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x19cd2
callq 0x52e0
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x40(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x48(%rsp)
movw $0x0, 0x50(%rsp)
movzwl 0x1c(%rsp), %eax
movw %ax, 0x52(%rsp)
movq %rcx, 0x58(%rsp)
movq 0x40(%rbx), %rax
movl 0x152c(%rax), %ecx
leaq 0x191ba(%rip), %rax     # 0x32ec9
cmpb %bpl, (%rax)
movzbl %bpl, %edx
movl %ecx, %eax
cmovnel %edx, %eax
leaq 0x1919f(%rip), %rsi     # 0x32ec1
cmpb %cl, (%rsi)
cmovel %edx, %eax
leaq 0x19198(%rip), %rcx     # 0x32ec6
cmpb (%rcx), %al
jne 0x19d3c
movl 0xc(%rsp), %ecx
movl %ecx, 0x40(%rsp)
jmp 0x19d6d
movq 0x20(%rsp), %rcx
movb 0x8(%rcx), %cl
cmpb %cl, %al
jne 0x19d54
movl $0x3fffffff, 0x40(%rsp) # imm = 0x3FFFFFFF
xorl %ecx, %ecx
jmp 0x19d6d
movb $0x3, %dl
subb %cl, %dl
xorl %ecx, %ecx
cmpb %dl, %al
sete %cl
movl $0x0, 0x40(%rsp)
orl $0x3ffffffe, %ecx       # imm = 0x3FFFFFFE
movl %ecx, 0x44(%rsp)
leaq 0x10(%rsp), %rdi
movl %r12d, %esi
callq 0x1b1d2
movq %rax, %rdi
movq %r13, %rsi
callq 0x1b230
movq %r13, %rdi
callq 0x1adaa
incl %r12d
movq %r15, %rdi
callq 0x12ea0
jmp 0x19bcd
xorl %ebp, %ebp
jmp 0x19e35
movl %r12d, %eax
addq %rax, 0x68(%rbx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1510(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x50(%rcx)
movq 0x10(%rsp), %rdi
movl %r12d, %esi
callq 0x1d5e0
movq 0x38(%rsp), %r15
movq (%r15), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r15)
movq %rax, 0x10(%rsp)
cmpq $0x1, %rcx
jbe 0x19df0
movq %r15, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x19dff
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x19e49
movq %r14, %rsi
callq 0x19ed2
cmpq $0x0, 0x10(%rsp)
jne 0x19e9b
movq 0x40(%rbx), %rax
movq 0x1510(%rax), %rcx
cmpq 0x1518(%rax), %rcx
setae %bpl
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x169d3(%rip), %rdi     # 0x30823
leaq 0x16c55(%rip), %rsi     # 0x30aac
leaq 0x16cb9(%rip), %rcx     # 0x30b17
movl $0x83, %edx
callq 0x51d0
jmp 0x19e90
jmp 0x19e90
jmp 0x19e90
jmp 0x19e72
jmp 0x19e90
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x9bb4
jmp 0x19e93
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x1adaa
jmp 0x19e93
movq %rax, %rbx
cmpq $0x0, 0x10(%rsp)
je 0x19ec9
leaq 0x1598e(%rip), %rdi     # 0x2f830
leaq 0x15994(%rip), %rsi     # 0x2f83d
leaq 0x17219(%rip), %rcx     # 0x310c9
movl $0x7a, %edx
callq 0x51d0
jmp 0x19ebc
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x9bb4
movq %rbx, %rdi
callq 0x5670
nop