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

		if(agent->lbdist)
			dists.run(&board);

		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);
			}

			if(agent->lbdist && outcome != Outcome::UNKNOWN)
				pd = dists.get(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 $0xb8, %rsp
movl %r9d, 0x30(%rsp)
movl %r8d, 0x38(%rsp)
movl %ecx, 0x20(%rsp)
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %rbx
leaq 0x18(%rdx), %r15
movq 0x18(%rdx), %rax
cmpq $0x2, %rax
movq %rsi, 0x28(%rsp)
jb 0x1ac5d
cmpw $0x0, 0x6(%rax)
je 0x1ac5d
leaq 0x10(%rbp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x28(%rbp), %rax
movq %rax, 0xa8(%rsp)
incl 0x20(%rsp)
movq %r12, 0x40(%rsp)
movq %r15, 0x58(%rsp)
movq (%r15), %rdx
leaq 0x10(%rdx), %rax
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rax, %r12
movq 0x40(%rbx), %rcx
cmpb $0x1, 0x152c(%rcx)
jne 0x1a9b6
cmpq $0x2, %rdx
jb 0x1a9cf
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rax
jmp 0x1a9d1
cmpq $0x2, %rdx
jb 0x1aa71
movzwl 0x6(%rdx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
jmp 0x1aa73
xorl %eax, %eax
movq %r12, %rdx
movq %r12, %rsi
cmpq %rax, %r12
je 0x1aa1b
movzwl 0x10(%rsi), %edi
addl 0x4(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x1aa0c
movzwl 0x10(%r12), %r9d
addl 0x4(%r12), %r9d
movq %rsi, %r8
cmpl %r9d, %edi
jb 0x1aa07
movq %r12, %r8
movq %r8, %r12
jmp 0x1aa12
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rax, %rsi
jne 0x1a9dc
movq %rbx, %r13
movl (%rdx), %r14d
addl 0x30(%rsp), %r14d
movq 0x40(%rsp), %rax
subl 0x4(%rax), %r14d
movl $0x1ffffffe, %eax       # imm = 0x1FFFFFFE
cmpl %eax, %r14d
cmovael %eax, %r14d
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vcvtss2sd 0x1530(%rcx), %xmm3, %xmm1
vmovsd 0x18f1d(%rip), %xmm2    # 0x33970
vaddsd %xmm2, %xmm1, %xmm1
vfmadd213sd %xmm2, %xmm0, %xmm1 # xmm1 = (xmm0 * xmm1) + xmm2
vcvttsd2usi %xmm1, %ecx
movl 0x38(%rsp), %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movl %ecx, 0x18(%rsp)
jmp 0x1aab5
xorl %eax, %eax
movl $0x0, 0x18(%rsp)
cmpq %rax, %r12
je 0x1ac52
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 0x1aaa3
movq %rcx, %rdx
addq $0x20, %r12
movq %rdx, %rcx
cmpq %rax, %r12
jne 0x1aa87
movq %rbx, %r13
xorl %r14d, %r14d
movq %rdx, %r12
movzwl 0x8(%rbp), %eax
movw %ax, 0x68(%rsp)
movq (%rbp), %rax
movq %rax, 0x60(%rsp)
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq 0xb0(%rsp), %rsi
callq 0xb34a
movq 0xa8(%rsp), %rax
vmovups (%rax), %xmm0
vmovups %xmm0, 0x18(%r15)
movq 0x40(%rbp), %rax
vmovups 0x38(%rbp), %xmm0
vmovups %xmm0, 0x98(%rsp)
testq %rax, %rax
movq %r13, %rbx
leaq 0x60(%rsp), %r13
je 0x1ab25
movq 0x2f4b1(%rip), %rdx     # 0x49fc8
cmpb $0x0, (%rdx)
je 0x1ab21
incl 0x8(%rax)
jmp 0x1ab25
lock
incl 0x8(%rax)
movq 0x40(%rsp), %r15
movsbl 0x12(%r12), %eax
movsbl 0x60(%rsp), %ecx
imull %eax, %ecx
movsbl 0x13(%r12), %eax
addl %ecx, %eax
movzwl 0x12(%r12), %ecx
movw %cx, 0x48(%rsp)
movw %ax, 0x4a(%rsp)
movq %r13, %rdi
leaq 0x48(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x13778
movq %r13, %rsi
lock  incw 0x10(%r12)
movq %rbx, %rbp
movq 0x9cc8(%rbx), %r13
movq %rbx, %rdi
movq %r12, %rdx
movl 0x20(%rsp), %ecx
movl %r14d, %r8d
movl 0x18(%rsp), %r9d
callq 0x1a912
movq %rax, 0x18(%rsp)
lock  decw 0x10(%r12)
movq 0x9cc8(%rbp), %rax
subq %r13, %rax
lock
addq %rax, 0x8(%r12)
movq %r15, %r12
movq %r15, %rsi
callq 0x1b114
testb %al, %al
je 0x1abca
movq 0x40(%rbx), %rax
cmpb $0x0, 0x152c(%rax)
je 0x1abcf
movb $0x1, %r14b
jmp 0x1abd2
xorl %r14d, %r14d
movq 0x58(%rsp), %r15
movq 0x28(%rsp), %rbp
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1abee
callq 0xb170
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x1abfd
callq 0x52e0
testb %r14b, %r14b
je 0x1afe0
movq 0x40(%rbx), %rax
movb 0x8(%rax), %cl
movq 0x18(%rsp), %r8
testb %r8b, %r8b
je 0x1b077
testb %cl, %cl
jne 0x1b077
cmpb $0x1, 0x152c(%rax)
jne 0x1a982
movl 0x38(%rsp), %eax
cmpl %eax, (%r12)
jae 0x1b077
movl 0x30(%rsp), %eax
cmpl %eax, 0x4(%r12)
jb 0x1a982
jmp 0x1b077
movq %rbx, %r13
xorl %r14d, %r14d
jmp 0x1aab8
leaq 0x9ca8(%rbx), %rdi
movl 0x20(%rsp), %esi
callq 0x179de
cmpl $0x0, (%r12)
movb $0x1, %r8b
je 0x1b077
cmpl $0x0, 0x4(%r12)
je 0x1b077
movq 0x40(%rbx), %rax
movq 0x78(%rax), %rcx
movq 0x80(%rax), %rdx
movl 0x8(%rdx), %edx
shlq $0x18, %rdx
movl 0x10(%rcx), %ecx
addq %rdx, %rcx
cmpq 0x60(%rax), %rcx
jae 0x1afd8
xorl %r8d, %r8d
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r15)
jne 0x1b077
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movb 0x9(%rbp), %al
leaq 0x1b6d9(%rip), %rsi     # 0x363b1
movswl 0x2(%rbp), %ecx
movswl 0x4(%rbp), %edx
subl %edx, %ecx
cmpb (%rsi), %al
cmovll %ecx, %r8d
movq 0x40(%rbx), %rdx
addq $0x70, %rdx
movl %r8d, %esi
callq 0x1c250
movq 0x40(%rbx), %rax
cmpb $0x1, 0x1535(%rax)
jne 0x1ad20
leaq 0x48(%rbx), %rdi
leaq 0x1b69c(%rip), %rax     # 0x363ac
movzbl (%rax), %ecx
movq %rbp, %rsi
movl $0x1, %edx
callq 0x179fe
movq %r12, 0x40(%rsp)
movq %rbp, %rdi
callq 0x13010
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x28(%rbp), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x48(%rbx), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x1b64d(%rip), %r12     # 0x363ae
leaq 0x60(%rsp), %rbp
xorl %r13d, %r13d
cmpw $-0x1, 0x52(%rsp)
je 0x1afea
movl 0x50(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1528(%rax)
je 0x1ae12
movl %r13d, 0x18(%rsp)
movq %rbx, %r13
movq %rbp, %rbx
movq %r15, %rbp
movq %r12, %r15
movq 0x28(%rsp), %r14
movzwl 0x8(%r14), %eax
movw %ax, 0x68(%rsp)
movq (%r14), %rax
movq %rax, 0x60(%rsp)
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0xb34a
movq 0x38(%rsp), %rax
vmovups (%rax), %xmm0
vmovups %xmm0, 0x18(%r12)
movq 0x40(%r14), %rax
vmovups 0x38(%r14), %xmm0
vmovups %xmm0, 0x98(%rsp)
testq %rax, %rax
movq %r15, %r12
movq %rbp, %r15
movq %rbx, %rbp
movq %r13, %rbx
movl 0x18(%rsp), %r13d
je 0x1ae35
movq 0x2f1c0(%rip), %rcx     # 0x49fc8
cmpb $0x0, (%rcx)
je 0x1ae31
incl 0x8(%rax)
jmp 0x1ae35
movl $0x1, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movl 0x8(%rdi), %edx
leaq 0xc(%rsp), %rsi
callq 0x17b04
movl %eax, %r14d
jmp 0x1aea1
lock
incl 0x8(%rax)
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x13778
movl $0x0, 0x8(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1528(%rax)
jne 0x1ae70
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x1c296
jmp 0x1ae7d
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x1c33a
movl %eax, %r14d
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1ae92
callq 0xb170
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x1aea1
callq 0x52e0
movq 0x40(%rbx), %rax
cmpb $0x1, 0x1535(%rax)
jne 0x1af10
cmpb (%r12), %r14b
je 0x1af10
leaq 0x1b4ef(%rip), %rcx     # 0x363aa
movsbq (%rcx), %rcx
imulq $0x9c4, %rcx, %rcx      # imm = 0x9C4
movq 0x30(%rsp), %r8
leaq -0x9c4(%r8,%rcx), %rcx
movswq 0xe(%rsp), %rdx
movl 0x1388(%rcx,%rdx,4), %esi
leaq 0x1b4c4(%rip), %rdi     # 0x363ab
movsbq (%rdi), %rdi
imulq $0x9c4, %rdi, %rdi      # imm = 0x9C4
leaq -0x9c4(%r8,%rdi), %rdi
addl (%rcx,%rdx,4), %esi
movl 0x1388(%rdi,%rdx,4), %ecx
addl (%rdi,%rdx,4), %ecx
cmpl %esi, %ecx
cmovgel %esi, %ecx
movl %ecx, 0x8(%rsp)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x60(%rsp)
xorl %edx, %edx
movq %rdx, 0x68(%rsp)
movw $0x0, 0x70(%rsp)
movzwl 0xc(%rsp), %ecx
movw %cx, 0x72(%rsp)
movq %rdx, 0x78(%rsp)
movl 0x1534(%rax), %ecx
leaq 0x1b468(%rip), %rax     # 0x363b1
cmpb %r14b, (%rax)
movzbl %r14b, %edx
movl %ecx, %eax
cmovnel %edx, %eax
leaq 0x1b44d(%rip), %rsi     # 0x363a9
cmpb %cl, (%rsi)
cmovel %edx, %eax
cmpb (%r12), %al
jne 0x1af71
movl 0x8(%rsp), %ecx
movl %ecx, 0x60(%rsp)
jmp 0x1afa2
movq 0x28(%rsp), %rcx
movb 0x8(%rcx), %cl
cmpb %cl, %al
jne 0x1af89
movl $0x3fffffff, 0x60(%rsp) # imm = 0x3FFFFFFF
xorl %ecx, %ecx
jmp 0x1afa2
movb $0x3, %dl
subb %cl, %dl
xorl %ecx, %ecx
cmpb %dl, %al
sete %cl
movl $0x0, 0x60(%rsp)
orl $0x3ffffffe, %ecx       # imm = 0x3FFFFFFE
movl %ecx, 0x64(%rsp)
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x1c446
movq %rax, %rdi
movq %rbp, %rsi
callq 0x1c4a4
movq %rbp, %rdi
callq 0x1c01e
incl %r13d
leaq 0x48(%rsp), %rdi
callq 0x13058
jmp 0x1ad69
xorl %r8d, %r8d
jmp 0x1b077
movq 0x18(%rsp), %r8
jmp 0x1b077
movl %r13d, %eax
addq %rax, 0x9cc8(%rbx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1518(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x58(%rcx)
movq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x1e890
movq (%r15), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r15)
movq %rax, 0x10(%rsp)
cmpq $0x1, %rcx
jbe 0x1b030
movq %r15, 0x8(%rcx)
cmpq $0x2, %rax
movq 0x40(%rsp), %rsi
jb 0x1b044
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x1b08c
callq 0x1b114
cmpq $0x0, 0x10(%rsp)
jne 0x1b0de
movq 0x40(%rbx), %rax
movq 0x1518(%rax), %rcx
cmpq 0x1520(%rax), %rcx
setae %r8b
movl %r8d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x187fb(%rip), %rdi     # 0x3388e
leaq 0x18ace(%rip), %rsi     # 0x33b68
leaq 0x18b2c(%rip), %rcx     # 0x33bcd
movl $0x89, %edx
callq 0x51d0
jmp 0x1b0d3
jmp 0x1b0d3
jmp 0x1b0d3
jmp 0x1b0b5
jmp 0x1b0d3
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x9ce0
jmp 0x1b0d6
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x1c01e
jmp 0x1b0d6
movq %rax, %rbx
cmpq $0x0, 0x10(%rsp)
je 0x1b10c
leaq 0x17753(%rip), %rdi     # 0x32838
leaq 0x17759(%rip), %rsi     # 0x32845
leaq 0x19044(%rip), %rcx     # 0x34137
movl $0x7a, %edx
callq 0x51d0
jmp 0x1b0ff
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x9ce0
movq %rbx, %rdi
callq 0x5670