// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
leaq 0x18(%rdx), %r14
movq %rdx, %r13
movq 0x18(%rdx), %rdx
movl %r9d, 0x18(%rsp)
movl %r8d, 0x20(%rsp)
movl %ecx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movq %r14, 0x48(%rsp)
cmpq $0x2, %rdx
jb 0x1aca5
cmpw $0x0, 0x6(%rdx)
je 0x1aca5
leaq 0x10(%r15), %rax
incl 0x10(%rsp)
leaq 0x28(%r15), %rcx
movq %r15, 0x40(%rsp)
movq %rax, 0xa0(%rsp)
movq 0x40(%rbx), %rax
movq %rcx, 0x98(%rsp)
movb 0x152c(%rax), %sil
leaq 0x10(%rdx), %rcx
cmpq $0x2, %rdx
movl $0x0, %r12d
cmovaeq %rcx, %r12
testb $0x1, %sil
je 0x1aa1a
cmpq $0x2, %rdx
jb 0x1aa33
movzwl 0x6(%rdx), %edx
shll $0x5, %edx
addq %rdx, %rcx
jmp 0x1aa35
cmpq $0x2, %rdx
jb 0x1aacd
movzwl 0x6(%rdx), %eax
shll $0x5, %eax
addq %rax, %rcx
jmp 0x1aacf
xorl %ecx, %ecx
movq %r12, %rdx
movq %r12, %rsi
cmpq %rcx, %r12
je 0x1aa7f
movzwl 0x10(%rsi), %edi
movzwl 0x10(%rdx), %r8d
addl 0x4(%rsi), %edi
addl 0x4(%rdx), %r8d
cmpl %r8d, %edi
jbe 0x1aa70
movzwl 0x10(%r12), %r9d
movq %rsi, %r8
addl 0x4(%r12), %r9d
cmpl %r9d, %edi
jb 0x1aa6b
movq %r12, %r8
movq %r8, %r12
jmp 0x1aa76
movq %rdx, %r12
movq %rsi, %rdx
addq $0x20, %rsi
cmpq %rcx, %rsi
jne 0x1aa40
vcvtusi2sdl 0x4(%r12), %xmm3, %xmm0
vmovsd 0x18ee1(%rip), %xmm2    # 0x33970
movl (%rdx), %r14d
movl $0x1ffffffe, %ecx       # imm = 0x1FFFFFFE
addl 0x18(%rsp), %r14d
subl 0x4(%r13), %r14d
vcvtss2sd 0x1530(%rax), %xmm3, %xmm1
movl 0x20(%rsp), %eax
cmpl %ecx, %r14d
cmovael %ecx, %r14d
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 0x1ab0e
xorl %ecx, %ecx
movl $0x0, 0x28(%rsp)
cmpq %rcx, %r12
je 0x1ac9d
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 0x1aaff
movq %rax, %rdx
addq $0x20, %r12
movq %rdx, %rax
cmpq %rcx, %r12
jne 0x1aae3
xorl %r14d, %r14d
movq %rdx, %r12
movzwl 0x8(%r15), %eax
movq 0xa0(%rsp), %rsi
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movw %ax, 0x58(%rsp)
movq (%r15), %rax
movq %rax, 0x50(%rsp)
callq 0xb44a
movq 0x98(%rsp), %rax
leaq 0x50(%rsp), %rdi
vmovups (%rax), %xmm0
vmovups %xmm0, 0x18(%rbp)
vmovups 0x38(%r15), %xmm0
movq 0x40(%r15), %rax
vmovups %xmm0, 0x88(%rsp)
testq %rax, %rax
je 0x1ab7b
movq 0x2e45b(%rip), %rcx     # 0x48fc8
cmpb $0x0, (%rcx)
je 0x1ab77
incl 0x8(%rax)
jmp 0x1ab7b
lock
incl 0x8(%rax)
movsbl 0x12(%r12), %eax
movsbl 0x50(%rsp), %ecx
movq %r13, %r15
imull %eax, %ecx
movsbl 0x13(%r12), %eax
addl %ecx, %eax
movzwl 0x12(%r12), %ecx
movw %cx, 0x30(%rsp)
movw %ax, 0x32(%rsp)
leaq 0x30(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
movq %rdi, %rbp
callq 0x1388c
lock  incw 0x10(%r12)
movq 0x9cc8(%rbx), %r13
movl 0x10(%rsp), %ecx
movl 0x28(%rsp), %r9d
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %r14d, %r8d
callq 0x1a97a
lock  decw 0x10(%r12)
movl %eax, %ebp
movq %r15, %rsi
movq 0x9cc8(%rbx), %rax
subq %r13, %rax
movq %r15, %r13
lock
addq %rax, 0x8(%r12)
callq 0x1b11e
testb %al, %al
je 0x1ac18
movq 0x40(%rbx), %rax
cmpb $0x0, 0x152c(%rax)
je 0x1ac1d
movb $0x1, %r14b
jmp 0x1ac20
xorl %r14d, %r14d
movq 0x40(%rsp), %r15
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1ac37
callq 0xb272
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1ac46
callq 0x52e0
testb %r14b, %r14b
je 0x1b081
movq 0x40(%rbx), %rax
movb 0x8(%rax), %cl
testb %bpl, %bpl
je 0x1b081
testb %cl, %cl
jne 0x1b081
movb 0x152c(%rax), %sil
movq 0x48(%rsp), %rcx
cmpb $0x1, %sil
jne 0x1ac95
movl 0x20(%rsp), %edx
cmpl %edx, (%r13)
jae 0x1b081
movl 0x18(%rsp), %edx
cmpl %edx, 0x4(%r13)
jae 0x1b081
movq (%rcx), %rdx
jmp 0x1a9f0
xorl %r14d, %r14d
jmp 0x1ab11
movl 0x10(%rsp), %esi
leaq 0x9ca8(%rbx), %rdi
callq 0x17a46
cmpl $0x0, (%r13)
movb $0x1, %bpl
je 0x1b081
cmpl $0x0, 0x4(%r13)
je 0x1b081
movq 0x40(%rbx), %rax
movq 0x80(%rax), %rdx
movq 0x78(%rax), %rcx
movl 0x8(%rdx), %edx
movl 0x10(%rcx), %ecx
shlq $0x18, %rdx
addq %rdx, %rcx
cmpq 0x60(%rax), %rcx
jae 0x1afe8
movl $0x1, %ecx
xorl %ebp, %ebp
xorl %eax, %eax
lock
cmpxchgq %rcx, (%r14)
jne 0x1b081
leaq 0x8(%rsp), %rdi
leaq 0x1b69d(%rip), %rsi     # 0x363b1
movq $0x0, (%rdi)
movswl 0x2(%r15), %ecx
movswl 0x4(%r15), %edx
movb 0x9(%r15), %al
subl %edx, %ecx
cmpb (%rsi), %al
movq 0x40(%rbx), %rdx
cmovll %ecx, %ebp
addq $0x70, %rdx
movl %ebp, %esi
callq 0x1c206
movq 0x40(%rbx), %rax
cmpb $0x1, 0x1535(%rax)
jne 0x1ad67
leaq 0x1b659(%rip), %rax     # 0x363ac
leaq 0x48(%rbx), %rdi
movzbl (%rax), %ecx
movl $0x1, %edx
movq %r15, %rsi
callq 0x17a66
movq %r15, %rdi
movq %r13, 0x18(%rsp)
callq 0x13114
leaq 0x30(%rsp), %rcx
leaq 0x60(%rsp), %r14
leaq 0x50(%rsp), %rbp
xorl %r13d, %r13d
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
leaq 0x10(%r15), %rcx
leaq 0x28(%r15), %rdx
movq %rcx, 0x28(%rsp)
leaq 0x48(%rbx), %rcx
movq %rdx, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
cmpw $-0x1, 0x3a(%rsp)
je 0x1afef
movl 0x38(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x1528(%rax)
je 0x1ae2a
movzwl 0x8(%r15), %eax
movw %ax, 0x58(%rsp)
movq (%r15), %rax
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rsi
movq %r14, %rdi
movq %r14, %r12
callq 0xb44a
movq 0x10(%rsp), %rax
vmovups (%rax), %xmm0
vmovups %xmm0, 0x18(%r12)
vmovups 0x38(%r15), %xmm0
movq 0x40(%r15), %rax
vmovups %xmm0, 0x88(%rsp)
testq %rax, %rax
je 0x1ae4b
movq 0x2e1a8(%rip), %rcx     # 0x48fc8
cmpb $0x0, (%rcx)
je 0x1ae47
incl 0x8(%rax)
jmp 0x1ae4b
movl $0x1, (%rsp)
movl 0x8(%r15), %edx
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x17b80
movl %eax, %r12d
jmp 0x1aeb2
lock
incl 0x8(%rax)
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
movl $0x1, %ecx
movq %rbp, %rdi
callq 0x1388c
movl $0x0, (%rsp)
movq 0x40(%rbx), %rax
cmpl $0x1, 0x1528(%rax)
jne 0x1ae83
movq %rbp, %rdi
movq %rsp, %rsi
callq 0x1c24c
jmp 0x1ae8e
movq %rbp, %rdi
movq %rsp, %rsi
callq 0x1c2fa
movq 0x90(%rsp), %rdi
movl %eax, %r12d
testq %rdi, %rdi
je 0x1aea3
callq 0xb272
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1aeb2
callq 0x52e0
leaq 0x1b4f5(%rip), %rax     # 0x363ae
movq 0x40(%rbx), %rcx
movb (%rax), %al
cmpb $0x1, 0x1535(%rcx)
jne 0x1af37
cmpb %al, %r12b
je 0x1af37
leaq 0x1b4d6(%rip), %rsi     # 0x363aa
leaq 0x1b4d0(%rip), %r9      # 0x363ab
movl 0x4(%rsp), %edx
movq 0x20(%rsp), %r10
movsbq (%rsi), %rsi
movsbq (%r9), %r9
movl %edx, %edi
sarl $0x10, %edi
movslq %edi, %rdi
imulq $0x9c4, %rsi, %rsi      # imm = 0x9C4
imulq $0x9c4, %r9, %r9        # imm = 0x9C4
leaq -0x9c4(%r10,%rsi), %rsi
leaq -0x9c4(%r10,%r9), %r9
movl 0x1388(%rsi,%rdi,4), %r8d
movl 0x1388(%r9,%rdi,4), %r10d
addl (%rsi,%rdi,4), %r8d
addl (%r9,%rdi,4), %r10d
cmpl %r8d, %r10d
cmovgel %r8d, %r10d
movl %r10d, (%rsp)
jmp 0x1af3c
movzwl 0x4(%rsp), %edx
xorl %esi, %esi
movq %rsi, 0x58(%rsp)
movw $0x0, 0x60(%rsp)
movw %dx, 0x62(%rsp)
movq %rsi, 0x68(%rsp)
leaq 0x1b44e(%rip), %rdi     # 0x363a9
movzbl %r12b, %esi
movl 0x1534(%rcx), %edx
leaq 0x1b445(%rip), %rcx     # 0x363b1
cmpb %r12b, (%rcx)
movl %edx, %ecx
cmovnel %esi, %ecx
cmpb %dl, (%rdi)
cmovel %esi, %ecx
cmpb %al, %cl
jne 0x1af86
movl (%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x1afb2
movb 0x8(%r15), %al
cmpb %al, %cl
jne 0x1af9a
movl $0x3fffffff, 0x50(%rsp) # imm = 0x3FFFFFFF
xorl %eax, %eax
jmp 0x1afb2
movb $0x3, %dl
movl $0x0, 0x50(%rsp)
subb %al, %dl
xorl %eax, %eax
cmpb %dl, %cl
sete %al
orl $0x3ffffffe, %eax       # imm = 0x3FFFFFFE
leaq 0x8(%rsp), %rdi
movl %r13d, %esi
movl %eax, 0x54(%rsp)
callq 0x1c3dc
movq %rax, %rdi
movq %rbp, %rsi
callq 0x1c43a
movq %rbp, %rdi
callq 0x1bfd4
incl %r13d
leaq 0x30(%rsp), %rdi
callq 0x1315e
jmp 0x1adae
xorl %ebp, %ebp
jmp 0x1b081
movl %r13d, %eax
addq %rax, 0x9cc8(%rbx)
movl %r13d, %esi
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x1518(%rcx)
movq 0x40(%rbx), %rcx
lock
addq %rax, 0x58(%rcx)
movq 0x8(%rsp), %rdi
callq 0x1e7ea
movq 0x48(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rdx), %rax
movq %rcx, (%rdx)
movq %rax, 0x8(%rsp)
cmpq $0x2, %rcx
jb 0x1b03a
movq %rdx, 0x8(%rcx)
movq 0x18(%rsp), %rsi
cmpq $0x2, %rax
jb 0x1b04e
leaq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x8(%rsp)
jne 0x1b095
callq 0x1b11e
cmpq $0x0, 0x8(%rsp)
jne 0x1b0e7
movq 0x40(%rbx), %rax
movq 0x1518(%rax), %rcx
cmpq 0x1520(%rax), %rcx
setae %bpl
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x187f2(%rip), %rdi     # 0x3388e
leaq 0x18ac5(%rip), %rsi     # 0x33b68
leaq 0x18b23(%rip), %rcx     # 0x33bcd
movl $0x89, %edx
callq 0x51d0
jmp 0x1b0dc
jmp 0x1b0dc
jmp 0x1b0dc
jmp 0x1b0be
jmp 0x1b0dc
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x9d96
jmp 0x1b0df
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x1bfd4
jmp 0x1b0df
movq %rax, %rbx
cmpq $0x0, 0x8(%rsp)
je 0x1b115
leaq 0x1774a(%rip), %rdi     # 0x32838
leaq 0x17750(%rip), %rsi     # 0x32845
leaq 0x1903b(%rip), %rcx     # 0x34137
movl $0x7a, %edx
callq 0x51d0
jmp 0x1b108
leaq 0x50(%rsp), %rdi
movq %rax, %rbx
callq 0x9d96
movq %rbx, %rdi
callq 0x5670
nop