// File: tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::create_children(Morat::Hex::Board const&, Morat::Hex::AgentMCTS::Node*)
bool AgentMCTS::AgentThread::create_children(const Board & board, Node * node){
	if(!node->children.lock())
		return false;

	if(agent->dists){
		dists.run(&board, (agent->dists > 0), board.to_play());
	}

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

	Side to_play = board.to_play();
	Side opponent = ~to_play;
	int losses = 0;

	Node * child = temp.begin(),
	     * loss  = NULL;
	for (auto move : board) {
		*child = Node(move);

		if(agent->minimax){
			child->outcome = board.test_outcome(move);

			if(agent->minimax >= 2 && board.test_outcome(move, opponent) == +opponent){
				losses++;
				loss = child;
			}

			if(child->outcome == +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(board, node, child);
		child++;
	}
	assert(child == temp.end());

	//Make a macro move, add experience to the move so the current simulation continues past this move
	if(losses == 1){
		Node macro = *loss;
		temp.dealloc(agent->ctmem);
		temp.alloc(1, agent->ctmem);
		macro.exp.addwins(agent->visitexpand);
		*(temp.begin()) = macro;
	}else if(losses >= 2){ //proven loss, but at least try to block one of them
		node->outcome = +opponent;
		node->proofdepth = 2;
		node->bestmove = loss->move;
		node->children.unlock();
		temp.dealloc(agent->ctmem);
		return true;
	}

	if(agent->dynwiden > 0) //sort in decreasing order by knowledge
		std::sort(temp.begin(), temp.end(), sort_node_know);

	PLUS(agent->nodes, temp.num());
	node->children.swap(temp);
	assert(temp.unlock());

	return true;
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
movq %rdx, 0x1c8(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x1c8(%rsp), %rdi
addq $0x28, %rdi
callq 0x29230
testb $0x1, %al
jne 0x27821
movb $0x0, 0x1e7(%rsp)
jmp 0x280f6
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0xc8(%rax)
je 0x278bb
movq 0xc0(%rsp), %rax
movq %rax, %rcx
addq $0x2f18, %rcx           # imm = 0x2F18
movq %rcx, 0xa8(%rsp)
movq 0x1d0(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
movq 0x40(%rax), %rax
cmpl $0x0, 0xc8(%rax)
setg %al
movb %al, 0xbf(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x1a8a0
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movb %al, %cl
movb 0xbf(%rsp), %al
movb %cl, 0x1c7(%rsp)
movb 0x1c7(%rsp), %cl
movzbl %cl, %ecx
movzbl %al, %edx
andl $0x1, %edx
callq 0x29270
leaq 0x1b8(%rsp), %rdi
callq 0x1e3f0
movq 0x1d0(%rsp), %rdi
callq 0x1d310
movl %eax, 0xa4(%rsp)
jmp 0x278de
movl 0xa4(%rsp), %esi
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0x4130, %rdx           # imm = 0x4130
leaq 0x1b8(%rsp), %rdi
callq 0x1d2a0
jmp 0x27907
movq 0x1d0(%rsp), %rdi
callq 0x1a8a0
movb %al, 0xa3(%rsp)
jmp 0x2791d
movb 0xa3(%rsp), %al
movb %al, 0x1ab(%rsp)
movb 0x1ab(%rsp), %al
movb %al, 0x1a9(%rsp)
movzbl 0x1a9(%rsp), %edi
callq 0x1c680
movb %al, 0xa2(%rsp)
jmp 0x2794f
movb 0xa2(%rsp), %al
movb %al, 0x1aa(%rsp)
movl $0x0, 0x1a4(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1c340
movq %rax, 0x98(%rsp)
jmp 0x2797f
movq 0x98(%rsp), %rax
movq %rax, 0x198(%rsp)
movq $0x0, 0x190(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdi
callq 0x1d370
movq %rdx, 0x88(%rsp)
movq %rax, 0x90(%rsp)
jmp 0x279ca
movq 0x88(%rsp), %rax
movq 0x90(%rsp), %rcx
movq %rcx, 0x168(%rsp)
movq %rax, 0x170(%rsp)
movq 0x168(%rsp), %rax
movq 0x16d(%rsp), %rcx
movq %rcx, 0x17d(%rsp)
movq %rax, 0x178(%rsp)
movq 0x188(%rsp), %rdi
callq 0x1d3a0
movq %rdx, 0x78(%rsp)
movq %rax, 0x80(%rsp)
jmp 0x27a26
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %rcx
movq %rcx, 0x148(%rsp)
movq %rax, 0x150(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x158(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x160(%rsp)
movb 0x154(%rsp), %al
movb %al, 0x164(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x1d3f0
movb %al, 0x77(%rsp)
jmp 0x27a8a
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0x27a97
jmp 0x27dc2
leaq 0x178(%rsp), %rdi
callq 0x1d420
movq %rax, 0x68(%rsp)
jmp 0x27aab
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x144(%rsp)
leaq 0x45a00(%rip), %rax     # 0x6d4c0
movb (%rax), %al
movb %al, 0x10f(%rsp)
movzbl 0x10f(%rsp), %edx
leaq 0x110(%rsp), %rdi
leaq 0x144(%rsp), %rsi
callq 0x1d430
jmp 0x27ae8
movq 0x198(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x1c1d0
jmp 0x27aff
leaq 0x110(%rsp), %rdi
callq 0x1bab0
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0xa4(%rax)
je 0x27d5c
movq 0x1d0(%rsp), %rdi
leaq 0x144(%rsp), %rsi
callq 0x29410
movb %al, 0x67(%rsp)
jmp 0x27b40
movq 0xc0(%rsp), %rax
movb 0x67(%rsp), %cl
movb %cl, 0x10e(%rsp)
movq 0x198(%rsp), %rcx
movb 0x10e(%rsp), %dl
movb %dl, 0x22(%rcx)
movq 0x40(%rax), %rcx
xorl %eax, %eax
cmpl $0x2, 0xa4(%rcx)
movb %al, 0x66(%rsp)
jl 0x27c09
movq 0x1d0(%rsp), %rdi
movb 0x1aa(%rsp), %al
movb %al, 0x10c(%rsp)
movzbl 0x10c(%rsp), %edx
leaq 0x144(%rsp), %rsi
callq 0x29460
movb %al, 0x65(%rsp)
jmp 0x27bad
movb 0x65(%rsp), %al
movb %al, 0x10d(%rsp)
movb 0x1aa(%rsp), %al
movb %al, 0x10a(%rsp)
movzbl 0x10a(%rsp), %edi
callq 0x1fba0
movb %al, 0x64(%rsp)
jmp 0x27bd9
movb 0x64(%rsp), %al
movb %al, 0x10b(%rsp)
leaq 0x10d(%rsp), %rdi
leaq 0x10b(%rsp), %rsi
callq 0x1c810
movb %al, 0x63(%rsp)
jmp 0x27bff
movb 0x63(%rsp), %al
movb %al, 0x66(%rsp)
jmp 0x27c09
movb 0x66(%rsp), %al
testb $0x1, %al
jne 0x27c13
jmp 0x27c75
movl 0x1a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a4(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x190(%rsp)
jmp 0x27c75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x280e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x1bab0
jmp 0x280e7
movq 0x198(%rsp), %rax
addq $0x22, %rax
movq %rax, 0x58(%rsp)
movb 0x1ab(%rsp), %al
movb %al, 0x108(%rsp)
movzbl 0x108(%rsp), %edi
callq 0x1fba0
movb %al, 0x62(%rsp)
jmp 0x27ca7
movq 0x58(%rsp), %rdi
movb 0x62(%rsp), %al
movb %al, 0x109(%rsp)
leaq 0x109(%rsp), %rsi
callq 0x1c810
movb %al, 0x57(%rsp)
jmp 0x27cca
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x27cd7
jmp 0x27d5a
movq 0x198(%rsp), %rcx
movq 0x1c8(%rsp), %rax
movb 0x22(%rcx), %cl
movb %cl, 0x22(%rax)
movq 0x1c8(%rsp), %rax
movb $0x1, 0x23(%rax)
movq 0x1c8(%rsp), %rax
movw 0x144(%rsp), %cx
movw %cx, 0x26(%rax)
movq 0x1c8(%rsp), %rdi
addq $0x28, %rdi
callq 0x296a0
jmp 0x27d20
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4130, %rsi           # imm = 0x4130
leaq 0x1b8(%rsp), %rdi
callq 0x1e450
jmp 0x27d42
movb $0x1, 0x1e7(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x280d8
jmp 0x27d5c
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x84(%rax)
je 0x27d9a
movq 0xc0(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
movq 0x1c8(%rsp), %rdx
movq 0x198(%rsp), %rcx
callq 0x285c0
jmp 0x27d98
jmp 0x27d9a
movq 0x198(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x198(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x1d4b0
jmp 0x27dbd
jmp 0x27a6f
movq 0x198(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1c390
movq %rax, 0x48(%rsp)
jmp 0x27de3
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, %rax
jne 0x27df4
jmp 0x27e13
leaq 0x2675b(%rip), %rdi     # 0x4e556
leaq 0x266f3(%rip), %rsi     # 0x4e4f5
movl $0xa3, %edx
leaq 0x2675c(%rip), %rcx     # 0x4e56a
callq 0x52a0
cmpl $0x1, 0x1a4(%rsp)
jne 0x27f04
movq 0x190(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x296e0
jmp 0x27e38
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4130, %rsi           # imm = 0x4130
leaq 0x1b8(%rsp), %rdi
callq 0x1e450
jmp 0x27e5a
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0x4130, %rdx           # imm = 0x4130
leaq 0x1b8(%rsp), %rdi
movl $0x1, %esi
callq 0x1d2a0
jmp 0x27e81
movq 0xc0(%rsp), %rax
leaq 0xe0(%rsp), %rdi
movq 0x40(%rax), %rax
movl 0xa8(%rax), %eax
movl %eax, %esi
callq 0x1c2a0
jmp 0x27ea4
leaq 0x1b8(%rsp), %rdi
callq 0x1c340
movq %rax, 0x38(%rsp)
jmp 0x27eb8
movq 0x38(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1c1d0
jmp 0x27ecc
leaq 0xd0(%rsp), %rdi
callq 0x1bab0
jmp 0x27fc3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x1bab0
jmp 0x280e7
cmpl $0x2, 0x1a4(%rsp)
jl 0x27fc1
movb 0x1aa(%rsp), %al
movb %al, 0xce(%rsp)
movzbl 0xce(%rsp), %edi
callq 0x1fba0
movb %al, 0x37(%rsp)
jmp 0x27f33
movb 0x37(%rsp), %al
movb %al, 0xcf(%rsp)
movq 0x1c8(%rsp), %rax
movb 0xcf(%rsp), %cl
movb %cl, 0x22(%rax)
movq 0x1c8(%rsp), %rax
movb $0x2, 0x23(%rax)
movq 0x190(%rsp), %rcx
movq 0x1c8(%rsp), %rax
movw 0x24(%rcx), %cx
movw %cx, 0x26(%rax)
movq 0x1c8(%rsp), %rdi
addq $0x28, %rdi
callq 0x296a0
jmp 0x27f87
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4130, %rsi           # imm = 0x4130
leaq 0x1b8(%rsp), %rdi
callq 0x1e450
jmp 0x27fa9
movb $0x1, 0x1e7(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x280d8
jmp 0x27fc3
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
vmovss 0x98(%rax), %xmm0
vpxor %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x28023
leaq 0x1b8(%rsp), %rdi
callq 0x1c340
movq %rax, 0x28(%rsp)
jmp 0x27ff5
leaq 0x1b8(%rsp), %rdi
callq 0x1c390
movq %rax, 0x20(%rsp)
jmp 0x28009
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
leaq 0x576(%rip), %rdx       # 0x28590
callq 0x29790
jmp 0x28021
jmp 0x28023
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
addq $0x4110, %rax           # imm = 0x4110
movq %rax, 0x10(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1a560
movl %eax, 0x1c(%rsp)
jmp 0x2804d
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %eax
movl %eax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rsi
callq 0x1d5c0
jmp 0x2806c
movq 0x1c8(%rsp), %rdi
addq $0x28, %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x1f390
jmp 0x28087
leaq 0x1b8(%rsp), %rdi
callq 0x296a0
movb %al, 0xf(%rsp)
jmp 0x2809a
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x280a4
jmp 0x280a6
jmp 0x280c5
leaq 0x2650d(%rip), %rdi     # 0x4e5ba
leaq 0x26441(%rip), %rsi     # 0x4e4f5
movl $0xba, %edx
leaq 0x264aa(%rip), %rcx     # 0x4e56a
callq 0x52a0
movb $0x1, 0x1e7(%rsp)
movl $0x1, 0x104(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1e410
jmp 0x280f6
leaq 0x1b8(%rsp), %rdi
callq 0x1e410
jmp 0x28107
movb 0x1e7(%rsp), %al
andb $0x1, %al
addq $0x1e8, %rsp            # imm = 0x1E8
retq
movq 0x1b0(%rsp), %rdi
callq 0x5760
nopw %cs:(%rax,%rax)