// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdx, 0xc8(%rsp)
movl %ecx, 0xc4(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd0(%rsp), %rax
movsbl 0x12(%rax), %eax
movl %eax, 0xc0(%rsp)
cmpl $0x0, 0xc0(%rsp)
jl 0x31f2d
movb $0x1, 0xe7(%rsp)
jmp 0x323c8
movq 0xc8(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $-0x3, %eax
jne 0x31f4b
movb $0x0, 0xe7(%rsp)
jmp 0x323c8
movq 0xc8(%rsp), %rax
movb 0x13(%rax), %al
movb %al, 0xbf(%rsp)
movq 0xc8(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl 0xc4(%rsp), %eax
je 0x3232a
movq $0x0, 0xb0(%rsp)
movq $0x0, 0xa8(%rsp)
movq $0x0, 0xa0(%rsp)
movq $0x0, 0x98(%rsp)
movq $0x0, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc40
movq %rax, 0x90(%rsp)
movq 0xd0(%rsp), %rdi
addq $0x20, %rdi
callq 0x2dc90
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rax
cmpq 0x88(%rsp), %rax
je 0x32310
movq 0x90(%rsp), %rax
movsbl 0x12(%rax), %eax
movl %eax, 0x84(%rsp)
movzbl 0xbf(%rsp), %eax
movq 0x90(%rsp), %rcx
movzbl 0x13(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3203e
movq 0x90(%rsp), %rax
movzbl 0x13(%rax), %eax
addl $0x1, %eax
movb %al, 0xbf(%rsp)
cmpl $-0x3, 0x84(%rsp)
jne 0x32059
movq $0x3, 0xa0(%rsp)
jmp 0x32239
movl 0x84(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jne 0x320a0
movq 0x90(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq $0x6, 0xa0(%rsp)
movq 0x90(%rsp), %rax
movzbl 0x13(%rax), %eax
addl $0x1, %eax
movb %al, 0xbf(%rsp)
jmp 0x32310
movl 0x84(%rsp), %eax
movl $0x3, %ecx
subl 0xc4(%rsp), %ecx
cmpl %ecx, %eax
jne 0x320c8
movq $0x0, 0xa0(%rsp)
jmp 0x32235
cmpl $0x0, 0x84(%rsp)
jne 0x32106
movl 0xc0(%rsp), %eax
movl 0xc4(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jne 0x320f5
movq $0x4, 0xa0(%rsp)
jmp 0x32101
movq $0x2, 0xa0(%rsp)
jmp 0x32233
movl 0x84(%rsp), %eax
xorl %ecx, %ecx
subl 0xc4(%rsp), %ecx
cmpl %ecx, %eax
jne 0x3212b
movq $0x5, 0xa0(%rsp)
jmp 0x32231
movl 0x84(%rsp), %eax
movl 0xc4(%rsp), %ecx
subl $0x3, %ecx
cmpl %ecx, %eax
jne 0x32151
movq $0x1, 0xa0(%rsp)
jmp 0x3222f
movl 0x84(%rsp), %esi
leaq 0x20(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x16070
movq (%rsp), %rdx
leaq 0xef92(%rip), %rsi      # 0x41103
leaq 0x40(%rsp), %rdi
callq 0xe5f0
jmp 0x3217d
leaq 0xb92c(%rip), %rdx      # 0x3dab0
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x93b0
jmp 0x32195
leaq 0x60(%rsp), %rdi
callq 0x15ad0
jmp 0x321a1
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
leaq 0x20(%rsp), %rdi
callq 0x4210
leaq 0xef4e(%rip), %rdi      # 0x41114
leaq 0xee74(%rip), %rsi      # 0x41041
movl $0xf3, %edx
leaq 0xef7c(%rip), %rcx      # 0x41155
callq 0x4250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
jmp 0x32220
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
jmp 0x32216
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x14(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x4210
leaq 0x40(%rsp), %rdi
callq 0x4210
leaq 0x20(%rsp), %rdi
callq 0x4210
jmp 0x323d9
jmp 0x32231
jmp 0x32233
jmp 0x32235
jmp 0x32237
jmp 0x32239
movq 0x90(%rsp), %rdi
callq 0x2e5d0
movq %rax, 0xb0(%rsp)
movq 0x98(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jae 0x32292
movq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xc8(%rsp)
jmp 0x322f5
movq 0x98(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jne 0x322f3
cmpq $0x0, 0xa0(%rsp)
jne 0x322c1
movq 0xa8(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jb 0x322d3
movq 0xa8(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jbe 0x322f3
movq 0xb0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xc8(%rsp)
jmp 0x322f5
jmp 0x322f7
movq 0x90(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x90(%rsp)
jmp 0x31fe4
cmpq $0x3, 0x98(%rsp)
jne 0x32328
movb $0x0, 0xe7(%rsp)
jmp 0x323c8
jmp 0x3232a
movq 0xd0(%rsp), %rdi
addq $0x12, %rdi
movq 0xc8(%rsp), %rdx
addq $0x12, %rdx
leaq 0xc0(%rsp), %rsi
callq 0x32f30
testb $0x1, %al
jne 0x32355
jmp 0x32381
movq 0xc8(%rsp), %rcx
movq 0xd0(%rsp), %rax
movw 0x14(%rcx), %cx
movw %cx, 0x18(%rax)
movb 0xbf(%rsp), %cl
movq 0xd0(%rsp), %rax
movb %cl, 0x13(%rax)
jmp 0x323ad
movq 0x8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movl 0xc4(%rsp), %ecx
callq 0x31ed0
andb $0x1, %al
movb %al, 0xe7(%rsp)
jmp 0x323c8
movq 0xd0(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movb %al, 0xe7(%rsp)
movb 0xe7(%rsp), %al
andb $0x1, %al
addq $0xe8, %rsp
retq
movq 0x18(%rsp), %rdi
callq 0x4660
nopw %cs:(%rax,%rax)