// File: /workspace/github_cpp_makefile_star2_O2/tewalds[P]pentagod/agentpns.cpp
// Function: AgentPNS::PNSThread::run()
void AgentPNS::PNSThread::run(){
	while(true){
		switch(agent->threadstate){
		case Thread_Cancelled:  //threads should exit
			return;

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

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

		case Thread_Running:    //threads are running
			if(agent->root.terminal()){ //solved
				CAS(agent->threadstate, Thread_Running, Thread_Wait_End);
				break;
			}
			if(agent->ctmem.memalloced() >= agent->memlimit){ //out of memory, start garbage collection
				CAS(agent->threadstate, Thread_Running, Thread_GC);
				break;
			}

			pns(agent->rootboard, &agent->root, 0, INF32/2, INF32/2);
			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(agent->gcbarrier.wait()){
				logerr("Starting solver GC with limit " + to_str(agent->gclimit) + " ... ");

				Time starttime;
				agent->garbage_collect(& agent->root);

				Time gctime;
				agent->ctmem.compact(1.0, 0.75);

				Time compacttime;
				logerr(to_str(100.0*agent->ctmem.meminuse()/agent->memlimit, 1) + " % of tree remains - " +
					to_str((gctime - starttime)*1000, 0)  + " msec gc, " + to_str((compacttime - gctime)*1000, 0) + " msec compact\n");

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
leaq 0x862c(%rip), %r12      # 0x20310
leaq 0xb8(%rsp), %r13
leaq 0xd8(%rsp), %r15
movq %rdi, %rbx
movq 0x38(%rbx), %rsi
movl 0x9f0(%rsi), %eax
cmpq $0x6, %rax
ja 0x17cf7
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x14eda
movq 0x38(%rbx), %rcx
pushq $0x1
popq %rax
pushq $0x3
popq %rdx
lock
cmpxchgl %edx, 0x9f0(%rcx)
movq 0x38(%rbx), %rcx
pushq $0x2
popq %rax
xorl %edx, %edx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x17cf7
addq $0xa70, %rsi            # imm = 0xA70
movq %rsi, %rdi
callq 0x14eda
testb %al, %al
je 0x17f8e
movq 0x38(%rbx), %rax
movq %r13, %rdi
movl 0x48(%rax), %esi
callq 0x83cc
leaq 0x8645(%rip), %rsi      # 0x203b6
movq %r15, %rdi
movq %r13, %rdx
callq 0x8423
leaq 0x118(%rsp), %r14
leaq 0x78df(%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
leaq 0x10(%rsp), %rdi
callq 0x4c50
movq 0x38(%rbx), %rdi
leaq 0xae0(%rdi), %rsi
callq 0x1854c
movq %rsp, %rdi
callq 0x4c50
vmovss 0x77bc(%rip), %xmm0     # 0x1f59c
vmovss 0x77b8(%rip), %xmm1     # 0x1f5a0
movq 0x38(%rbx), %rdi
addq $0x50, %rdi
callq 0x18ce8
leaq 0x8(%rsp), %rdi
callq 0x4c50
movq 0x38(%rbx), %rax
vcvtusi2sdq 0x9d8(%rax), %xmm2, %xmm0
leaq 0x78(%rsp), %r14
movq %r14, %rdi
vmulsd 0x548b(%rip), %xmm0, %xmm0 # 0x1d2a8
vcvtusi2sdq 0x40(%rax), %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
pushq $0x1
popq %rsi
callq 0xf350
leaq 0x18(%rsp), %rdi
leaq 0x7834(%rip), %rdx      # 0x1f670
movq %r14, %rsi
callq 0x6821
vmovsd (%rsp), %xmm0
vsubsd 0x10(%rsp), %xmm0, %xmm0
vmulsd 0x41b1(%rip), %xmm0, %xmm0 # 0x1c008
leaq 0x58(%rsp), %r14
xorl %esi, %esi
movq %r14, %rdi
callq 0xf350
leaq 0x98(%rsp), %rbp
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %rdi
callq 0x8365
leaq 0x7801(%rip), %rdx      # 0x1f686
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6821
vmovsd 0x8(%rsp), %xmm0
vsubsd (%rsp), %xmm0, %xmm0
vmulsd 0x4165(%rip), %xmm0, %xmm0 # 0x1c008
leaq 0x38(%rsp), %r14
xorl %esi, %esi
movq %r14, %rdi
callq 0xf350
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x8365
leaq 0xf8(%rsp), %r14
leaq 0x77c2(%rip), %rdx      # 0x1f691
movq %r15, %rsi
movq %r14, %rdi
callq 0x6821
movq %r14, %rdi
callq 0xac65
movq %r14, %rdi
callq 0x4130
movq %r15, %rdi
callq 0x4130
leaq 0x38(%rsp), %rdi
callq 0x4130
movq %r13, %rdi
callq 0x4130
leaq 0x98(%rsp), %rdi
callq 0x4130
leaq 0x58(%rsp), %rdi
callq 0x4130
leaq 0x18(%rsp), %rdi
callq 0x4130
leaq 0x78(%rsp), %rdi
callq 0x4130
movq 0x38(%rbx), %rcx
vmovsd 0x766d(%rip), %xmm0     # 0x1f5a8
movq 0x40(%rcx), %rdx
movl 0x48(%rcx), %eax
shrq %rdx
cmpq %rdx, 0x9d8(%rcx)
jae 0x17f5b
vmovsd 0x765a(%rip), %xmm0     # 0x1f5b0
cmpl $0x6, %eax
jb 0x17f6e
vcvtusi2sd %eax, %xmm2, %xmm1
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2usi %xmm0, %eax
movl %eax, 0x48(%rcx)
pushq $0x4
popq %rax
pushq $0x3
popq %rdx
lock
cmpxchgl %edx, 0x9f0(%rcx)
movq 0x38(%rbx), %rcx
pushq $0x5
popq %rax
pushq $0x6
popq %rdx
lock
cmpxchgl %edx, 0x9f0(%rcx)
movq 0x38(%rbx), %rdi
movl $0xa70, %eax            # imm = 0xA70
addq %rax, %rdi
callq 0x14eda
jmp 0x17cf7
cmpl $0x0, 0xae0(%rsi)
je 0x1801d
cmpl $0x0, 0xae4(%rsi)
je 0x1801d
movq 0x60(%rsi), %rcx
movq 0x58(%rsi), %rax
movl 0x8(%rcx), %ecx
movl 0x10(%rax), %eax
shlq $0x18, %rcx
addq %rcx, %rax
cmpq 0x40(%rsi), %rax
jae 0x18030
leaq 0xae0(%rsi), %rdx
addq $0x10, %rsi
movl $0x1ffffffe, %r8d       # imm = 0x1FFFFFFE
movl $0x1ffffffe, %r9d       # imm = 0x1FFFFFFE
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x180ec
jmp 0x17cf7
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x14eda
movq 0x38(%rbx), %rcx
pushq $0x6
popq %rax
pushq $0x1
popq %rdx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x17cf7
pushq $0x3
popq %rax
pushq $0x6
popq %rcx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x17cf7
pushq $0x3
popq %rax
pushq $0x4
popq %rcx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x17cf7
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd8(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
jmp 0x180d7
movq %rax, %rbx
jmp 0x180cd
movq %rax, %rbx
jmp 0x180c3
movq %rax, %rbx
jmp 0x180b9
movq %rax, %rbx
jmp 0x180ac
movq %rax, %rbx
jmp 0x1809f
movq %rax, %rbx
jmp 0x18095
leaq 0xd8(%rsp), %rdi
movq %rax, %rbx
callq 0x4130
leaq 0x38(%rsp), %rdi
callq 0x4130
leaq 0xb8(%rsp), %rdi
callq 0x4130
leaq 0x98(%rsp), %rdi
callq 0x4130
leaq 0x58(%rsp), %rdi
callq 0x4130
leaq 0x18(%rsp), %rdi
callq 0x4130
leaq 0x78(%rsp), %rdi
jmp 0x180df
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4130
movq %rbx, %rdi
callq 0x4590