// File: /workspace/github_cpp_makefile_star2_O2/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
movzbl 0x19(%rsi), %r12d
movq 0x20(%rdx), %rax
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
cmpq $0x2, %rax
jb 0x16d0d
cmpw $0x0, 0x6(%rax)
je 0x16d0d
cmpb $0x0, 0x12(%r15)
js 0x16d67
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x16d3e
cmpl $0x0, 0x70(%rbx)
jne 0x16d3e
movq %rsp, %r13
movq %r13, %rdi
movl $0x1, 0x70(%rbx)
callq 0x4c50
movq (%r13), %rax
movq %rax, 0xe0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0x64(%rax)
je 0x16dee
movb 0x12(%r15), %al
jmp 0x16df6
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x1715c
testb %al, %al
jne 0x16e4c
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x16ff4
cmpb $0x0, 0x12(%rax)
movq %rax, %r13
jns 0x16d51
movl 0x14(%r13), %esi
movq %r14, %rdi
callq 0x7ada
testb %al, %al
je 0x16ef4
lock
incq 0x8(%r13)
incl %ebp
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x16cd4
movl %r12d, %eax
shll $0x4, %eax
leaq 0x40(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x16dc1
lock
addq %rcx, (%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x16dcf
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 0x1715c
movq %r14, %rdi
callq 0xae70
testb %al, %al
js 0x16e5e
movzbl %al, %esi
leaq 0x50(%rbx), %rdi
callq 0x176de
leaq 0x78(%rbx), %rdi
movl %ebp, %esi
callq 0x17708
decq 0x58(%rbx)
decq 0x68(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x16e4c
movq %rsp, %r14
movq %r14, %rdi
callq 0x4c50
movq (%r14), %rax
movq %rax, 0xf0(%rbx)
cmpl $0x1, 0x70(%rbx)
jne 0x16e45
movq %rax, 0xe8(%rbx)
movl $0x3, 0x70(%rbx)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbx), %rax
movl 0x68(%rax), %ecx
incl %ecx
cmpq %rcx, 0x8(%r15)
jb 0x16e98
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x173b0
testb %al, %al
je 0x16e94
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x16cd4
jmp 0x16e4c
movq 0x40(%rbx), %rax
cmpb $0x1, 0x48(%rax)
jne 0x16eba
movq %rsp, %r15
movq %r15, %rdi
movl $0x2, 0x70(%rbx)
callq 0x4c50
movq (%r15), %rax
movq %rax, 0xe8(%rbx)
xorl %r12d, %r12d
movq %rsp, %r15
movq 0x40(%rbx), %rax
cmpl 0x5c(%rax), %r12d
jge 0x16e06
movq 0x20(%r14), %rax
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, 0x20(%rsp)
vmovups (%r14), %ymm0
vmovups %ymm0, (%rsp)
vzeroupper
callq 0x1765e
incl %r12d
jmp 0x16ec0
addq $0x14, %r13
leaq 0x68(%rsp), %rbx
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8178
leaq 0x9112(%rip), %rsi      # 0x20024
leaq 0x48(%rsp), %rdi
movq %rbx, %rdx
callq 0x8423
leaq 0x5b92(%rip), %rdx      # 0x1cab8
leaq 0x48(%rsp), %rsi
movq %rsp, %rdi
callq 0x6821
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0xf8fc
leaq 0x88(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rsp, %rsi
callq 0x8365
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0xac65
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 0x9095(%rip), %rdi      # 0x20032
leaq 0x90a5(%rip), %rsi      # 0x20049
leaq 0x90b2(%rip), %rcx      # 0x2005d
pushq $0x2a
popq %rdx
callq 0x41c0
leaq 0x28(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
jmp 0x16fc5
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4130
jmp 0x16fd2
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4130
jmp 0x16fe1
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x4130
movq %rbx, %rdi
callq 0x4590
nop