// File: 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:
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, 0x10(%rsp)
movq 0x148(%rsp), %rdi
callq 0x14b50
movb %al, 0x13b(%rsp)
movq 0x140(%rsp), %rdi
addq $0x18, %rdi
callq 0x1ca70
testb $0x1, %al
jne 0x25128
movq 0x140(%rsp), %rdi
addq $0x12, %rdi
leaq 0x36a38(%rip), %rsi     # 0x5b81b
callq 0x163d0
testb $0x1, %al
jne 0x24df1
jmp 0x25128
jmp 0x24df3
movq 0x10(%rsp), %rdi
movq 0x140(%rsp), %rsi
movb 0x13b(%rsp), %al
movb %al, 0x12f(%rsp)
movzbl 0x12f(%rsp), %edx
callq 0x25370
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rdi
addq $0x12, %rdi
leaq 0x369e5(%rip), %rsi     # 0x5b81b
callq 0x163d0
testb $0x1, %al
jne 0x24e44
jmp 0x250e9
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movq 0x130(%rsp), %rsi
addq $0x14, %rsi
movb 0x13b(%rsp), %al
movb %al, 0x12e(%rsp)
movzbl 0x12e(%rsp), %edx
callq 0x263b0
movq 0x148(%rsp), %rdi
movq 0x130(%rsp), %rax
movw 0x14(%rax), %ax
movw %ax, 0x12c(%rsp)
movzwl 0x12c(%rsp), %esi
callq 0x14c70
testb $0x1, %al
jne 0x2504f
movq 0x130(%rsp), %rsi
addq $0x14, %rsi
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x14740
movq 0x8(%rsp), %rdx
leaq 0x209ed(%rip), %rsi     # 0x458bc
leaq 0xc8(%rsp), %rdi
callq 0x14410
jmp 0x24ede
leaq 0x1e93f(%rip), %rdx     # 0x43824
leaq 0xe8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xaa40
jmp 0x24efc
movq 0x148(%rsp), %rsi
leaq 0x78(%rsp), %rdi
movl $0x1, %edx
callq 0x2f570
jmp 0x24f15
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x14510
jmp 0x24f31
leaq 0x108(%rsp), %rdi
callq 0x143d0
jmp 0x24f40
leaq 0x108(%rsp), %rdi
callq 0x5290
leaq 0x78(%rsp), %rdi
callq 0x5290
leaq 0xe8(%rsp), %rdi
callq 0x5290
leaq 0xc8(%rsp), %rdi
callq 0x5290
leaq 0xa8(%rsp), %rdi
callq 0x5290
leaq 0x20945(%rip), %rdi     # 0x458ca
leaq 0x20955(%rip), %rsi     # 0x458e1
movl $0x31, %edx
leaq 0x20965(%rip), %rcx     # 0x458fd
callq 0x52d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x2503d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x25030
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x25023
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x25019
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x5290
leaq 0x78(%rsp), %rdi
callq 0x5290
leaq 0xe8(%rsp), %rdi
callq 0x5290
leaq 0xc8(%rsp), %rdi
callq 0x5290
leaq 0xa8(%rsp), %rdi
callq 0x5290
jmp 0x25362
movq 0x130(%rsp), %rdi
callq 0x262f0
movq 0x10(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x130(%rsp), %rdx
movl 0x13c(%rsp), %ecx
addl $0x1, %ecx
callq 0x24d70
movq 0x10(%rsp), %rdi
movq 0x130(%rsp), %rax
movq %rax, (%rsp)
addq $0x58, %rdi
movb 0x13b(%rsp), %al
movb %al, 0x77(%rsp)
movzbl 0x77(%rsp), %esi
callq 0x26370
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x26310
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x140(%rsp), %rsi
movq 0x130(%rsp), %rdx
movb 0x13b(%rsp), %al
movb %al, 0x76(%rsp)
movzbl 0x76(%rsp), %ecx
callq 0x255a0
jmp 0x2535a
jmp 0x250eb
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rdi
movq 0x140(%rsp), %rsi
movq 0x130(%rsp), %rdx
movb 0x13b(%rsp), %al
movb %al, 0x75(%rsp)
movzbl 0x75(%rsp), %ecx
callq 0x255a0
xorb $-0x1, %al
testb $0x1, %al
jne 0x24df3
jmp 0x2535a
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x25169
movq 0x10(%rsp), %rax
cmpl $0x0, 0x78(%rax)
jne 0x25169
movq 0x10(%rsp), %rax
movl $0x1, 0x78(%rax)
leaq 0x68(%rsp), %rdi
callq 0x67a0
movq 0x10(%rsp), %rax
movq 0x68(%rsp), %rcx
movq %rcx, 0xe8(%rax)
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x0, 0x64(%rax)
je 0x25189
movq 0x140(%rsp), %rax
movb 0x12(%rax), %al
movb %al, 0x67(%rsp)
jmp 0x2519a
movq 0x148(%rsp), %rdi
callq 0x14100
movb %al, 0x67(%rsp)
leaq 0x67(%rsp), %rdi
leaq 0x36675(%rip), %rsi     # 0x5b81b
callq 0x163d0
testb $0x1, %al
jne 0x251b4
jmp 0x252c3
movq 0x140(%rsp), %rdi
callq 0x1da00
movq 0x10(%rsp), %rcx
movq 0x40(%rcx), %rcx
movl 0x68(%rcx), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jb 0x2521d
movq 0x10(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
callq 0x25be0
testb $0x1, %al
jne 0x251f7
jmp 0x2521d
movq 0x10(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
movl 0x13c(%rsp), %ecx
callq 0x24d70
jmp 0x2535a
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x25253
movq 0x10(%rsp), %rax
movl $0x2, 0x78(%rax)
leaq 0x58(%rsp), %rdi
callq 0x67a0
movq 0x10(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0xf0(%rax)
movl $0x0, 0x54(%rsp)
movq 0x10(%rsp), %rcx
movl 0x54(%rsp), %eax
movq 0x40(%rcx), %rcx
cmpl 0x5c(%rcx), %eax
jge 0x252c1
movq 0x148(%rsp), %rsi
leaq 0x28(%rsp), %rdi
movl $0x28, %edx
callq 0x5380
movq 0x10(%rsp), %rdi
movq 0x140(%rsp), %rax
movw 0x14(%rax), %ax
movw %ax, 0x26(%rsp)
movl 0x13c(%rsp), %ecx
leaq 0x28(%rsp), %rsi
movzwl 0x26(%rsp), %edx
callq 0x26140
movb %al, 0x25(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x2525b
jmp 0x252de
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movb 0x67(%rsp), %al
movb %al, 0x24(%rsp)
movzbl 0x24(%rsp), %esi
callq 0x263d0
movq 0x10(%rsp), %rdi
addq $0x80, %rdi
movl 0x13c(%rsp), %esi
callq 0x26460
movq 0x10(%rsp), %rdi
addq $0x58, %rdi
movl $0x1, %esi
callq 0x264d0
movq 0x10(%rsp), %rax
movq 0x40(%rax), %rax
testb $0x1, 0x48(%rax)
je 0x25358
leaq 0x18(%rsp), %rdi
callq 0x67a0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0xf8(%rax)
cmpl $0x1, 0x78(%rax)
jne 0x2534c
movq 0x10(%rsp), %rax
movq 0xf8(%rax), %rcx
movq %rcx, 0xf0(%rax)
movq 0x10(%rsp), %rax
movl $0x3, 0x78(%rax)
jmp 0x2535a
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xa0(%rsp), %rdi
callq 0x57a0
nop