// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]pentagod/agentmctsthread.cpp
// Function: AgentMCTS::MCTSThread::create_children(Board const&, AgentMCTS::Node*, int)
bool AgentMCTS::MCTSThread::create_children(const Board & board, Node * node, int toplay){
	if(!node->children.lock())
		return false;

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

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

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

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

		if(player->knowledge)
			add_knowledge(after, node, child);
		nummoves++;
	}

	if(player->prunesymmetry)
		temp.shrink(nummoves); //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(player->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 $0xa8, %rsp
movq %rdx, %r12
pushq $0x1
popq %rdx
xorl %eax, %eax
lock
cmpxchgq %rdx, 0x20(%r12)
movl %ecx, 0x1c(%rsp)
sete %r15b
jne 0x175cf
andq $0x0, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0xb35c
movl $0xf0, %edx
addq 0x40(%r14), %rdx
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x17728
movq 0x10(%rsp), %rcx
xorl %ebp, %ebp
movl $0x0, %r13d
leaq 0x10(%rcx), %rax
cmpq $0x2, %rcx
cmovaeq %rax, %r13
jb 0x17430
movzwl 0x6(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, %rbp
movq 0x40(%r14), %rax
movb %r15b, 0xf(%rsp)
movzbl 0x6c(%rax), %edx
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0xac78
leaq 0x20(%r12), %rax
leaq 0x60(%rsp), %rbx
leaq 0x30(%rsp), %r15
movq %r12, 0x28(%rsp)
xorl %r12d, %r12d
movq %rax, 0x20(%rsp)
movb 0x88(%rsp), %cl
cmpb $0x23, %cl
jg 0x17509
cmpq %rbp, %r13
je 0x17509
movl 0x88(%rsp), %eax
vxorps %xmm0, %xmm0, %xmm0
movl $0xfd0000, 0x40(%rsp)   # imm = 0xFD0000
vmovaps %xmm0, 0x30(%rsp)
movl %eax, 0x44(%rsp)
movw $0x8efc, 0x48(%rsp)     # imm = 0x8EFC
andq $0x0, 0x50(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x159f6
movq %r15, %rdi
callq 0x157d4
movq 0x40(%r14), %rax
cmpl $0x0, 0x64(%rax)
je 0x174df
movq %rbx, %rdi
callq 0xae70
movb %al, 0x12(%r13)
movsbl %al, %ecx
cmpl 0x1c(%rsp), %ecx
je 0x1752b
movq 0x40(%r14), %rax
cmpb $0x1, 0x50(%rax)
jne 0x174f3
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rcx
callq 0x176b6
leaq 0x58(%rsp), %rdi
callq 0xad32
incl %r12d
addq $0x28, %r13
jmp 0x17466
movq 0x40(%r14), %rax
cmpb $0x1, 0x6c(%rax)
jne 0x17568
movq 0x10(%rsp), %rdi
movl %r12d, %esi
callq 0x17b24
movq 0x40(%r14), %rax
movb 0xf(%rsp), %r15b
jmp 0x1757f
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
movb %al, 0x12(%rdx)
movb $0x1, 0x13(%rdx)
movzwl 0x88(%rsp), %eax
movw %ax, 0x18(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x20(%rdx)
movl $0xf0, %esi
addq 0x40(%r14), %rsi
leaq 0x10(%rsp), %rdi
callq 0x161c2
movb 0xf(%rsp), %r15b
jmp 0x175b8
movb 0xf(%rsp), %r15b
cmpb $0x24, %cl
jl 0x17603
cmpq %rbp, %r13
jne 0x17603
movq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x17590
movzwl 0x6(%rcx), %ecx
jmp 0x17592
xorl %ecx, %ecx
movq 0x20(%rsp), %rdi
lock
addq %rcx, 0xd0(%rax)
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x165b8
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%rbx)
jne 0x175e4
leaq 0x90(%rsp), %rdi
callq 0xb368
leaq 0x10(%rsp), %rdi
callq 0x1619c
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8b12(%rip), %rdi      # 0x200fd
leaq 0x8a57(%rip), %rsi      # 0x20049
leaq 0x8abc(%rip), %rcx      # 0x200b5
movl $0x95, %edx
callq 0x41c0
leaq 0x8a8f(%rip), %rdi      # 0x20099
leaq 0x8a38(%rip), %rsi      # 0x20049
leaq 0x8a9d(%rip), %rcx      # 0x200b5
movl $0x8d, %edx
callq 0x41c0
jmp 0x1762d
jmp 0x17626
movq %rax, %rbx
jmp 0x1764c
jmp 0x1762d
movq %rax, %rbx
jmp 0x1763f
leaq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0x157d4
leaq 0x90(%rsp), %rdi
callq 0xb368
leaq 0x10(%rsp), %rdi
callq 0x1619c
movq %rbx, %rdi
callq 0x4590