// File: /workspace/github_c_makefile_star2_O2/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 0xc237
cmpb $0x1, (%rdi)
movl %edx, %r15d
movq %rdi, %rbx
movq %rsi, (%rsp)
jne 0xc192
leaq 0x18(%rbx), %rdi
callq 0x23b0
testl %r15d, %r15d
jle 0xc1e3
leaq 0x8(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x2230
movl $0x3e8, %ecx            # imm = 0x3E8
movl %r15d, %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 0xc1e3
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0xc25f
leaq 0x40(%rbx), %r13
leaq 0x18(%rbx), %r12
pushq $-0x2
popq %rbp
leaq 0x8(%rsp), %r14
movl 0xc(%rbx), %eax
cmpl %eax, 0x8(%rbx)
jne 0xc26c
cmpl $-0x1, %r15d
je 0xc22a
testl %r15d, %r15d
je 0xc2ae
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2410
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
jne 0xc1f8
jmp 0xc2ae
movq %r13, %rdi
movq %r12, %rsi
callq 0x2420
jmp 0xc1f8
leaq 0x908a(%rip), %rax      # 0x152c8
pushq $-0x1
popq %rbp
cmpl $0x0, (%rax)
js 0xc2b6
leaq 0x9083(%rip), %rax      # 0x152d0
leaq 0x3576(%rip), %rsi      # 0xf7ca
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xc2b6
movl 0xc(%rbx), %eax
cmpl %eax, 0x8(%rbx)
jne 0xc26c
pushq $-0x2
popq %rbp
jmp 0xc2b6
xorl %edx, %edx
xorl %ebp, %ebp
divl 0x4(%rbx)
movq 0x10(%rbx), %rax
movq (%rsp), %rdi
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 0xc2a0
andq $0x0, 0x8(%rbx)
cmpb $0x1, (%rbx)
jne 0xc2b6
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