// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movq %rdx, 0x138(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x138(%rsp), %rdi
addq $0x18, %rdi
callq 0x26510
testb $0x1, %al
jne 0x25c2e
movb $0x0, 0x157(%rsp)
jmp 0x26118
leaq 0x130(%rsp), %rdi
callq 0x1f2f0
movq 0x140(%rsp), %rdi
callq 0x168e0
movl %eax, 0x6c(%rsp)
jmp 0x25c4e
movl 0x6c(%rsp), %esi
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0xe8, %rdx
leaq 0x130(%rsp), %rdi
callq 0x1e3e0
jmp 0x25c71
leaq 0x130(%rsp), %rdi
callq 0x1d7b0
movq %rax, 0x60(%rsp)
jmp 0x25c85
movq 0x60(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x1d800
movq %rax, 0x58(%rsp)
jmp 0x25ca6
movq 0x70(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0x110(%rsp)
movq 0x140(%rsp), %rsi
movq 0x40(%rax), %rax
movzbl 0x6c(%rax), %edx
andl $0x1, %edx
leaq 0xc0(%rsp), %rdi
callq 0x15670
jmp 0x25cda
movl $0x0, 0xbc(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x157e0
movb %al, 0x57(%rsp)
jmp 0x25cf8
movb 0x57(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x56(%rsp)
jne 0x25d20
jmp 0x25d09
movq 0x118(%rsp), %rax
cmpq 0x110(%rsp), %rax
setne %al
movb %al, 0x56(%rsp)
movb 0x56(%rsp), %al
testb $0x1, %al
jne 0x25d2d
jmp 0x25fa5
leaq 0xc0(%rsp), %rdi
callq 0x15960
movq %rax, 0x48(%rsp)
jmp 0x25d41
movq 0x48(%rsp), %rsi
leaq 0x35acb(%rip), %rax     # 0x5b818
movb (%rax), %al
movb %al, 0x97(%rsp)
movzbl 0x97(%rsp), %edx
leaq 0x98(%rsp), %rdi
callq 0x1e450
jmp 0x25d6d
movq 0x118(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x1d6c0
jmp 0x25d84
leaq 0x98(%rsp), %rdi
callq 0x1cfa0
leaq 0xc0(%rsp), %rdi
callq 0x15810
movq %rax, 0x40(%rsp)
jmp 0x25da5
movq 0x70(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x88(%rsp)
movq 0x40(%rax), %rax
cmpl $0x0, 0x64(%rax)
je 0x25f37
movq 0x88(%rsp), %rdi
callq 0x14100
movb %al, 0x3f(%rsp)
jmp 0x25dd8
movb 0x3f(%rsp), %al
movb %al, 0x87(%rsp)
movq 0x118(%rsp), %rax
movb 0x87(%rsp), %cl
movb %cl, 0x12(%rax)
movq 0x118(%rsp), %rax
addq $0x12, %rax
movq %rax, 0x30(%rsp)
movq 0x140(%rsp), %rdi
callq 0x14b50
movb %al, 0x3e(%rsp)
jmp 0x25e19
movq 0x30(%rsp), %rdi
movb 0x3e(%rsp), %al
movb %al, 0x86(%rsp)
leaq 0x86(%rsp), %rsi
callq 0x1d9c0
movb %al, 0x2f(%rsp)
jmp 0x25e3c
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x25e49
jmp 0x25f35
movq 0x118(%rsp), %rcx
movq 0x138(%rsp), %rax
movb 0x12(%rcx), %cl
movb %cl, 0x12(%rax)
movq 0x138(%rsp), %rax
movb $0x1, 0x13(%rax)
leaq 0xc0(%rsp), %rdi
callq 0x15960
movq %rax, 0x20(%rsp)
jmp 0x25e7f
movq 0x20(%rsp), %rcx
movq 0x138(%rsp), %rax
movw (%rcx), %cx
movw %cx, 0x16(%rax)
movq 0x138(%rsp), %rdi
addq $0x18, %rdi
callq 0x26550
jmp 0x25ea6
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0xe8, %rsi
leaq 0x130(%rsp), %rdi
callq 0x1f350
jmp 0x25ec5
movb $0x1, 0x157(%rsp)
movl $0x1, 0x80(%rsp)
jmp 0x260e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x26109
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x260fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x1cfa0
jmp 0x260fc
jmp 0x25f37
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x50(%rax)
je 0x25f6c
movq 0x70(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq 0x118(%rsp), %rcx
callq 0x26230
jmp 0x25f6a
jmp 0x25f6c
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x15820
jmp 0x25f8c
movq 0x118(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x118(%rsp)
jmp 0x25ce5
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x6c(%rax)
je 0x25fcc
movl 0xbc(%rsp), %esi
leaq 0x130(%rsp), %rdi
callq 0x1e4c0
jmp 0x25fca
jmp 0x26034
leaq 0xc0(%rsp), %rdi
callq 0x157e0
movb %al, 0x1f(%rsp)
jmp 0x25fdf
movb 0x1f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x1e(%rsp)
jne 0x25ff0
jmp 0x26007
movq 0x118(%rsp), %rax
cmpq 0x110(%rsp), %rax
sete %al
movb %al, 0x1e(%rsp)
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0x26011
jmp 0x26013
jmp 0x26032
leaq 0x1f930(%rip), %rdi     # 0x4594a
leaq 0x1f8c0(%rip), %rsi     # 0x458e1
movl $0x94, %edx
leaq 0x1f939(%rip), %rcx     # 0x45966
callq 0x52d0
jmp 0x26034
movq 0x70(%rsp), %rax
movq 0x40(%rax), %rax
addq $0xc8, %rax
movq %rax, 0x10(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x1bc90
movl %eax, 0x18(%rsp)
jmp 0x2605b
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0x7c(%rsp), %rsi
callq 0x1e4f0
jmp 0x26074
movq 0x138(%rsp), %rdi
addq $0x18, %rdi
leaq 0x130(%rsp), %rsi
callq 0x1f760
jmp 0x2608f
leaq 0x130(%rsp), %rdi
callq 0x26550
movb %al, 0xf(%rsp)
jmp 0x260a2
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x260ac
jmp 0x260ae
jmp 0x260cd
leaq 0x1f905(%rip), %rdi     # 0x459ba
leaq 0x1f825(%rip), %rsi     # 0x458e1
movl $0x9c, %edx
leaq 0x1f89e(%rip), %rcx     # 0x45966
callq 0x52d0
movb $0x1, 0x157(%rsp)
movl $0x1, 0x80(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x15940
leaq 0x130(%rsp), %rdi
callq 0x1f310
jmp 0x26118
leaq 0xc0(%rsp), %rdi
callq 0x15940
leaq 0x130(%rsp), %rdi
callq 0x1f310
jmp 0x26129
movb 0x157(%rsp), %al
andb $0x1, %al
addq $0x158, %rsp            # imm = 0x158
retq
movq 0x128(%rsp), %rdi
callq 0x57a0
nopw %cs:(%rax,%rax)