// File: /workspace/github_cpp_makefile_star2_O1/tewalds[P]pentagod/agentmctsthread.cpp
// Function: AgentMCTS::MCTSThread::walk_tree(Board&, AgentMCTS::Node*, int)
void AgentMCTS::MCTSThread::walk_tree(Board & board, Node * node, int depth){
	int toplay = board.toplay();

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

			if(child->outcome < 0){
				movelist.addtree(child->move, toplay);

				if(!board.move(child->move)){
					logerr("move failed: " + child->move.to_s(true) + "\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(toplay));
				player->do_backup(node, child, toplay);
				return;
			}
		}while(!player->do_backup(node, child, toplay));

		return;
	}

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

	int won = (player->minimax ? node->outcome : board.won());

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

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

		//do random game on this node
		for(int i = 0; i < player->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(player->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 0x1a(%rsi), %rbp
leaq 0x12(%rdx), %r13
movzbl 0x19(%r14), %r12d
movq 0x20(%r15), %rax
cmpq $0x2, %rax
jb 0x21031
cmpw $0x0, 0x6(%rax)
je 0x21031
cmpb $0x0, 0x12(%r15)
js 0x21171
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x21076
cmpl $0x0, 0x70(%rbx)
jne 0x21076
movl $0x1, 0x70(%rbx)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x18(%rsp), %xmm2, %xmm1
vdivsd 0x7f9e(%rip), %xmm1, %xmm1 # 0x29008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x64(%rax)
je 0x21085
movq %r13, %rax
jmp 0x2109e
cmpb $-0x4, 0x1a(%r14)
movq %rbp, %rax
jne 0x2109e
movq %r14, %rdi
callq 0xdab0
movb %al, 0x1a(%r14)
movq %rbp, %rax
movsbl (%rax), %esi
testl %esi, %esi
jns 0x211fe
movq 0x40(%rbx), %rax
movl 0x68(%rax), %eax
incl %eax
cmpq %rax, 0x8(%r15)
jb 0x210d1
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x21794
testb %al, %al
jne 0x21010
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x21110
movl $0x2, 0x70(%rbx)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%r15), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r15), %xmm2, %xmm1
vdivsd 0x7f04(%rip), %xmm1, %xmm1 # 0x29008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe8(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x21207
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 0x21ad2
incl %ebp
movq 0x40(%rbx), %rax
cmpl 0x5c(%rax), %ebp
jl 0x21125
jmp 0x21207
movq 0x40(%rbx), %rdi
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x21562
testb %al, %al
jne 0x21269
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x213ee
movq %rax, %r13
cmpb $0x0, 0x12(%rax)
jns 0x21157
movl 0x14(%r13), %esi
movq %r14, %rdi
callq 0x9240
testb %al, %al
je 0x2127b
lock
incq 0x8(%r13)
movl 0xc(%rsp), %ecx
incl %ecx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x20fea
movl %r12d, %eax
shll $0x4, %eax
leaq 0x40(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x211cd
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x211db
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 0x21562
leaq 0x50(%rbx), %rdi
callq 0x21cd0
leaq 0x78(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x21d00
decq 0x58(%rbx)
decq 0x68(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x21269
leaq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%r14), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r14), %xmm2, %xmm1
vdivsd 0x7dc0(%rip), %xmm1, %xmm1 # 0x29008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf0(%rbx)
cmpl $0x1, 0x70(%rbx)
jne 0x21262
vmovsd %xmm0, 0xe8(%rbx)
movl $0x3, 0x70(%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x14, %r13
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x99d8
leaq 0xbe29(%rip), %rsi      # 0x2d0c4
leaq 0x58(%rsp), %rdi
movq %rbx, %rdx
callq 0x21c5e
leaq 0x8833(%rip), %rsi      # 0x29ae2
leaq 0x58(%rsp), %rdi
callq 0x4610
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x212dc
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r15)
jmp 0x212e9
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 0x15f84
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x21b5f
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0x49ef
movq %rbx, %rdi
callq 0x4130
leaq 0x38(%rsp), %rdi
callq 0x4130
leaq 0x10(%rsp), %rdi
callq 0x4130
leaq 0x58(%rsp), %rdi
callq 0x4130
leaq 0x78(%rsp), %rdi
callq 0x4130
leaq 0xbd61(%rip), %rdi      # 0x2d0d2
leaq 0xbd71(%rip), %rsi      # 0x2d0e9
leaq 0xbd7e(%rip), %rcx      # 0x2d0fd
movl $0x2a, %edx
callq 0x41b0
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x213a4
callq 0x42b0
jmp 0x213a4
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x213b8
callq 0x42b0
jmp 0x213b8
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x213d0
callq 0x42b0
jmp 0x213d0
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x213e6
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0