// File: /workspace/github_cpp_makefile_star2_O3/tewalds[P]morat/hex/agentmctsthread.cpp
// Function: Morat::Hex::AgentMCTS::AgentThread::walk_tree(Morat::Hex::Board&, Morat::Hex::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{
			int remain = board.moves_remain();
			child = choose_move(node, to_play, remain);

			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));

				if(!agent->do_backup(node, child, to_play) && //not solved
					agent->ravefactor > min_rave &&  //using rave
					node->children.num() > 1 &&       //not a macro move
					50*remain*(agent->ravefactor + agent->decrrave*remain) > node->exp.num()) //rave is still significant
					update_rave(node, 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
		random_policy.prepare(board);
		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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
leaq 0x2034d(%rip), %rbp     # 0x363b1
leaq 0x22(%rdx), %r13
leaq 0x9(%rsi), %r12
movl %ecx, 0x4(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0x28(%r14), %rax
cmpq $0x2, %rax
jb 0x16097
cmpw $0x0, 0x6(%rax)
je 0x16097
movb (%r13), %al
cmpb (%rbp), %al
jl 0x162cb
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x160e6
cmpl $0x0, 0x12120(%rbx)
jne 0x160e6
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x1, 0x12120(%rbx)
callq 0x55b0
vcvtsi2sdq 0x18(%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x20(%rsp), %xmm2, %xmm1
vdivsd 0x1af32(%rip), %xmm1, %xmm1 # 0x31008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0x121d0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0xa4(%rax)
movq %r13, %rcx
cmoveq %r12, %rcx
movb (%rcx), %cl
cmpb (%rbp), %cl
jge 0x16239
movl 0xa8(%rax), %ecx
incl %ecx
cmpq %rcx, 0x18(%r14)
jb 0x16127
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x16b5a
testb %al, %al
jne 0x16079
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x16167
leaq 0x18(%rsp), %r12
movl $0x2, 0x12120(%rbx)
xorl %esi, %esi
movq %r12, %rdi
callq 0x55b0
vcvtsi2sdq (%r12), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r12), %xmm2, %xmm1
vdivsd 0x1aead(%rip), %xmm1, %xmm1 # 0x31008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0x121d8(%rbx)
leaq 0xa14(%rbx), %rdi
movq %r15, %rsi
callq 0x17916
movq 0x40(%rbx), %rax
cmpl $0x0, 0x94(%rax)
jle 0x16248
leaq 0x10(%r15), %rax
leaq 0x28(%r15), %rbp
xorl %r12d, %r12d
movq %rax, 0x8(%rsp)
movzwl 0x8(%r15), %eax
movq 0x8(%rsp), %rsi
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movw %ax, 0x20(%rsp)
movq (%r15), %rax
movq %rax, 0x18(%rsp)
callq 0xb44a
vmovups (%rbp), %xmm0
vmovups %xmm0, 0x18(%r13)
vmovupd 0x38(%r15), %xmm0
movq 0x40(%r15), %rax
vmovupd %xmm0, 0x50(%rsp)
testq %rax, %rax
je 0x161f0
movq 0x32de6(%rip), %rcx     # 0x48fc8
cmpb $0x0, (%rcx)
je 0x161ec
incl 0x8(%rax)
jmp 0x161f0
lock
incl 0x8(%rax)
movl 0x24(%r14), %edx
movl 0x4(%rsp), %ecx
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x17074
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x16214
callq 0xb272
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x16223
callq 0x52e0
movq 0x40(%rbx), %rax
incl %r12d
cmpl 0x94(%rax), %r12d
jl 0x16197
jmp 0x16248
leaq 0xcb78(%rbx), %rdi
movzbl %cl, %esi
callq 0x17988
movl 0x4(%rsp), %esi
leaq 0x12128(%rbx), %rdi
callq 0x17a46
decq 0xcb80(%rbx)
decq 0xcb90(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x162b9
leaq 0x18(%rsp), %r14
xorl %esi, %esi
movq %r14, %rdi
callq 0x55b0
vcvtsi2sdq (%r14), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r14), %xmm2, %xmm1
vdivsd 0x1ad76(%rip), %xmm1, %xmm1 # 0x31008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0x121e0(%rbx)
cmpl $0x1, 0x12120(%rbx)
jne 0x162af
vmovsd %xmm0, 0x121d8(%rbx)
movl $0x3, 0x12120(%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb 0x8(%r15), %al
movb %al, 0x8(%rsp)
movsbq %al, %rax
movzbl %al, %r12d
movq %rax, 0x60(%rsp)
movb 0x9(%r15), %al
cmpb (%rbp), %al
jl 0x162ed
xorl %ebp, %ebp
jmp 0x162f9
movswl 0x2(%r15), %ebp
movswl 0x4(%r15), %eax
subl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x16608
leaq 0x200a1(%rip), %rcx     # 0x363b1
movq %rax, %r13
movb 0x22(%rax), %al
cmpb (%rcx), %al
jl 0x16339
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x16844
leaq 0x2007e(%rip), %rbp     # 0x363b1
testb %al, %al
je 0x162e0
jmp 0x162b9
movslq 0x1210c(%rbx), %rcx
movzwl 0x24(%r13), %eax
leaq 0x18(%rsp), %rsi
movl %ebp, 0x14(%rsp)
movq %r15, %rdi
leal 0x1(%rcx), %edx
leaq (%rcx,%rcx,2), %rcx
movl %edx, 0x1210c(%rbx)
movl $0x1, %edx
movw %ax, 0x119b8(%rbx,%rcx)
movb 0x8(%rsp), %al
movb %al, 0x119ba(%rbx,%rcx)
movsbl 0x24(%r13), %eax
movsbl (%r15), %ecx
imull %eax, %ecx
movsbl 0x25(%r13), %eax
addl %ecx, %eax
movzwl 0x24(%r13), %ecx
movw %cx, (%rsi)
movl $0x1, %ecx
movw %ax, 0x2(%rsi)
callq 0x1388c
testb %al, %al
je 0x16479
lock
incq 0x18(%r13)
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movl 0x4(%rsp), %ecx
incl %ecx
callq 0x1604c
movq 0x60(%rsp), %rax
shlq $0x4, %rax
leaq 0xcb68(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x163de
lock
addq %rcx, 0x10(%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x163ec
lock
addq %rax, 0x18(%r13)
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x16844
testb %al, %al
jne 0x162b9
movq 0x40(%rbx), %rax
leaq 0x1bff3(%rip), %rcx     # 0x32404
vmovss 0x7c(%rax), %xmm0
vucomiss (%rcx), %xmm0
jbe 0x162b9
movq 0x28(%r14), %rcx
cmpq $0x2, %rcx
jb 0x162b9
cmpw $0x2, 0x6(%rcx)
jb 0x162b9
movl 0x14(%rsp), %edx
vcvtsi2ss %edx, %xmm2, %xmm2
vfmadd132ss 0x80(%rax), %xmm0, %xmm2 # xmm2 = (xmm2 * mem) + xmm0
imull $0x32, %edx, %ecx
vcvtsi2ss %ecx, %xmm3, %xmm1
vmulss %xmm1, %xmm2, %xmm0
vcvtusi2ssq 0x18(%r14), %xmm3, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x162b9
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x16aca
jmp 0x162b9
addq $0x24, %r13
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10e7e
leaq 0x1d30d(%rip), %rsi     # 0x337a4
leaq 0x88(%rsp), %rdi
movq %rbx, %rdx
callq 0x178a3
leaq 0x1b458(%rip), %rsi     # 0x31906
leaq 0x88(%rsp), %rdi
callq 0x56e0
leaq 0x28(%rsp), %r14
movq %rax, %rcx
addq $0x10, %rcx
movq %r14, -0x10(%r14)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x164de
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r14)
jmp 0x164eb
movq %rdx, 0x18(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x28(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x20(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x68(%rsp), %rdi
movl $0x1, %edx
movq %r15, %rsi
callq 0x1f644
leaq 0xc8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x177a4
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x58bc
movq %rbx, %rdi
callq 0x5150
leaq 0x68(%rsp), %rdi
callq 0x5150
leaq 0x18(%rsp), %rdi
callq 0x5150
leaq 0x88(%rsp), %rdi
callq 0x5150
leaq 0xa8(%rsp), %rdi
callq 0x5150
leaq 0x1d239(%rip), %rdi     # 0x337b2
leaq 0x1d249(%rip), %rsi     # 0x337c9
leaq 0x1d25a(%rip), %rcx     # 0x337e1
movl $0x33, %edx
callq 0x51d0
leaq 0x78(%rsp), %rcx
movq %rax, %rbx
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x165ac
callq 0x52e0
jmp 0x165ac
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x165c0
callq 0x52e0
jmp 0x165c0
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x165db
callq 0x52e0
jmp 0x165db
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16600
callq 0x52e0
jmp 0x16600
leaq 0x18(%rsp), %rdi
movq %rax, %rbx
callq 0x9d96
movq %rbx, %rdi
callq 0x5670