// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]morat/pentago/agentmctsthread.cpp
// Function: Morat::Pentago::AgentMCTS::do_backup(Morat::Pentago::AgentMCTS::Node*, Morat::Pentago::AgentMCTS::Node*, Morat::Side)
bool AgentMCTS::do_backup(Node * node, 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){
		uint64_t sims = 0, bestsims = 0, outcome = 0, best_outcome = 0;
		backup = NULL;

		Node * child = node->children.begin(),
			 * end = node->children.end();

		for( ; child != end; child++){
			Outcome child_outcome = child->outcome; //save a copy to avoid race conditions

			if(proofdepth < child->proofdepth+1)
				proofdepth = child->proofdepth+1;

			//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;
				outcome = 6;
				proofdepth = child->proofdepth+1;
				break;
			}else if(child_outcome == ~to_play){ //loss
				outcome = 0;
			}else if(child_outcome == Outcome::DRAW){ //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");
			}

			sims = child->exp.num();
			if(best_outcome < outcome){ //better outcome is always preferable
				best_outcome = outcome;
				bestsims = sims;
				backup = child;
			}else if(best_outcome == outcome && ((outcome == 0 && bestsims < sims) || bestsims > sims)){
				//find long losses or easy wins/draws
				bestsims = sims;
				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;
	}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
leaq 0x12(%rsi), %rax
movq %rax, 0x20(%rsp)
leaq 0x1a735(%rip), %r15     # 0x31da9
movb $0x3, %al
subb %r14b, %al
movb %al, 0xa(%rsp)
movl %r14d, %eax
negb %al
movb %al, 0x9(%rsp)
leal -0x3(%r14), %eax
movl %eax, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
movb 0x12(%rbx), %al
movb (%r15), %cl
cmpb %cl, %al
jge 0x17842
movb 0x12(%r12), %sil
leaq 0x1a6f9(%rip), %rdx     # 0x31da6
movb (%rdx), %dl
cmpb %dl, %sil
je 0x1783e
movb 0x13(%r12), %r13b
cmpb %r14b, %sil
je 0x1780a
movq 0x18(%rbx), %r9
leaq 0x10(%r9), %r8
movl $0x0, %esi
movl $0x0, %edi
cmpq $0x2, %r9
cmovaeq %r8, %rsi
jb 0x176f0
movzwl 0x6(%r9), %edi
shll $0x5, %edi
addq %rdi, %r8
movq %r8, %rdi
cmpq %rdi, %rsi
je 0x17801
xorl %r8d, %r8d
cmpb 0xc(%rsp), %al
sete %r8b
leaq 0x2(%r8,%r8), %r8
movq %r8, 0x18(%rsp)
addq $0x20, %rsi
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r8d, %r8d
leaq -0x20(%rsi), %r11
movb -0xe(%rsi), %r9b
movb %r9b, 0xb(%rsp)
movb -0xd(%rsi), %r15b
leal 0x1(%r15), %r10d
cmpb %r13b, %r15b
movzbl %r13b, %r15d
movzbl %r10b, %r13d
cmovbl %r15d, %r13d
cmpb %dl, %r9b
jne 0x1775d
movl $0x3, %r9d
xorl %r15d, %r15d
movq -0x18(%rsi), %r10
cmpq %r9, %r8
jae 0x1776a
xorl %r15d, %r15d
movq %r9, %r8
jmp 0x17788
movb $0x1, %r15b
cmpb %r14b, %r9b
jne 0x177a5
movl %r10d, %r13d
jmp 0x1778b
jne 0x177b4
cmpq %r10, %rbx
setb %r9b
seta %bpl
andb %r9b, %r15b
orb %bpl, %r15b
jne 0x17785
movq %rbx, %r10
movq %r12, %r11
xorl %r15d, %r15d
movq %r10, %rbx
movq %r11, %r12
testb %r15b, %r15b
jne 0x177ec
leaq 0x20(%rsi), %r9
cmpq %rdi, %rsi
movq %r9, %rsi
jne 0x1771a
jmp 0x177ec
cmpb 0xa(%rsp), %r9b
jne 0x177b9
movb $0x1, %r15b
xorl %r9d, %r9d
jmp 0x1774c
xorl %r15d, %r15d
jmp 0x1778e
cmpb %cl, %r9b
jne 0x177c8
xorl %r15d, %r15d
movq 0x18(%rsp), %r9
jmp 0x1774c
cmpb 0x9(%rsp), %r9b
jne 0x177da
movl $0x5, %r9d
jmp 0x17749
cmpb 0xc(%rsp), %r9b
jne 0x17856
movl $0x1, %r9d
jmp 0x17749
cmpq $0x3, %r8
sete %cl
movq 0x10(%rsp), %rbx
leaq 0x1a5aa(%rip), %r15     # 0x31da9
jmp 0x17806
xorl %ecx, %ecx
xorl %r12d, %r12d
testb %cl, %cl
jne 0x1783e
movzbl 0x12(%r12), %edx
movzbl %al, %esi
movq 0x20(%rsp), %rdi
callq 0x2a990
testb %al, %al
je 0x17693
movzwl 0x14(%r12), %eax
movw %ax, 0x16(%rbx)
movb %r13b, 0x13(%rbx)
movb 0x12(%rbx), %al
cmpb (%r15), %al
setge %al
jmp 0x17844
xorl %eax, %eax
jmp 0x17844
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 0x2a76e
leaq 0x17219(%rip), %rsi     # 0x2ea88
leaq 0x28(%rsp), %rdi
movq %rbx, %rdx
callq 0xd1f7
leaq 0x14fbf(%rip), %rdx     # 0x2c842
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x17d0b
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x59d7
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x178c1
callq 0x5310
jmp 0x178c1
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x178d4
callq 0x5310
movq %rbx, %rdi
callq 0x56c0