// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %rbx
leaq 0x11ea5(%rip), %rbp     # 0x2c65c
movl $0x3, %r12d
xorl %r14d, %r14d
movl $0xb00, %r13d           # imm = 0xB00
movq 0x40(%rbx), %r15
movl 0xa80(%r15), %eax
cmpq $0x6, %rax
ja 0x1a7c6
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
addq $0xaa0, %r15            # imm = 0xAA0
movq %r15, %rdi
callq 0x1e3ea
movq 0x40(%rbx), %rcx
movl $0x1, %eax
lock
cmpxchgl %r12d, 0xa80(%rcx)
movq 0x40(%rbx), %rcx
movl $0x2, %eax
lock
cmpxchgl %r14d, 0xa80(%rcx)
jmp 0x1a7c6
addq $0xb00, %r15            # imm = 0xB00
movq %r15, %rdi
callq 0x1e3ea
testb %al, %al
jne 0x1a96d
movq 0x40(%rbx), %rdi
addq %r13, %rdi
callq 0x1e3ea
jmp 0x1a7c6
cmpb $-0x4, 0x9a(%r15)
jne 0x1a858
leaq 0x80(%r15), %rdi
callq 0xdab0
movb %al, 0x9a(%r15)
cmpb $0x0, 0x9a(%r15)
jns 0x1a873
movq 0x40(%rbx), %rcx
cmpb $0x0, 0xba(%rcx)
js 0x1a91a
movq 0x40(%rbx), %rcx
movl $0x3, %eax
movl $0x6, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jne 0x1a7c6
movq 0x40(%rbx), %rax
movsbl 0xba(%rax), %esi
testl %esi, %esi
js 0x1a7c6
movq %rsp, %r15
movq %r15, %rdi
callq 0xd56b
movl $0xa, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x11e1b(%rip), %rcx     # 0x2c6dc
callq 0x4330
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1b050
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1b05d
addq $0xaa0, %r15            # imm = 0xAA0
movq %r15, %rdi
callq 0x1e3ea
movq 0x40(%rbx), %rcx
movl $0x6, %eax
movl $0x1, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x1a7c6
movq 0xe8(%rcx), %rax
testq %rax, %rax
je 0x1a933
cmpq %rax, 0xe0(%rcx)
jae 0x1a873
movq 0xf8(%rcx), %rax
movq 0x100(%rcx), %rdx
movl 0x8(%rdx), %edx
shlq $0x18, %rdx
movl 0x10(%rax), %eax
addq %rdx, %rax
cmpq 0x40(%rcx), %rax
jae 0x1acd0
lock
incq 0xe0(%rcx)
movq %rbx, %rdi
callq 0x20e80
jmp 0x1a7c6
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x20(%rsp), %xmm1, %xmm0
vmovsd %xmm0, 0x70(%rsp)
vcvtsi2sdq 0x28(%rsp), %xmm1, %xmm0
vdivsd 0xe673(%rip), %xmm0, %xmm0 # 0x29008
vmovsd %xmm0, 0x120(%rsp)
movq 0x40(%rbx), %rax
movl 0xd8(%rax), %esi
movq %rsp, %r15
movq %r15, %rdi
callq 0xd56b
movl $0x1e, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x11d20(%rip), %rcx     # 0x2c6e7
callq 0x4330
leaq 0x30(%rsp), %rsi
movq %rsi, 0x20(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1a9ef
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1a9fc
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
leaq 0x88(%rsp), %r14
movq 0x8(%rax), %rdx
movq %rdx, 0x28(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rdi
leaq 0x11cde(%rip), %rsi     # 0x2c706
callq 0x4610
leaq 0x158(%rsp), %r15
movq %r15, 0x148(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1aa57
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1aa6a
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 0x1c554(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0x148(%rsp), %rdi
callq 0x4320
movq 0x148(%rsp), %rdi
cmpq %r15, %rdi
je 0x1aaae
callq 0x42b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1aac2
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1aad5
callq 0x42b0
movq 0x40(%rbx), %rdi
vcvtusi2sdq 0xd0(%rdi), %xmm1, %xmm0
vmovsd %xmm0, 0x118(%rsp)
movq 0xa0(%rdi), %rax
movq %rax, 0x40(%rsp)
vmovupd 0x80(%rdi), %ymm0
vmovupd %ymm0, 0x20(%rsp)
leaq 0xa8(%rdi), %rdx
leaq 0x20(%rsp), %rsi
vzeroupper
callq 0x1b24e
movq %rsp, %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%rsp), %xmm1, %xmm0
vmovsd %xmm0, 0x110(%rsp)
vcvtsi2sdq 0x8(%rsp), %xmm1, %xmm0
vmovsd %xmm0, 0x108(%rsp)
movq 0x40(%rbx), %rdi
movl $0xf0, %eax
addq %rax, %rdi
vmovss 0x11af9(%rip), %xmm0    # 0x2c650
vmovss 0x11af5(%rip), %xmm1    # 0x2c654
callq 0x1e4a0
movq %r15, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq (%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0x100(%rsp)
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xf8(%rsp)
movq 0x40(%rbx), %rax
vcvtusi2sdq 0xd0(%rax), %xmm2, %xmm0
vmulsd 0xf748(%rip), %xmm0, %xmm0 # 0x2a2e8
vdivsd 0x118(%rsp), %xmm0, %xmm0
leaq 0x188(%rsp), %r15
movq %r15, %rdi
movl $0x1, %esi
callq 0x1579c
movq %r15, %rdi
leaq 0x11b44(%rip), %rsi     # 0x2c70c
callq 0x4610
movq %r14, %r13
movq %r14, 0x78(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1abf0
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r13)
jmp 0x1ac00
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
xorl %r14d, %r14d
vmovsd 0x120(%rsp), %xmm0
vaddsd 0x70(%rsp), %xmm0, %xmm0
vmovsd 0x108(%rsp), %xmm1
vdivsd 0xe3e5(%rip), %xmm1, %xmm1 # 0x29008
vaddsd 0x110(%rsp), %xmm1, %xmm1
movq 0x8(%rax), %rdx
movq %rdx, 0x80(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vmovsd %xmm1, 0x70(%rsp)
vsubsd %xmm0, %xmm1, %xmm0
vmulsd 0xe3b7(%rip), %xmm0, %xmm0 # 0x29010
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
callq 0x1579c
movq 0x78(%rsp), %rcx
movl $0xf, %esi
cmpq %r13, %rcx
je 0x1ac7f
movq 0x88(%rsp), %rsi
movq 0x80(%rsp), %r8
movq 0xe0(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0x1acbc
movl $0xf, %esi
leaq 0xe8(%rsp), %rdi
cmpq %rdi, 0xd8(%rsp)
je 0x1acb7
movq 0xe8(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x1ace7
movq 0xd8(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x40b0
jmp 0x1acf8
movl $0x3, %eax
movl $0x4, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x1a7c6
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x60(%rsp), %rsi
movq %rsi, 0x50(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jne 0x1ad18
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1ad25
movq %rdx, 0x50(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x60(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x58(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x50(%rsp), %rdi
leaq 0x119d4(%rip), %rsi     # 0x2c722
callq 0x4610
leaq 0xa8(%rsp), %rsi
movq %rsi, 0x98(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1ad7c
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1ad8f
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
vmovsd 0xf8(%rsp), %xmm0
vdivsd 0xe268(%rip), %xmm0, %xmm0 # 0x29008
vaddsd 0x100(%rsp), %xmm0, %xmm0
movq 0x8(%rax), %rdx
movq %rdx, 0xa0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vsubsd 0x70(%rsp), %xmm0, %xmm0
vmulsd 0xe23e(%rip), %xmm0, %xmm0 # 0x29010
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0x1579c
movq 0x98(%rsp), %rcx
movl $0xf, %esi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rcx
je 0x1ae03
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %r8
movq 0xc0(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0x1ae40
movl $0xf, %esi
leaq 0xc8(%rsp), %rdi
cmpq %rdi, 0xb8(%rsp)
je 0x1ae3b
movq 0xc8(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x1ae57
movq 0xb8(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x40b0
jmp 0x1ae68
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x10(%rsp), %rsi
movq %rsi, (%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jne 0x1ae87
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x1ae93
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq %rsp, %rdi
leaq 0x11873(%rip), %rsi     # 0x2c72d
callq 0x4610
leaq 0x138(%rsp), %r15
movq %r15, 0x128(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1aee9
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1aefc
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 0x1c0c2(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0x128(%rsp), %rdi
callq 0x4320
movq 0x128(%rsp), %rdi
cmpq %r15, %rdi
je 0x1af40
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1af53
callq 0x42b0
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1af6d
callq 0x42b0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x1af87
callq 0x42b0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x1af9b
callq 0x42b0
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1afb5
callq 0x42b0
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x1afc4
callq 0x42b0
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
cmpq %rax, %rdi
movl $0xb00, %r13d           # imm = 0xB00
je 0x1afe4
callq 0x42b0
movq 0x40(%rbx), %rcx
movq 0x40(%rcx), %rdx
shrq %rdx
movl 0xd8(%rcx), %eax
cmpq %rdx, 0xa78(%rcx)
jae 0x1b011
cmpl $0x6, %eax
jl 0x1b027
vcvtsi2sd %eax, %xmm1, %xmm0
vmulsd 0x11619(%rip), %xmm0, %xmm0 # 0x2c628
jmp 0x1b01d
vcvtsi2sd %eax, %xmm1, %xmm0
vmulsd 0x11603(%rip), %xmm0, %xmm0 # 0x2c620
vcvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rcx)
movl $0x4, %eax
lock
cmpxchgl %r12d, 0xa80(%rcx)
movq 0x40(%rbx), %rcx
movl $0x5, %eax
movl $0x6, %edx
lock
cmpxchgl %edx, 0xa80(%rcx)
jmp 0x1a82d
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 0xea61(%rip), %rsi      # 0x29ae2
callq 0x4610
leaq 0x178(%rsp), %r15
movq %r15, 0x168(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x1b0b0
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r15)
jmp 0x1b0c3
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 0x1befb(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0x168(%rsp), %rdi
callq 0x4320
movq 0x168(%rsp), %rdi
cmpq %r15, %rdi
je 0x1b107
callq 0x42b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1b11b
callq 0x42b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1a7c6
callq 0x42b0
jmp 0x1a7c6
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1b217
jmp 0x1b230
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1b16e
callq 0x42b0
jmp 0x1b16e
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x1b188
callq 0x42b0
leaq 0x88(%rsp), %r13
jmp 0x1b195
movq %rax, %rbx
leaq 0x60(%rsp), %r14
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x1b1be
callq 0x42b0
jmp 0x1b1be
movq %rax, %rbx
leaq 0x60(%rsp), %r14
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x1b1d2
callq 0x42b0
jmp 0x1b1d2
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x1b1f1
callq 0x42b0
jmp 0x1b1f1
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x1b205
callq 0x42b0
jmp 0x1b205
movq %rax, %rbx
movq 0x188(%rsp), %rdi
leaq 0x198(%rsp), %rax
jmp 0x1b23c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x1b233
callq 0x42b0
jmp 0x1b233
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x1b246
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0