// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0x1aebb(%rip), %rbp     # 0x31da9
leaq 0x1aeb0(%rip), %r13     # 0x31da5
leaq 0x10(%rsp), %r12
movl %ecx, 0xc(%rsp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq 0x18(%r15), %rax
cmpq $0x2, %rax
jb 0x16f25
cmpw $0x0, 0x6(%rax)
je 0x16f25
movb 0x12(%r15), %al
cmpb (%rbp), %al
jl 0x170c4
movq 0x40(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x16f6f
cmpl $0x0, 0x78(%r14)
jne 0x16f6f
movl $0x1, 0x78(%r14)
movq %r12, %rdi
xorl %esi, %esi
callq 0x5600
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x18(%rsp), %xmm2, %xmm1
vdivsd 0x150aa(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe8(%r14)
movq 0x40(%r14), %rax
cmpl $0x0, 0x64(%rax)
je 0x16f7b
movb 0x12(%r15), %al
jmp 0x16f8f
movb 0x1a(%rbx), %al
cmpb (%r13), %al
jne 0x16f8f
movq %rbx, %rdi
callq 0xe2a8
movb %al, 0x1a(%rbx)
cmpb (%rbp), %al
jge 0x1703d
movq 0x40(%r14), %rax
movl 0x68(%rax), %ecx
incl %ecx
cmpq %rcx, 0x8(%r15)
jb 0x16fc1
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x176f8
testb %al, %al
jne 0x16f07
movq 0x40(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x17002
leaq 0x10(%rsp), %r15
movl $0x2, 0x78(%r14)
xorl %esi, %esi
movq %r15, %rdi
callq 0x5600
vcvtsi2sdq (%r15), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r15), %xmm2, %xmm1
vdivsd 0x15017(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf0(%r14)
movq 0x40(%r14), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x17049
leaq 0x10(%rsp), %r15
xorl %ebp, %ebp
movq 0x20(%rbx), %rax
movq %r14, %rdi
movq %r15, %rsi
movq %rax, 0x30(%rsp)
vmovupd (%rbx), %ymm0
vmovupd %ymm0, 0x10(%rsp)
vzeroupper
callq 0x17a5e
movq 0x40(%r14), %rax
incl %ebp
cmpl 0x5c(%rax), %ebp
jl 0x1700f
jmp 0x17049
leaq 0x58(%r14), %rdi
movzbl %al, %esi
callq 0x17b66
movl 0xc(%rsp), %esi
leaq 0x80(%r14), %rdi
callq 0x17b9e
decq 0x60(%r14)
decq 0x70(%r14)
movq 0x40(%r14), %rax
cmpb $0x1, 0x48(%rax)
jne 0x170b2
leaq 0x10(%rsp), %rbx
xorl %esi, %esi
movq %rbx, %rdi
callq 0x5600
vcvtsi2sdq (%rbx), %xmm2, %xmm0
vcvtsi2sdq 0x8(%rbx), %xmm2, %xmm1
vdivsd 0x14f7b(%rip), %xmm1, %xmm1 # 0x2c008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf8(%r14)
cmpl $0x1, 0x78(%r14)
jne 0x170aa
vmovsd %xmm0, 0xf0(%r14)
movl $0x3, 0x78(%r14)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbq 0x19(%rbx), %r12
movq %r14, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x172d4
movq %rax, %r13
movb 0x12(%rax), %al
cmpb (%rbp), %al
jl 0x170f6
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x17476
testb %al, %al
je 0x170c9
jmp 0x170b2
movl 0x14(%r13), %esi
movq %rbx, %rdi
callq 0xd6ec
testb %al, %al
je 0x17165
lock
incq 0x8(%r13)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movl 0xc(%rsp), %ecx
incl %ecx
callq 0x16ed6
movq %r12, %rax
shlq $0x4, %rax
leaq 0x48(%rax,%r14), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x17138
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x17146
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 0x17476
addq $0x14, %r13
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0xd3c2
leaq 0x177fc(%rip), %rsi     # 0x2e97c
leaq 0x58(%rsp), %rdi
movq %r14, %rdx
callq 0xd1a3
leaq 0x156ae(%rip), %rsi     # 0x2c842
leaq 0x58(%rsp), %rdi
callq 0x5720
leaq 0x20(%rsp), %r14
movq %rax, %rcx
addq $0x10, %rcx
movq %r14, -0x10(%r14)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x171c1
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r14)
jmp 0x171ce
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
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1d25a
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0xd294
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0x59c3
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 0x17734(%rip), %rdi     # 0x2e98a
leaq 0x17744(%rip), %rsi     # 0x2e9a1
leaq 0x17759(%rip), %rcx     # 0x2e9bd
movl $0x31, %edx
callq 0x5200
leaq 0x48(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x17289
callq 0x5310
jmp 0x17289
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x1729d
callq 0x5310
jmp 0x1729d
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x172b5
callq 0x5310
jmp 0x172b5
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x172cb
callq 0x5310
movq %rbx, %rdi
callq 0x56c0
nop