// File: 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:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x138(%rsp)
movq %rsi, 0x130(%rsp)
movq %rdx, 0x128(%rsp)
movl %ecx, 0x124(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x128(%rsp), %rdi
addq $0x20, %rdi
callq 0x32c20
testb $0x1, %al
jne 0x32445
movb $0x0, 0x147(%rsp)
jmp 0x328b8
leaq 0x118(%rsp), %rdi
callq 0x2ead0
movq 0x130(%rsp), %rdi
callq 0x16f50
movl %eax, 0x5c(%rsp)
jmp 0x32465
movl 0x5c(%rsp), %esi
movq 0x60(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0xf0, %rdx
leaq 0x118(%rsp), %rdi
callq 0x32c60
jmp 0x32488
leaq 0x118(%rsp), %rdi
callq 0x2dc40
movq %rax, 0x50(%rsp)
jmp 0x3249c
movq 0x50(%rsp), %rax
movq %rax, 0x100(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x2dc90
movq %rax, 0x48(%rsp)
jmp 0x324bd
movq 0x60(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0xf8(%rsp)
movq 0x130(%rsp), %rsi
movq 0x40(%rax), %rax
movzbl 0x6c(%rax), %edx
andl $0x1, %edx
leaq 0xa8(%rsp), %rdi
callq 0x15b10
jmp 0x324f1
movl $0x0, 0xa4(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x15c80
movb %al, 0x47(%rsp)
jmp 0x3250f
movb 0x47(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x46(%rsp)
jne 0x32537
jmp 0x32520
movq 0x100(%rsp), %rax
cmpq 0xf8(%rsp), %rax
setne %al
movb %al, 0x46(%rsp)
movb 0x46(%rsp), %al
testb $0x1, %al
jne 0x32544
jmp 0x32748
leaq 0xa8(%rsp), %rdi
callq 0x32cd0
movq %rax, 0x38(%rsp)
jmp 0x32558
movq 0x38(%rsp), %rsi
leaq 0x78(%rsp), %rdi
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
callq 0x32ce0
jmp 0x3256e
movq 0x100(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x2db10
jmp 0x32582
leaq 0x78(%rsp), %rdi
callq 0x2d750
leaq 0xa8(%rsp), %rdi
callq 0x32d50
movq %rax, 0x30(%rsp)
jmp 0x325a0
movq 0x60(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq 0x40(%rax), %rax
cmpl $0x0, 0x64(%rax)
je 0x326dd
movq 0x70(%rsp), %rdi
callq 0x15fb0
movb %al, 0x2f(%rsp)
jmp 0x325cd
movb 0x2f(%rsp), %cl
movq 0x100(%rsp), %rax
movb %cl, 0x12(%rax)
movq 0x100(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl 0x124(%rsp), %eax
jne 0x326db
movq 0x100(%rsp), %rax
movb 0x12(%rax), %cl
movq 0x128(%rsp), %rax
movb %cl, 0x12(%rax)
movq 0x128(%rsp), %rax
movb $0x1, 0x13(%rax)
leaq 0xa8(%rsp), %rdi
callq 0x32cd0
movq %rax, 0x20(%rsp)
jmp 0x3262b
movq 0x20(%rsp), %rcx
movq 0x128(%rsp), %rax
movw (%rcx), %cx
movw %cx, 0x18(%rax)
movq 0x128(%rsp), %rdi
addq $0x20, %rdi
callq 0x32d60
jmp 0x32652
movq 0x60(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0xf0, %rsi
leaq 0x118(%rsp), %rdi
callq 0x2ebc0
jmp 0x32671
movb $0x1, 0x147(%rsp)
movl $0x1, 0x6c(%rsp)
jmp 0x32880
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x328a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x3289c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x2d750
jmp 0x3289c
jmp 0x326dd
movq 0x60(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x50(%rax)
je 0x3270f
movq 0x60(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq 0x100(%rsp), %rcx
callq 0x329a0
jmp 0x3270d
jmp 0x3270f
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x15cb0
jmp 0x3272f
movq 0x100(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x100(%rsp)
jmp 0x324fc
movq 0x60(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x6c(%rax)
je 0x3276f
movl 0xa4(%rsp), %esi
leaq 0x118(%rsp), %rdi
callq 0x32da0
jmp 0x3276d
jmp 0x327d7
leaq 0xa8(%rsp), %rdi
callq 0x15c80
movb %al, 0x1f(%rsp)
jmp 0x32782
movb 0x1f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x1e(%rsp)
jne 0x32793
jmp 0x327aa
movq 0x100(%rsp), %rax
cmpq 0xf8(%rsp), %rax
sete %al
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x327b4
jmp 0x327b6
jmp 0x327d5
leaq 0xe8d4(%rip), %rdi      # 0x41091
leaq 0xe87d(%rip), %rsi      # 0x41041
movl $0x8d, %edx
leaq 0xe8dd(%rip), %rcx      # 0x410ad
callq 0x4250
jmp 0x327d7
movq 0x60(%rsp), %rax
movq 0x40(%rax), %rax
addq $0xd0, %rax
movq %rax, 0x10(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x2dc00
movl %eax, 0x18(%rsp)
jmp 0x327fe
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x68(%rsp), %rsi
callq 0x2edb0
jmp 0x32817
movq 0x128(%rsp), %rdi
addq $0x20, %rdi
leaq 0x118(%rsp), %rsi
callq 0x2fd70
jmp 0x32832
leaq 0x118(%rsp), %rdi
callq 0x32d60
movb %al, 0xf(%rsp)
jmp 0x32845
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x3284f
jmp 0x32851
jmp 0x32870
leaq 0xe89d(%rip), %rdi      # 0x410f5
leaq 0xe7e2(%rip), %rsi      # 0x41041
movl $0x95, %edx
leaq 0xe842(%rip), %rcx      # 0x410ad
callq 0x4250
movb $0x1, 0x147(%rsp)
movl $0x1, 0x6c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x15e30
leaq 0x118(%rsp), %rdi
callq 0x2eb80
jmp 0x328b8
leaq 0xa8(%rsp), %rdi
callq 0x15e30
leaq 0x118(%rsp), %rdi
callq 0x2eb80
jmp 0x328c9
movb 0x147(%rsp), %al
andb $0x1, %al
addq $0x148, %rsp            # imm = 0x148
retq
movq 0x110(%rsp), %rdi
callq 0x4660
nopw %cs:(%rax,%rax)