// File: /workspace/github_c_makefile_star2_O1/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
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x1, (%rdi)
jne 0xd60a
leaq 0x20(%rbx), %rdi
callq 0x23b0
testl %ebp, %ebp
jle 0xd668
leaq 0x8(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x2230
movl %ebp, %eax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
movq (%r15), %rax
addq %rcx, %rax
movq %rax, (%r15)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
movl %ebp, %edx
subl %ecx, %edx
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
addq 0x8(%r15), %rcx
movq %rcx, 0x8(%r15)
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
jl 0xd668
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
movq %rcx, 0x10(%rsp)
incq %rax
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0xd6fa
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6fa
leaq 0x48(%rbx), %r12
leaq 0x20(%rbx), %r15
leaq 0x8(%rsp), %r13
movq 0x10(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6fa
movq 0x18(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6fa
cmpl $-0x1, %ebp
je 0xd6e3
testl %ebp, %ebp
je 0xd754
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2410
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6ee
movq 0x10(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6ee
movq 0x18(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd6ee
jmp 0xd754
movq %r12, %rdi
movq %r15, %rsi
callq 0x2420
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
je 0xd68a
movq 0x18(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xd723
movq 0x10(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xd723
movq 0x8(%rbx), %rdi
movl 0x8(%rdi), %eax
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl 0xc(%rdi), %eax
je 0xd72f
movq %r14, %rsi
xorl %edx, %edx
callq 0xd0df
movl %eax, %ebp
cmpb $0x1, (%rbx)
jne 0xd743
addq $0x20, %rbx
movq %rbx, %r15
movq %r15, %rdi
callq 0x2190
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0xd73b