// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl $0x1, %ecx
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
sete 0x1e(%rsp)
jne 0x17563
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x40(%rdi), %rcx
movl 0x124(%rcx), %eax
testl %eax, %eax
jne 0x170b9
cmpb $0x1, 0x100(%rcx)
jne 0x17133
leaq 0x28e8(%r14), %rbx
xorl %edx, %edx
testl %eax, %eax
setg %al
cmpb $0x0, 0x100(%rcx)
je 0x170d9
leaq 0x2202a(%rip), %rcx     # 0x39101
jmp 0x170dd
leaq 0xa(%r13), %rcx
movzbl (%rcx), %ecx
movb %al, %dl
movq %rbx, %rdi
movq %r13, %rsi
callq 0x180e2
movq 0x40(%r14), %rax
cmpb $0x1, 0x100(%rax)
jne 0x17133
movq %rbx, %rdi
callq 0x181e6
movb %al, 0x22(%r12)
leaq 0x21ffb(%rip), %rcx     # 0x39109
cmpb (%rcx), %al
jne 0x17133
movq %r13, %rdi
callq 0x13354
movw %dx, 0x26(%r12)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%r12)
jmp 0x17563
leaq 0x10(%rsp), %rdi
movq $0x0, (%rdi)
movb 0xb(%r13), %al
leaq 0x21fbf(%rip), %rcx     # 0x39109
movswl 0x4(%r13), %edx
movswl 0x6(%r13), %esi
subl %esi, %edx
xorl %esi, %esi
cmpb (%rcx), %al
cmovll %edx, %esi
movl $0x4190, %edx           # imm = 0x4190
addq 0x40(%r14), %rdx
callq 0x1330e
movb 0xa(%r13), %bpl
movq 0x10(%rsp), %rbx
movq %r13, %rdi
callq 0x13354
leaq 0x28(%r12), %rcx
movq %rcx, 0x38(%rsp)
movb $0x3, %dil
movb %bpl, 0x1f(%rsp)
subb %bpl, %dil
leaq 0x10(%rbx), %r15
xorl %esi, %esi
cmpq $0x2, %rbx
movl $0x0, %ecx
movq %rcx, 0x28(%rsp)
cmovbq %rsi, %r15
leaq 0x78(%rsp), %rcx
movq %rax, (%rcx)
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movb %dl, 0xc(%rcx)
leaq 0x40(%rsp), %rbp
movb %dil, 0xf(%rsp)
movzbl %dil, %eax
movl %eax, 0x34(%rsp)
movl $0x0, 0x24(%rsp)
movzwl 0x82(%rsp), %ebx
cmpw $-0x1, %bx
je 0x17332
movl 0x80(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x21f0d(%rip), %rcx     # 0x39106
movb (%rcx), %cl
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x40(%rsp)
movw $0x0, 0x60(%rsp)
movb %cl, 0x62(%rsp)
movb $0x0, 0x63(%rsp)
movw %ax, 0x64(%rsp)
movw $0x78fc, 0x66(%rsp)     # imm = 0x78FC
movq $0x0, 0x68(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
vzeroupper
callq 0x12b70
movq %rbp, %rdi
callq 0x125fe
movq 0x40(%r14), %rax
cmpl $0x0, 0xfc(%rax)
je 0x172fb
movzbl 0xa(%r13), %edx
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x18386
movb %al, 0x22(%r15)
movq 0x40(%r14), %rax
cmpl $0x2, 0xfc(%rax)
jl 0x172a0
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
movl 0x34(%rsp), %edx
callq 0x18386
xorl %ecx, %ecx
cmpb 0xf(%rsp), %al
sete %cl
movq 0x28(%rsp), %rax
cmoveq %r15, %rax
movq %rax, 0x28(%rsp)
addl %ecx, 0x24(%rsp)
movb 0x22(%r15), %al
cmpb 0x1f(%rsp), %al
jne 0x172fb
movb %al, 0x22(%r12)
movb $0x1, 0x23(%r12)
movzwl 0x20(%rsp), %eax
movw %ax, 0x26(%r12)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%r12)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x1731f
movq 0x40(%r14), %rdi
movl $0x4190, %eax           # imm = 0x4190
addq %rax, %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x1731f
callq 0x138ac
xorl %ecx, %ecx
jmp 0x1731f
movq 0x40(%r14), %rax
cmpb $0x1, 0xdc(%rax)
jne 0x17319
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x17a02
addq $0x30, %r15
movb $0x1, %cl
testb %cl, %cl
je 0x17332
leaq 0x78(%rsp), %rdi
callq 0x1339c
jmp 0x171d5
cmpw $-0x1, %bx
jne 0x17557
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x17359
movzwl 0x6(%rax), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
leaq 0x10(%rax,%rcx), %rax
jmp 0x1735b
xorl %eax, %eax
cmpq %rax, %r15
jne 0x17579
cmpl $0x1, 0x24(%rsp)
jne 0x1740b
leaq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x184ee
movl $0x4190, %ebx           # imm = 0x4190
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x173a7
movq 0x40(%r14), %rdi
addq %rbx, %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x173a7
callq 0x138ac
addq 0x40(%r14), %rbx
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x1330e
movq 0x40(%r14), %rax
movl 0x104(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
leaq 0x40(%rsp), %rsi
vpaddq 0x10(%rsi), %xmm0, %xmm0
vmovdqu %xmm0, 0x10(%rsi)
movq 0x10(%rsp), %rax
leaq 0x10(%rax), %rcx
xorl %edi, %edi
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
callq 0x12b70
leaq 0x40(%rsp), %rdi
callq 0x125fe
jmp 0x1746c
jle 0x1746c
movb 0xf(%rsp), %al
movb %al, 0x22(%r12)
movb $0x2, 0x23(%r12)
movq 0x28(%rsp), %rax
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%r12)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%r12)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x17557
movl $0x4190, %edi           # imm = 0x4190
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17557
callq 0x138ac
jmp 0x17557
movq 0x40(%r14), %rax
vmovss 0xf0(%rax), %xmm0
vpxor %xmm1, %xmm1, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x174fa
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
xorl %r15d, %r15d
movl $0x0, %ebx
cmpq $0x2, %rcx
cmovaeq %rax, %rbx
jb 0x174ae
movzwl 0x6(%rcx), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
addq %rcx, %rax
movq %rax, %r15
cmpq %r15, %rbx
je 0x174fa
movq %r15, %rax
subq %rbx, %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
lzcntq %rcx, %rdx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x518(%rip), %rcx       # 0x179f5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x195f7
leaq 0x506(%rip), %rdx       # 0x179f5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x196d6
movq 0x40(%r14), %rax
movq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x1750f
movzwl 0x6(%rcx), %ecx
jmp 0x17511
xorl %ecx, %ecx
movq 0x38(%rsp), %rdx
lock
addq %rcx, 0x4170(%rax)
movq (%rdx), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rdx)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x17538
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x17547
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
xorl %ecx, %ecx
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17598
cmpq $0x0, 0x10(%rsp)
jne 0x175e9
movb 0x1e(%rsp), %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e680(%rip), %rdi     # 0x35c00
leaq 0x1e60e(%rip), %rsi     # 0x35b95
leaq 0x1e686(%rip), %rcx     # 0x35c14
movl $0xae, %edx
callq 0x51d0
leaq 0x1e6ca(%rip), %rdi     # 0x35c69
leaq 0x1e5ef(%rip), %rsi     # 0x35b95
leaq 0x1e667(%rip), %rcx     # 0x35c14
movl $0xc5, %edx
callq 0x51d0
jmp 0x175c7
jmp 0x175c7
jmp 0x175cc
jmp 0x175c7
jmp 0x175c7
jmp 0x175c7
jmp 0x175c7
jmp 0x175c7
movq %rax, %rbx
jmp 0x175d9
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x125fe
cmpq $0x0, 0x10(%rsp)
jne 0x175e9
movq %rbx, %rdi
callq 0x5670
leaq 0x1d3d0(%rip), %rdi     # 0x349c0
leaq 0x1d3d6(%rip), %rsi     # 0x349cd
leaq 0x1d3ed(%rip), %rcx     # 0x349eb
movl $0x7a, %edx
callq 0x51d0