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

	if(agent->dists || agent->detectdraw){
		dists.run(&board, (agent->dists > 0), (agent->detectdraw ? Side::NONE : board.to_play()));

		if(agent->detectdraw){
//			assert(node->outcome < Outcome::DRAW);
			node->outcome = dists.isdraw(); //could be winnable by only one side

			if(node->outcome == Outcome::DRAW){ //proven draw, neither side can influence the outcome
				node->bestmove = *(board.begin()); //just choose the first move since all are equal at this point
				node->children.unlock();
				return true;
			}
		}
	}

	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 $0x208, %rsp            # imm = 0x208
movq %rdi, 0x1f8(%rsp)
movq %rsi, 0x1f0(%rsp)
movq %rdx, 0x1e8(%rsp)
movq 0x1f8(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x1e8(%rsp), %rdi
addq $0x28, %rdi
callq 0x29b60
testb $0x1, %al
jne 0x27f01
movb $0x0, 0x207(%rsp)
jmp 0x288f9
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0x124(%rax)
jne 0x27f2f
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x100(%rax)
je 0x280be
movq 0xc0(%rsp), %rax
movq %rax, %rcx
addq $0x28e8, %rcx           # imm = 0x28E8
movq %rcx, 0xa8(%rsp)
movq 0x1f0(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
movq 0x40(%rax), %rcx
cmpl $0x0, 0x124(%rcx)
setg %cl
movb %cl, 0xbf(%rsp)
movq 0x40(%rax), %rax
testb $0x1, 0x100(%rax)
je 0x27f8d
leaq 0x47559(%rip), %rax     # 0x6f4db
movb (%rax), %al
movb %al, 0x1e7(%rsp)
jmp 0x27fa1
movq 0x1f0(%rsp), %rdi
callq 0x1aa40
movb %al, 0x1e7(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movb 0xbf(%rsp), %al
movb 0x1e7(%rsp), %cl
movzbl %cl, %ecx
movzbl %al, %edx
andl $0x1, %edx
callq 0x29ba0
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x100(%rax)
je 0x280bc
movq 0xc0(%rsp), %rdi
addq $0x28e8, %rdi           # imm = 0x28E8
callq 0x29d40
movb %al, 0x1e6(%rsp)
movq 0x1e8(%rsp), %rax
movb 0x1e6(%rsp), %cl
movb %cl, 0x22(%rax)
movq 0x1e8(%rsp), %rdi
addq $0x22, %rdi
leaq 0x474bd(%rip), %rsi     # 0x6f4e3
callq 0x1c9e0
testb $0x1, %al
jne 0x28034
jmp 0x280ba
movq 0x1f0(%rsp), %rdi
callq 0x1d540
movq %rdx, 0x1c8(%rsp)
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movl 0x1c8(%rsp), %eax
movl %eax, 0x1d8(%rsp)
movb 0x1cc(%rsp), %al
movb %al, 0x1dc(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x1d5f0
movq %rax, %rcx
movq 0x1e8(%rsp), %rax
movw (%rcx), %cx
movw %cx, 0x26(%rax)
movq 0x1e8(%rsp), %rdi
addq $0x28, %rdi
callq 0x29f60
movb $0x1, 0x207(%rsp)
jmp 0x288f9
jmp 0x280bc
jmp 0x280be
leaq 0x1b8(%rsp), %rdi
callq 0x1e5c0
movq 0x1f0(%rsp), %rdi
callq 0x1d4e0
movl %eax, 0xa4(%rsp)
jmp 0x280e1
movl 0xa4(%rsp), %esi
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0x4190, %rdx           # imm = 0x4190
leaq 0x1b8(%rsp), %rdi
callq 0x1d470
jmp 0x2810a
movq 0x1f0(%rsp), %rdi
callq 0x1aa40
movb %al, 0xa3(%rsp)
jmp 0x28120
movb 0xa3(%rsp), %al
movb %al, 0x1ab(%rsp)
movb 0x1ab(%rsp), %al
movb %al, 0x1a9(%rsp)
movzbl 0x1a9(%rsp), %edi
callq 0x1c850
movb %al, 0xa2(%rsp)
jmp 0x28152
movb 0xa2(%rsp), %al
movb %al, 0x1aa(%rsp)
movl $0x0, 0x1a4(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1c510
movq %rax, 0x98(%rsp)
jmp 0x28182
movq 0x98(%rsp), %rax
movq %rax, 0x198(%rsp)
movq $0x0, 0x190(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdi
callq 0x1d540
movq %rdx, 0x88(%rsp)
movq %rax, 0x90(%rsp)
jmp 0x281cd
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 0x1d570
movq %rdx, 0x78(%rsp)
movq %rax, 0x80(%rsp)
jmp 0x28229
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 0x1d5c0
movb %al, 0x77(%rsp)
jmp 0x2828d
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0x2829a
jmp 0x285c5
leaq 0x178(%rsp), %rdi
callq 0x1d5f0
movq %rax, 0x68(%rsp)
jmp 0x282ae
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x144(%rsp)
leaq 0x4721d(%rip), %rax     # 0x6f4e0
movb (%rax), %al
movb %al, 0x10f(%rsp)
movzbl 0x10f(%rsp), %edx
leaq 0x110(%rsp), %rdi
leaq 0x144(%rsp), %rsi
callq 0x1d600
jmp 0x282eb
movq 0x198(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x1c370
jmp 0x28302
leaq 0x110(%rsp), %rdi
callq 0x1bc50
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0xfc(%rax)
je 0x2855f
movq 0x1f0(%rsp), %rdi
leaq 0x144(%rsp), %rsi
callq 0x29fa0
movb %al, 0x67(%rsp)
jmp 0x28343
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, 0xfc(%rcx)
movb %al, 0x66(%rsp)
jl 0x2840c
movq 0x1f0(%rsp), %rdi
movb 0x1aa(%rsp), %al
movb %al, 0x10c(%rsp)
movzbl 0x10c(%rsp), %edx
leaq 0x144(%rsp), %rsi
callq 0x29ff0
movb %al, 0x65(%rsp)
jmp 0x283b0
movb 0x65(%rsp), %al
movb %al, 0x10d(%rsp)
movb 0x1aa(%rsp), %al
movb %al, 0x10a(%rsp)
movzbl 0x10a(%rsp), %edi
callq 0x20150
movb %al, 0x64(%rsp)
jmp 0x283dc
movb 0x64(%rsp), %al
movb %al, 0x10b(%rsp)
leaq 0x10d(%rsp), %rdi
leaq 0x10b(%rsp), %rsi
callq 0x1c9e0
movb %al, 0x63(%rsp)
jmp 0x28402
movb 0x63(%rsp), %al
movb %al, 0x66(%rsp)
jmp 0x2840c
movb 0x66(%rsp), %al
testb $0x1, %al
jne 0x28416
jmp 0x28478
movl 0x1a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a4(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x190(%rsp)
jmp 0x28478
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x288ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x1bc50
jmp 0x288ea
movq 0x198(%rsp), %rax
addq $0x22, %rax
movq %rax, 0x58(%rsp)
movb 0x1ab(%rsp), %al
movb %al, 0x108(%rsp)
movzbl 0x108(%rsp), %edi
callq 0x20150
movb %al, 0x62(%rsp)
jmp 0x284aa
movq 0x58(%rsp), %rdi
movb 0x62(%rsp), %al
movb %al, 0x109(%rsp)
leaq 0x109(%rsp), %rsi
callq 0x1c9e0
movb %al, 0x57(%rsp)
jmp 0x284cd
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x284da
jmp 0x2855d
movq 0x198(%rsp), %rcx
movq 0x1e8(%rsp), %rax
movb 0x22(%rcx), %cl
movb %cl, 0x22(%rax)
movq 0x1e8(%rsp), %rax
movb $0x1, 0x23(%rax)
movq 0x1e8(%rsp), %rax
movw 0x144(%rsp), %cx
movw %cx, 0x26(%rax)
movq 0x1e8(%rsp), %rdi
addq $0x28, %rdi
callq 0x29f60
jmp 0x28523
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4190, %rsi           # imm = 0x4190
leaq 0x1b8(%rsp), %rdi
callq 0x1e620
jmp 0x28545
movb $0x1, 0x207(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x288db
jmp 0x2855f
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0xdc(%rax)
je 0x2859d
movq 0xc0(%rsp), %rdi
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x198(%rsp), %rcx
callq 0x28e70
jmp 0x2859b
jmp 0x2859d
movq 0x198(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x198(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x1d680
jmp 0x285c0
jmp 0x28272
movq 0x198(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1c560
movq %rax, 0x48(%rsp)
jmp 0x285e6
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, %rax
jne 0x285f7
jmp 0x28616
leaq 0x2817e(%rip), %rdi     # 0x5077c
leaq 0x2810c(%rip), %rsi     # 0x50711
movl $0xae, %edx
leaq 0x2817f(%rip), %rcx     # 0x50790
callq 0x52a0
cmpl $0x1, 0x1a4(%rsp)
jne 0x28707
movq 0x190(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x2a290
jmp 0x2863b
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4190, %rsi           # imm = 0x4190
leaq 0x1b8(%rsp), %rdi
callq 0x1e620
jmp 0x2865d
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rdx
addq $0x4190, %rdx           # imm = 0x4190
leaq 0x1b8(%rsp), %rdi
movl $0x1, %esi
callq 0x1d470
jmp 0x28684
movq 0xc0(%rsp), %rax
leaq 0xe0(%rsp), %rdi
movq 0x40(%rax), %rax
movl 0x104(%rax), %eax
movl %eax, %esi
callq 0x1c440
jmp 0x286a7
leaq 0x1b8(%rsp), %rdi
callq 0x1c510
movq %rax, 0x38(%rsp)
jmp 0x286bb
movq 0x38(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1c370
jmp 0x286cf
leaq 0xd0(%rsp), %rdi
callq 0x1bc50
jmp 0x287c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x1bc50
jmp 0x288ea
cmpl $0x2, 0x1a4(%rsp)
jl 0x287c4
movb 0x1aa(%rsp), %al
movb %al, 0xce(%rsp)
movzbl 0xce(%rsp), %edi
callq 0x20150
movb %al, 0x37(%rsp)
jmp 0x28736
movb 0x37(%rsp), %al
movb %al, 0xcf(%rsp)
movq 0x1e8(%rsp), %rax
movb 0xcf(%rsp), %cl
movb %cl, 0x22(%rax)
movq 0x1e8(%rsp), %rax
movb $0x2, 0x23(%rax)
movq 0x190(%rsp), %rcx
movq 0x1e8(%rsp), %rax
movw 0x24(%rcx), %cx
movw %cx, 0x26(%rax)
movq 0x1e8(%rsp), %rdi
addq $0x28, %rdi
callq 0x29f60
jmp 0x2878a
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rsi
addq $0x4190, %rsi           # imm = 0x4190
leaq 0x1b8(%rsp), %rdi
callq 0x1e620
jmp 0x287ac
movb $0x1, 0x207(%rsp)
movl $0x1, 0x104(%rsp)
jmp 0x288db
jmp 0x287c6
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
vmovss 0xf0(%rax), %xmm0
vpxor %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x28826
leaq 0x1b8(%rsp), %rdi
callq 0x1c510
movq %rax, 0x28(%rsp)
jmp 0x287f8
leaq 0x1b8(%rsp), %rdi
callq 0x1c560
movq %rax, 0x20(%rsp)
jmp 0x2880c
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
leaq 0x623(%rip), %rdx       # 0x28e40
callq 0x2a340
jmp 0x28824
jmp 0x28826
movq 0xc0(%rsp), %rax
movq 0x40(%rax), %rax
addq $0x4170, %rax           # imm = 0x4170
movq %rax, 0x10(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1a700
movl %eax, 0x1c(%rsp)
jmp 0x28850
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %eax
movl %eax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rsi
callq 0x1d790
jmp 0x2886f
movq 0x1e8(%rsp), %rdi
addq $0x28, %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x1f560
jmp 0x2888a
leaq 0x1b8(%rsp), %rdi
callq 0x29f60
movb %al, 0xf(%rsp)
jmp 0x2889d
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x288a7
jmp 0x288a9
jmp 0x288c8
leaq 0x27f35(%rip), %rdi     # 0x507e5
leaq 0x27e5a(%rip), %rsi     # 0x50711
movl $0xc5, %edx
leaq 0x27ecd(%rip), %rcx     # 0x50790
callq 0x52a0
movb $0x1, 0x207(%rsp)
movl $0x1, 0x104(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x1e5e0
jmp 0x288f9
leaq 0x1b8(%rsp), %rdi
callq 0x1e5e0
jmp 0x2890a
movb 0x207(%rsp), %al
andb $0x1, %al
addq $0x208, %rsp            # imm = 0x208
retq
movq 0x1b0(%rsp), %rdi
callq 0x5770
nopw (%rax,%rax)