// File: 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:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0x238(%rsp)
movq %rsi, 0x230(%rsp)
movq %rdx, 0x228(%rsp)
movl %ecx, 0x224(%rsp)
movl %r8d, 0x220(%rsp)
movl %r9d, 0x21c(%rsp)
movq 0x238(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x228(%rsp), %rdi
addq $0x18, %rdi
callq 0x2e140
testb $0x1, %al
jne 0x2bd5d
jmp 0x2c2bf
movq 0x70(%rsp), %rdi
addq $0x48, %rdi
movl 0x224(%rsp), %esi
callq 0x28770
movq 0x228(%rsp), %rdi
callq 0x2e160
testb $0x1, %al
jne 0x2bd85
jmp 0x2bd92
movb $0x1, 0x247(%rsp)
jmp 0x2c796
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rdi
callq 0x2e1a0
testb $0x1, %al
jne 0x2bda6
jmp 0x2bdb3
movb $0x0, 0x247(%rsp)
jmp 0x2c796
movq 0x228(%rsp), %rdi
addq $0x18, %rdi
callq 0x2e1d0
testb $0x1, %al
jne 0x2bdd5
movb $0x0, 0x247(%rsp)
jmp 0x2c796
leaq 0x210(%rsp), %rdi
callq 0x2e210
movq 0x230(%rsp), %rdi
callq 0x1d110
movl %eax, 0x6c(%rsp)
jmp 0x2bdf5
movl 0x6c(%rsp), %esi
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0x68, %rdx
leaq 0x210(%rsp), %rdi
callq 0x2e230
jmp 0x2be15
movl $0x0, 0x200(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0x1d170
movq %rdx, 0x58(%rsp)
movq %rax, 0x60(%rsp)
jmp 0x2be49
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0x1d8(%rsp)
movq %rax, 0x1e0(%rsp)
movq 0x1d8(%rsp), %rax
movq 0x1dd(%rsp), %rcx
movq %rcx, 0x1ed(%rsp)
movq %rax, 0x1e8(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0x1d1a0
movq %rdx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
jmp 0x2be9c
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x1b8(%rsp)
movq %rax, 0x1c0(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x1c8(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, 0x1d0(%rsp)
movb 0x1c4(%rsp), %al
movb %al, 0x1d4(%rsp)
leaq 0x1e8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x1d1f0
movb %al, 0x47(%rsp)
jmp 0x2befd
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x2bf0a
jmp 0x2c1a2
leaq 0x1e8(%rsp), %rdi
callq 0x1d220
movq %rax, 0x38(%rsp)
jmp 0x2bf1e
movq 0x38(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1b4(%rsp)
leaq 0x1af(%rsp), %rdi
callq 0xfe10
jmp 0x2bf3b
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0x1520(%rax)
je 0x2c04d
movq 0x230(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0x11710
jmp 0x2bf68
leaq 0x168(%rsp), %rdi
leaq 0x1b4(%rsp), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x1f210
jmp 0x2bf86
movq 0x70(%rsp), %rax
movl $0x0, 0x1b0(%rsp)
movq 0x40(%rax), %rax
cmpl $0x1, 0x1520(%rax)
jne 0x2bfcb
leaq 0x168(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x2e2a0
movb %al, 0x37(%rsp)
jmp 0x2bfbe
movb 0x37(%rsp), %al
movb %al, 0x167(%rsp)
jmp 0x2bff1
leaq 0x168(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x2e3f0
movb %al, 0x36(%rsp)
jmp 0x2bfe6
movb 0x36(%rsp), %al
movb %al, 0x167(%rsp)
movb 0x167(%rsp), %al
movb %al, 0x1af(%rsp)
leaq 0x168(%rsp), %rdi
callq 0xbf10
jmp 0x2c08c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x2c2ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x168(%rsp), %rdi
callq 0xbf10
jmp 0x2c2ad
movl $0x1, 0x1b0(%rsp)
movq 0x230(%rsp), %rdi
leaq 0x1b4(%rsp), %rsi
callq 0x28860
movb %al, 0x35(%rsp)
jmp 0x2c073
movb 0x35(%rsp), %al
movb %al, 0x166(%rsp)
movb 0x166(%rsp), %al
movb %al, 0x1af(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x1b4(%rsp), %rsi
movl $0x1, %edx
callq 0x2dbf0
jmp 0x2c0a8
movb 0x1af(%rsp), %al
movb %al, 0x13f(%rsp)
movq 0x230(%rsp), %rdi
callq 0x1a6a0
movb %al, 0x34(%rsp)
jmp 0x2c0c9
movq 0x70(%rsp), %rax
movb 0x34(%rsp), %cl
movb %cl, 0x13e(%rsp)
movq 0x40(%rax), %rax
movb 0x152c(%rax), %al
movb %al, 0x13d(%rsp)
movl 0x1b0(%rsp), %r8d
movzbl 0x13d(%rsp), %ecx
movzbl 0x13e(%rsp), %edx
movzbl 0x13f(%rsp), %esi
leaq 0x140(%rsp), %rdi
callq 0x2e5d0
movq %rax, 0x28(%rsp)
jmp 0x2c11e
movl 0x200(%rsp), %esi
leaq 0x210(%rsp), %rdi
callq 0x2e720
movq %rax, 0x20(%rsp)
jmp 0x2c139
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2e7d0
jmp 0x2c14a
leaq 0x140(%rsp), %rdi
callq 0x2dc80
movl 0x200(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x200(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x1d2b0
jmp 0x2c177
jmp 0x2bee2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x2dc80
jmp 0x2c2ad
movq 0x70(%rsp), %rax
movl 0x200(%rsp), %ecx
movl %ecx, %edx
movq 0x68(%rax), %rcx
addq %rdx, %rcx
movq %rcx, 0x68(%rax)
movq 0x40(%rax), %rdi
addq $0x1510, %rdi           # imm = 0x1510
leaq 0x200(%rsp), %rsi
callq 0x1d3c0
jmp 0x2c1d5
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0x50, %rdi
leaq 0x200(%rsp), %rsi
callq 0x1d3c0
jmp 0x2c1f1
movl 0x200(%rsp), %esi
leaq 0x210(%rsp), %rdi
callq 0x2e860
jmp 0x2c207
movq 0x228(%rsp), %rdi
addq $0x18, %rdi
leaq 0x210(%rsp), %rsi
callq 0x2e890
jmp 0x2c222
leaq 0x210(%rsp), %rdi
callq 0x2e910
movb %al, 0x1f(%rsp)
jmp 0x2c235
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x2c23f
jmp 0x2c241
jmp 0x2c260
leaq 0x1d3ef(%rip), %rdi     # 0x49637
leaq 0x1d579(%rip), %rsi     # 0x497c8
movl $0x83, %edx
leaq 0x1d5d8(%rip), %rcx     # 0x49833
callq 0x52a0
movq 0x70(%rsp), %rdi
movq 0x228(%rsp), %rsi
callq 0x2c7c0
jmp 0x2c274
movq 0x70(%rsp), %rcx
movq 0x40(%rcx), %rax
movq 0x1510(%rax), %rax
movq 0x40(%rcx), %rcx
cmpq 0x1518(%rcx), %rax
setae %al
andb $0x1, %al
movb %al, 0x247(%rsp)
leaq 0x210(%rsp), %rdi
callq 0x2e950
jmp 0x2c796
leaq 0x210(%rsp), %rdi
callq 0x2e950
jmp 0x2c7a7
jmp 0x2c2c1
movq 0x228(%rsp), %rdi
addq $0x18, %rdi
callq 0x2e990
movq %rax, 0x130(%rsp)
movq 0x228(%rsp), %rdi
addq $0x18, %rdi
callq 0x2e990
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x128(%rsp)
movq 0x40(%rax), %rax
testb $0x1, 0x1524(%rax)
je 0x2c4c7
movq 0x228(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x2e990
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rdi
callq 0x2e9e0
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
cmpq 0x108(%rsp), %rax
je 0x2c40f
movq 0x110(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rdi
callq 0x2ea30
movl %eax, 0x18(%rsp)
movq 0x130(%rsp), %rdi
callq 0x2ea30
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
ja 0x2c3ba
movq 0x130(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x130(%rsp)
jmp 0x2c3f4
movq 0x100(%rsp), %rdi
callq 0x2ea30
movl %eax, 0x14(%rsp)
movq 0x128(%rsp), %rdi
callq 0x2ea30
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jae 0x2c3f2
movq 0x100(%rsp), %rax
movq %rax, 0x128(%rsp)
jmp 0x2c3f4
jmp 0x2c3f6
movq 0x110(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x110(%rsp)
jmp 0x2c34a
movl $0x1ffffffe, 0xfc(%rsp) # imm = 0x1FFFFFFE
movl 0x21c(%rsp), %eax
movq 0x130(%rsp), %rcx
movl (%rcx), %ecx
addl %ecx, %eax
movq 0x228(%rsp), %rcx
movl 0x4(%rcx), %ecx
subl %ecx, %eax
movl %eax, 0xf8(%rsp)
leaq 0xfc(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x2ea50
movq %rax, %rcx
movq 0x70(%rsp), %rax
movl (%rcx), %ecx
movl %ecx, 0x124(%rsp)
movq 0x128(%rsp), %rcx
vcvtusi2sdl 0x4(%rcx), %xmm0, %xmm1
movq 0x40(%rax), %rax
vmovss 0x1528(%rax), %xmm2
vcvtss2sd %xmm2, %xmm0, %xmm0
vmovsd 0x1ab82(%rip), %xmm2    # 0x47010
vaddsd %xmm2, %xmm0, %xmm0
vfmadd213sd %xmm2, %xmm1, %xmm0 # xmm0 = (xmm1 * xmm0) + xmm2
vcvttsd2usi %xmm0, %eax
movl %eax, 0xf4(%rsp)
leaq 0x220(%rsp), %rdi
leaq 0xf4(%rsp), %rsi
callq 0x2ea50
movl (%rax), %eax
movl %eax, 0x120(%rsp)
jmp 0x2c58f
movl $0x0, 0x120(%rsp)
movl $0x0, 0x124(%rsp)
movq 0x228(%rsp), %rax
addq $0x18, %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x2e990
movq %rax, 0xe0(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x2e9e0
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x2c58d
movq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x130(%rsp), %rdi
callq 0x2ea30
movl %eax, 0x10(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x2ea30
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jbe 0x2c575
movq 0xd0(%rsp), %rax
movq %rax, 0x130(%rsp)
jmp 0x2c577
movq 0xe0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xe0(%rsp)
jmp 0x2c51b
jmp 0x2c58f
movq 0x230(%rsp), %rsi
leaq 0x90(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x11710
movq 0x8(%rsp), %rdi
movq 0x130(%rsp), %rsi
addq $0x12, %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x1c300
jmp 0x2c5c8
movq 0x130(%rsp), %rdi
callq 0x2ea90
jmp 0x2c5d7
movq 0x70(%rsp), %rdi
movq 0x68(%rdi), %rax
movq %rax, 0x88(%rsp)
movq 0x130(%rsp), %rdx
movl 0x224(%rsp), %ecx
incl %ecx
movl 0x124(%rsp), %r8d
movl 0x120(%rsp), %r9d
leaq 0x90(%rsp), %rsi
callq 0x2bd00
movb %al, 0x7(%rsp)
jmp 0x2c61c
movb 0x7(%rsp), %al
andb $0x1, %al
movb %al, 0x13c(%rsp)
movq 0x130(%rsp), %rdi
callq 0x2eac0
jmp 0x2c638
movq 0x70(%rsp), %rax
movq 0x130(%rsp), %rdi
addq $0x8, %rdi
movq 0x68(%rax), %rax
movq 0x88(%rsp), %rcx
subq %rcx, %rax
movq %rax, 0x80(%rsp)
leaq 0x80(%rsp), %rsi
callq 0x29140
jmp 0x2c66f
movq 0x70(%rsp), %rdi
movq 0x228(%rsp), %rsi
callq 0x2c7c0
movb %al, 0x6(%rsp)
jmp 0x2c687
movb 0x6(%rsp), %al
testb $0x1, %al
jne 0x2c691
jmp 0x2c6d3
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x1524(%rax)
jne 0x2c6d3
movl $0x4, 0x7c(%rsp)
jmp 0x2c6db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xbf10
jmp 0x2c7a7
movl $0x0, 0x7c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xbf10
movl 0x7c(%rsp), %eax
testl %eax, %eax
je 0x2c6f7
jmp 0x2c6f2
jmp 0x2c786
jmp 0x2c6f9
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
movb 0x8(%rax), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x5(%rsp)
jne 0x2c77a
xorl %eax, %eax
testb $0x1, 0x13c(%rsp)
movb %al, 0x5(%rsp)
je 0x2c77a
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rcx
movb $0x1, %al
testb $0x1, 0x1524(%rcx)
movb %al, 0x4(%rsp)
je 0x2c772
movq 0x228(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl 0x220(%rsp), %ecx
movb %al, 0x3(%rsp)
jae 0x2c76a
movq 0x228(%rsp), %rax
movl 0x4(%rax), %eax
cmpl 0x21c(%rsp), %eax
setb %al
movb %al, 0x3(%rsp)
movb 0x3(%rsp), %al
movb %al, 0x4(%rsp)
movb 0x4(%rsp), %al
movb %al, 0x5(%rsp)
movb 0x5(%rsp), %al
testb $0x1, %al
jne 0x2c2c1
movb 0x13c(%rsp), %al
andb $0x1, %al
movb %al, 0x247(%rsp)
movb 0x247(%rsp), %al
andb $0x1, %al
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x208(%rsp), %rdi
callq 0x5760
nopw %cs:(%rax,%rax)