// File: /workspace/github_c_makefile_star2_O0/aerospike[P]aerospike-common/src/main/citrusleaf/cf_queue.c
// Function: cf_queue_reduce_pop
int
cf_queue_reduce_pop(cf_queue *q, void *buf, int ms_wait, cf_queue_reduce_fn cb,
		void *udata)
{
	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;
	}

	uint32_t best_index = q->read_offset;

	for (uint32_t i = q->read_offset; i < q->write_offset; i++) {
		int rv = cb(CF_Q_ELEM_PTR(q, i), udata);

		if (rv == 0) {
			continue;
		}

		if (rv == -1) {
			// Found what it was looking for, so break.
			best_index = i;
			break;
		}

		if (rv == -2) {
			// Found new candidate, but keep looking for a better one.
			best_index = i;
		}
	}

	memcpy(buf, CF_Q_ELEM_PTR(q, best_index), q->element_sz);
	cf_queue_delete_offset(q, best_index);

	cf_queue_unlock(q);

	return CF_QUEUE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
cmpl $0x0, -0x1c(%rbp)
jle 0x2613d
movl -0x1c(%rbp), %edi
leaq -0x40(%rbp), %rsi
callq 0x25e50
movq -0x10(%rbp), %rdi
callq 0x25260
movq -0x10(%rbp), %rax
testb $0x1, (%rax)
je 0x261f7
jmp 0x26155
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x261f5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x1c(%rbp), %eax
jne 0x2618a
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movq -0x10(%rbp), %rsi
addq $0x20, %rsi
callq 0x8240
jmp 0x261f0
xorl %eax, %eax
cmpl -0x1c(%rbp), %eax
jne 0x261aa
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x8740
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x262ea
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movq -0x10(%rbp), %rsi
addq $0x20, %rsi
leaq -0x40(%rbp), %rdx
callq 0x8a10
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x261ec
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
callq 0x8740
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x262ea
jmp 0x261ee
jmp 0x261f0
jmp 0x26155
jmp 0x26215
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x26213
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x262ea
jmp 0x26215
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x2629b
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
movq -0x58(%rbp), %rax
movl %edx, %ecx
movq -0x10(%rbp), %rdx
imulq 0x18(%rdx), %rcx
addq %rcx, %rdi
movq -0x30(%rbp), %rsi
callq *%rax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x26274
jmp 0x26290
cmpl $-0x1, -0x4c(%rbp)
jne 0x26282
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x2629b
cmpl $-0x2, -0x4c(%rbp)
jne 0x2628e
movl -0x48(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x26290
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x26229
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rsi
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
xorl %edx, %edx
divl 0x4(%rcx)
movl %edx, %eax
movq -0x10(%rbp), %rcx
imulq 0x18(%rcx), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x86b0
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x8380
movq -0x10(%rbp), %rdi
callq 0x25420
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)