// File: /workspace/github_cpp_makefile_star2_O3/tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::do_backup(Morat::Hex::AgentMCTS::Node*, Morat::Hex::AgentMCTS::Node const*, Morat::Side)
bool AgentMCTS::do_backup(Node * node, const Node * backup, Side to_play){
	Outcome node_outcome = node->outcome;
	if(node_outcome >= Outcome::DRAW) //already proven, probably by a different thread
		return true;

	if(backup->outcome == Outcome::UNKNOWN) //nothing proven by this child, so no chance
		return false;

	uint8_t proofdepth = backup->proofdepth;
	if(backup->outcome != to_play){
		int best_outcome = 0;
		backup = NULL;

		for (const auto& child : node->children) {
			Outcome child_outcome = child.outcome; //save a copy to avoid race conditions
			int outcome = 0;

			//these should be sorted in likelyness of matching, most likely first
			if(child_outcome == Outcome::UNKNOWN){ // win/draw/loss
				outcome = 3;
			}else if(child_outcome == to_play){ //win
				backup = &child;
				best_outcome = 6;
				proofdepth = child.proofdepth;
				break;
			}else if(child_outcome == ~to_play){ //loss
				outcome = 0;
			}else if(child_outcome == Outcome::DRAW ||
			         child_outcome == Outcome::DRAW2){ //draw
				if(node_outcome == -to_play) //draw/loss, ie I can't win
					outcome = 4;
				else
					outcome = 2;
			}else if(child_outcome == -~to_play){ //win/draw, ie opponent can't win
				outcome = 5;
			}else if(child_outcome == -to_play){ //draw/loss, ie I can't win
				outcome = 1;
			}else{
				logerr("child_outcome == " + child_outcome.to_s() + "\n");
				assert(false && "How'd I get here? All outcomes should be tested above");
			}

			if(proofdepth < child.proofdepth)
				proofdepth = child.proofdepth;

			auto better_than_best = [&]() -> bool {
				// any child is better than nothing
				if (backup == NULL)
					return true;

				// a better outcome is always preferable
				if (best_outcome < outcome)
					return true;
				if (best_outcome > outcome)
					return false;

				// do we care about depth? if so, take the longest.
				if (longestloss) {
					if (backup->proofdepth < child.proofdepth)
						return true;
					if (backup->proofdepth > child.proofdepth)
						return false;
				}

				// all else equal, take the hardest to solve
				return (backup->exp.num() < child.exp.num());
			};

			if (better_than_best()) {
				best_outcome = outcome;
				backup = &child;
			}
		}

		if(best_outcome == 3) //no win, but found an unknown
			return false;
	}

	if(node->outcome.cas(node_outcome, backup->outcome)){
		node->bestmove = backup->move;
		node->proofdepth = proofdepth + 1;
	}else //if it was in a race, try again, might promote a partial solve to full solve
		return do_backup(node, backup, to_play);

	return (node->outcome >= Outcome::DRAW);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x1fb55(%rip), %r12     # 0x363b1
movl %ecx, %r14d
movl %r14d, %eax
movb $0x3, %bpl
leaq 0x22(%rsi), %r15
movq %rdx, %r13
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
negb %al
subb %r14b, %bpl
movq %r15, 0x10(%rsp)
movb %al, 0x5(%rsp)
leal -0x3(%r14), %eax
movl %eax, 0xc(%rsp)
movb (%r15), %cl
movb (%r12), %dl
cmpb %dl, %cl
setge %r8b
jge 0x16a1e
leaq 0x1fb08(%rip), %rax     # 0x363ae
movb 0x22(%r13), %dil
movb (%rax), %sil
cmpb %sil, %dil
je 0x16a26
movb 0x23(%r13), %bl
cmpb %r14b, %dil
jne 0x168c7
movl %r14d, %eax
jmp 0x169e9
movq 0x18(%rsp), %rax
movl %r8d, 0x8(%rsp)
movl $0x0, %r13d
movl $0x0, %edi
movq 0x28(%rax), %r9
leaq 0x10(%r9), %r8
cmpq $0x2, %r9
cmovaeq %r8, %r13
jb 0x16900
movzwl 0x6(%r9), %edi
leaq (%rdi,%rdi,2), %rdi
shll $0x4, %edi
addq %rdi, %r8
movq %r8, %rdi
leaq 0x1faad(%rip), %rax     # 0x363b4
xorl %r8d, %r8d
cmpb 0xc(%rsp), %cl
movb (%rax), %al
sete %r8b
xorl %r15d, %r15d
xorl %r11d, %r11d
leal 0x2(%r8,%r8), %r12d
movb %al, 0x6(%rsp)
movq 0x20(%rsp), %rax
movb 0xb0(%rax), %al
movb 0x22(%r13), %r8b
movl $0x3, %r9d
movb %r8b, 0x7(%rsp)
cmpb %sil, %r8b
je 0x16953
cmpb %r14b, %r8b
je 0x169d6
xorl %r9d, %r9d
cmpb %bpl, %r8b
jne 0x16999
movzbl 0x23(%r13), %r8d
movzbl %bl, %r10d
cmpb %r8b, %r10b
movl %r8d, %ebx
cmoval %r10d, %ebx
testq %r11, %r11
je 0x16988
cmpl %r9d, %r15d
jl 0x16988
ja 0x1698e
testb $0x1, %al
je 0x1697e
cmpb %r8b, 0x23(%r11)
jb 0x16988
ja 0x1698e
movq 0x18(%r11), %r8
cmpq 0x18(%r13), %r8
jae 0x1698e
movq %r13, %r11
movl %r9d, %r15d
addq $0x30, %r13
cmpq %rdi, %r13
jne 0x1692e
jmp 0x169c7
movl %r12d, %r9d
cmpb %dl, %r8b
je 0x16953
movl %r12d, %r9d
cmpb 0x6(%rsp), %r8b
je 0x16953
movl $0x5, %r9d
cmpb 0x5(%rsp), %r8b
je 0x16953
movl $0x1, %r9d
cmpb 0xc(%rsp), %r8b
je 0x16953
jmp 0x16a44
cmpl $0x3, %r15d
je 0x16a2e
movb 0x22(%r11), %al
movq %r11, %r13
jmp 0x169dd
movb 0x23(%r13), %bl
movl %r14d, %eax
movq 0x10(%rsp), %r15
leaq 0x1f9c8(%rip), %r12     # 0x363b1
movzbl %cl, %esi
movzbl %al, %edx
movq %r15, %rdi
callq 0x2f6dc
testb %al, %al
je 0x1688c
movzwl 0x24(%r13), %eax
movq 0x18(%rsp), %rcx
incb %bl
movw %ax, 0x26(%rcx)
movb %bl, 0x23(%rcx)
movb 0x22(%rcx), %al
cmpb (%r12), %al
setge %al
jmp 0x16a20
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x16a2e
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48(%rsp), %rbx
leaq 0x7(%rsp), %rsi
movq %rbx, %rdi
callq 0x2f4de
leaq 0x1ce3f(%rip), %rsi     # 0x3389c
leaq 0x28(%rsp), %rdi
movq %rbx, %rdx
callq 0x178a3
leaq 0x1ae95(%rip), %rdx     # 0x31906
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1784e
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x58ce
leaq 0x38(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x16aaf
callq 0x52e0
jmp 0x16aaf
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16ac2
callq 0x52e0
movq %rbx, %rdi
callq 0x5670