// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cf_queue.c
// Function: cf_queue_priority_pop
int 
cf_queue_priority_pop(cf_queue_priority *q, void *buf, int ms_wait)
{
	QUEUE_LOCK(q);

	struct timespec tp;
	if (ms_wait > 0) {
#ifdef OSX
		uint64_t curms = cf_getms(); // using the cl generic functions defined in cf_clock.h. It is going to have slightly less resolution than the pure linux version
		tp.tv_sec = (curms + ms_wait)/1000;
		tp.tv_nsec = (ms_wait %1000) * 1000000;
#else // linux
		clock_gettime( CLOCK_REALTIME, &tp); 
		tp.tv_sec += ms_wait / 1000;
		tp.tv_nsec += (ms_wait % 1000) * 1000000;
		if (tp.tv_nsec > 1000000000) {
			tp.tv_nsec -= 1000000000;
			tp.tv_sec++;
		}
#endif
	}

	if (q->threadsafe) {
#ifdef EXTERNAL_LOCKS
		if (CF_Q_PRI_EMPTY(q)) {
			QUEUE_UNLOCK(q);
			return -1;
		}
#else
		while (CF_Q_PRI_EMPTY(q)) {
			if (CF_QUEUE_FOREVER == ms_wait) {
				pthread_cond_wait(&q->CV, &q->LOCK);
			}
			else if (CF_QUEUE_NOWAIT == ms_wait) {
				pthread_mutex_unlock(&q->LOCK);
				return(CF_QUEUE_EMPTY);
			}
			else {
				pthread_cond_timedwait(&q->CV, &q->LOCK, &tp);
				if (CF_Q_PRI_EMPTY(q)) {
					pthread_mutex_unlock(&q->LOCK);
					return(CF_QUEUE_EMPTY);
				}
			}
		}
#endif //EXERNAL_LOCKS
	}
	
	int rv;
	if (CF_Q_SZ(q->high_q))
		rv = cf_queue_pop(q->high_q, buf, 0);
	else if (CF_Q_SZ(q->medium_q))
		rv = cf_queue_pop(q->medium_q, buf, 0);
	else if (CF_Q_SZ(q->low_q))
		rv = cf_queue_pop(q->low_q, buf, 0);
	else rv = CF_QUEUE_EMPTY;
		
	QUEUE_UNLOCK(q);

		
	return(rv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpb $0x1, (%rdi)
movl %edx, %ebp
movq %rdi, %rbx
movq %rsi, (%rsp)
jne 0xc657
leaq 0x20(%rbx), %rdi
callq 0x23b0
testl %ebp, %ebp
jle 0xc6a6
leaq 0x8(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x2230
movl $0x3e8, %ecx            # imm = 0x3E8
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
addq (%r14), %rax
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
movq %rax, (%r14)
addq 0x8(%r14), %rcx
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
movq %rcx, 0x8(%r14)
jl 0xc6a6
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0xc732
leaq 0x48(%rbx), %r13
leaq 0x20(%rbx), %r12
pushq $-0x2
popq %r15
leaq 0x8(%rsp), %r14
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc732
movq 0x10(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc732
movq 0x18(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc732
cmpl $-0x1, %ebp
je 0xc725
testl %ebp, %ebp
je 0xc776
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2410
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc6c0
movq 0x10(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc6c0
movq 0x18(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xc6c0
jmp 0xc776
movq %r13, %rdi
movq %r12, %rsi
callq 0x2420
jmp 0xc6c0
movq 0x18(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xc75c
movq 0x10(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xc75c
movq 0x8(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xc75c
pushq $-0x2
popq %r15
jmp 0xc76a
movq (%rsp), %rsi
xorl %edx, %edx
callq 0xc163
movl %eax, %r15d
cmpb $0x1, (%rbx)
jne 0xc77e
addq $0x20, %rbx
movq %rbx, %r12
movq %r12, %rdi
callq 0x2190
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq