// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x10, %rsp
testq %rdi, %rdi
je 0xd189
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x1, (%rdi)
jne 0xd10a
leaq 0x18(%rbx), %rdi
callq 0x23b0
testl %ebp, %ebp
jle 0xd165
movq %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 0xd165
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
movq %rcx, 0x8(%rsp)
incq %rax
movq %rax, (%rsp)
cmpb $0x1, (%rbx)
jne 0xd1ba
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
jne 0xd203
leaq 0x40(%rbx), %r12
leaq 0x18(%rbx), %r15
cmpl $-0x1, %ebp
je 0xd1f0
testl %ebp, %ebp
je 0xd1cc
jmp 0xd1d8
leaq 0xa138(%rip), %rax      # 0x172c8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, (%rax)
js 0xd252
leaq 0xa12b(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x45eb(%rip), %rsi      # 0x1179a
xorl %edi, %edi
xorl %eax, %eax
callq *%rcx
jmp 0xd252
movl 0x8(%rbx), %eax
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl 0xc(%rbx), %eax
jne 0xd203
jmp 0xd252
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0xd24a
cmpl $-0x1, %ebp
je 0xd1f0
movq %rsp, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2410
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
jne 0xd1fb
jmp 0xd1cc
movq %r12, %rdi
movq %r15, %rsi
callq 0x2420
movl 0x8(%rbx), %eax
cmpl 0xc(%rbx), %eax
je 0xd1d3
movl 0xc(%rbx), %eax
xorl %ebp, %ebp
xorl %edx, %edx
divl 0x4(%rbx)
movq 0x10(%rbx), %rax
imulq %rax, %rdx
addq 0x70(%rbx), %rdx
movq %r14, %rdi
movq %rdx, %rsi
movq %rax, %rdx
callq 0x21b0
movl 0xc(%rbx), %eax
incl %eax
movl %eax, 0xc(%rbx)
cmpl 0x8(%rbx), %eax
jne 0xd23c
movq $0x0, 0x8(%rbx)
cmpb $0x1, (%rbx)
jne 0xd252
addq $0x18, %rbx
xorl %ebp, %ebp
movq %rbx, %r15
movq %r15, %rdi
callq 0x2190
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq