// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
pushq $0x1
popq %rcx
movq %rdx, 0x18(%rsp)
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
sete %al
jne 0x11ee6
movq 0x40(%rdi), %rdx
movb %al, 0x7(%rsp)
movq %rsi, %r13
movq %rdi, %r14
movl 0xc8(%rdx), %eax
testl %eax, %eax
je 0x11b9b
movl 0x8(%r13), %ecx
xorl %edx, %edx
testl %eax, %eax
leaq 0x2f18(%r14), %rdi
movq %r13, %rsi
setg %dl
callq 0x126d2
movq 0x40(%r14), %rdx
leaq 0x8(%rsp), %rdi
leaq 0x1530a(%rip), %rcx     # 0x26eb1
andq $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 0xe748
movb 0x8(%r13), %bl
movq 0x8(%rsp), %r12
movq %r13, %rdi
callq 0xe78e
movq 0x18(%rsp), %rcx
movb $0x3, %dil
leaq 0x10(%r12), %r15
xorl %esi, %esi
subb %bl, %dil
cmpq $0x2, %r12
leaq 0x30(%rsp), %r12
cmovbq %rsi, %r15
leaq 0x68(%rsp), %rsi
xorl %ebp, %ebp
movb %dil, 0x6(%rsp)
movq %rax, (%rsi)
movl %edx, 0x8(%rsi)
shrq $0x20, %rdx
movzbl %dil, %eax
movb %dl, 0xc(%rsi)
movl %eax, 0x2c(%rsp)
addq $0x28, %rcx
movq %rcx, 0x60(%rsp)
movl $0x0, %ecx
movq %rcx, 0x20(%rsp)
cmpw $-0x1, 0x72(%rsp)
je 0x11d22
leaq 0x15267(%rip), %rcx     # 0x26eae
movl 0x70(%rsp), %eax
vpxor %xmm0, %xmm0, %xmm0
andw $0x0, 0x50(%rsp)
vmovdqu %ymm0, 0x30(%rsp)
movb (%rcx), %cl
movl %eax, 0x14(%rsp)
movb %cl, 0x52(%rsp)
movb $0x0, 0x53(%rsp)
movw %ax, 0x54(%rsp)
movw $0x78fc, 0x56(%rsp)     # imm = 0x78FC
andq $0x0, 0x58(%rsp)
movq %r15, %rdi
movq %r12, %rsi
vzeroupper
callq 0xe0da
movq %r12, %rdi
callq 0xdc78
movq 0x40(%r14), %rax
cmpl $0x0, 0xa4(%rax)
je 0x11cf3
leaq 0x14(%rsp), %rsi
movq %r13, %rdi
callq 0x12774
movb %al, 0x22(%r15)
movq 0x40(%r14), %rcx
cmpl $0x2, 0xa4(%rcx)
jl 0x11ceb
movl 0x2c(%rsp), %edx
leaq 0x14(%rsp), %rsi
movq %r13, %rdi
callq 0x1277c
xorl %ecx, %ecx
cmpb 0x6(%rsp), %al
movq 0x20(%rsp), %rax
cmoveq %r15, %rax
sete %cl
movq %rax, 0x20(%rsp)
movb 0x22(%r15), %al
addl %ecx, %ebp
cmpb %bl, %al
je 0x11d3c
movq 0x40(%r14), %rax
cmpb $0x1, 0x84(%rax)
jne 0x11d0f
movq 0x18(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x12220
addq $0x30, %r15
leaq 0x68(%rsp), %rdi
callq 0xe7ae
jmp 0x11c34
movq 0x8(%rsp), %rax
cmpq $0x2, %rax
jb 0x11d74
movzwl 0x6(%rax), %ecx
imulq $0x30, %rcx, %rcx
leaq 0x10(%rax,%rcx), %rcx
jmp 0x11d76
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
movb %bl, 0x22(%rdx)
movb $0x1, 0x23(%rdx)
movzwl 0x14(%rsp), %eax
movw %ax, 0x26(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x28(%rdx)
movl $0x4130, %esi           # imm = 0x4130
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0xec5e
jmp 0x11ed8
xorl %ecx, %ecx
cmpq %rcx, %r15
jne 0x11ef5
cmpl $0x1, %ebp
jne 0x11e14
movq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x1283c
movq 0x40(%r14), %rsi
movl $0x4130, %ebx           # imm = 0x4130
addq %rbx, %rsi
leaq 0x8(%rsp), %rdi
callq 0xec5e
addq 0x40(%r14), %rbx
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
callq 0xe748
movq 0x40(%r14), %rax
leaq 0x30(%rsp), %rsi
xorl %edi, %edi
movl 0xa8(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
movq 0x8(%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 0xe0da
leaq 0x30(%rsp), %rdi
callq 0xdc78
movq 0x8(%rsp), %rax
jmp 0x11e56
jle 0x11e56
movq 0x18(%rsp), %rdx
movb 0x6(%rsp), %al
xorl %ecx, %ecx
movb %al, 0x22(%rdx)
movq 0x20(%rsp), %rax
movb $0x2, 0x23(%rdx)
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x28(%rdx)
movl $0x4130, %esi           # imm = 0x4130
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0xec5e
jmp 0x11ed8
movq 0x40(%r14), %rcx
vpxor %xmm1, %xmm1, %xmm1
vmovss 0x98(%rcx), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x11ea4
leaq 0x10(%rax), %rcx
xorl %esi, %esi
movl $0x0, %edi
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
jb 0x11e8f
movzwl 0x6(%rax), %eax
imulq $0x30, %rax, %rax
addq %rax, %rcx
movq %rcx, %rsi
leaq 0x37d(%rip), %rdx       # 0x12213
callq 0x133ee
movq 0x40(%r14), %rcx
movq 0x8(%rsp), %rax
cmpq $0x2, %rax
jb 0x11eb0
movzwl 0x6(%rax), %eax
jmp 0x11eb2
xorl %eax, %eax
lock
addq %rax, 0x4110(%rcx)
leaq 0x8(%rsp), %rbx
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0xef10
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%rbx)
jne 0x11f14
leaq 0x8(%rsp), %rdi
callq 0xec38
movb 0x7(%rsp), %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1269e(%rip), %rdi     # 0x2459a
leaq 0x12636(%rip), %rsi     # 0x24539
leaq 0x126a4(%rip), %rcx     # 0x245ae
movl $0xa3, %edx
callq 0x51f0
leaq 0x126e3(%rip), %rdi     # 0x245fe
leaq 0x12617(%rip), %rsi     # 0x24539
leaq 0x12685(%rip), %rcx     # 0x245ae
movl $0xba, %edx
callq 0x51f0
jmp 0x11f45
jmp 0x11f45
jmp 0x11f45
jmp 0x11f4a
jmp 0x11f45
jmp 0x11f45
jmp 0x11f45
jmp 0x11f45
jmp 0x11f45
movq %rax, %rbx
jmp 0x11f57
leaq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0xdc78
leaq 0x8(%rsp), %rdi
callq 0xec38
movq %rbx, %rdi
callq 0x5650
nop