// File: /workspace/github_cpp_makefile_star2_O2/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
movq 0x28(%rdx), %rax
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
cmpq $0x2, %rax
jb 0x11233
cmpw $0x0, 0x6(%rax)
je 0x11233
leaq 0x15d01(%rip), %rcx     # 0x26eb1
movb 0x22(%r14), %al
cmpb (%rcx), %al
jge 0x11233
movb 0x8(%r15), %al
movb %al, 0xf(%rsp)
movsbq %al, %rax
movzbl %al, %r12d
movq %rax, 0x18(%rsp)
movl %ebp, 0x14(%rsp)
leaq 0x15cd9(%rip), %rcx     # 0x26eb1
movb 0x9(%r15), %al
cmpb (%rcx), %al
jl 0x111e5
xorl %r13d, %r13d
jmp 0x111f2
movswl 0x2(%r15), %r13d
movswl 0x4(%r15), %eax
subl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x1160c
leaq 0x15ca7(%rip), %rcx     # 0x26eb1
movq %rax, %rbp
movb 0x22(%rax), %al
cmpb (%rcx), %al
jl 0x113bb
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movl %r12d, %ecx
callq 0x1180c
testb %al, %al
je 0x111d1
jmp 0x1132c
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x1126c
cmpl $0x0, 0x12120(%rbx)
jne 0x1126c
leaq 0x20(%rsp), %r12
movl $0x1, 0x12120(%rbx)
movq %r12, %rdi
callq 0x5ca0
movq (%r12), %rax
movq %rax, 0x121d0(%rbx)
movq 0x40(%rbx), %rax
cmpl $0x0, 0xa4(%rax)
leaq 0x22(%r14), %rcx
leaq 0x9(%r15), %rdx
leaq 0x15c2f(%rip), %rsi     # 0x26eb1
cmoveq %rdx, %rcx
movb (%rcx), %cl
cmpb (%rsi), %cl
jge 0x112c6
movl 0xa8(%rax), %ecx
incl %ecx
cmpq %rcx, 0x18(%r14)
jb 0x11342
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11b3e
testb %al, %al
je 0x1133e
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x11174
jmp 0x1132c
leaq 0xcb78(%rbx), %rdi
movzbl %cl, %esi
callq 0x12606
leaq 0x12128(%rbx), %rdi
movl %ebp, %esi
callq 0x126b2
decq 0xcb80(%rbx)
decq 0xcb90(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x1132c
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x5ca0
movq (%r14), %rax
movq %rax, 0x121e0(%rbx)
cmpl $0x1, 0x12120(%rbx)
jne 0x11322
movq %rax, 0x121d8(%rbx)
movl $0x3, 0x12120(%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x1136a
leaq 0x20(%rsp), %r12
movl $0x2, 0x12120(%rbx)
movq %r12, %rdi
callq 0x5ca0
movq (%r12), %rax
movq %rax, 0x121d8(%rbx)
leaq 0xa14(%rbx), %rdi
movq %r15, %rsi
callq 0x1259c
leaq 0x20(%rsp), %r12
xorl %r13d, %r13d
movq 0x40(%rbx), %rax
cmpl 0x94(%rax), %r13d
jge 0x112d5
movq %r12, %rdi
movq %r15, %rsi
callq 0x9908
movl 0x24(%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %ecx
callq 0x11f6a
movq %r12, %rdi
callq 0x83bc
incl %r13d
jmp 0x11381
movslq 0x1210c(%rbx), %rcx
movzwl 0x24(%rbp), %eax
movl %r12d, 0x10(%rsp)
movq %rbp, %rsi
addq $0x24, %rsi
leal 0x1(%rcx), %edx
leaq (%rcx,%rcx,2), %rcx
movl %edx, 0x1210c(%rbx)
movw %ax, 0x119b8(%rbx,%rcx)
movb 0xf(%rsp), %al
movb %al, 0x119ba(%rbx,%rcx)
pushq $0x1
popq %rcx
movq %r15, %rdi
movl %ecx, %edx
movq %rsi, %r12
callq 0xe1ea
testb %al, %al
je 0x114e8
lock
incq 0x18(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movl 0x14(%rsp), %ecx
incl %ecx
callq 0x11174
movq 0x18(%rsp), %rax
shlq $0x4, %rax
leaq 0xcb68(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x11441
lock
addq %rcx, 0x10(%rbp)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x1144f
lock
addq %rax, 0x18(%rbp)
movq %rbp, %rdx
movl 0x10(%rsp), %ebp
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %ecx
callq 0x1180c
testb %al, %al
jne 0x1132c
movq 0x40(%rbx), %rax
leaq 0x11e45(%rip), %rcx     # 0x232bc
vmovss 0x7c(%rax), %xmm0
vucomiss (%rcx), %xmm0
jbe 0x1132c
movq 0x28(%r14), %rcx
cmpq $0x2, %rcx
jb 0x1132c
cmpw $0x2, 0x6(%rcx)
jb 0x1132c
vcvtsi2ss %r13d, %xmm1, %xmm2
vfmadd132ss 0x80(%rax), %xmm0, %xmm2 # xmm2 = (xmm2 * mem) + xmm0
imull $0x32, %r13d, %ecx
vcvtsi2ss %ecx, %xmm1, %xmm1
vmulss %xmm1, %xmm2, %xmm0
vcvtusi2ssq 0x18(%r14), %xmm3, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x1132c
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11ab4
leaq 0xa8(%rsp), %rbx
movq %r12, %rsi
movq %rbx, %rdi
callq 0xcfb0
leaq 0x13012(%rip), %rsi     # 0x24514
leaq 0x88(%rsp), %rdi
movq %rbx, %rdx
callq 0xcf8b
leaq 0x113c3(%rip), %rdx     # 0x228dc
leaq 0x20(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x8051
leaq 0x68(%rsp), %rdi
pushq $0x1
popq %rdx
movq %r15, %rsi
callq 0x17dc6
leaq 0xc8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0xcf24
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0xd451
movq %rbx, %rdi
callq 0x5150
leaq 0x68(%rsp), %rdi
callq 0x5150
leaq 0x20(%rsp), %rdi
callq 0x5150
leaq 0x88(%rsp), %rdi
callq 0x5150
leaq 0xa8(%rsp), %rdi
callq 0x5150
leaq 0x12f83(%rip), %rdi     # 0x24522
leaq 0x12f93(%rip), %rsi     # 0x24539
leaq 0x12fa4(%rip), %rcx     # 0x24551
pushq $0x33
popq %rdx
callq 0x51f0
leaq 0x68(%rsp), %rdi
movq %rax, %rbx
callq 0x5150
jmp 0x115c7
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5150
jmp 0x115d6
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x5150
jmp 0x115e8
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x5150
jmp 0x11604
leaq 0x20(%rsp), %rdi
movq %rax, %rbx
callq 0x83bc
movq %rbx, %rdi
callq 0x5650