// File: /workspace/github_c_makefile_star2_O3/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 %rsi, %r14
movq %rdi, %rbx
jne 0xd207
leaq 0x20(%rbx), %rdi
callq 0x23b0
testl %ebp, %ebp
jle 0xd265
leaq 0x8(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x2230
movl %ebp, %eax
movl %ebp, %edx
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq (%r15), %rax
shrq $0x26, %rcx
addq %rcx, %rax
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %edx
movq %rax, (%r15)
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
addq 0x8(%r15), %rcx
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
movq %rcx, 0x8(%r15)
jl 0xd265
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0xd30a
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd30a
leaq 0x48(%rbx), %r12
leaq 0x20(%rbx), %r15
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movq 0x10(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd30a
movq 0x18(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl 0xc(%rax), %ecx
jne 0xd30a
cmpl $-0x1, %ebp
je 0xd2eb
testl %ebp, %ebp
je 0xd34d
movq %r12, %rdi
movq %r15, %rsi
leaq 0x8(%rsp), %rdx
callq 0x2410
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
movl 0xc(%rcx), %ecx
cmpl %ecx, %eax
jne 0xd300
movq 0x10(%rbx), %rcx
movl 0x8(%rcx), %edx
cmpl 0xc(%rcx), %edx
jne 0xd306
movq 0x18(%rbx), %rcx
movl 0x8(%rcx), %edx
cmpl 0xc(%rcx), %edx
movl %eax, %ecx
jne 0xd300
jmp 0xd34d
movq %r12, %rdi
movq %r15, %rsi
callq 0x2420
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
movl 0xc(%rcx), %ecx
cmpl %ecx, %eax
je 0xd28c
jmp 0xd30a
movl %eax, %ecx
jmp 0xd300
movq 0x18(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xd334
movq 0x10(%rbx), %rdi
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
jne 0xd334
movq 0x8(%rbx), %rdi
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movl 0x8(%rdi), %eax
cmpl 0xc(%rdi), %eax
je 0xd341
movq %r14, %rsi
xorl %edx, %edx
callq 0xccdc
movl %eax, %r13d
cmpb $0x1, (%rbx)
jne 0xd355
addq $0x20, %rbx
movq %rbx, %r15
movq %r15, %rdi
callq 0x2190
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq