// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]pentagod/agentmcts.cpp
// Function: AgentMCTS::MCTSThread::run()
void AgentMCTS::MCTSThread::run(){
	while(true){
		switch(player->threadstate){
		case Thread_Cancelled:  //threads should exit
			return;

		case Thread_Wait_Start: //threads are waiting to start
		case Thread_Wait_Start_Cancelled:
			player->runbarrier.wait();
			CAS(player->threadstate, Thread_Wait_Start, Thread_Running);
			CAS(player->threadstate, Thread_Wait_Start_Cancelled, Thread_Cancelled);
			break;

		case Thread_Wait_End:   //threads are waiting to end
			player->runbarrier.wait();
			CAS(player->threadstate, Thread_Wait_End, Thread_Wait_Start);
			break;

		case Thread_Running:    //threads are running
			if(player->rootboard.won() >= 0 || player->root.outcome >= 0 || (player->maxruns > 0 && player->runs >= player->maxruns)){ //solved or finished runs
				if(CAS(player->threadstate, Thread_Running, Thread_Wait_End) && player->root.outcome >= 0)
					logerr("Solved as " + to_str((int)player->root.outcome) + "\n");
				break;
			}
			if(player->ctmem.memalloced() >= player->maxmem){ //out of memory, start garbage collection
				CAS(player->threadstate, Thread_Running, Thread_GC);
				break;
			}

			INCR(player->runs);
			iterate();
			break;

		case Thread_GC:         //one thread is running garbage collection, the rest are waiting
		case Thread_GC_End:     //once done garbage collecting, go to wait_end instead of back to running
			if(player->gcbarrier.wait()){
				Time starttime;
				logerr("Starting player GC with limit " + to_str(player->gclimit) + " ... ");
				uint64_t nodesbefore = player->nodes;
				Board copy = player->rootboard;
				player->garbage_collect(copy, & player->root);
				Time gctime;
				player->ctmem.compact(1.0, 0.75);
				Time compacttime;
				logerr(to_str(100.0*player->nodes/nodesbefore, 1) + " % of tree remains - " +
					to_str((gctime - starttime)*1000, 0)  + " msec gc, " + to_str((compacttime - gctime)*1000, 0) + " msec compact\n");

				if(player->ctmem.meminuse() >= player->maxmem/2)
					player->gclimit = (int)(player->gclimit*1.3);
				else if(player->gclimit > 5)
					player->gclimit = (int)(player->gclimit*0.9); //slowly decay to a minimum of 5

				CAS(player->threadstate, Thread_GC,     Thread_Running);
				CAS(player->threadstate, Thread_GC_End, Thread_Wait_End);
			}
			player->gcbarrier.wait();
			break;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
leaq 0xc8d4(%rip), %r12      # 0x1f5d8
leaq 0x88(%rsp), %r13
leaq 0x20(%rsp), %r15
movq %rdi, %rbx
movq 0x40(%rbx), %rdi
movl 0xa80(%rdi), %eax
cmpq $0x6, %rax
ja 0x12d14
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
addq $0xaa0, %rdi            # imm = 0xAA0
callq 0x14eda
movq 0x40(%rbx), %rcx
pushq $0x1
popq %rax
pushq $0x3
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
movq 0x40(%rbx), %rcx
pushq $0x2
popq %rax
xorl %edx, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x12d14
addq $0xb00, %rdi            # imm = 0xB00
callq 0x14eda
testb %al, %al
je 0x130b9
leaq 0x18(%rsp), %rdi
callq 0x4c50
movq 0x40(%rbx), %rax
movq %r13, %rdi
movl 0xd8(%rax), %esi
callq 0xaee1
leaq 0xc8b6(%rip), %rsi      # 0x1f64b
movq %r15, %rdi
movq %r13, %rdx
callq 0x8423
leaq 0x148(%rsp), %r14
leaq 0xc8bb(%rip), %rdx      # 0x1f66a
movq %r15, %rsi
movq %r14, %rdi
callq 0x6821
movq %r14, %rdi
callq 0xac65
movq %r14, %rdi
callq 0x4130
movq %r15, %rdi
callq 0x4130
movq %r13, %rdi
callq 0x4130
movq 0x40(%rbx), %rdi
vcvtusi2sdq 0xd0(%rdi), %xmm1, %xmm0
movq %r15, %rsi
movq 0xa0(%rdi), %rax
leaq 0xa8(%rdi), %rdx
movq %rax, 0x40(%rsp)
vmovups 0x80(%rdi), %ymm1
vmovsd %xmm0, 0x8(%rsp)
vmovups %ymm1, 0x20(%rsp)
vzeroupper
callq 0x131e2
movq %rsp, %rdi
callq 0x4c50
vmovss 0xc775(%rip), %xmm0     # 0x1f59c
vmovss 0xc771(%rip), %xmm1     # 0x1f5a0
movq 0x40(%rbx), %rdi
movl $0xf0, %eax
addq %rax, %rdi
callq 0x14f66
leaq 0x10(%rsp), %rdi
callq 0x4c50
movq 0x40(%rbx), %rax
vcvtusi2sdq 0xd0(%rax), %xmm2, %xmm0
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
vmulsd 0xa440(%rip), %xmm0, %xmm0 # 0x1d2a8
vdivsd 0x8(%rsp), %xmm0, %xmm0
pushq $0x1
popq %rsi
callq 0xf350
leaq 0x48(%rsp), %rdi
leaq 0xc7ee(%rip), %rdx      # 0x1f670
movq %r14, %rsi
callq 0x6821
vmovsd (%rsp), %xmm0
vsubsd 0x18(%rsp), %xmm0, %xmm0
vmulsd 0x916b(%rip), %xmm0, %xmm0 # 0x1c008
leaq 0xc8(%rsp), %r14
xorl %esi, %esi
movq %r14, %rdi
callq 0xf350
leaq 0x108(%rsp), %rbp
leaq 0x48(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %rdi
callq 0x8365
leaq 0x68(%rsp), %rdi
leaq 0xc7b3(%rip), %rdx      # 0x1f686
movq %rbp, %rsi
callq 0x6821
vmovsd 0x10(%rsp), %xmm0
vsubsd (%rsp), %xmm0, %xmm0
vmulsd 0x911a(%rip), %xmm0, %xmm0 # 0x1c008
leaq 0xa8(%rsp), %r14
xorl %esi, %esi
movq %r14, %rdi
callq 0xf350
leaq 0x68(%rsp), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x8365
leaq 0x128(%rsp), %r14
leaq 0xc772(%rip), %rdx      # 0x1f691
movq %r13, %rsi
movq %r14, %rdi
callq 0x6821
movq %r14, %rdi
callq 0xac65
movq %r14, %rdi
callq 0x4130
movq %r13, %rdi
callq 0x4130
leaq 0xa8(%rsp), %rdi
callq 0x4130
leaq 0x68(%rsp), %rdi
callq 0x4130
leaq 0x108(%rsp), %rdi
callq 0x4130
leaq 0xc8(%rsp), %rdi
callq 0x4130
leaq 0x48(%rsp), %rdi
callq 0x4130
leaq 0xe8(%rsp), %rdi
callq 0x4130
movq 0x40(%rbx), %rcx
movq 0x40(%rcx), %rdx
movl 0xd8(%rcx), %eax
shrq %rdx
cmpq %rdx, 0xa78(%rcx)
jae 0x13083
cmpl $0x6, %eax
jl 0x13099
vcvtsi2sd %eax, %xmm2, %xmm0
vmulsd 0xc5f3(%rip), %xmm0, %xmm0 # 0x1f5b0
jmp 0x1308f
subq $-0x80, %rdi
callq 0xae70
movq 0x40(%rbx), %rcx
testb %al, %al
jns 0x12fe0
cmpb $0x0, 0xba(%rcx)
js 0x130cf
pushq $0x3
popq %rax
pushq $0x6
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
jne 0x12d14
movq 0x40(%rbx), %rax
movsbl 0xba(%rax), %esi
testl %esi, %esi
js 0x12d14
movq %r13, %rdi
callq 0xaee1
leaq 0xc62a(%rip), %rsi      # 0x1f640
movq %r15, %rdi
movq %r13, %rdx
callq 0x8423
leaq 0x168(%rsp), %r14
leaq 0x9a88(%rip), %rdx      # 0x1cab8
movq %r15, %rsi
movq %r14, %rdi
callq 0x6821
movq %r14, %rdi
callq 0xac65
movq %r14, %rdi
callq 0x4130
movq %r15, %rdi
callq 0x4130
movq %r13, %rdi
callq 0x4130
jmp 0x12d14
addq $0xaa0, %rdi            # imm = 0xAA0
callq 0x14eda
movq 0x40(%rbx), %rcx
pushq $0x6
popq %rax
pushq $0x1
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x12d14
vcvtsi2sd %eax, %xmm2, %xmm0
vmulsd 0xc519(%rip), %xmm0, %xmm0 # 0x1f5a8
vcvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rcx)
pushq $0x4
popq %rax
pushq $0x3
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
movq 0x40(%rbx), %rcx
pushq $0x5
popq %rax
pushq $0x6
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
movq 0x40(%rbx), %rdi
movl $0xb00, %eax            # imm = 0xB00
addq %rax, %rdi
callq 0x14eda
jmp 0x12d14
movq 0xe8(%rcx), %rax
testq %rax, %rax
je 0x130e8
cmpq %rax, 0xe0(%rcx)
jae 0x12fe0
movq 0x100(%rcx), %rdx
movq 0xf8(%rcx), %rax
movl 0x8(%rdx), %edx
movl 0x10(%rax), %eax
shlq $0x18, %rdx
addq %rdx, %rax
cmpq 0x40(%rcx), %rax
jae 0x1311e
lock
incq 0xe0(%rcx)
movq %rbx, %rdi
callq 0x16b9c
jmp 0x12d14
pushq $0x3
popq %rax
pushq $0x4
popq %rdx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x12d14
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1315e
jmp 0x13159
leaq 0x88(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
jmp 0x13191
movq %rax, %rbx
jmp 0x1316b
leaq 0x20(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
leaq 0x88(%rsp), %rdi
jmp 0x131d4
movq %rax, %rbx
jmp 0x131cc
movq %rax, %rbx
jmp 0x131c2
movq %rax, %rbx
jmp 0x131b5
movq %rax, %rbx
jmp 0x131a8
movq %rax, %rbx
jmp 0x1319e
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x4130
leaq 0x68(%rsp), %rdi
callq 0x4130
leaq 0x108(%rsp), %rdi
callq 0x4130
leaq 0xc8(%rsp), %rdi
callq 0x4130
leaq 0x48(%rsp), %rdi
callq 0x4130
leaq 0xe8(%rsp), %rdi
callq 0x4130
movq %rbx, %rdi
callq 0x4590
nop