// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %rbx
leaq 0x108(%rsp), %r12
leaq 0xb015(%rip), %r14      # 0x2d400
movl $0x1, %r13d
movl $0x3, %r15d
movl $0xa70, %ebp            # imm = 0xA70
movq 0x38(%rbx), %rsi
movl 0x9f0(%rsi), %eax
cmpq $0x6, %rax
ja 0x223fc
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x1e3ea
movq 0x38(%rbx), %rcx
movl $0x1, %eax
lock
cmpxchgl %r15d, 0x9f0(%rcx)
movq 0x38(%rbx), %rcx
movl $0x2, %eax
xorl %edx, %edx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x223fc
addq $0xa70, %rsi            # imm = 0xA70
movq %rsi, %rdi
callq 0x1e3ea
testb %al, %al
jne 0x22517
movq 0x38(%rbx), %rdi
addq %rbp, %rdi
callq 0x1e3ea
jmp 0x223fc
cmpl $0x0, 0xae0(%rsi)
je 0x224e9
cmpl $0x0, 0xae4(%rsi)
je 0x224e9
movq 0x58(%rsi), %rax
movq 0x60(%rsi), %rcx
movl 0x8(%rcx), %ecx
shlq $0x18, %rcx
movl 0x10(%rax), %eax
addq %rcx, %rax
cmpq 0x40(%rsi), %rax
jae 0x22500
leaq 0xae0(%rsi), %rdx
addq $0x10, %rsi
movq %rbx, %rdi
xorl %ecx, %ecx
movl $0x1ffffffe, %r8d       # imm = 0x1FFFFFFE
movl $0x1ffffffe, %r9d       # imm = 0x1FFFFFFE
callq 0x22ca2
jmp 0x223fc
addq $0xa10, %rsi            # imm = 0xA10
movq %rsi, %rdi
callq 0x1e3ea
movq 0x38(%rbx), %rcx
movl $0x6, %eax
lock
cmpxchgl %r13d, 0x9f0(%rcx)
jmp 0x223fc
movl $0x3, %eax
movl $0x6, %ecx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x223fc
movl $0x3, %eax
movl $0x4, %ecx
lock
cmpxchgl %ecx, 0x9f0(%rsi)
jmp 0x223fc
movq 0x38(%rbx), %rax
movl 0x48(%rax), %esi
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x9de2
movl $0x1e, %r8d
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xafe3(%rip), %rcx      # 0x2d522
callq 0x4330
leaq 0x18(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x22567
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x22574
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq %r12, %rbp
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x8(%rsp), %rdi
leaq 0xa16b(%rip), %rsi      # 0x2c706
callq 0x4610
leaq 0x128(%rsp), %r12
movq %r12, 0x118(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x225cb
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r12)
jmp 0x225de
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 0x149e0(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0x118(%rsp), %rdi
callq 0x4320
movq 0x118(%rsp), %rdi
cmpq %r12, %rdi
je 0x22622
callq 0x42b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x22636
callq 0x42b0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
movl $0x1, %r13d
je 0x22650
callq 0x42b0
leaq 0x8(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0x68(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xf0(%rsp)
movq 0x38(%rbx), %rdi
leaq 0xae0(%rdi), %rsi
callq 0x23190
movq %r12, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xe8(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xe0(%rsp)
movq 0x38(%rbx), %rdi
addq $0x50, %rdi
vmovss 0x9f8a(%rip), %xmm0     # 0x2c650
vmovss 0x9f86(%rip), %xmm1     # 0x2c654
callq 0x23bfc
movq %r12, %rdi
xorl %esi, %esi
callq 0x4500
vcvtsi2sdq 0x8(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xd8(%rsp)
vcvtsi2sdq 0x10(%rsp), %xmm2, %xmm0
vmovsd %xmm0, 0xd0(%rsp)
movq 0x38(%rbx), %rax
vcvtusi2sdq 0x9d8(%rax), %xmm2, %xmm0
vmulsd 0x7bd5(%rip), %xmm0, %xmm0 # 0x2a2e8
vcvtusi2sdq 0x40(%rax), %xmm2, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
leaq 0x138(%rsp), %r12
movq %r12, %rdi
movl $0x1, %esi
callq 0x1579c
movq %r12, %rdi
leaq 0x9fcf(%rip), %rsi      # 0x2c70c
callq 0x4610
leaq 0x80(%rsp), %rsi
movq %rsi, 0x70(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x22768
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x22778
movq %rdx, 0x70(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x80(%rsp)
movq %rbp, %r12
vmovsd 0x6885(%rip), %xmm1     # 0x29008
vmovsd 0xf0(%rsp), %xmm0
vdivsd %xmm1, %xmm0, %xmm0
vaddsd 0x68(%rsp), %xmm0, %xmm0
vmovsd 0xe0(%rsp), %xmm2
vdivsd %xmm1, %xmm2, %xmm1
vaddsd 0xe8(%rsp), %xmm1, %xmm1
movq 0x8(%rax), %rdx
movq %rdx, 0x78(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vmovsd %xmm1, 0x68(%rsp)
vsubsd %xmm0, %xmm1, %xmm0
vmulsd 0x683a(%rip), %xmm0, %xmm0 # 0x29010
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x1579c
movl $0xa70, %ebp            # imm = 0xA70
movq 0x70(%rsp), %rcx
movl $0xf, %esi
leaq 0x80(%rsp), %rax
cmpq %rax, %rcx
je 0x22809
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %r8
movq 0xb8(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0x22843
movl $0xf, %esi
leaq 0xc0(%rsp), %rdi
cmpq %rdi, 0xb0(%rsp)
je 0x2283e
movq 0xc0(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x22857
movq 0xb0(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x40b0
jmp 0x22868
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x58(%rsp), %rsi
movq %rsi, 0x48(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jne 0x22888
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x22895
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x48(%rsp), %rdi
leaq 0x9e64(%rip), %rsi      # 0x2c722
callq 0x4610
leaq 0x38(%rsp), %rsi
movq %rsi, 0x28(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x228e6
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x228f3
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
vmovsd 0xd0(%rsp), %xmm0
vdivsd 0x6704(%rip), %xmm0, %xmm0 # 0x29008
vaddsd 0xd8(%rsp), %xmm0, %xmm0
movq 0x8(%rax), %rdx
movq %rdx, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
vsubsd 0x68(%rsp), %xmm0, %xmm0
vmulsd 0x66dd(%rip), %xmm0, %xmm0 # 0x29010
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x1579c
movq 0x28(%rsp), %rcx
movl $0xf, %esi
leaq 0x38(%rsp), %rax
cmpq %rax, %rcx
je 0x2295b
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x98(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0x22995
movl $0xf, %esi
leaq 0xa0(%rsp), %rdi
cmpq %rdi, 0x90(%rsp)
je 0x22990
movq 0xa0(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x229a9
movq 0x90(%rsp), %rsi
leaq 0x28(%rsp), %rdi
callq 0x40b0
jmp 0x229ba
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4330
leaq 0x18(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jne 0x229da
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%rsi)
jmp 0x229e7
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x8(%rsp), %rdi
leaq 0x9d1d(%rip), %rsi      # 0x2c72d
callq 0x4610
movq %r12, 0xf8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
jne 0x22a38
vmovupd (%rcx), %xmm0
vmovupd %xmm0, (%r12)
jmp 0x22a4b
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 0x14573(%rip), %rax     # 0x36fe0
movq (%rax), %rsi
movq 0xf8(%rsp), %rdi
callq 0x4320
movq 0xf8(%rsp), %rdi
cmpq %r12, %rdi
je 0x22a8f
callq 0x42b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x22aa3
callq 0x42b0
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x22abd
callq 0x42b0
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x22ad1
callq 0x42b0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x22ae5
callq 0x42b0
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x22aff
callq 0x42b0
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x22b16
callq 0x42b0
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x22b30
callq 0x42b0
movq 0x38(%rbx), %rcx
movq 0x40(%rcx), %rdx
shrq %rdx
movl 0x48(%rcx), %eax
vmovsd 0x9ada(%rip), %xmm0     # 0x2c620
cmpq %rdx, 0x9d8(%rcx)
jae 0x22b5c
vmovsd 0x9ad1(%rip), %xmm0     # 0x2c628
cmpl $0x6, %eax
jb 0x22b6f
vcvtusi2sd %eax, %xmm1, %xmm1
vmulsd %xmm1, %xmm0, %xmm0
vcvttsd2usi %xmm0, %eax
movl %eax, 0x48(%rcx)
movl $0x4, %eax
lock
cmpxchgl %r15d, 0x9f0(%rcx)
movq 0x38(%rbx), %rcx
movl $0x5, %eax
movl $0x6, %edx
lock
cmpxchgl %edx, 0x9f0(%rcx)
jmp 0x22462
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x22bc6
callq 0x42b0
jmp 0x22bc6
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x22be5
callq 0x42b0
jmp 0x22be5
movq %rax, %rbx
leaq 0x58(%rsp), %r14
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x22c08
callq 0x42b0
jmp 0x22c08
movq %rax, %rbx
leaq 0x58(%rsp), %r14
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x22c1c
callq 0x42b0
jmp 0x22c1c
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x22c3b
callq 0x42b0
jmp 0x22c3b
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x22c57
callq 0x42b0
jmp 0x22c57
movq %rax, %rbx
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
jmp 0x22c8f
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x22c85
callq 0x42b0
jmp 0x22c85
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x22c99
callq 0x42b0
movq %rbx, %rdi
callq 0x45b0
nop