// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
leaq 0x11434(%rip), %rbp     # 0x2b65c
movl $0x3, %r12d
movl $0xb00, %r13d           # imm = 0xB00
movq %rdi, %rbx
xorl %r14d, %r14d
movq 0x40(%rbx), %r15
movl 0xa80(%r15), %eax
cmpq $0x6, %rax
ja 0x1a23a
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
addq $0xaa0, %r15            # imm = 0xAA0
movq %r15, %rdi
callq 0x1ddd8
movq 0x40(%rbx), %rcx
movl $0x1, %eax
lock
cmpxchgl %r12d, 0xa80(%rcx)
movl $0x2, %eax
movq 0x40(%rbx), %rcx
lock
cmpxchgl %r14d, 0xa80(%rcx)
jmp 0x1a23a
addq $0xb00, %r15            # imm = 0xB00
movq %r15, %rdi
callq 0x1ddd8
testb %al, %al
jne 0x1a3da
movq 0x40(%rbx), %rdi
addq %r13, %rdi
callq 0x1ddd8
jmp 0x1a23a
movb 0x9a(%r15), %al
cmpb $-0x4, %al
jne 0x1a2d1
leaq 0x80(%r15), %rdi
callq 0xd9e4
movb %al, 0x9a(%r15)
movq 0x40(%rbx), %r15
testb %al, %al
jns 0x1a2e3
cmpb $0x0, 0xba(%r15)
js 0x1a387
movl $0x3, %eax
movl $0x6, %ecx
lock
cmpxchgl %ecx, 0xa80(%r15)
jne 0x1a23a
movq 0x40(%rbx), %rax
movsbl 0xba(%rax), %esi
testl %esi, %esi
js 0x1a23a
movq %rsp, %r15
movq %r15, %rdi
callq 0xd4eb
movl $0xa, %r8d
leaq 0x113b5(%rip), %rcx     # 0x2b6dc
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x30(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1aab4
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1aac1
addq $0xaa0, %r15            # imm = 0xAA0
movq %r15, %rdi
callq 0x1ddd8
movq 0x40(%rbx), %rcx
movl $0x6, %eax
movl $0x1, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x1a23a
movq 0xe8(%r15), %rax
testq %rax, %rax
je 0x1a3a0
cmpq %rax, 0xe0(%r15)
jae 0x1a2e3
movq 0x100(%r15), %rcx
movq 0xf8(%r15), %rax
movl 0x8(%rcx), %ecx
movl 0x10(%rax), %eax
shlq $0x18, %rcx
addq %rcx, %rax
cmpq 0x40(%r15), %rax
jae 0x1a738
lock
incq 0xe0(%r15)
movq %rbx, %rdi
callq 0x20830
jmp 0x1a23a
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x20(%rsp), %xmm1, %xmm0
movq 0x40(%rbx), %rax
movq %rsp, %r15
movq %r15, %rdi
movl 0xd8(%rax), %esi
vmovsd %xmm0, 0x70(%rsp)
vcvtsi2sdq 0x28(%rsp), %xmm1, %xmm0
vdivsd 0xdbf6(%rip), %xmm0, %xmm0 # 0x28008
vmovsd %xmm0, 0x120(%rsp)
callq 0xd4eb
movl $0x1e, %r8d
leaq 0x112ba(%rip), %rcx     # 0x2b6e7
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x30(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a45c
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1a469
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x8(%rax), %rdx
leaq 0x88(%rsp), %r14
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rdi
leaq 0x11271(%rip), %rsi     # 0x2b706
callq 0x4610
leaq 0x158(%rsp), %r15
movq %rax, %rcx
addq $0x10, %rcx
movq %r15, 0x148(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a4c4
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1a4d7
movq %rdx, 0x148(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x158(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x150(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1bae7(%rip), %rax     # 0x35fe0
movq 0x148(%rsp), %rdi
movq (%rax), %rsi
callq 0x4320
movq 0x148(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a51b
callq 0x42b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1a52f
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a542
callq 0x42b0
movq 0x40(%rbx), %rdi
vcvtusi2sdq 0xd0(%rdi), %xmm1, %xmm0
leaq 0x20(%rsp), %rsi
movq 0xa0(%rdi), %rax
leaq 0xa8(%rdi), %rdx
movq %rax, 0x40(%rsp)
vmovups 0x80(%rdi), %ymm1
vmovsd %xmm0, 0x118(%rsp)
vmovups %ymm1, 0x20(%rsp)
vzeroupper
callq 0x1acb2
movq %rsp, %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%rsp), %xmm2, %xmm0
vmovss 0x110b5(%rip), %xmm1    # 0x2b654
movq 0x40(%rbx), %rdi
movl $0xf0, %eax
addq %rax, %rdi
vmovsd %xmm0, 0x110(%rsp)
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0x108(%rsp)
vmovss 0x11084(%rip), %xmm0    # 0x2b650
callq 0x1de8e
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%rsp), %xmm2, %xmm0
movq 0x40(%rbx), %rax
leaq 0x188(%rsp), %r15
movl $0x1, %esi
movq %r15, %rdi
vmovsd %xmm0, 0x100(%rsp)
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xf8(%rsp)
vcvtusi2sdq 0xd0(%rax), %xmm2, %xmm0
vmulsd 0xecbb(%rip), %xmm0, %xmm0 # 0x292d8
vdivsd 0x118(%rsp), %xmm0, %xmm0
callq 0x15694
leaq 0x110da(%rip), %rsi     # 0x2b70c
movq %r15, %rdi
callq 0x4610
movq %r14, 0x78(%rsp)
movq %rax, %rcx
addq $0x10, %rcx
movq %r14, %r13
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a65d
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r13)
jmp 0x1a66d
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
vmovsd 0x108(%rsp), %xmm1
vmovsd 0x120(%rsp), %xmm0
movq 0x8(%rax), %rdx
xorl %r14d, %r14d
vdivsd 0xd97a(%rip), %xmm1, %xmm1 # 0x28008
vaddsd 0x70(%rsp), %xmm0, %xmm0
movq %rdx, 0x80(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vaddsd 0x110(%rsp), %xmm1, %xmm1
vsubsd %xmm0, %xmm1, %xmm0
vmulsd 0xd950(%rip), %xmm0, %xmm0 # 0x28010
vmovsd %xmm1, 0x70(%rsp)
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
callq 0x15694
movq 0x78(%rsp), %rcx
movl $0xf, %edi
cmpq %r13, %rcx
je 0x1a6ec
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %r8
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %rsi
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0x1a72c
movl $0xf, %edi
leaq 0xe8(%rsp), %r9
cmpq %r9, %rsi
je 0x1a727
movq 0xe8(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x1a750
leaq 0x78(%rsp), %rdi
callq 0x40b0
jmp 0x1a761
movl $0x3, %eax
movl $0x4, %ecx
lock
cmpxchgl %ecx, 0xa80(%r15)
jmp 0x1a23a
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x60(%rsp), %rsi
leaq 0x10(%rax), %rcx
movq %rsi, 0x50(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a781
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1a78e
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
movq 0x8(%rax), %rsi
movq %rax, %rdx
addq $0x8, %rdx
movq %rsi, 0x58(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x50(%rsp), %rdi
leaq 0x10f6b(%rip), %rsi     # 0x2b722
callq 0x4610
leaq 0xa8(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x98(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a7e5
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1a7f8
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
vmovsd 0xf8(%rsp), %xmm0
movq 0x8(%rax), %rdx
vdivsd 0xd7fb(%rip), %xmm0, %xmm0 # 0x28008
movq %rdx, 0xa0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vaddsd 0x100(%rsp), %xmm0, %xmm0
vsubsd 0x70(%rsp), %xmm0, %xmm0
vmulsd 0xd7d5(%rip), %xmm0, %xmm0 # 0x28010
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x15694
movq 0x98(%rsp), %rcx
movl $0xf, %edi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rcx
je 0x1a86c
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %r8
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rsi
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0x1a8ac
movl $0xf, %edi
leaq 0xc8(%rsp), %r9
cmpq %r9, %rsi
je 0x1a8a7
movq 0xc8(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x1a8bb
leaq 0x98(%rsp), %rdi
callq 0x40b0
jmp 0x1a8cc
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x10(%rsp), %rsi
leaq 0x10(%rax), %rcx
movq %rsi, (%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a8eb
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1a8f7
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rax), %rsi
movq %rax, %rdx
addq $0x8, %rdx
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x10e12(%rip), %rsi     # 0x2b72d
movq %rsp, %rdi
callq 0x4610
leaq 0x138(%rsp), %r15
movq %rax, %rcx
addq $0x10, %rcx
movq %r15, 0x128(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1a94d
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1a960
movq %rdx, 0x128(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x138(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x130(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1b65e(%rip), %rax     # 0x35fe0
movq 0x128(%rsp), %rdi
movq (%rax), %rsi
callq 0x4320
movq 0x128(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a9a4
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a9b7
callq 0x42b0
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1a9d1
callq 0x42b0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x1a9eb
callq 0x42b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x1a9ff
callq 0x42b0
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1aa19
callq 0x42b0
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x1aa28
callq 0x42b0
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
movl $0xb00, %r13d           # imm = 0xB00
cmpq %rax, %rdi
je 0x1aa48
callq 0x42b0
movq 0x40(%rbx), %rcx
movq 0x40(%rcx), %rdx
movl 0xd8(%rcx), %eax
shrq %rdx
cmpq %rdx, 0xa78(%rcx)
jae 0x1aa75
cmpl $0x6, %eax
jl 0x1aa8b
vcvtsi2sd %eax, %xmm1, %xmm0
vmulsd 0x10bb5(%rip), %xmm0, %xmm0 # 0x2b628
jmp 0x1aa81
vcvtsi2sd %eax, %xmm1, %xmm0
vmulsd 0x10b9f(%rip), %xmm0, %xmm0 # 0x2b620
vcvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rcx)
movl $0x4, %eax
movl $0x6, %edx
lock
cmpxchgl %r12d, 0xa80(%rcx)
movl $0x5, %eax
movq 0x40(%rbx), %rcx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x1a2a1
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rdi
leaq 0xdffd(%rip), %rsi      # 0x28ae2
callq 0x4610
leaq 0x178(%rsp), %r15
movq %rax, %rcx
addq $0x10, %rcx
movq %r15, 0x168(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x1ab14
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1ab27
movq %rdx, 0x168(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x178(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x170(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1b497(%rip), %rax     # 0x35fe0
movq 0x168(%rsp), %rdi
movq (%rax), %rsi
callq 0x4320
movq 0x168(%rsp), %rdi
cmpq %r15, %rdi
je 0x1ab6b
callq 0x42b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1ab7f
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a23a
callq 0x42b0
jmp 0x1a23a
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1ac7b
jmp 0x1ac94
movq (%rsp), %rdi
movq %rax, %rbx
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1abd2
callq 0x42b0
jmp 0x1abd2
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1abec
callq 0x42b0
leaq 0x88(%rsp), %r13
jmp 0x1abf9
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0x60(%rsp), %r14
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x1ac22
callq 0x42b0
jmp 0x1ac22
movq %rax, %rbx
leaq 0x60(%rsp), %r14
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x1ac36
callq 0x42b0
jmp 0x1ac36
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1ac55
callq 0x42b0
jmp 0x1ac55
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x1ac69
callq 0x42b0
jmp 0x1ac69
movq %rax, %rbx
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
jmp 0x1aca0
movq 0x20(%rsp), %rdi
movq %rax, %rbx
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1ac97
callq 0x42b0
jmp 0x1ac97
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1acaa
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0