// File: /workspace/github_c_makefile_star2_O3/aerospike[P]aerospike-common/src/main/citrusleaf/cf_queue.c
// Function: cf_queue_pop
int
cf_queue_pop(cf_queue *q, void *buf, int ms_wait)
{
	struct timespec tp;

	if (ms_wait > 0) {
		cf_set_wait_timespec(ms_wait, &tp);
	}

	cf_queue_lock(q);

	if (q->threadsafe) {

		// Note that we have to use a while() loop. The pthread_cond_signal()
		// documentation says that AT LEAST ONE waiting thread will be awakened.
		// If more than one are awakened, the first will get the popped element,
		// others will find the queue empty and go back to waiting.

		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;
				}
			}
		}
	}
	else if (CF_Q_EMPTY(q)) {
		return CF_QUEUE_EMPTY;
	}

	memcpy(buf, CF_Q_ELEM_PTR(q, q->read_offset), q->element_sz);
	q->read_offset++;
	q->n_eles--;

	// This probably keeps the cache fresher because the queue is fully empty.
	if (q->read_offset == q->write_offset) {
		q->read_offset = q->write_offset = 0;
	}

	cf_queue_unlock(q);
	return CF_QUEUE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
testl %edx, %edx
jle 0x16d14
leaq 0x8(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x81d0
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 0x16d14
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movb (%rbx), %al
cmpb $0x1, %al
jne 0x16d25
leaq 0x20(%rbx), %rdi
callq 0x8ad0
movb (%rbx), %al
testb $0x1, %al
je 0x16d4e
movl 0x8(%rbx), %eax
cmpl %eax, 0xc(%rbx)
jne 0x16d97
leaq 0x48(%rbx), %r13
leaq 0x20(%rbx), %r12
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl $-0x1, %ebp
je 0x16d82
testl %ebp, %ebp
je 0x16de0
jmp 0x16d61
movl 0x8(%rbx), %eax
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
cmpl %eax, 0xc(%rbx)
jne 0x16d97
jmp 0x16de8
leaq 0x8(%rsp), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x88c0
movl 0x8(%rbx), %eax
movl 0xc(%rbx), %ecx
cmpl %eax, %ecx
jne 0x16d93
jmp 0x16de0
cmpl $-0x1, %ebp
jne 0x16d61
movq %r13, %rdi
movq %r12, %rsi
callq 0x8200
movl 0x8(%rbx), %eax
movl 0xc(%rbx), %ecx
cmpl %eax, %ecx
je 0x16d7d
xorl %edx, %edx
movq %r14, %rdi
xorl %r15d, %r15d
divl 0x4(%rbx)
movq 0x18(%rbx), %rax
imulq %rax, %rdx
addq 0x78(%rbx), %rdx
movq %rdx, %rsi
movq %rax, %rdx
callq 0x85c0
movl 0x8(%rbx), %eax
incl %eax
movl %eax, 0x8(%rbx)
decl 0x10(%rbx)
cmpl 0xc(%rbx), %eax
jne 0x16dd1
movq $0x0, 0x8(%rbx)
cmpb $0x1, (%rbx)
jne 0x16de8
addq $0x20, %rbx
xorl %r15d, %r15d
movq %rbx, %r12
movq %r12, %rdi
callq 0x8630
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq