// File: /workspace/github_cpp_makefile_star2_O1/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
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
leaq 0x22(%rsi), %rax
movq %rax, 0x20(%rsp)
leaq 0x1fc3c(%rip), %r13     # 0x363b1
movb $0x3, %al
subb %r14b, %al
movb %al, 0xa(%rsp)
movl %r14d, %eax
negb %al
movb %al, 0x8(%rsp)
leal -0x3(%r14), %eax
movl %eax, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
movb 0x22(%rbx), %al
movb (%r13), %cl
cmpb %cl, %al
jge 0x16978
movb 0x22(%r12), %sil
leaq 0x1fbff(%rip), %rdx     # 0x363ae
movb (%rdx), %dl
cmpb %dl, %sil
je 0x16974
movb 0x23(%r12), %r15b
cmpb %r14b, %sil
je 0x1693c
movq 0x28(%rbx), %r9
leaq 0x10(%r9), %r8
movl $0x0, %esi
movl $0x0, %edi
cmpq $0x2, %r9
cmovaeq %r8, %rsi
jb 0x167f6
movzwl 0x6(%r9), %edi
leaq (%rdi,%rdi,2), %rdi
shll $0x4, %edi
addq %rdi, %r8
movq %r8, %rdi
xorl %r8d, %r8d
cmpq %rdi, %rsi
je 0x16934
leaq 0x1fbab(%rip), %r9      # 0x363b4
movb (%r9), %r9b
movb %r9b, 0x9(%rsp)
xorl %r9d, %r9d
movb %al, 0x7(%rsp)
cmpb 0xc(%rsp), %al
sete %r9b
leal 0x2(%r9,%r9), %eax
movq 0x18(%rsp), %r9
movb 0xb0(%r9), %r11b
addq $0x30, %rsi
xorl %r12d, %r12d
leaq -0x30(%rsi), %r13
movb -0xe(%rsi), %r9b
movb %r9b, 0xb(%rsp)
movl $0x3, %r10d
cmpb %dl, %r9b
jne 0x1687a
movzbl -0xd(%rsi), %ebp
movzbl %r15b, %r9d
cmpb %bpl, %r9b
movl %ebp, %r15d
cmoval %r9d, %r15d
movb $0x1, %r9b
testq %r12, %r12
je 0x168f1
cmpl %r10d, %r8d
jl 0x168f1
jle 0x168cc
xorl %r9d, %r9d
jmp 0x168f1
cmpb %r14b, %r9b
jne 0x16891
movb -0xd(%rsi), %r15b
movb $0x1, %r9b
movl $0x6, %r8d
movq %r13, %r12
jmp 0x16905
xorl %r10d, %r10d
cmpb 0xa(%rsp), %r9b
je 0x16850
movl %eax, %r10d
cmpb %cl, %r9b
je 0x16850
movl %eax, %r10d
cmpb 0x9(%rsp), %r9b
je 0x16850
movl $0x5, %r10d
cmpb 0x8(%rsp), %r9b
je 0x16850
movl $0x1, %r10d
cmpb 0xc(%rsp), %r9b
je 0x16850
jmp 0x1698c
testb $0x1, %r11b
je 0x168e4
movl %r11d, %ebx
movb 0x23(%r12), %r11b
cmpb %bpl, %r11b
movl %ebx, %r11d
jb 0x168f1
ja 0x16875
movq 0x18(%r12), %r9
cmpq -0x18(%rsi), %r9
setb %r9b
testb %r9b, %r9b
jne 0x168fc
movq %r12, %r13
movl %r8d, %r10d
xorl %r9d, %r9d
movq %r13, %r12
movl %r10d, %r8d
testb %r9b, %r9b
jne 0x1691a
leaq 0x30(%rsi), %r9
cmpq %rdi, %rsi
movq %r9, %rsi
jne 0x16838
cmpl $0x3, %r8d
sete %r8b
movq 0x10(%rsp), %rbx
leaq 0x1fa83(%rip), %r13     # 0x363b1
movb 0x7(%rsp), %al
jmp 0x16937
xorl %r12d, %r12d
testb %r8b, %r8b
jne 0x16974
movzbl 0x22(%r12), %edx
movzbl %al, %esi
movq 0x20(%rsp), %rdi
callq 0x2f810
testb %al, %al
je 0x16794
movzwl 0x24(%r12), %eax
movw %ax, 0x26(%rbx)
incb %r15b
movb %r15b, 0x23(%rbx)
movb 0x22(%rbx), %al
cmpb (%r13), %al
setge %al
jmp 0x1697a
xorl %eax, %eax
jmp 0x1697a
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48(%rsp), %rbx
leaq 0xb(%rsp), %rsi
movq %rbx, %rdi
callq 0x2f5ee
leaq 0x1cef7(%rip), %rsi     # 0x3389c
leaq 0x28(%rsp), %rdi
movq %rbx, %rdx
callq 0x17841
leaq 0x1af4d(%rip), %rdx     # 0x31906
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x177ec
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x58ce
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x169f7
callq 0x52e0
jmp 0x169f7
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16a0a
callq 0x52e0
movq %rbx, %rdi
callq 0x5670