// File: /workspace/github_cpp_makefile_star2_O1/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
movl %ecx, 0xc(%rsp)
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x1ace6(%rip), %rbp     # 0x31da9
leaq 0x1a(%rsi), %r13
leaq 0x12(%rdx), %r12
movq 0x18(%r15), %rax
cmpq $0x2, %rax
jb 0x170e9
cmpw $0x0, 0x6(%rax)
je 0x170e9
movb 0x12(%r15), %al
cmpb (%rbp), %al
jl 0x17290
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x1712e
cmpl $0x0, 0x78(%rbx)
jne 0x1712e
movl $0x1, 0x78(%rbx)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x5600
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x18(%rsp), %xmm2, %xmm1
vdivsd 0x14ee6(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe8(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x64(%rax)
je 0x1713d
movq %r12, %rax
jmp 0x1715e
movb 0x1a(%r14), %cl
movq %r13, %rax
leaq 0x1ac5a(%rip), %rdx     # 0x31da5
cmpb (%rdx), %cl
jne 0x1715e
movq %r14, %rdi
callq 0xe358
movb %al, 0x1a(%r14)
movq %r13, %rax
movb (%rax), %al
cmpb (%rbp), %al
jge 0x1720d
movq 0x40(%rbx), %rax
movl 0x68(%rax), %eax
incl %eax
cmpq %rax, 0x8(%r15)
jb 0x1718e
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x178dc
testb %al, %al
jne 0x170cb
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x171cd
movl $0x2, 0x78(%rbx)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x5600
vcvtsi2sdq (%r15), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r15), %xmm2, %xmm1
vdivsd 0x14e47(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x17219
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movq 0x20(%r14), %rax
movq %rax, 0x30(%rsp)
vmovupd (%r14), %ymm0
vmovupd %ymm0, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
vzeroupper
callq 0x17c56
incl %ebp
movq 0x40(%rbx), %rax
cmpl 0x5c(%rax), %ebp
jl 0x171de
jmp 0x17219
leaq 0x58(%rbx), %rdi
movzbl %al, %esi
callq 0x17d60
leaq 0x80(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x17d98
decq 0x60(%rbx)
decq 0x70(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x1727e
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x5600
vcvtsi2sdq (%r14), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r14), %xmm2, %xmm1
vdivsd 0x14dab(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf8(%rbx)
cmpl $0x1, 0x78(%rbx)
jne 0x17277
vmovsd %xmm0, 0xf0(%rbx)
movl $0x3, 0x78(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbq 0x19(%r14), %r12
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x174a8
movq %rax, %r13
movb 0x12(%rax), %al
cmpb (%rbp), %al
jl 0x172c6
movq 0x40(%rbx), %rdi
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x1764a
testb %al, %al
je 0x17295
jmp 0x1727e
movl 0x14(%r13), %esi
movq %r14, %rdi
callq 0xd74e
testb %al, %al
je 0x17339
lock
incq 0x8(%r13)
movl 0xc(%rsp), %ecx
incl %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1709e
movq %r12, %rax
shlq $0x4, %rax
leaq 0x48(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x17308
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x17316
lock
addq %rax, 0x8(%r13)
movq 0x40(%rbx), %rdi
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 0x1764a
addq $0x14, %r13
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd416
leaq 0x17628(%rip), %rsi     # 0x2e97c
leaq 0x58(%rsp), %rdi
movq %rbx, %rdx
callq 0xd1f7
leaq 0x154da(%rip), %rsi     # 0x2c842
leaq 0x58(%rsp), %rdi
callq 0x5720
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x17395
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r15)
jmp 0x173a2
movq %rdx, 0x10(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x1d55e
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0xd2e8
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0x59c5
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 0x17560(%rip), %rdi     # 0x2e98a
leaq 0x17570(%rip), %rsi     # 0x2e9a1
leaq 0x17585(%rip), %rcx     # 0x2e9bd
movl $0x31, %edx
callq 0x5200
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1745d
callq 0x5310
jmp 0x1745d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x17471
callq 0x5310
jmp 0x17471
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17489
callq 0x5310
jmp 0x17489
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1749f
callq 0x5310
movq %rbx, %rdi
callq 0x56c0
nop