// File: /workspace/github_cpp_makefile_star2_O3/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 %bpl
jne 0x17432
movq %rdx, %r15
movq 0x40(%rdi), %rdx
movq %rsi, %r13
movq %rdi, %r14
movl 0x124(%rdx), %eax
movb 0x100(%rdx), %cl
testl %eax, %eax
setne %sil
orb %cl, %sil
testb $0x1, %sil
je 0x17015
leaq 0x2215e(%rip), %rsi     # 0x39101
leaq 0xa(%r13), %rdi
testb $0x1, %cl
leaq 0x28e8(%r14), %rbx
cmovneq %rsi, %rdi
xorl %edx, %edx
testl %eax, %eax
cmoveq %rsi, %rdi
setg %dl
movq %r13, %rsi
movzbl (%rdi), %ecx
movq %rbx, %rdi
callq 0x17f70
movq 0x40(%r14), %rdx
cmpb $0x1, 0x100(%rdx)
jne 0x17015
movq %rbx, %rdi
callq 0x1807c
leaq 0x2211f(%rip), %rcx     # 0x39109
movb %al, 0x22(%r15)
cmpb (%rcx), %al
jne 0x17011
movq %r13, %rdi
callq 0x1328c
movw %dx, 0x26(%r15)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x28(%r15)
jmp 0x17432
movq 0x40(%r14), %rdx
leaq 0x10(%rsp), %rdi
leaq 0x220e8(%rip), %rcx     # 0x39109
movq $0x0, (%rdi)
movswl 0x4(%r13), %r8d
movswl 0x6(%r13), %esi
movb 0xb(%r13), %al
subl %esi, %r8d
xorl %esi, %esi
cmpb (%rcx), %al
cmovll %r8d, %esi
addq $0x4190, %rdx           # imm = 0x4190
callq 0x13246
movb 0xa(%r13), %bl
movq 0x10(%rsp), %r12
movb %bpl, 0xe(%rsp)
movq %r13, %rdi
callq 0x1328c
movb $0x3, %dil
leaq 0x28(%r15), %rcx
movq %r15, 0x28(%rsp)
leaq 0x10(%r12), %r15
xorl %esi, %esi
subb %bl, %dil
cmpq $0x2, %r12
movq %rcx, 0x38(%rsp)
movl $0x0, %ecx
leaq 0x40(%rsp), %r12
cmovbq %rsi, %r15
leaq 0x78(%rsp), %rsi
xorl %ebp, %ebp
movq %rcx, 0x20(%rsp)
movb %dil, 0xf(%rsp)
movq %rax, (%rsi)
movl %edx, 0x8(%rsi)
shrq $0x20, %rdx
movzbl %dil, %eax
movb %dl, 0xc(%rsi)
movl %eax, 0x34(%rsp)
cmpw $-0x1, 0x82(%rsp)
je 0x171b3
leaq 0x2203a(%rip), %rcx     # 0x39106
movl 0x80(%rsp), %eax
vpxor %xmm0, %xmm0, %xmm0
movw $0x0, 0x60(%rsp)
vmovdqu %ymm0, 0x40(%rsp)
movb (%rcx), %cl
movl %eax, 0x1c(%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 %r12, %rsi
vzeroupper
callq 0x12af4
movq %r12, %rdi
callq 0x125c0
movq 0x40(%r14), %rax
cmpl $0x0, 0xfc(%rax)
je 0x17184
movzbl 0xa(%r13), %edx
leaq 0x1c(%rsp), %rsi
movq %r13, %rdi
callq 0x181ca
movb %al, 0x22(%r15)
movq 0x40(%r14), %rcx
cmpl $0x2, 0xfc(%rcx)
jl 0x1717c
movl 0x34(%rsp), %edx
leaq 0x1c(%rsp), %rsi
movq %r13, %rdi
callq 0x181ca
xorl %ecx, %ecx
cmpb 0xf(%rsp), %al
movq 0x20(%rsp), %rax
cmoveq %r15, %rax
sete %cl
movq %rax, 0x20(%rsp)
movb 0x22(%r15), %al
addl %ecx, %ebp
cmpb %bl, %al
je 0x171d0
movq 0x40(%r14), %rax
cmpb $0x1, 0xdc(%rax)
jne 0x171a0
movq 0x28(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x178c2
addq $0x30, %r15
leaq 0x78(%rsp), %rdi
callq 0x132d6
jmp 0x170b6
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x17222
movzwl 0x6(%rax), %ecx
leaq (%rcx,%rcx,2), %rcx
shll $0x4, %ecx
leaq 0x10(%rax,%rcx), %rcx
jmp 0x17224
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
movb %bl, 0x22(%rdx)
movb $0x1, 0x23(%rdx)
movzwl 0x1c(%rsp), %eax
movw %ax, 0x26(%rdx)
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x17218
movl $0x4190, %edi           # imm = 0x4190
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17218
callq 0x137d8
movb 0xe(%rsp), %bpl
jmp 0x17426
xorl %ecx, %ecx
cmpq %rcx, %r15
jne 0x17446
movq 0x28(%rsp), %rdx
cmpl $0x1, %ebp
jne 0x172e1
movq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x1833e
movb 0xe(%rsp), %bpl
movq 0x10(%rsp), %rsi
movl $0x4190, %ebx           # imm = 0x4190
testq %rsi, %rsi
je 0x17278
movq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
addq %rbx, %rdi
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17278
callq 0x137d8
addq 0x40(%r14), %rbx
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x13246
movq 0x40(%r14), %rax
leaq 0x40(%rsp), %rsi
xorl %edi, %edi
movl 0x104(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
movq 0x10(%rsp), %rax
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpaddq 0x10(%rsi), %xmm0, %xmm0
leaq 0x10(%rax), %rcx
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
vmovdqu %xmm0, 0x10(%rsi)
callq 0x12af4
leaq 0x40(%rsp), %rdi
callq 0x125c0
movq 0x10(%rsp), %rax
jmp 0x17340
movb 0xe(%rsp), %bpl
jle 0x17340
movb 0xf(%rsp), %al
xorl %ecx, %ecx
movb %al, 0x22(%rdx)
movq 0x20(%rsp), %rax
movb $0x2, 0x23(%rdx)
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%rdx)
movl $0x1, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x17432
movl $0x4190, %edi           # imm = 0x4190
addq 0x40(%r14), %rdi
xorl %ecx, %ecx
movq %rsi, %rax
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17426
callq 0x137d8
jmp 0x17426
movq 0x40(%r14), %rcx
vpxor %xmm1, %xmm1, %xmm1
vmovss 0xf0(%rcx), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x173d2
leaq 0x10(%rax), %rdx
xorl %r15d, %r15d
movl $0x0, %ebx
cmpq $0x2, %rax
cmovaeq %rdx, %rbx
jb 0x1737d
movzwl 0x6(%rax), %esi
leaq (%rsi,%rsi,2), %rsi
shll $0x4, %esi
addq %rsi, %rdx
movq %rdx, %r15
cmpq %r15, %rbx
je 0x173d2
movq %r15, %rax
subq %rbx, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
sarq $0x4, %rax
imulq %rax, %rcx
lzcntq %rcx, %rdx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x50a(%rip), %rcx       # 0x178b6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19491
leaq 0x4f8(%rip), %rdx       # 0x178b6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x19570
movq 0x40(%r14), %rcx
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
jb 0x173de
movzwl 0x6(%rax), %eax
jmp 0x173e0
xorl %eax, %eax
movq 0x38(%rsp), %rdx
lock
addq %rax, 0x4170(%rcx)
movq 0x10(%rsp), %rcx
movq (%rdx), %rax
movq %rcx, (%rdx)
movq %rax, 0x10(%rsp)
cmpq $0x2, %rcx
jb 0x17407
movq %rdx, 0x8(%rcx)
cmpq $0x2, %rax
jb 0x17416
leaq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, %eax
xorl %ecx, %ecx
lock
cmpxchgq %rcx, 0x10(%rsp)
jne 0x17465
cmpq $0x0, 0x10(%rsp)
jne 0x174b8
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e7b3(%rip), %rdi     # 0x35c00
leaq 0x1e741(%rip), %rsi     # 0x35b95
leaq 0x1e7b9(%rip), %rcx     # 0x35c14
movl $0xae, %edx
callq 0x51d0
leaq 0x1e7fd(%rip), %rdi     # 0x35c69
leaq 0x1e722(%rip), %rsi     # 0x35b95
leaq 0x1e79a(%rip), %rcx     # 0x35c14
movl $0xc5, %edx
callq 0x51d0
jmp 0x17496
jmp 0x17496
jmp 0x17496
jmp 0x1749b
jmp 0x17496
jmp 0x17496
jmp 0x17496
jmp 0x17496
jmp 0x17496
movq %rax, %rbx
jmp 0x174a8
leaq 0x40(%rsp), %rdi
movq %rax, %rbx
callq 0x125c0
cmpq $0x0, 0x10(%rsp)
jne 0x174b8
movq %rbx, %rdi
callq 0x5670
leaq 0x1d501(%rip), %rdi     # 0x349c0
leaq 0x1d507(%rip), %rsi     # 0x349cd
leaq 0x1d51e(%rip), %rcx     # 0x349eb
movl $0x7a, %edx
callq 0x51d0
nop