// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
pushq $0x1
popq %rcx
xorl %eax, %eax
lock
cmpxchgq %rcx, 0x28(%rdx)
sete %bpl
jne 0x1239b
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 0x12040
leaq 0x17a31(%rip), %rsi     # 0x29a01
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 0x12c44
movq 0x40(%r14), %rdx
cmpb $0x1, 0x100(%rdx)
jne 0x12040
movq %rbx, %rdi
callq 0x12ce6
leaq 0x179f2(%rip), %rcx     # 0x29a09
movb %al, 0x22(%r15)
cmpb (%rcx), %al
jne 0x1203c
movq %r13, %rdi
callq 0xe9b0
movw %dx, 0x26(%r15)
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x28(%r15)
jmp 0x1239b
movq 0x40(%r14), %rdx
leaq 0x8(%rsp), %rdi
leaq 0x179bd(%rip), %rcx     # 0x29a09
andq $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 0xe96a
movb 0xa(%r13), %bl
movq 0x8(%rsp), %r12
movb %bpl, 0x6(%rsp)
movq %r13, %rdi
callq 0xe9b0
movb $0x3, %dil
leaq 0x28(%r15), %rcx
movq %r15, 0x20(%rsp)
leaq 0x10(%r12), %r15
xorl %esi, %esi
subb %bl, %dil
cmpq $0x2, %r12
movq %rcx, 0x60(%rsp)
movl $0x0, %ecx
leaq 0x30(%rsp), %r12
cmovbq %rsi, %r15
leaq 0x68(%rsp), %rsi
xorl %ebp, %ebp
movq %rcx, 0x18(%rsp)
movb %dil, 0x7(%rsp)
movq %rax, (%rsi)
movl %edx, 0x8(%rsi)
shrq $0x20, %rdx
movzbl %dil, %eax
movb %dl, 0xc(%rsi)
movl %eax, 0x2c(%rsp)
cmpw $-0x1, 0x72(%rsp)
je 0x121cc
leaq 0x17915(%rip), %rcx     # 0x29a06
movl 0x70(%rsp), %eax
vpxor %xmm0, %xmm0, %xmm0
andw $0x0, 0x50(%rsp)
vmovdqu %ymm0, 0x30(%rsp)
movb (%rcx), %cl
movl %eax, 0x14(%rsp)
movb %cl, 0x52(%rsp)
movb $0x0, 0x53(%rsp)
movw %ax, 0x54(%rsp)
movw $0x78fc, 0x56(%rsp)     # imm = 0x78FC
andq $0x0, 0x58(%rsp)
movq %r15, %rdi
movq %r12, %rsi
vzeroupper
callq 0xe2ce
movq %r12, %rdi
callq 0xde6c
movq 0x40(%r14), %rax
cmpl $0x0, 0xfc(%rax)
je 0x1219d
leaq 0x14(%rsp), %rsi
movq %r13, %rdi
callq 0x12e2a
movb %al, 0x22(%r15)
movq 0x40(%r14), %rcx
cmpl $0x2, 0xfc(%rcx)
jl 0x12195
movl 0x2c(%rsp), %edx
leaq 0x14(%rsp), %rsi
movq %r13, %rdi
callq 0x12e34
xorl %ecx, %ecx
cmpb 0x7(%rsp), %al
movq 0x18(%rsp), %rax
cmoveq %r15, %rax
sete %cl
movq %rax, 0x18(%rsp)
movb 0x22(%r15), %al
addl %ecx, %ebp
cmpb %bl, %al
je 0x121e6
movq 0x40(%r14), %rax
cmpb $0x1, 0xdc(%rax)
jne 0x121b9
movq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x1274e
addq $0x30, %r15
leaq 0x68(%rsp), %rdi
callq 0xe9d0
jmp 0x120de
movq 0x8(%rsp), %rax
cmpq $0x2, %rax
jb 0x12223
movzwl 0x6(%rax), %ecx
imulq $0x30, %rcx, %rcx
leaq 0x10(%rax,%rcx), %rcx
jmp 0x12225
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movb %bl, 0x22(%rdx)
movb $0x1, 0x23(%rdx)
movzwl 0x14(%rsp), %eax
movw %ax, 0x26(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x28(%rdx)
movl $0x4190, %esi           # imm = 0x4190
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0xeea4
movb 0x6(%rsp), %bpl
jmp 0x12391
xorl %ecx, %ecx
cmpq %rcx, %r15
jne 0x123ac
movq 0x20(%rsp), %rdx
cmpl $0x1, %ebp
jne 0x122cd
movq 0x18(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x12f78
movb 0x6(%rsp), %bpl
movq 0x40(%r14), %rsi
movl $0x4190, %ebx           # imm = 0x4190
addq %rbx, %rsi
leaq 0x8(%rsp), %rdi
callq 0xeea4
addq 0x40(%r14), %rbx
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
callq 0xe96a
movq 0x40(%r14), %rax
leaq 0x30(%rsp), %rsi
xorl %edi, %edi
movl 0x104(%rax), %eax
vmovq %rax, %xmm0
addq %rax, %rax
vmovq %rax, %xmm1
movq 0x8(%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 0xe2ce
leaq 0x30(%rsp), %rdi
callq 0xde6c
movq 0x8(%rsp), %rax
jmp 0x1230f
movb 0x6(%rsp), %bpl
jle 0x1230f
movb 0x7(%rsp), %al
xorl %ecx, %ecx
movb %al, 0x22(%rdx)
movq 0x18(%rsp), %rax
movb $0x2, 0x23(%rdx)
movzwl 0x24(%rax), %eax
movw %ax, 0x26(%rdx)
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, 0x28(%rdx)
movl $0x4190, %esi           # imm = 0x4190
addq 0x40(%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0xeea4
jmp 0x12391
movq 0x40(%r14), %rcx
vpxor %xmm1, %xmm1, %xmm1
vmovss 0xf0(%rcx), %xmm0
vucomiss %xmm1, %xmm0
jbe 0x1235d
leaq 0x10(%rax), %rcx
xorl %esi, %esi
movl $0x0, %edi
cmpq $0x2, %rax
cmovaeq %rcx, %rdi
jb 0x12348
movzwl 0x6(%rax), %eax
imulq $0x30, %rax, %rax
addq %rax, %rcx
movq %rcx, %rsi
leaq 0x3f3(%rip), %rdx       # 0x12742
callq 0x13f0c
movq 0x40(%r14), %rcx
movq 0x8(%rsp), %rax
cmpq $0x2, %rax
jb 0x12369
movzwl 0x6(%rax), %eax
jmp 0x1236b
xorl %eax, %eax
lock
addq %rax, 0x4170(%rcx)
leaq 0x8(%rsp), %rbx
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0xf156
xorl %ecx, %ecx
pushq $0x1
popq %rax
lock
cmpxchgq %rcx, (%rbx)
jne 0x123cb
leaq 0x8(%rsp), %rdi
callq 0xee7e
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1443d(%rip), %rdi     # 0x267f0
leaq 0x143cb(%rip), %rsi     # 0x26785
leaq 0x14443(%rip), %rcx     # 0x26804
movl $0xae, %edx
callq 0x51f0
leaq 0x14487(%rip), %rdi     # 0x26859
leaq 0x143ac(%rip), %rsi     # 0x26785
leaq 0x14424(%rip), %rcx     # 0x26804
movl $0xc5, %edx
callq 0x51f0
jmp 0x123fc
jmp 0x123fc
jmp 0x123fc
jmp 0x12401
jmp 0x123fc
jmp 0x123fc
jmp 0x123fc
jmp 0x123fc
jmp 0x123fc
movq %rax, %rbx
jmp 0x1240e
leaq 0x30(%rsp), %rdi
movq %rax, %rbx
callq 0xde6c
leaq 0x8(%rsp), %rdi
callq 0xee7e
movq %rbx, %rdi
callq 0x5650