// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movl %edx, 0x1c(%rsp)
xorl %eax, %eax
cmpq 0x28(%rsp), %rax
jne 0x145de
leaq 0x9d26(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x145d1
leaq 0x9d1f(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x5254(%rip), %rsi      # 0x19821
movb $0x0, %al
callq *%rcx
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
jmp 0x147e1
movq 0x28(%rsp), %rax
testb $0x1, (%rax)
je 0x145f6
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
callq 0x23e0
cmpl $0x0, 0x1c(%rsp)
jle 0x1466a
xorl %edi, %edi
leaq 0x8(%rsp), %rsi
callq 0x2260
movl 0x1c(%rsp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cltq
addq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x1c(%rsp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
imull $0xf4240, %edx, %eax    # imm = 0xF4240
cltq
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x3b9aca00, 0x10(%rsp) # imm = 0x3B9ACA00
jle 0x14668
movq 0x10(%rsp), %rax
subq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x8(%rsp)
jmp 0x1466a
movq 0x28(%rsp), %rax
testb $0x1, (%rax)
je 0x1472b
jmp 0x1467a
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x14729
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x1c(%rsp), %eax
jne 0x146b4
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
movq 0x28(%rsp), %rsi
addq $0x18, %rsi
callq 0x2450
jmp 0x14724
xorl %eax, %eax
cmpl 0x1c(%rsp), %eax
jne 0x146d7
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
callq 0x21c0
movl $0xfffffffe, 0x34(%rsp) # imm = 0xFFFFFFFE
jmp 0x147e1
movq 0x28(%rsp), %rdi
addq $0x40, %rdi
movq 0x28(%rsp), %rsi
addq $0x18, %rsi
leaq 0x8(%rsp), %rdx
callq 0x2440
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x14720
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
callq 0x21c0
movl $0xfffffffe, 0x34(%rsp) # imm = 0xFFFFFFFE
jmp 0x147e1
jmp 0x14722
jmp 0x14724
jmp 0x1467a
jmp 0x1474c
movq 0x28(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x1474a
movl $0xfffffffe, 0x34(%rsp) # imm = 0xFFFFFFFE
jmp 0x147e1
jmp 0x1474c
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x70(%rax), %rsi
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
movl %edx, %eax
movq 0x28(%rsp), %rcx
imulq 0x10(%rcx), %rax
addq %rax, %rsi
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdx
callq 0x21e0
movq 0x28(%rsp), %rax
movl 0xc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xc(%rax)
movq 0x28(%rsp), %rax
movl 0xc(%rax), %eax
movq 0x28(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x147c1
movq 0x28(%rsp), %rax
movl $0x0, 0x8(%rax)
movq 0x28(%rsp), %rax
movl $0x0, 0xc(%rax)
movq 0x28(%rsp), %rax
testb $0x1, (%rax)
je 0x147d9
movq 0x28(%rsp), %rdi
addq $0x18, %rdi
callq 0x21c0
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw (%rax,%rax)