// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]pentagod/agentmctsthread.cpp
// Function: AgentMCTS::do_backup(AgentMCTS::Node*, AgentMCTS::Node*, int)
bool AgentMCTS::do_backup(Node * node, Node * backup, int toplay){
	int nodeoutcome = node->outcome;
	if(nodeoutcome >= 0) //already proven, probably by a different thread
		return true;

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


	uint8_t proofdepth = backup->proofdepth;
	if(backup->outcome != toplay){
		uint64_t sims = 0, bestsims = 0, outcome = 0, bestoutcome = 0;
		backup = NULL;

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

		for( ; child != end; child++){
			int childoutcome = 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(childoutcome == -3){ // win/draw/loss
				outcome = 3;
			}else if(childoutcome == toplay){ //win
				backup = child;
				outcome = 6;
				proofdepth = child->proofdepth+1;
				break;
			}else if(childoutcome == 3-toplay){ //loss
				outcome = 0;
			}else if(childoutcome == 0){ //draw
				if(nodeoutcome == toplay-3) //draw/loss
					outcome = 4;
				else
					outcome = 2;
			}else if(childoutcome == -toplay){ //win/draw
				outcome = 5;
			}else if(childoutcome == toplay-3){ //draw/loss
				outcome = 1;
			}else{
				logerr("childoutcome == " + to_str(childoutcome) + "\n");
				assert(false && "How'd I get here? All outcomes should be tested above");
			}

			sims = child->exp.num();
			if(bestoutcome < outcome){ //better outcome is always preferable
				bestoutcome = outcome;
				bestsims = sims;
				backup = child;
			}else if(bestoutcome == outcome && ((outcome == 0 && bestsims < sims) || bestsims > sims)){
				//find long losses or easy wins/draws
				bestsims = sims;
				backup = child;
			}
		}

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

	if(CAS(node->outcome, nodeoutcome, 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, toplay);

	return (node->outcome >= 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
pushq $0x3
popq %r9
movl %ecx, %eax
subl %ecx, %r9d
negl %eax
leal -0x3(%rcx), %edi
movl %eax, 0x4(%rsp)
movl %edi, (%rsp)
pushq $0x3
popq %r15
movq %rsi, 0x8(%rsp)
movsbl 0x12(%rsi), %eax
testl %eax, %eax
setns %dil
jns 0x172f1
movsbl 0x12(%rdx), %r8d
cmpl $-0x3, %r8d
je 0x172f6
movb 0x13(%rdx), %bpl
cmpl %ecx, %r8d
je 0x172cf
movq 0x20(%rsi), %r8
movl $0x0, %r12d
movl $0x0, %r13d
leaq 0x10(%r8), %rdx
cmpq $0x2, %r8
cmovaeq %rdx, %r12
jb 0x171e1
movzwl 0x6(%r8), %r8d
imulq $0x28, %r8, %r8
addq %r8, %rdx
movq %rdx, %r13
xorl %edx, %edx
cmpl %eax, (%rsp)
sete %dl
xorl %r11d, %r11d
xorl %r14d, %r14d
leaq 0x2(%rdx,%rdx), %rdx
movq %rdx, 0x10(%rsp)
xorl %edx, %edx
cmpq %r13, %r12
je 0x172c0
movb 0x13(%r12), %bl
movsbl 0x12(%r12), %r8d
leal 0x1(%rbx), %r10d
cmpb %bpl, %bl
movzbl %bpl, %ebx
movzbl %r10b, %ebp
cmovbl %ebx, %ebp
cmpl $-0x3, %r8d
jne 0x1722f
xorl %r10d, %r10d
movq %r15, %rbx
jmp 0x17271
cmpl %ecx, %r8d
je 0x172ba
cmpl %r8d, %r9d
jne 0x17244
movb $0x1, %r10b
xorl %ebx, %ebx
jmp 0x17271
testb %r8b, %r8b
je 0x17257
cmpl 0x4(%rsp), %r8d
jne 0x17261
xorl %r10d, %r10d
pushq $0x5
jmp 0x17270
movq 0x10(%rsp), %rbx
xorl %r10d, %r10d
jmp 0x17271
cmpl %r8d, (%rsp)
jne 0x17309
xorl %r10d, %r10d
pushq $0x1
popq %rbx
movq 0x8(%r12), %r8
cmpq %rbx, %r14
jae 0x17283
movq %rbx, %r14
movq %r12, %rdx
jmp 0x172ae
jne 0x172ab
cmpq %r8, %r11
movq %r15, %rsi
setb %bl
seta %r15b
andb %bl, %r10b
orb %r15b, %r10b
movq %r12, %r10
jne 0x172a3
movq %r11, %r8
movq %rdx, %r10
movq %r10, %rdx
movq %rsi, %r15
jmp 0x172ae
movq %r11, %r8
addq $0x28, %r12
movq %r8, %r11
jmp 0x171fb
movl %r10d, %ebp
movq %r12, %rdx
cmpq $0x3, %r14
je 0x172f8
movb 0x12(%rdx), %r8b
movq 0x8(%rsp), %rsi
lock
cmpxchgb %r8b, 0x12(%rsi)
jne 0x17188
movzwl 0x14(%rdx), %eax
movw %ax, 0x18(%rsi)
movb %bpl, 0x13(%rsi)
cmpb $0x0, 0x12(%rsi)
setns %dil
jmp 0x172f8
movb $0x1, %dil
jmp 0x172f8
xorl %edi, %edi
movl %edi, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38(%rsp), %rbx
movl %r8d, %esi
movq %rbx, %rdi
callq 0xaee1
leaq 0x8deb(%rip), %rsi      # 0x2010b
leaq 0x18(%rsp), %rdi
movq %rbx, %rdx
callq 0x8423
leaq 0x5784(%rip), %rdx      # 0x1cab8
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x6821
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0xac65
movq %rbx, %rdi
callq 0x4130
leaq 0x18(%rsp), %rdi
callq 0x4130
leaq 0x38(%rsp), %rdi
callq 0x4130
leaq 0x8da9(%rip), %rdi      # 0x2011c
leaq 0x8ccf(%rip), %rsi      # 0x20049
leaq 0x8ddc(%rip), %rcx      # 0x2015d
movl $0xf3, %edx
callq 0x41c0
leaq 0x18(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
jmp 0x1739d
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4130
movq %rbx, %rdi
callq 0x4590
nop