// File: /workspace/github_cpp_makefile_star2_O3/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
movb $0x3, %bpl
movl %r14d, %eax
leaq 0x12(%rsi), %r15
leal -0x3(%r14), %ecx
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
subb %r14b, %bpl
negb %al
movl %ecx, 0xc(%rsp)
movq %r15, 0x18(%rsp)
movb %al, 0xa(%rsp)
leaq 0x1a8f0(%rip), %rax     # 0x31da9
movb (%r15), %r11b
movb (%rax), %dl
cmpb %dl, %r11b
setge %al
jge 0x1765a
leaq 0x1a8d5(%rip), %rcx     # 0x31da6
movb 0x12(%r12), %dil
movb (%rcx), %sil
cmpb %sil, %dil
je 0x1765e
movb 0x13(%r12), %bl
cmpb %r14b, %dil
jne 0x174f4
movl %r14d, %eax
jmp 0x17620
movq 0x10(%rsp), %rdi
movl $0x0, %r8d
movq 0x18(%rdi), %r10
movl $0x0, %edi
leaq 0x10(%r10), %r9
cmpq $0x2, %r10
cmovaeq %r9, %rdi
jb 0x17525
movzwl 0x6(%r10), %r8d
shll $0x5, %r8d
addq %r8, %r9
movq %r9, %r8
xorl %r9d, %r9d
cmpb 0xc(%rsp), %r11b
movb %r11b, 0x9(%rsp)
sete %r9b
xorl %r12d, %r12d
xorl %r10d, %r10d
xorl %r15d, %r15d
leaq 0x2(%r9,%r9), %rcx
movq %rcx, 0x20(%rsp)
movb 0x12(%rdi), %r9b
movb %r9b, 0xb(%rsp)
movb 0x13(%rdi), %r13b
leal 0x1(%r13), %r11d
cmpb %bl, %r13b
movzbl %bl, %r13d
movzbl %r11b, %ebx
cmovbl %r13d, %ebx
cmpb %sil, %r9b
jne 0x17579
movl $0x3, %r9d
xorl %r13d, %r13d
jmp 0x1758d
cmpb %r14b, %r9b
je 0x17605
cmpb %bpl, %r9b
jne 0x175d8
movb $0x1, %r13b
xorl %r9d, %r9d
movq 0x8(%rdi), %r11
cmpq %r9, %r15
jae 0x175a1
movq %r9, %r15
movq %r11, %r10
movq %rdi, %r12
jmp 0x175c9
jne 0x175c9
cmpq %r11, %r10
movl %ebp, %ecx
setb %r9b
seta %bpl
andb %r9b, %r13b
movq %rdi, %r9
orb %bpl, %r13b
jne 0x175c1
movq %r10, %r11
movq %r12, %r9
movl %ecx, %ebp
movq %r11, %r10
movq %r9, %r12
addq $0x20, %rdi
cmpq %r8, %rdi
jne 0x17549
jmp 0x1760b
cmpb %dl, %r9b
jne 0x175e4
movq 0x20(%rsp), %r9
jmp 0x17574
cmpb 0xa(%rsp), %r9b
jne 0x175f3
movl $0x5, %r9d
jmp 0x17574
cmpb 0xc(%rsp), %r9b
jne 0x17672
movl $0x1, %r9d
jmp 0x17574
movl %r11d, %ebx
movq %rdi, %r12
cmpq $0x3, %r15
je 0x17660
movb 0x12(%r12), %al
movq 0x18(%rsp), %r15
movb 0x9(%rsp), %r11b
movzbl %r11b, %esi
movzbl %al, %edx
movq %r15, %rdi
callq 0x2a504
testb %al, %al
je 0x174b2
movzwl 0x14(%r12), %eax
movq 0x10(%rsp), %rcx
leaq 0x1a760(%rip), %rdx     # 0x31da9
movw %ax, 0x16(%rcx)
movb %bl, 0x13(%rcx)
movb 0x12(%rcx), %al
cmpb (%rdx), %al
setge %al
jmp 0x17660
movb $0x1, %al
jmp 0x17660
xorl %eax, %eax
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 0x2a306
leaq 0x173fd(%rip), %rsi     # 0x2ea88
leaq 0x28(%rsp), %rdi
movq %rbx, %rdx
callq 0xd1a3
leaq 0x151a3(%rip), %rdx     # 0x2c842
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x17b11
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x59d5
leaq 0x38(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x176dd
callq 0x5310
jmp 0x176dd
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x176f0
callq 0x5310
movq %rbx, %rdi
callq 0x56c0