// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movl %ecx, 0x13c(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x148(%rsp), %rdi
callq 0x17190
movzbl %al, %eax
movl %eax, 0x138(%rsp)
movq 0x140(%rsp), %rdi
addq $0x20, %rdi
callq 0x2d520
testb $0x1, %al
jne 0x31aa1
movq 0x140(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x31aa1
jmp 0x317aa
movq 0x18(%rsp), %rdi
movq 0x140(%rsp), %rsi
movl 0x138(%rsp), %edx
callq 0x31ce0
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
movsbl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x31a6b
movq 0x18(%rsp), %rdi
addq $0x50, %rdi
movq 0x130(%rsp), %rsi
addq $0x14, %rsi
movl 0x138(%rsp), %eax
movsbl %al, %edx
callq 0x32ae0
movq 0x148(%rsp), %rdi
movq 0x130(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x12c(%rsp)
movl 0x12c(%rsp), %esi
callq 0xd040
testb $0x1, %al
jne 0x319e1
movq 0x130(%rsp), %rsi
addq $0x14, %rsi
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x1, %edx
callq 0xdec0
movq 0x10(%rsp), %rdx
leaq 0xf7bb(%rip), %rsi      # 0x4101c
leaq 0xc8(%rsp), %rdi
callq 0xe5f0
jmp 0x31870
leaq 0xc239(%rip), %rdx      # 0x3dab0
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x93b0
jmp 0x3188e
movq 0x148(%rsp), %rsi
leaq 0x78(%rsp), %rdi
movl $0x1, %edx
callq 0x1f740
jmp 0x318a7
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0xe470
jmp 0x318c3
leaq 0x108(%rsp), %rdi
callq 0x15ad0
jmp 0x318d2
leaq 0x108(%rsp), %rdi
callq 0x4210
leaq 0x78(%rsp), %rdi
callq 0x4210
leaq 0xe8(%rsp), %rdi
callq 0x4210
leaq 0xc8(%rsp), %rdi
callq 0x4210
leaq 0xa8(%rsp), %rdi
callq 0x4210
leaq 0xf713(%rip), %rdi      # 0x4102a
leaq 0xf723(%rip), %rsi      # 0x41041
movl $0x2a, %edx
leaq 0xf72b(%rip), %rcx      # 0x41055
callq 0x4250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x319cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x319c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x319b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x319ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x4210
leaq 0x78(%rsp), %rdi
callq 0x4210
leaq 0xe8(%rsp), %rdi
callq 0x4210
leaq 0xc8(%rsp), %rdi
callq 0x4210
leaq 0xa8(%rsp), %rdi
callq 0x4210
jmp 0x31ccb
movq 0x130(%rsp), %rdi
callq 0x32a40
movq 0x18(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x130(%rsp), %rdx
movl 0x13c(%rsp), %ecx
addl $0x1, %ecx
callq 0x31730
movq 0x18(%rsp), %rdi
movq 0x130(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x50, %rdi
movl 0x138(%rsp), %esi
callq 0x32ac0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x32a60
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x140(%rsp), %rsi
movq 0x130(%rsp), %rdx
movl 0x138(%rsp), %ecx
callq 0x31ed0
jmp 0x31cc3
jmp 0x31a6d
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x140(%rsp), %rsi
movq 0x130(%rsp), %rdx
movl 0x138(%rsp), %ecx
callq 0x31ed0
xorb $-0x1, %al
testb $0x1, %al
jne 0x317aa
jmp 0x31cc3
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x31ae2
movq 0x18(%rsp), %rax
cmpl $0x0, 0x70(%rax)
jne 0x31ae2
movq 0x18(%rsp), %rax
movl $0x1, 0x70(%rax)
leaq 0x70(%rsp), %rdi
callq 0x5500
movq 0x18(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, 0xe0(%rax)
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0x64(%rax)
je 0x31b02
movq 0x140(%rsp), %rax
movb 0x12(%rax), %al
movb %al, 0x7(%rsp)
jmp 0x31b13
movq 0x148(%rsp), %rdi
callq 0x15fb0
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
movsbl %al, %eax
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
jge 0x31c38
movq 0x140(%rsp), %rdi
callq 0x2e5d0
movq 0x18(%rsp), %rcx
movq 0x40(%rcx), %rcx
movl 0x68(%rcx), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jb 0x31b99
movq 0x18(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
movl 0x138(%rsp), %ecx
callq 0x323f0
testb $0x1, %al
jne 0x31b73
jmp 0x31b99
movq 0x18(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
movl 0x13c(%rsp), %ecx
callq 0x31730
jmp 0x31cc3
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x31bcf
movq 0x18(%rsp), %rax
movl $0x2, 0x70(%rax)
leaq 0x60(%rsp), %rdi
callq 0x5500
movq 0x18(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0xe8(%rax)
movl $0x0, 0x5c(%rsp)
movq 0x18(%rsp), %rcx
movl 0x5c(%rsp), %eax
movq 0x40(%rcx), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x31c36
movq 0x148(%rsp), %rsi
leaq 0x30(%rsp), %rdi
movl $0x28, %edx
callq 0x4300
movq 0x18(%rsp), %rdi
movq 0x140(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x13c(%rsp), %ecx
movl 0x2c(%rsp), %edx
leaq 0x30(%rsp), %rsi
callq 0x328e0
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x31bd7
jmp 0x31c4a
movq 0x18(%rsp), %rdi
addq $0x50, %rdi
movl 0x6c(%rsp), %esi
callq 0x32b00
movq 0x18(%rsp), %rdi
addq $0x78, %rdi
movl 0x13c(%rsp), %esi
callq 0x32b70
movq 0x18(%rsp), %rdi
addq $0x50, %rdi
movl $0x1, %esi
callq 0x32be0
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x31cc1
leaq 0x20(%rsp), %rdi
callq 0x5500
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0xf0(%rax)
cmpl $0x1, 0x70(%rax)
jne 0x31cb5
movq 0x18(%rsp), %rax
movq 0xf0(%rax), %rcx
movq %rcx, 0xe8(%rax)
movq 0x18(%rsp), %rax
movl $0x3, 0x70(%rax)
jmp 0x31cc3
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xa0(%rsp), %rdi
callq 0x4660
nopl (%rax,%rax)