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

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

	Node * child = temp.begin(),
	     * end   = temp.end();
	MoveIterator move(board, agent->prunesymmetry);
	int num_moves = 0;
	for(; !move.done() && child != end; ++move, ++child){
		*child = Node(*move);
		const Board & after = move.board();

		if(agent->minimax){
			child->outcome = after.outcome();

			if(child->outcome == board.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(after, node, child);
		num_moves++;
	}

	if(agent->prunesymmetry)
		temp.shrink(num_moves); //shrink the node to ignore the extra moves
	else{ //both end conditions should happen in parallel
		assert(move.done() && child == end);
	}

	//sort in decreasing order by knowledge
//	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 $0x98, %rsp
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x18(%rdx)
sete %bl
jne 0x1795a
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r12
movq $0x0, 0x10(%rsp)
movq %rsi, %rdi
callq 0xe682
movl $0xe8, %edx
addq 0x40(%r12), %rdx
shll $0x3, %eax
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x14a38
movq 0x10(%rsp), %rcx
xorl %r15d, %r15d
movl $0x0, %r14d
leaq 0x10(%rcx), %rax
cmpq $0x2, %rcx
cmovaeq %rax, %r14
jb 0x1777b
movzwl 0x6(%rcx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
movq %rax, %r15
movq 0x40(%r12), %rax
movq %r13, 0x18(%rsp)
movzbl 0x6c(%rax), %edx
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, 0x40(%rsp)
callq 0xe03e
movb 0x78(%rsp), %dl
movb %bl, 0xf(%rsp)
cmpb $0x24, %dl
setge %al
cmpq %r15, %r14
sete %cl
xorl %ebp, %ebp
cmpb $0x23, %dl
jg 0x178b1
cmpq %r15, %r14
je 0x178b1
addq $0x20, %r14
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
leaq 0x1a5d1(%rip), %rax     # 0x31da6
movzwl 0x78(%rsp), %ecx
vxorps %xmm0, %xmm0, %xmm0
leaq -0x20(%r14), %rdi
movw $0x0, 0x30(%rsp)
vmovaps %xmm0, 0x20(%rsp)
movb (%rax), %al
movb %al, 0x32(%rsp)
movb $0x0, 0x33(%rsp)
movw %cx, 0x34(%rsp)
movw $0xefc, 0x36(%rsp)      # imm = 0xEFC
movq $0x0, 0x38(%rsp)
movq %rbx, %rsi
callq 0x142f0
movq %rbx, %rdi
callq 0x13dc2
movq 0x40(%r12), %rax
cmpl $0x0, 0x64(%rax)
je 0x1785e
movb 0x6a(%rsp), %al
leaq 0x1a570(%rip), %rcx     # 0x31da5
cmpb (%rcx), %al
jne 0x17847
leaq 0x50(%rsp), %rdi
callq 0xe2a8
movb %al, 0x6a(%rsp)
movq 0x40(%rsp), %rcx
movb %al, -0xe(%r14)
cmpb 0x19(%rcx), %al
je 0x1796e
movq 0x40(%r12), %rax
cmpb $0x1, 0x50(%rax)
jne 0x17880
movl 0x74(%rax), %r13d
testl %r13d, %r13d
jle 0x17880
leaq 0x50(%rsp), %rdi
callq 0xe48a
imull %r13d, %eax
movw %ax, -0x10(%r14)
leaq 0x48(%rsp), %rdi
callq 0xe122
movb 0x78(%rsp), %dl
incl %ebp
cmpb $0x24, %dl
setge %al
cmpq %r15, %r14
sete %cl
cmpb $0x23, %dl
jg 0x178b1
leaq 0x20(%r14), %rdx
cmpq %r15, %r14
movq %rdx, %r14
jne 0x177ce
movq 0x40(%r12), %rdx
cmpb $0x1, 0x6c(%rdx)
jne 0x178cf
movq 0x10(%rsp), %rdi
movl %ebp, %esi
callq 0x16b38
movq 0x40(%r12), %rdx
jmp 0x178d7
testb %cl, %al
je 0x179df
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rax
addq $0x18, %rsi
cmpq $0x2, %rax
jb 0x178f1
movzwl 0x6(%rax), %eax
jmp 0x178f3
xorl %eax, %eax
movb 0xf(%rsp), %bl
lock
addq %rax, 0xc8(%rdx)
movq (%rsi), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rsi)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x17919
movq %rsi, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x17928
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x179c0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1794e
callq 0x5490
cmpq $0x0, 0x10(%rsp)
jne 0x17a3f
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdx
xorl %ecx, %ecx
movb %al, 0x12(%rdx)
movb $0x1, 0x13(%rdx)
movzwl 0x78(%rsp), %eax
movw %ax, 0x16(%rdx)
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x18(%rdx)
movq 0x10(%rsp), %rsi
movb 0xf(%rsp), %bl
testq %rsi, %rsi
je 0x1793c
movl $0xe8, %edi
addq 0x40(%r12), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x1793c
callq 0x14ec2
jmp 0x1793c
leaq 0x170b3(%rip), %rdi     # 0x2ea7a
leaq 0x16fd3(%rip), %rsi     # 0x2e9a1
leaq 0x17051(%rip), %rcx     # 0x2ea26
movl $0x9c, %edx
callq 0x5200
leaq 0x17024(%rip), %rdi     # 0x2ea0a
leaq 0x16fb4(%rip), %rsi     # 0x2e9a1
leaq 0x17032(%rip), %rcx     # 0x2ea26
movl $0x94, %edx
callq 0x5200
jmp 0x17a0b
jmp 0x17a02
movq %rax, %rbx
jmp 0x17a2f
jmp 0x17a0b
jmp 0x17a0b
movq %rax, %rbx
jmp 0x17a1d
leaq 0x20(%rsp), %rdi
movq %rax, %rbx
callq 0x13dc2
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x17a2f
callq 0x5490
cmpq $0x0, 0x10(%rsp)
jne 0x17a3f
movq %rbx, %rdi
callq 0x56c0
leaq 0x15f82(%rip), %rdi     # 0x2d9c8
leaq 0x15f88(%rip), %rsi     # 0x2d9d5
leaq 0x15f9e(%rip), %rcx     # 0x2d9f2
movl $0x7a, %edx
callq 0x5200