// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, 0x20(%rsp)
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
sete %al
jne 0x16fd0
movq 0x40(%rdi), %rdx
movb %al, 0x1b(%rsp)
movq %rsi, %r13
movq %rdi, %r14
movl 0xc8(%rdx), %eax
testl %eax, %eax
je 0x16bbc
movl 0x8(%r13), %ecx
xorl %edx, %edx
testl %eax, %eax
leaq 0x2f18(%r14), %rdi
movq %r13, %rsi
setg %dl
callq 0x17a66
movq 0x40(%r14), %rdx
leaq 0x10(%rsp), %rdi
leaq 0x1f7e9(%rip), %rcx     # 0x363b1
movq $0x0, (%rdi)
movswl 0x2(%r13), %r8d
movswl 0x4(%r13), %esi
movb 0x9(%r13), %al
subl %esi, %r8d
xorl %esi, %esi
cmpb (%rcx), %al
cmovll %r8d, %esi
addq $0x4130, %rdx           # imm = 0x4130
callq 0x130ce
movb 0x8(%r13), %bl
movq 0x10(%rsp), %r12
movq %r13, %rdi
callq 0x13114
movq 0x20(%rsp), %rcx
movb $0x3, %dil
leaq 0x10(%r12), %r15
xorl %esi, %esi
subb %bl, %dil
cmpq $0x2, %r12
leaq 0x40(%rsp), %r12
cmovbq %rsi, %r15
leaq 0x78(%rsp), %rsi
xorl %ebp, %ebp
movb %dil, 0xf(%rsp)
movq %rax, (%rsi)
movl %edx, 0x8(%rsi)
shrq $0x20, %rdx
movzbl %dil, %eax
movb %dl, 0xc(%rsi)
movl %eax, 0x34(%rsp)
addq $0x28, %rcx
movq %rcx, 0x38(%rsp)
movl $0x0, %ecx
movq %rcx, 0x28(%rsp)
cmpw $-0x1, 0x82(%rsp)
je 0x16d54
leaq 0x1f740(%rip), %rcx     # 0x363ae
movl 0x80(%rsp), %eax
vpxor %xmm0, %xmm0, %xmm0
movw $0x0, 0x60(%rsp)
vmovdqu %ymm0, 0x40(%rsp)
movb (%rcx), %cl
movl %eax, 0x1c(%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 %r12, %rsi
vzeroupper
callq 0x1297c
movq %r12, %rdi
callq 0x12448
movq 0x40(%r14), %rax
cmpl $0x0, 0xa4(%rax)
je 0x16d25
movl 0x8(%r13), %edx
leaq 0x1c(%rsp), %rsi
movq %r13, %rdi
callq 0x17b80
movb %al, 0x22(%r15)
movq 0x40(%r14), %rcx
cmpl $0x2, 0xa4(%rcx)
jl 0x16d1d
movl 0x34(%rsp), %edx
leaq 0x1c(%rsp), %rsi
movq %r13, %rdi
callq 0x17b80
xorl %ecx, %ecx
cmpb 0xf(%rsp), %al
movq 0x28(%rsp), %rax
cmoveq %r15, %rax
sete %cl
movq %rax, 0x28(%rsp)
movb 0x22(%r15), %al
addl %ecx, %ebp
cmpb %bl, %al
je 0x16d71
movq 0x40(%r14), %rax
cmpb $0x1, 0x84(%rax)
jne 0x16d41
movq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x173f0
addq $0x30, %r15
leaq 0x78(%rsp), %rdi
callq 0x1315e
jmp 0x16c58
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x16dc6
movzwl 0x6(%rax), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
leaq 0x10(%rax,%rcx), %rcx
jmp 0x16dc8
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movb %bl, 0x22(%rdx)
movb $0x1, 0x23(%rdx)
movzwl 0x1c(%rsp), %eax
movw %ax, 0x26(%rdx)
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16fc0
movl $0x4130, %edi           # imm = 0x4130
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16fc0
callq 0x13640
jmp 0x16fc0
xorl %ecx, %ecx
cmpq %rcx, %r15
jne 0x16fe2
cmpl $0x1, %ebp
jne 0x16e7b
movq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x17c98
movq 0x10(%rsp), %rsi
movl $0x4130, %ebx           # imm = 0x4130
testq %rsi, %rsi
je 0x16e12
movq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
addq %rbx, %rdi
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16e12
callq 0x13640
addq 0x40(%r14), %rbx
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x130ce
movq 0x40(%r14), %rax
leaq 0x40(%rsp), %rsi
xorl %edi, %edi
movl 0xa8(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
movq 0x10(%rsp), %rax
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpaddq 0x10(%rsi), %xmm0, %xmm0
leaq 0x10(%rax), %rcx
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
vmovdqu %xmm0, 0x10(%rsi)
callq 0x1297c
leaq 0x40(%rsp), %rdi
callq 0x12448
movq 0x10(%rsp), %rax
jmp 0x16eda
jle 0x16eda
movq 0x20(%rsp), %rdx
movb 0xf(%rsp), %al
xorl %ecx, %ecx
movb %al, 0x22(%rdx)
movq 0x28(%rsp), %rax
movb $0x2, 0x23(%rdx)
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%rdx)
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16fcc
movl $0x4130, %edi           # imm = 0x4130
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x16fc0
callq 0x13640
jmp 0x16fc0
movq 0x40(%r14), %rcx
vpxor %xmm1, %xmm1, %xmm1
vmovss 0x98(%rcx), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x16f6c
leaq 0x10(%rax), %rdx
xorl %r15d, %r15d
movl $0x0, %ebx
cmpq $0x2, %rax
cmovaeq %rdx, %rbx
jb 0x16f17
movzwl 0x6(%rax), %esi
leaq (%rsi,%rsi,2), %rsi
shll $0x4, %esi
addq %rsi, %rdx
movq %rdx, %r15
cmpq %r15, %rbx
je 0x16f6c
movq %r15, %rax
subq %rbx, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
sarq $0x4, %rax
imulq %rax, %rcx
lzcntq %rcx, %rdx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x49e(%rip), %rcx       # 0x173e4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18861
leaq 0x48c(%rip), %rdx       # 0x173e4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x18940
movq 0x40(%r14), %rcx
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x16f78
movzwl 0x6(%rax), %eax
jmp 0x16f7a
xorl %eax, %eax
movq 0x38(%rsp), %rdx
lock
addq %rax, 0x4110(%rcx)
movq 0x10(%rsp), %rcx
movq (%rdx), %rax
movq %rcx, (%rdx)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x16fa1
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x16fb0
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17001
cmpq $0x0, 0x10(%rsp)
jne 0x17054
movb 0x1b(%rsp), %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c841(%rip), %rdi     # 0x3382a
leaq 0x1c7d9(%rip), %rsi     # 0x337c9
leaq 0x1c847(%rip), %rcx     # 0x3383e
movl $0xa3, %edx
callq 0x51d0
leaq 0x1c886(%rip), %rdi     # 0x3388e
leaq 0x1c7ba(%rip), %rsi     # 0x337c9
leaq 0x1c828(%rip), %rcx     # 0x3383e
movl $0xba, %edx
callq 0x51d0
jmp 0x17032
jmp 0x17032
jmp 0x17032
jmp 0x17037
jmp 0x17032
jmp 0x17032
jmp 0x17032
jmp 0x17032
jmp 0x17032
movq %rax, %rbx
jmp 0x17044
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x12448
cmpq $0x0, 0x10(%rsp)
jne 0x17054
movq %rbx, %rdi
callq 0x5670
leaq 0x1b7dd(%rip), %rdi     # 0x32838
leaq 0x1b7e3(%rip), %rsi     # 0x32845
leaq 0x1b7f5(%rip), %rcx     # 0x3285e
movl $0x7a, %edx
callq 0x51d0
nop