// File: /workspace/github_c_makefile_star2_O3/sharpyfox[P]citrusleaf-client/src/cf_queue.c
// Function: cf_queue_pop
int
cf_queue_pop(cf_queue *q, void *buf, int ms_wait)
{
	if (NULL == q) {
		cf_error("cf_queue_pop: try passing in a queue");
		return(-1);
	}

#ifdef EXTERNAL_LOCKS 
	if (ms_wait != CF_QUEUE_NOWAIT) {   // this implementation won't wait
		cf_error("cf_queue_pop: only nowait supported");
		return(-1);
	}
#endif // EXTERNAL_LOCKS

	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
	}

	/* FIXME error checking */
	/* Note that we apparently have to use a while() loop.  Careful reading
	 * of the pthread_cond_signal() documentation says that AT LEAST ONE
	 * waiting thread will be awakened... */
	if (q->threadsafe) {
#ifdef EXTERNAL_LOCKS
		if (CF_Q_EMPTY(q)) {
			QUEUE_UNLOCK(q);
			return(CF_QUEUE_EMPTY);
		}
#else
		while (CF_Q_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_EMPTY(q)) {
					pthread_mutex_unlock(&q->LOCK);
					return(CF_QUEUE_EMPTY);
				}
			}
		}
#endif // EXTERNAL_LOCKS
	} else if (CF_Q_EMPTY(q))
		return(CF_QUEUE_EMPTY);

	memcpy(buf, CF_Q_ELEM_PTR(q,q->read_offset), q->elementsz);
	q->read_offset++;
	
	// interesting idea - this probably keeps the cache fresher
	// because the queue is fully empty just make it all zero
	if (q->read_offset == q->write_offset) {
		q->read_offset = q->write_offset = 0;
	}

	QUEUE_UNLOCK(q);

	return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0xcd9e
cmpb $0x1, (%rdi)
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
jne 0xcd0a
leaq 0x18(%rbx), %rdi
callq 0x23b0
testl %r15d, %r15d
jle 0xcd6f
leaq 0x8(%rsp), %r12
xorl %edi, %edi
movq %r12, %rsi
callq 0x2230
movl %r15d, %eax
movl %r15d, %edx
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq (%r12), %rax
shrq $0x26, %rcx
addq %rcx, %rax
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %edx
movq %rax, (%r12)
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
addq 0x8(%r12), %rcx
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
movq %rcx, 0x8(%r12)
jl 0xcd6f
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0xcdcf
movl 0xc(%rbx), %eax
cmpl %eax, 0x8(%rbx)
jne 0xce18
leaq 0x40(%rbx), %r13
leaq 0x18(%rbx), %r12
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl $-0x1, %r15d
je 0xce03
testl %r15d, %r15d
je 0xce5c
jmp 0xcde1
leaq 0x9523(%rip), %rax      # 0x162c8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xce64
leaq 0x9516(%rip), %rax      # 0x162d0
leaq 0x39f9(%rip), %rsi      # 0x107ba
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xce64
movl 0xc(%rbx), %eax
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl %eax, 0x8(%rbx)
jne 0xce18
jmp 0xce64
leaq 0x8(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2410
movl 0x8(%rbx), %ecx
movl 0xc(%rbx), %eax
cmpl %eax, %ecx
jne 0xce14
jmp 0xce5c
cmpl $-0x1, %r15d
jne 0xcde1
movq %r13, %rdi
movq %r12, %rsi
callq 0x2420
movl 0x8(%rbx), %ecx
movl 0xc(%rbx), %eax
cmpl %eax, %ecx
je 0xcdfd
xorl %edx, %edx
movq %r14, %rdi
xorl %ebp, %ebp
divl 0x4(%rbx)
movq 0x10(%rbx), %rax
imulq %rax, %rdx
addq 0x70(%rbx), %rdx
movq %rdx, %rsi
movq %rax, %rdx
callq 0x21b0
movl 0xc(%rbx), %eax
incl %eax
movl %eax, 0xc(%rbx)
cmpl 0x8(%rbx), %eax
jne 0xce4e
movq $0x0, 0x8(%rbx)
cmpb $0x1, (%rbx)
jne 0xce64
addq $0x18, %rbx
xorl %ebp, %ebp
movq %rbx, %r12
movq %r12, %rdi
callq 0x2190
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq