// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]morat/pentago/agentmctsthread.cpp
// Function: Morat::Pentago::AgentMCTS::AgentThread::walk_tree(Morat::Pentago::Board&, Morat::Pentago::AgentMCTS::Node*, int)
void AgentMCTS::AgentThread::walk_tree(Board & board, Node * node, int depth){
	Side to_play = board.to_play();

	if(!node->children.empty() && node->outcome < Outcome::DRAW){
	//choose a child and recurse
		Node * child;
		do{
			child = choose_move(node, to_play);

			if(child->outcome < Outcome::DRAW){
				movelist.addtree(child->move, to_play);

				if(!board.move(child->move)){
					logerr("move failed: " + child->move.to_s() + "\n" + board.to_s(true));
					assert(false && "move failed");
				}

				child->exp.addvloss(); //balanced out after rollouts

				walk_tree(board, child, depth+1);

				child->exp.addv(movelist.getexp(to_play));
				agent->do_backup(node, child, to_play);
				return;
			}
		}while(!agent->do_backup(node, child, to_play));

		return;
	}

	if(agent->profile && stage == 0){
		stage = 1;
		timestamps[1] = Time();
	}

	Outcome won = (agent->minimax ? node->outcome : board.outcome());

	//if it's not already decided
	if(won < Outcome::DRAW){
		//create children if valid
		if(node->exp.num() >= agent->visitexpand+1 && create_children(board, node)){
			walk_tree(board, node, depth);
			return;
		}

		if(agent->profile){
			stage = 2;
			timestamps[2] = Time();
		}

		//do random game on this node
		for(int i = 0; i < agent->rollouts; i++){
			Board copy = board;
			rollout(copy, node->move, depth);
		}
	}else{
		movelist.finishrollout(won); //got to a terminal state, it's worth recording
	}

	treelen.add(depth);

	movelist.subvlosses(1);

	if(agent->profile){
		timestamps[3] = Time();
		if(stage == 1)
			timestamps[2] = timestamps[3];
		stage = 3;
	}

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x18(%rdx), %rax
movl %ecx, %r13d
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
cmpq $0x2, %rax
jb 0x117ee
cmpw $0x0, 0x6(%rax)
je 0x117ee
leaq 0x13472(%rip), %rbp     # 0x24c19
movb 0x12(%r15), %al
cmpb (%rbp), %al
jge 0x117ee
movsbq 0x19(%r14), %r12
movl %r13d, 0xc(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x11ab8
movq %rax, %r13
movb 0x12(%rax), %al
cmpb (%rbp), %al
jl 0x11944
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x11c3e
testb %al, %al
je 0x117ba
jmp 0x118d0
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x11821
cmpl $0x0, 0x78(%rbx)
jne 0x11821
leaq 0x10(%rsp), %r12
movl $0x1, 0x78(%rbx)
movq %r12, %rdi
callq 0x5df0
movq (%r12), %rax
movq %rax, 0xe8(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x64(%rax)
je 0x1182d
movb 0x12(%r15), %al
jmp 0x11835
movq %r14, %rdi
callq 0xa9d0
leaq 0x133dd(%rip), %rcx     # 0x24c19
cmpb (%rcx), %al
jge 0x11878
movq 0x40(%rbx), %rax
movl 0x68(%rax), %ecx
incl %ecx
cmpq %rcx, 0x8(%r15)
jb 0x118e6
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x11eec
testb %al, %al
je 0x118e2
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r13d, %ecx
callq 0x11772
jmp 0x118d0
leaq 0x58(%rbx), %rdi
movzbl %al, %esi
callq 0x12224
leaq 0x80(%rbx), %rdi
movl %r13d, %esi
callq 0x12258
decq 0x60(%rbx)
decq 0x70(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x118d0
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x5df0
movq (%r14), %rax
movq %rax, 0xf8(%rbx)
cmpl $0x1, 0x78(%rbx)
jne 0x118c9
movq %rax, 0xf0(%rbx)
movl $0x3, 0x78(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x1190a
leaq 0x10(%rsp), %r15
movl $0x2, 0x78(%rbx)
movq %r15, %rdi
callq 0x5df0
movq (%r15), %rax
movq %rax, 0xf0(%rbx)
leaq 0x10(%rsp), %r15
xorl %ebp, %ebp
movq 0x40(%rbx), %rax
cmpl 0x5c(%rax), %ebp
jge 0x11884
movq 0x20(%r14), %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, 0x30(%rsp)
vmovups (%r14), %ymm0
vmovups %ymm0, 0x10(%rsp)
vzeroupper
callq 0x1218e
incl %ebp
jmp 0x11911
movl 0x14(%r13), %esi
movq %r14, %rdi
callq 0xad6a
testb %al, %al
je 0x119b3
lock
incq 0x8(%r13)
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl 0xc(%rsp), %ecx
incl %ecx
callq 0x11772
movq %r12, %rax
shlq $0x4, %rax
leaq 0x48(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x11986
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x11994
lock
addq %rax, 0x8(%r13)
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11c3e
addq $0x14, %r13
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xabfe
leaq 0xff0e(%rip), %rsi      # 0x218dc
leaq 0x58(%rsp), %rdi
movq %rbx, %rdx
callq 0xaac0
leaq 0xde36(%rip), %rdx      # 0x1f818
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x7b4d
leaq 0x38(%rsp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0x15f06
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0xab3b
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0xaaae
movq %rbx, %rdi
callq 0x5170
leaq 0x38(%rsp), %rdi
callq 0x5170
leaq 0x10(%rsp), %rdi
callq 0x5170
leaq 0x58(%rsp), %rdi
callq 0x5170
leaq 0x78(%rsp), %rdi
callq 0x5170
leaq 0xfe8b(%rip), %rdi      # 0x218ea
leaq 0xfe9b(%rip), %rsi      # 0x21901
leaq 0xfeb0(%rip), %rcx      # 0x2191d
pushq $0x31
popq %rdx
callq 0x5220
leaq 0x38(%rsp), %rdi
movq %rax, %rbx
callq 0x5170
jmp 0x11a87
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5170
jmp 0x11a96
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5170
jmp 0x11aa5
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5170
movq %rbx, %rdi
callq 0x5690
nop