// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rsp, %r13
movzbl 0x19(%r14), %r12d
movq 0x20(%r15), %rax
cmpq $0x2, %rax
jb 0x209da
cmpw $0x0, 0x6(%rax)
je 0x209da
cmpb $0x0, 0x12(%r15)
js 0x20b0a
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x20a20
cmpl $0x0, 0x70(%rbx)
jne 0x20a20
movl $0x1, 0x70(%rbx)
movq %r13, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm1
vdivsd 0x75f8(%rip), %xmm1, %xmm1 # 0x28008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x64(%rax)
je 0x20a2c
movb 0x12(%r15), %al
jmp 0x20a40
movb 0x1a(%r14), %al
cmpb $-0x4, %al
jne 0x20a40
movq %r14, %rdi
callq 0xd9e4
movb %al, 0x1a(%r14)
testb %al, %al
jns 0x20b91
movq 0x40(%rbx), %rax
movl 0x68(%rax), %ecx
incl %ecx
cmpq %rcx, 0x8(%r15)
jb 0x20a74
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x2112e
testb %al, %al
jne 0x209b9
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x20ab1
movq %rsp, %r15
movl $0x2, 0x70(%rbx)
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%r15), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r15), %xmm2, %xmm1
vdivsd 0x7567(%rip), %xmm1, %xmm1 # 0x28008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xe8(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x20bc4
xorl %r12d, %r12d
movq %rsp, %r15
movq 0x20(%r14), %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, 0x20(%rsp)
vmovupd (%r14), %ymm0
vmovupd %ymm0, (%rsp)
vzeroupper
callq 0x21474
movq 0x40(%rbx), %rax
incl %r12d
cmpl 0x5c(%rax), %r12d
jl 0x20ac1
jmp 0x20bc4
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x20f12
testb %al, %al
jne 0x20c22
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x20d9e
cmpb $0x0, 0x12(%rax)
movq %rax, %r13
jns 0x20af4
movl 0x14(%r13), %esi
movq %r14, %rdi
callq 0x9282
testb %al, %al
je 0x20c34
lock
incq 0x8(%r13)
incl %ebp
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x2099a
movl %r12d, %eax
shll $0x4, %eax
leaq 0x40(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x20b64
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x20b72
lock
addq %rax, 0x8(%r13)
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f12
incq 0x58(%rbx)
incq 0x68(%rbx)
testb %al, %al
je 0x20bb4
movzbl %al, %eax
leaq 0x50(%rbx), %rcx
decl %eax
shlq $0x4, %rax
addq %rcx, %rax
movl $0x2, %ecx
jmp 0x20bc1
incq 0x50(%rbx)
leaq 0x60(%rbx), %rax
movl $0x1, %ecx
addq %rcx, (%rax)
leaq 0x78(%rbx), %rdi
movl %ebp, %esi
callq 0x216a6
decq 0x58(%rbx)
decq 0x68(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x20c22
movq %rsp, %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%r14), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r14), %xmm2, %xmm1
vdivsd 0x7407(%rip), %xmm1, %xmm1 # 0x28008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0xf0(%rbx)
cmpl $0x1, 0x70(%rbx)
jne 0x20c1b
vmovsd %xmm0, 0xe8(%rbx)
movl $0x3, 0x70(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x14, %r13
leaq 0x68(%rsp), %rbx
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x999a
leaq 0xb470(%rip), %rsi      # 0x2c0c4
leaq 0x48(%rsp), %rdi
movq %rbx, %rdx
callq 0x21634
leaq 0x7e7a(%rip), %rsi      # 0x28ae2
leaq 0x48(%rsp), %rdi
callq 0x4610
leaq 0x10(%rsp), %r15
movq %rax, %rcx
addq $0x10, %rcx
movq %r15, -0x10(%r15)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x20c95
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r15)
jmp 0x20ca1
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x28(%rsp), %rdi
movl $0x1, %edx
movq %r14, %rsi
callq 0x15e7a
leaq 0x88(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rsp, %rsi
callq 0x21535
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x4a5d
movq %rbx, %rdi
callq 0x4130
leaq 0x28(%rsp), %rdi
callq 0x4130
movq %rsp, %rdi
callq 0x4130
leaq 0x48(%rsp), %rdi
callq 0x4130
leaq 0x68(%rsp), %rdi
callq 0x4130
leaq 0xb3ad(%rip), %rdi      # 0x2c0d2
leaq 0xb3bd(%rip), %rsi      # 0x2c0e9
leaq 0xb3ca(%rip), %rcx      # 0x2c0fd
movl $0x2a, %edx
callq 0x41b0
leaq 0x38(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x20d58
callq 0x42b0
jmp 0x20d58
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x20d6b
callq 0x42b0
jmp 0x20d6b
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20d83
callq 0x42b0
jmp 0x20d83
movq %rax, %rbx
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20d96
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0