// File: 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:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x290(%rsp)
movq 0x290(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
movl 0xa80(%rax), %eax
movq %rax, 0x30(%rsp)
subq $0x6, %rax
ja 0x2904b
movq 0x30(%rsp), %rax
leaq 0x17e82(%rip), %rcx     # 0x405c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addq $0x298, %rsp            # imm = 0x298
retq
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
callq 0x2c5d0
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x1, 0x28c(%rsp)
movl $0x3, 0x288(%rsp)
leaq 0x28c(%rsp), %rsi
leaq 0x288(%rsp), %rdx
callq 0x2c6b0
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x2, 0x284(%rsp)
movl $0x0, 0x280(%rsp)
leaq 0x284(%rsp), %rsi
leaq 0x280(%rsp), %rdx
callq 0x2c6b0
jmp 0x2904b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xaa0, %rdi            # imm = 0xAA0
callq 0x2c5d0
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x6, 0x27c(%rsp)
movl $0x1, 0x278(%rsp)
leaq 0x27c(%rsp), %rsi
leaq 0x278(%rsp), %rdx
callq 0x2c6b0
jmp 0x2904b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0x80, %rdi
callq 0x15fb0
movsbl %al, %eax
cmpl $0x0, %eax
jge 0x288a6
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
movsbl 0xba(%rax), %eax
cmpl $0x0, %eax
jge 0x288a6
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
cmpq $0x0, 0xe8(%rax)
jbe 0x28a1b
movq 0x38(%rsp), %rcx
movq 0x40(%rcx), %rax
movq 0xe0(%rax), %rax
movq 0x40(%rcx), %rcx
cmpq 0xe8(%rcx), %rax
jb 0x28a1b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x3, 0x274(%rsp)
movl $0x6, 0x270(%rsp)
leaq 0x274(%rsp), %rsi
leaq 0x270(%rsp), %rdx
callq 0x2c6b0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x2f(%rsp)
jne 0x288f0
jmp 0x2890a
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
movsbl 0xba(%rax), %eax
cmpl $0x0, %eax
setge %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x28917
jmp 0x28a16
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
movsbl 0xba(%rax), %esi
leaq 0x210(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x16070
movq 0x20(%rsp), %rdx
leaq 0x17cfb(%rip), %rsi     # 0x40640
leaq 0x230(%rsp), %rdi
callq 0xe5f0
jmp 0x28954
leaq 0x15155(%rip), %rdx     # 0x3dab0
leaq 0x250(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0x93b0
jmp 0x28972
leaq 0x250(%rsp), %rdi
callq 0x15ad0
jmp 0x28981
leaq 0x250(%rsp), %rdi
callq 0x4210
leaq 0x230(%rsp), %rdi
callq 0x4210
leaq 0x210(%rsp), %rdi
callq 0x4210
jmp 0x28a16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x289f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x4210
leaq 0x230(%rsp), %rdi
callq 0x4210
leaq 0x210(%rsp), %rdi
callq 0x4210
jmp 0x29050
jmp 0x2904b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xf0, %rdi
callq 0x2c6e0
movq 0x38(%rsp), %rcx
movq 0x40(%rcx), %rcx
cmpq 0x40(%rcx), %rax
jb 0x28a7f
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x3, 0x200(%rsp)
movl $0x4, 0x1fc(%rsp)
leaq 0x200(%rsp), %rsi
leaq 0x1fc(%rsp), %rdx
callq 0x2c6b0
jmp 0x2904b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xe0, %rdi
callq 0x2c700
movq 0x38(%rsp), %rdi
callq 0x31540
jmp 0x2904b
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xb00, %rdi            # imm = 0xB00
callq 0x2c5d0
testb $0x1, %al
jne 0x28ac1
jmp 0x29036
leaq 0x1f0(%rsp), %rdi
callq 0x5500
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
movl 0xd8(%rax), %esi
leaq 0x190(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x16070
movq 0x18(%rsp), %rdx
leaq 0x17b50(%rip), %rsi     # 0x4064b
leaq 0x1b0(%rsp), %rdi
callq 0xe5f0
jmp 0x28b0a
leaq 0x17b59(%rip), %rdx     # 0x4066a
leaq 0x1d0(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x93b0
jmp 0x28b28
leaq 0x1d0(%rsp), %rdi
callq 0x15ad0
jmp 0x28b37
leaq 0x1d0(%rsp), %rdi
callq 0x4210
leaq 0x1b0(%rsp), %rdi
callq 0x4210
leaq 0x190(%rsp), %rdi
callq 0x4210
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rcx
movq 0xd0(%rcx), %rcx
movq %rcx, 0x188(%rsp)
movq 0x40(%rax), %rcx
movq 0xa0(%rcx), %rdx
movq %rdx, 0x180(%rsp)
vmovdqu 0x80(%rcx), %ymm0
vmovdqu %ymm0, 0x160(%rsp)
movq 0x40(%rax), %rdi
movq %rdi, %rdx
addq $0xa8, %rdx
leaq 0x160(%rsp), %rsi
vzeroupper
callq 0x29060
leaq 0x158(%rsp), %rdi
callq 0x5500
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xf0, %rdi
vmovss 0x179ab(%rip), %xmm0    # 0x40588
vmovss 0x179a7(%rip), %xmm1    # 0x4058c
callq 0x2c720
leaq 0x150(%rsp), %rdi
callq 0x5500
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
vcvtusi2sdq 0xd0(%rax), %xmm0, %xmm0
vmovsd 0x15689(%rip), %xmm1    # 0x3e298
vmulsd %xmm1, %xmm0, %xmm0
vcvtusi2sdq 0x188(%rsp), %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x1, %esi
callq 0x1e870
movq 0x10(%rsp), %rsi
leaq 0x17a2e(%rip), %rdx     # 0x40670
leaq 0xb0(%rsp), %rdi
callq 0x93b0
jmp 0x28c51
leaq 0x158(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x116e0
vmovsd %xmm0, 0x8(%rsp)
jmp 0x28c6e
vmovsd 0x8(%rsp), %xmm0
vmovsd 0x14394(%rip), %xmm1    # 0x3d010
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x1e870
jmp 0x28c8e
leaq 0xd0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0xe470
jmp 0x28caa
leaq 0x179d5(%rip), %rdx     # 0x40686
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x93b0
jmp 0x28cc8
leaq 0x150(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x116e0
vmovsd %xmm0, (%rsp)
jmp 0x28ce4
vmovsd (%rsp), %xmm0
vmovsd 0x1431f(%rip), %xmm1    # 0x3d010
vmulsd %xmm1, %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x1e870
jmp 0x28d03
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0xe470
jmp 0x28d1f
leaq 0x1796b(%rip), %rdx     # 0x40691
leaq 0x130(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x93b0
jmp 0x28d3d
leaq 0x130(%rsp), %rdi
callq 0x15ad0
jmp 0x28d4c
leaq 0x130(%rsp), %rdi
callq 0x4210
leaq 0x110(%rsp), %rdi
callq 0x4210
leaq 0x50(%rsp), %rdi
callq 0x4210
leaq 0xf0(%rsp), %rdi
callq 0x4210
leaq 0xd0(%rsp), %rdi
callq 0x4210
leaq 0x70(%rsp), %rdi
callq 0x4210
leaq 0xb0(%rsp), %rdi
callq 0x4210
leaq 0x90(%rsp), %rdi
callq 0x4210
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xf0, %rdi
callq 0x2cd10
movq 0x38(%rsp), %rcx
movq 0x40(%rcx), %rcx
movq 0x40(%rcx), %rcx
shrq %rcx
cmpq %rcx, %rax
jb 0x28f99
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rcx
vcvtsi2sdl 0xd8(%rcx), %xmm0, %xmm0
vmovsd 0x177ab(%rip), %xmm1    # 0x405a0
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2si %xmm0, %ecx
movq 0x40(%rax), %rax
movl %ecx, 0xd8(%rax)
jmp 0x28fd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28e66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28e59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x4210
leaq 0x1b0(%rsp), %rdi
callq 0x4210
leaq 0x190(%rsp), %rdi
callq 0x4210
jmp 0x29050
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
jmp 0x28f3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x208(%rsp)
movl %eax, 0x204(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x4210
leaq 0x110(%rsp), %rdi
callq 0x4210
leaq 0x50(%rsp), %rdi
callq 0x4210
leaq 0xf0(%rsp), %rdi
callq 0x4210
leaq 0xd0(%rsp), %rdi
callq 0x4210
leaq 0x70(%rsp), %rdi
callq 0x4210
leaq 0xb0(%rsp), %rdi
callq 0x4210
leaq 0x90(%rsp), %rdi
callq 0x4210
jmp 0x29050
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rax
cmpl $0x5, 0xd8(%rax)
jle 0x28fd6
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rcx
vcvtsi2sdl 0xd8(%rcx), %xmm0, %xmm0
vmovsd 0x175d4(%rip), %xmm1    # 0x40598
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2si %xmm0, %ecx
movq 0x40(%rax), %rax
movl %ecx, 0xd8(%rax)
jmp 0x28fd8
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x4, 0x4c(%rsp)
movl $0x3, 0x48(%rsp)
leaq 0x4c(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x2c6b0
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x5, 0x44(%rsp)
movl $0x6, 0x40(%rsp)
leaq 0x44(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x2c6b0
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
addq $0xb00, %rdi            # imm = 0xB00
callq 0x2c5d0
jmp 0x2871c
movq 0x208(%rsp), %rdi
callq 0x4660
nopl (%rax)