// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %r14
movl $0x1, %edx
xorl %eax, %eax
lock
cmpxchgq %rdx, 0x20(%r14)
movl %ecx, 0x1c(%rsp)
sete %al
jne 0x21a1a
movq %rsi, %rbx
movq %rdi, %r12
movb %al, 0xf(%rsp)
movq $0x0, 0x10(%rsp)
movq %rsi, %rdi
callq 0xda1e
movl $0xf0, %edx
addq 0x40(%r12), %rdx
leaq 0x10(%rsp), %rdi
movl %eax, %esi
callq 0x21d20
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
xorl %ebp, %ebp
movl $0x0, %r13d
cmpq $0x2, %rcx
cmovaeq %rax, %r13
jb 0x2181a
movzwl 0x6(%rcx), %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rbp
movq 0x40(%r12), %rax
movzbl 0x6c(%rax), %edx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0xd316
movb 0x78(%rsp), %dl
cmpb $0x24, %dl
setge %al
cmpq %rbp, %r13
sete %cl
xorl %r15d, %r15d
cmpb $0x23, %dl
jg 0x21979
cmpq %rbp, %r13
je 0x21979
xorl %r15d, %r15d
leaq 0x20(%rsp), %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
movl $0xfd0000, 0x30(%rsp)   # imm = 0xFD0000
movl 0x78(%rsp), %eax
movl %eax, 0x34(%rsp)
movw $0x8efc, 0x38(%rsp)     # imm = 0x8EFC
movq $0x0, 0x40(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1f57c
movq %rbx, %rdi
callq 0x1f26a
movq 0x40(%r12), %rax
cmpl $0x0, 0x64(%rax)
je 0x21917
cmpb $-0x4, 0x6a(%rsp)
jne 0x218ba
leaq 0x50(%rsp), %rdi
callq 0xdab0
movb %al, 0x6a(%rsp)
movsbl 0x6a(%rsp), %eax
movb %al, 0x12(%r13)
cmpl 0x1c(%rsp), %eax
jne 0x21917
movb %al, 0x12(%r14)
movb $0x1, 0x13(%r14)
movzwl 0x78(%rsp), %eax
movw %ax, 0x18(%r14)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x20(%r14)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x21940
movq 0x40(%r12), %rdi
movl $0xf0, %eax
addq %rax, %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x21940
callq 0x2062e
xorl %ecx, %ecx
jmp 0x21940
movq 0x40(%r12), %rax
cmpb $0x1, 0x50(%rax)
jne 0x2193b
movl 0x74(%rax), %ebx
testl %ebx, %ebx
jle 0x2193b
leaq 0x50(%rsp), %rdi
callq 0x21e14
imull %ebx, %eax
movw %ax, 0x10(%r13)
incl %r15d
movb $0x1, %cl
testb %cl, %cl
je 0x219f8
leaq 0x48(%rsp), %rdi
callq 0xd402
leaq 0x20(%rsp), %rbx
addq $0x28, %r13
movb 0x78(%rsp), %dl
cmpb $0x24, %dl
setge %al
cmpq %rbp, %r13
sete %cl
cmpb $0x23, %dl
jg 0x21979
cmpq %rbp, %r13
jne 0x2185d
movq 0x40(%r12), %rdx
cmpb $0x1, 0x6c(%rdx)
jne 0x21993
movq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0x221ac
jmp 0x2199b
testb %cl, %al
je 0x21a4b
addq $0x20, %r14
movq 0x40(%r12), %rax
movq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x219b5
movzwl 0x6(%rcx), %ecx
jmp 0x219b7
xorl %ecx, %ecx
lock
addq %rcx, 0xd0(%rax)
movq (%r14), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r14)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x219d9
movq %r14, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x219e8
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x21a2c
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x21a0a
callq 0x43d0
cmpq $0x0, 0x10(%rsp)
jne 0x21ab3
movb 0xf(%rsp), %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb76a(%rip), %rdi      # 0x2d19d
leaq 0xb6af(%rip), %rsi      # 0x2d0e9
leaq 0xb714(%rip), %rcx      # 0x2d155
movl $0x95, %edx
callq 0x41b0
leaq 0xb6e7(%rip), %rdi      # 0x2d139
leaq 0xb690(%rip), %rsi      # 0x2d0e9
leaq 0xb6f5(%rip), %rcx      # 0x2d155
movl $0x8d, %edx
callq 0x41b0
jmp 0x21a73
jmp 0x21a6e
movq %rax, %rbx
jmp 0x21aa3
movq %rax, %rbx
jmp 0x21a85
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x1f26a
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x21a97
callq 0x43d0
movq $0x0, 0x90(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x21ab3
movq %rbx, %rdi
callq 0x45b0
leaq 0x86d1(%rip), %rdi      # 0x2a18b
leaq 0x8634(%rip), %rsi      # 0x2a0f5
leaq 0xafd3(%rip), %rcx      # 0x2ca9b
movl $0x73, %edx
callq 0x41b0