// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::create_children(Morat::Hex::Board const&, Morat::Hex::AgentMCTS::Node*)
bool AgentMCTS::AgentThread::create_children(const Board & board, Node * node){
	if(!node->children.lock())
		return false;

	if(agent->dists){
		dists.run(&board, (agent->dists > 0), board.to_play());
	}

	CompactTree<Node>::Children temp;
	temp.alloc(board.moves_avail(), agent->ctmem);

	Side to_play = board.to_play();
	Side opponent = ~to_play;
	int losses = 0;

	Node * child = temp.begin(),
	     * loss  = NULL;
	for (auto move : board) {
		*child = Node(move);

		if(agent->minimax){
			child->outcome = board.test_outcome(move);

			if(agent->minimax >= 2 && board.test_outcome(move, opponent) == +opponent){
				losses++;
				loss = child;
			}

			if(child->outcome == +to_play){ //proven win from here, don't need children
				node->outcome = child->outcome;
				node->proofdepth = 1;
				node->bestmove = move;
				node->children.unlock();
				temp.dealloc(agent->ctmem);
				return true;
			}
		}

		if(agent->knowledge)
			add_knowledge(board, node, child);
		child++;
	}
	assert(child == temp.end());

	//Make a macro move, add experience to the move so the current simulation continues past this move
	if(losses == 1){
		Node macro = *loss;
		temp.dealloc(agent->ctmem);
		temp.alloc(1, agent->ctmem);
		macro.exp.addwins(agent->visitexpand);
		*(temp.begin()) = macro;
	}else if(losses >= 2){ //proven loss, but at least try to block one of them
		node->outcome = +opponent;
		node->proofdepth = 2;
		node->bestmove = loss->move;
		node->children.unlock();
		temp.dealloc(agent->ctmem);
		return true;
	}

	if(agent->dynwiden > 0) //sort in decreasing order by knowledge
		std::sort(temp.begin(), temp.end(), sort_node_know);

	PLUS(agent->nodes, temp.num());
	node->children.swap(temp);
	assert(temp.unlock());

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
sete 0x1e(%rsp)
jne 0x16f2b
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x40(%rdi), %rax
movl 0xc8(%rax), %eax
testl %eax, %eax
je 0x16afc
leaq 0x2f18(%r14), %rdi
xorl %edx, %edx
testl %eax, %eax
setg %dl
movl 0x8(%r13), %ecx
movq %r13, %rsi
callq 0x179fe
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movb 0x9(%r13), %al
leaq 0x1f89e(%rip), %rcx     # 0x363b1
movswl 0x2(%r13), %edx
movswl 0x4(%r13), %esi
subl %esi, %edx
xorl %esi, %esi
cmpb (%rcx), %al
cmovll %edx, %esi
movl $0x4130, %edx           # imm = 0x4130
addq 0x40(%r14), %rdx
callq 0x12fca
movb 0x8(%r13), %bpl
movq 0x10(%rsp), %rbx
movq %r13, %rdi
callq 0x13010
leaq 0x28(%r12), %rcx
movq %rcx, 0x38(%rsp)
movb $0x3, %dil
movb %bpl, 0x1f(%rsp)
subb %bpl, %dil
leaq 0x10(%rbx), %r15
xorl %esi, %esi
cmpq $0x2, %rbx
movl $0x0, %ecx
movq %rcx, 0x28(%rsp)
cmovbq %rsi, %r15
leaq 0x78(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
leaq 0x40(%rsp), %rbp
movb %dil, 0xf(%rsp)
movzbl %dil, %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x24(%rsp)
movzwl 0x82(%rsp), %ebx
cmpw $-0x1, %bx
je 0x16cfa
movl 0x80(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x1f7ec(%rip), %rcx     # 0x363ae
movb (%rcx), %cl
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x40(%rsp)
movw $0x0, 0x60(%rsp)
movb %cl, 0x62(%rsp)
movb $0x0, 0x63(%rsp)
movw %ax, 0x64(%rsp)
movw $0x78fc, 0x66(%rsp)     # imm = 0x78FC
movq $0x0, 0x68(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
vzeroupper
callq 0x1282c
movq %rbp, %rdi
callq 0x122ba
movq 0x40(%r14), %rax
cmpl $0x0, 0xa4(%rax)
je 0x16cc3
movl 0x8(%r13), %edx
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x17b04
movb %al, 0x22(%r15)
movq 0x40(%r14), %rax
cmpl $0x2, 0xa4(%rax)
jl 0x16c68
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
movl 0x34(%rsp), %edx
callq 0x17b04
xorl %ecx, %ecx
cmpb 0xf(%rsp), %al
sete %cl
movq 0x28(%rsp), %rax
cmoveq %r15, %rax
movq %rax, 0x28(%rsp)
addl %ecx, 0x24(%rsp)
movb 0x22(%r15), %al
cmpb 0x1f(%rsp), %al
jne 0x16cc3
movb %al, 0x22(%r12)
movb $0x1, 0x23(%r12)
movzwl 0x20(%rsp), %eax
movw %ax, 0x26(%r12)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%r12)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16ce7
movq 0x40(%r14), %rdi
movl $0x4130, %eax           # imm = 0x4130
addq %rax, %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16ce7
callq 0x1353a
xorl %ecx, %ecx
jmp 0x16ce7
movq 0x40(%r14), %rax
cmpb $0x1, 0x84(%rax)
jne 0x16ce1
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x17354
addq $0x30, %r15
movb $0x1, %cl
testb %cl, %cl
je 0x16cfa
leaq 0x78(%rsp), %rdi
callq 0x13058
jmp 0x16b9e
cmpw $-0x1, %bx
jne 0x16f1f
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x16d21
movzwl 0x6(%rax), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
leaq 0x10(%rax,%rcx), %rax
jmp 0x16d23
xorl %eax, %eax
cmpq %rax, %r15
jne 0x16f41
cmpl $0x1, 0x24(%rsp)
jne 0x16dd3
leaq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x17c0e
movl $0x4130, %ebx           # imm = 0x4130
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16d6f
movq 0x40(%r14), %rdi
addq %rbx, %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16d6f
callq 0x1353a
addq 0x40(%r14), %rbx
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x12fca
movq 0x40(%r14), %rax
movl 0xa8(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
leaq 0x40(%rsp), %rsi
vpaddq 0x10(%rsi), %xmm0, %xmm0
vmovdqu %xmm0, 0x10(%rsi)
movq 0x10(%rsp), %rax
leaq 0x10(%rax), %rcx
xorl %edi, %edi
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
callq 0x1282c
leaq 0x40(%rsp), %rdi
callq 0x122ba
jmp 0x16e34
jle 0x16e34
movb 0xf(%rsp), %al
movb %al, 0x22(%r12)
movb $0x2, 0x23(%r12)
movq 0x28(%rsp), %rax
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%r12)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%r12)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16f1f
movl $0x4130, %edi           # imm = 0x4130
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16f1f
callq 0x1353a
jmp 0x16f1f
movq 0x40(%r14), %rax
vmovss 0x98(%rax), %xmm0
vpxor %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x16ec2
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
xorl %r15d, %r15d
movl $0x0, %ebx
cmpq $0x2, %rcx
cmovaeq %rax, %rbx
jb 0x16e76
movzwl 0x6(%rcx), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
addq %rcx, %rax
movq %rax, %r15
cmpq %r15, %rbx
je 0x16ec2
movq %r15, %rax
subq %rbx, %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
lzcntq %rcx, %rdx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x4a3(%rip), %rcx       # 0x17348
movq %rbx, %rdi
movq %r15, %rsi
callq 0x187d5
leaq 0x491(%rip), %rdx       # 0x17348
movq %rbx, %rdi
movq %r15, %rsi
callq 0x188b4
movq 0x40(%r14), %rax
movq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x16ed7
movzwl 0x6(%rcx), %ecx
jmp 0x16ed9
xorl %ecx, %ecx
movq 0x38(%rsp), %rdx
lock
addq %rcx, 0x4110(%rax)
movq (%rdx), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rdx)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x16f00
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x16f0f
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16f60
cmpq $0x0, 0x10(%rsp)
jne 0x16fb1
movb 0x1e(%rsp), %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c8e2(%rip), %rdi     # 0x3382a
leaq 0x1c87a(%rip), %rsi     # 0x337c9
leaq 0x1c8e8(%rip), %rcx     # 0x3383e
movl $0xa3, %edx
callq 0x51d0
leaq 0x1c927(%rip), %rdi     # 0x3388e
leaq 0x1c85b(%rip), %rsi     # 0x337c9
leaq 0x1c8c9(%rip), %rcx     # 0x3383e
movl $0xba, %edx
callq 0x51d0
jmp 0x16f8f
jmp 0x16f8f
jmp 0x16f94
jmp 0x16f8f
jmp 0x16f8f
jmp 0x16f8f
jmp 0x16f8f
jmp 0x16f8f
movq %rax, %rbx
jmp 0x16fa1
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x122ba
cmpq $0x0, 0x10(%rsp)
jne 0x16fb1
movq %rbx, %rdi
callq 0x5670
leaq 0x1b880(%rip), %rdi     # 0x32838
leaq 0x1b886(%rip), %rsi     # 0x32845
leaq 0x1b898(%rip), %rcx     # 0x3285e
movl $0x7a, %edx
callq 0x51d0