// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x10, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
testl %edx, %edx
jle 0x16c63
movq %rsp, %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x81d0
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 0x16c63
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
movq %rcx, 0x8(%rsp)
incq %rax
movq %rax, (%rsp)
cmpb $0x1, (%rbx)
jne 0x16c71
leaq 0x20(%rbx), %rdi
callq 0x8ad0
cmpb $0x1, (%rbx)
jne 0x16c91
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
jne 0x16cda
leaq 0x48(%rbx), %r12
leaq 0x20(%rbx), %r15
cmpl $-0x1, %ebp
je 0x16cc7
testl %ebp, %ebp
je 0x16ca3
jmp 0x16caf
movl 0xc(%rbx), %eax
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl 0x8(%rbx), %eax
jne 0x16cda
jmp 0x16d2c
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x16d24
cmpl $-0x1, %ebp
je 0x16cc7
movq %rsp, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x88c0
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
jne 0x16cd2
jmp 0x16ca3
movq %r12, %rdi
movq %r15, %rsi
callq 0x8200
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
je 0x16caa
movl 0x8(%rbx), %eax
xorl %ebp, %ebp
xorl %edx, %edx
divl 0x4(%rbx)
movq 0x18(%rbx), %rax
imulq %rax, %rdx
addq 0x78(%rbx), %rdx
movq %r14, %rdi
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 0x16d16
movq $0x0, 0x8(%rbx)
cmpb $0x1, (%rbx)
jne 0x16d2c
addq $0x20, %rbx
xorl %ebp, %ebp
movq %rbx, %r15
movq %r15, %rdi
callq 0x8630
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq