// File: /workspace/github_cpp_makefile_star2_O1/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
movl %ecx, 0x4(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2043e(%rip), %rbp     # 0x363b1
leaq 0x22(%rdx), %r13
leaq 0x9(%rsi), %r12
movq 0x28(%r14), %rax
cmpq $0x2, %rax
jb 0x15f99
cmpw $0x0, 0x6(%rax)
je 0x15f99
movb 0x22(%r14), %al
cmpb (%rbp), %al
jl 0x161cd
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x15fe4
cmpl $0x0, 0x12120(%rbx)
jne 0x15fe4
movl $0x1, 0x12120(%rbx)
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x55b0
vcvtsi2sdq 0x18(%rsp), %xmm2, %xmm0
vcvtsi2sdq 0x20(%rsp), %xmm2, %xmm1
vdivsd 0x1b030(%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 0x1613b
movl 0xa8(%rax), %eax
incl %eax
cmpq %rax, 0x18(%r14)
jb 0x16025
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x16aa2
testb %al, %al
jne 0x15f7b
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x16069
movl $0x2, 0x12120(%rbx)
leaq 0x18(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x55b0
vcvtsi2sdq (%r12), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r12), %xmm2, %xmm1
vdivsd 0x1afab(%rip), %xmm1, %xmm1 # 0x31008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0x121d8(%rbx)
leaq 0xa14(%rbx), %rdi
movq %r15, %rsi
callq 0x178b4
movq 0x40(%rbx), %rax
cmpl $0x0, 0x94(%rax)
jle 0x1614a
leaq 0x10(%r15), %rax
movq %rax, 0x8(%rsp)
leaq 0x28(%r15), %rbp
xorl %r12d, %r12d
movzwl 0x8(%r15), %eax
movw %ax, 0x20(%rsp)
movq (%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0xb34a
vmovups (%rbp), %xmm0
vmovups %xmm0, 0x18(%r13)
movq 0x40(%r15), %rax
vmovupd 0x38(%r15), %xmm0
vmovupd %xmm0, 0x50(%rsp)
testq %rax, %rax
je 0x160f2
movq 0x33ee4(%rip), %rcx     # 0x49fc8
cmpb $0x0, (%rcx)
je 0x160ee
incl 0x8(%rax)
jmp 0x160f2
lock
incl 0x8(%rax)
movl 0x24(%r14), %edx
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
movl 0x4(%rsp), %ecx
callq 0x16fd0
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x16116
callq 0xb170
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x16125
callq 0x52e0
incl %r12d
movq 0x40(%rbx), %rax
cmpl 0x94(%rax), %r12d
jl 0x16099
jmp 0x1614a
leaq 0xcb78(%rbx), %rdi
movzbl %cl, %esi
callq 0x17920
leaq 0x12128(%rbx), %rdi
movl 0x4(%rsp), %esi
callq 0x179de
decq 0xcb80(%rbx)
decq 0xcb90(%rbx)
movq 0x40(%rbx), %rax
cmpb $0x1, 0x68(%rax)
jne 0x161bb
leaq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x55b0
vcvtsi2sdq (%r14), %xmm2, %xmm0
vcvtsi2sdq 0x8(%r14), %xmm2, %xmm1
vdivsd 0x1ae74(%rip), %xmm1, %xmm1 # 0x31008
vaddsd %xmm0, %xmm1, %xmm0
vmovsd %xmm0, 0x121e0(%rbx)
cmpl $0x1, 0x12120(%rbx)
jne 0x161b1
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
movq %rax, 0x60(%rsp)
movzbl %al, %r12d
movb 0x9(%r15), %al
cmpb (%rbp), %al
jl 0x161ef
xorl %ebp, %ebp
jmp 0x161fb
movswl 0x2(%r15), %ebp
movswl 0x4(%r15), %eax
subl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x1650a
movq %rax, %r13
movb 0x22(%rax), %al
leaq 0x20199(%rip), %rcx     # 0x363b1
cmpb (%rcx), %al
jl 0x1623b
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x16746
testb %al, %al
leaq 0x2017a(%rip), %rbp     # 0x363b1
je 0x161e2
jmp 0x161bb
movl %ebp, 0x14(%rsp)
movzwl 0x24(%r13), %eax
movslq 0x1210c(%rbx), %rcx
leal 0x1(%rcx), %edx
leaq (%rcx,%rcx,2), %rcx
movl %edx, 0x1210c(%rbx)
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
leaq 0x18(%rsp), %rsi
movw %cx, (%rsi)
movw %ax, 0x2(%rsi)
movq %r15, %rdi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x13778
testb %al, %al
je 0x1637b
lock
incq 0x18(%r13)
movl 0x4(%rsp), %ecx
incl %ecx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x15f4e
movq 0x60(%rsp), %rax
shlq $0x4, %rax
leaq 0xcb68(%rax,%rbx), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x162e0
lock
addq %rcx, 0x10(%r13)
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x162ee
lock
addq %rax, 0x18(%r13)
movq 0x40(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x16746
testb %al, %al
jne 0x161bb
movq 0x40(%rbx), %rax
vmovss 0x7c(%rax), %xmm0
leaq 0x1c0ec(%rip), %rcx     # 0x32404
vucomiss (%rcx), %xmm0
jbe 0x161bb
movq 0x28(%r14), %rcx
cmpq $0x2, %rcx
jb 0x161bb
cmpw $0x2, 0x6(%rcx)
jb 0x161bb
movl 0x14(%rsp), %edx
imull $0x32, %edx, %ecx
vcvtsi2ss %ecx, %xmm2, %xmm1
vcvtsi2ss %edx, %xmm2, %xmm2
vfmadd132ss 0x80(%rax), %xmm0, %xmm2 # xmm2 = (xmm2 * mem) + xmm0
vmulss %xmm1, %xmm2, %xmm0
vcvtusi2ssq 0x18(%r14), %xmm3, %xmm1
vucomiss %xmm1, %xmm0
jbe 0x161bb
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x16a12
jmp 0x161bb
addq $0x24, %r13
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10cdc
leaq 0x1d40b(%rip), %rsi     # 0x337a4
leaq 0x88(%rsp), %rdi
movq %rbx, %rdx
callq 0x17841
leaq 0x1b556(%rip), %rsi     # 0x31906
leaq 0x88(%rsp), %rdi
callq 0x56e0
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x163e0
vmovups (%rcx), %xmm0
vmovups %xmm0, (%r14)
jmp 0x163ed
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
movq %r15, %rsi
movl $0x1, %edx
callq 0x1f6ee
leaq 0xc8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x17742
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 0x1d337(%rip), %rdi     # 0x337b2
leaq 0x1d347(%rip), %rsi     # 0x337c9
leaq 0x1d358(%rip), %rcx     # 0x337e1
movl $0x33, %edx
callq 0x51d0
movq %rax, %rbx
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x164ae
callq 0x52e0
jmp 0x164ae
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x164c2
callq 0x52e0
jmp 0x164c2
movq %rax, %rbx
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x164dd
callq 0x52e0
jmp 0x164dd
movq %rax, %rbx
leaq 0xb8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16502
callq 0x52e0
jmp 0x16502
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x9ce0
movq %rbx, %rdi
callq 0x5670