// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x1
popq %rcx
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x18(%rdx)
sete %bl
jne 0x120fd
andq $0x0, 0x10(%rsp)
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r14
movq %rsi, %rdi
callq 0xb90a
movl $0xe8, %edx
addq 0x40(%r14), %rdx
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0xf6f6
movq 0x10(%rsp), %rcx
xorl %r12d, %r12d
movl $0x0, %ebp
leaq 0x10(%rcx), %rax
cmpq $0x2, %rcx
cmovaeq %rax, %rbp
jb 0x11f65
movzwl 0x6(%rcx), %ecx
shll $0x5, %ecx
addq %rcx, %rax
movq %rax, %r12
movq 0x40(%r14), %rax
movb %bl, 0xf(%rsp)
movzbl 0x6c(%rax), %edx
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rbx
callq 0xb2ac
leaq 0x18(%r15), %rax
movq %r15, 0x40(%rsp)
leaq 0x20(%rsp), %r15
xorl %r13d, %r13d
movq %rax, 0x18(%rsp)
movb 0x78(%rsp), %cl
cmpb $0x23, %cl
jg 0x12045
cmpq %r12, %rbp
je 0x12045
leaq 0x12c62(%rip), %rax     # 0x24c16
movzwl 0x78(%rsp), %ecx
vxorps %xmm0, %xmm0, %xmm0
andw $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
andq $0x0, 0x38(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf182
movq %r15, %rdi
callq 0xed1e
movq 0x40(%r14), %rax
cmpl $0x0, 0x64(%rax)
je 0x12019
leaq 0x50(%rsp), %rdi
callq 0xa9d0
movb %al, 0x12(%rbp)
cmpb 0x19(%rbx), %al
je 0x12062
movq 0x40(%r14), %rax
cmpb $0x1, 0x50(%rax)
jne 0x1202f
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
movq %rbp, %rcx
callq 0x121fc
leaq 0x48(%rsp), %rdi
callq 0xb372
incl %r13d
addq $0x20, %rbp
jmp 0x11f97
movq 0x40(%r14), %rax
cmpb $0x1, 0x6c(%rax)
jne 0x12097
movq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x11468
movq 0x40(%r14), %rax
jmp 0x120a9
movq 0x40(%rsp), %rdx
xorl %ecx, %ecx
movb %al, 0x12(%rdx)
movb $0x1, 0x13(%rdx)
movzwl 0x78(%rsp), %eax
movw %ax, 0x16(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x18(%rdx)
movl $0xe8, %esi
addq 0x40(%r14), %rsi
leaq 0x10(%rsp), %rdi
callq 0xfb70
jmp 0x120e2
cmpb $0x24, %cl
jl 0x12130
cmpq %r12, %rbp
jne 0x12130
movq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x120ba
movzwl 0x6(%rcx), %ecx
jmp 0x120bc
xorl %ecx, %ecx
movq 0x18(%rsp), %rdi
lock
addq %rcx, 0xc8(%rax)
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0xfcc2
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%rbx)
jne 0x12111
leaq 0x80(%rsp), %rdi
callq 0xb916
leaq 0x10(%rsp), %rdi
callq 0xfb4a
movb 0xf(%rsp), %bl
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf8c2(%rip), %rdi      # 0x219da
leaq 0xf7e2(%rip), %rsi      # 0x21901
leaq 0xf860(%rip), %rcx      # 0x21986
movl $0x9c, %edx
callq 0x5220
leaq 0xf833(%rip), %rdi      # 0x2196a
leaq 0xf7c3(%rip), %rsi      # 0x21901
leaq 0xf841(%rip), %rcx      # 0x21986
movl $0x94, %edx
callq 0x5220
jmp 0x1215c
jmp 0x12153
movq %rax, %rbx
jmp 0x1217b
jmp 0x1215c
jmp 0x1215c
movq %rax, %rbx
jmp 0x1216e
leaq 0x20(%rsp), %rdi
movq %rax, %rbx
callq 0xed1e
leaq 0x80(%rsp), %rdi
callq 0xb916
leaq 0x10(%rsp), %rdi
callq 0xfb4a
movq %rbx, %rdi
callq 0x5690
nop