// File: /workspace/github_cpp_makefile_star2_O3/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 $0x158, %rsp            # imm = 0x158
leaq 0x108(%rsp), %r12
leaq 0xa6c2(%rip), %r14      # 0x2c400
movl $0x1, %r13d
movl $0x3, %r15d
movl $0xa70, %ebp            # imm = 0xA70
movq %rdi, %rbx
movq 0x38(%rbx), %rsi
movl 0x9f0(%rsi), %eax
cmpq $0x6, %rax
ja 0x21d52
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x1ddd8
movq 0x38(%rbx), %rcx
movl $0x1, %eax
xorl %edx, %edx
lock
cmpxchgl %r15d, 0x9f0(%rcx)
movl $0x2, %eax
movq 0x38(%rbx), %rcx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x21d52
addq $0xa70, %rsi            # imm = 0xA70
movq %rsi, %rdi
callq 0x1ddd8
testb %al, %al
jne 0x21e6d
movq 0x38(%rbx), %rdi
addq %rbp, %rdi
callq 0x1ddd8
jmp 0x21d52
cmpl $0x0, 0xae0(%rsi)
je 0x21e3f
cmpl $0x0, 0xae4(%rsi)
je 0x21e3f
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 0x21e56
leaq 0xae0(%rsi), %rdx
addq $0x10, %rsi
movl $0x1ffffffe, %r8d       # imm = 0x1FFFFFFE
movl $0x1ffffffe, %r9d       # imm = 0x1FFFFFFE
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x225ee
jmp 0x21d52
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x1ddd8
movq 0x38(%rbx), %rcx
movl $0x6, %eax
lock
cmpxchgl %r13d, 0x9f0(%rcx)
jmp 0x21d52
movl $0x3, %eax
movl $0x6, %ecx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x21d52
movl $0x3, %eax
movl $0x4, %ecx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x21d52
movq 0x38(%rbx), %rax
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
movl 0x48(%rax), %esi
callq 0x9d82
movl $0x1e, %r8d
leaq 0xa694(%rip), %rcx      # 0x2c522
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x18(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x8(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x21ebd
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x21eca
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rax), %rdx
movq %r12, %rbp
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x8(%rsp), %rdi
leaq 0x9815(%rip), %rsi      # 0x2b706
callq 0x4610
leaq 0x128(%rsp), %r12
movq %rax, %rcx
addq $0x10, %rcx
movq %r12, 0x118(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x21f21
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r12)
jmp 0x21f34
movq %rdx, 0x118(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x128(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x120(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x1408a(%rip), %rax     # 0x35fe0
movq 0x118(%rsp), %rdi
movq (%rax), %rsi
callq 0x4320
movq 0x118(%rsp), %rdi
cmpq %r12, %rdi
je 0x21f78
callq 0x42b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x21f8c
callq 0x42b0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
movl $0x1, %r13d
cmpq %rax, %rdi
je 0x21fa6
callq 0x42b0
leaq 0x8(%rsp), %r12
xorl %esi, %esi
movq %r12, %rdi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
movq 0x38(%rbx), %rdi
leaq 0xae0(%rdi), %rsi
vmovsd %xmm0, 0x68(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xf0(%rsp)
callq 0x22aec
movq %r12, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovss 0x9659(%rip), %xmm1     # 0x2b654
movq 0x38(%rbx), %rdi
addq $0x50, %rdi
vmovsd %xmm0, 0xe8(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xe0(%rsp)
vmovss 0x962c(%rip), %xmm0     # 0x2b650
callq 0x2353c
movq %r12, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
movq 0x38(%rbx), %rax
leaq 0x138(%rsp), %r12
movl $0x1, %esi
movq %r12, %rdi
vmovsd %xmm0, 0xd8(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xd0(%rsp)
vcvtusi2sdq 0x9d8(%rax), %xmm2, %xmm0
vmulsd 0x725f(%rip), %xmm0, %xmm0 # 0x292d8
vcvtusi2sdq 0x40(%rax), %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
callq 0x15694
leaq 0x967c(%rip), %rsi      # 0x2b70c
movq %r12, %rdi
callq 0x4610
leaq 0x80(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x70(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x220be
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x220ce
movq %rdx, 0x70(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x80(%rsp)
vmovsd 0x5f32(%rip), %xmm1     # 0x28008
vmovsd 0xf0(%rsp), %xmm0
vmovsd 0xe0(%rsp), %xmm2
movq 0x8(%rax), %rdx
movq %rbp, %r12
movq %rdx, 0x78(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vdivsd %xmm1, %xmm0, %xmm0
vdivsd %xmm1, %xmm2, %xmm1
vaddsd 0x68(%rsp), %xmm0, %xmm0
vaddsd 0xe8(%rsp), %xmm1, %xmm1
vsubsd %xmm0, %xmm1, %xmm0
vmulsd 0x5eea(%rip), %xmm0, %xmm0 # 0x28010
vmovsd %xmm1, 0x68(%rsp)
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x15694
movl $0xa70, %ebp            # imm = 0xA70
movq 0x70(%rsp), %rcx
movl $0xf, %edi
leaq 0x80(%rsp), %rax
cmpq %rax, %rcx
je 0x2215f
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %r8
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %rsi
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0x2219c
movl $0xf, %edi
leaq 0xc0(%rsp), %r9
cmpq %r9, %rsi
je 0x22197
movq 0xc0(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x221a8
leaq 0x70(%rsp), %rdi
callq 0x40b0
jmp 0x221b9
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x58(%rsp), %rsi
leaq 0x10(%rax), %rcx
movq %rsi, 0x48(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x221d9
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x221e6
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
movq 0x8(%rax), %rsi
movq %rax, %rdx
addq $0x8, %rdx
movq %rsi, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x48(%rsp), %rdi
leaq 0x9513(%rip), %rsi      # 0x2b722
callq 0x4610
leaq 0x38(%rsp), %rsi
movq %rax, %rcx
addq $0x10, %rcx
movq %rsi, 0x28(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x22237
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x22244
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
vmovsd 0xd0(%rsp), %xmm0
movq 0x8(%rax), %rdx
vdivsd 0x5daf(%rip), %xmm0, %xmm0 # 0x28008
movq %rdx, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vaddsd 0xd8(%rsp), %xmm0, %xmm0
vsubsd 0x68(%rsp), %xmm0, %xmm0
vmulsd 0x5d8c(%rip), %xmm0, %xmm0 # 0x28010
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x15694
movq 0x28(%rsp), %rcx
movl $0xf, %edi
leaq 0x38(%rsp), %rax
cmpq %rax, %rcx
je 0x222ac
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %r8
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rsi
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0x222e9
movl $0xf, %edi
leaq 0xa0(%rsp), %r9
cmpq %r9, %rsi
je 0x222e4
movq 0xa0(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x222f5
leaq 0x28(%rsp), %rdi
callq 0x40b0
jmp 0x22306
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x18(%rsp), %rsi
leaq 0x10(%rax), %rcx
movq %rsi, 0x8(%rsp)
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x22326
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x22333
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rax), %rsi
movq %rax, %rdx
addq $0x8, %rdx
movq %rsi, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x8(%rsp), %rdi
leaq 0x93d1(%rip), %rsi      # 0x2b72d
callq 0x4610
movq %r12, 0xf8(%rsp)
movq %rax, %rcx
addq $0x10, %rcx
movq (%rax), %rdx
cmpq %rcx, %rdx
jne 0x22384
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r12)
jmp 0x22397
movq %rdx, 0xf8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x108(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x100(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x13c27(%rip), %rax     # 0x35fe0
movq 0xf8(%rsp), %rdi
movq (%rax), %rsi
callq 0x4320
movq 0xf8(%rsp), %rdi
cmpq %r12, %rdi
je 0x223db
callq 0x42b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x223ef
callq 0x42b0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x22409
callq 0x42b0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2241d
callq 0x42b0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x22431
callq 0x42b0
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x2244b
callq 0x42b0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x22462
callq 0x42b0
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x2247c
callq 0x42b0
movq 0x38(%rbx), %rcx
vmovsd 0x9198(%rip), %xmm0     # 0x2b620
movq 0x40(%rcx), %rdx
movl 0x48(%rcx), %eax
shrq %rdx
cmpq %rdx, 0x9d8(%rcx)
jae 0x224a8
vmovsd 0x9185(%rip), %xmm0     # 0x2b628
cmpl $0x6, %eax
jb 0x224bb
vcvtusi2sd %eax, %xmm1, %xmm1
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2usi %xmm0, %eax
movl %eax, 0x48(%rcx)
movl $0x4, %eax
movl $0x6, %edx
lock
cmpxchgl %r15d, 0x9f0(%rcx)
movl $0x5, %eax
movq 0x38(%rbx), %rcx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x21db8
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movq %rax, %rbx
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x22512
callq 0x42b0
jmp 0x22512
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x22531
callq 0x42b0
jmp 0x22531
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x58(%rsp), %r14
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x22554
callq 0x42b0
jmp 0x22554
movq %rax, %rbx
leaq 0x58(%rsp), %r14
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x22568
callq 0x42b0
jmp 0x22568
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x22587
callq 0x42b0
jmp 0x22587
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x225a3
callq 0x42b0
jmp 0x225a3
movq %rax, %rbx
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
jmp 0x225db
movq 0x8(%rsp), %rdi
movq %rax, %rbx
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x225d1
callq 0x42b0
jmp 0x225d1
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x225e5
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0
nop