// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movq %rcx, %r12
movl %edx, %ebp
movq %rdi, %rbx
testl %edx, %edx
movq %rsi, 0x10(%rsp)
jle 0x16fa0
leaq 0x18(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x81d0
movl %ebp, %eax
movl %ebp, %edx
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq (%r14), %rax
shrq $0x26, %rcx
addq %rcx, %rax
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %edx
movq %rax, (%r14)
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
addq 0x8(%r14), %rcx
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
movq %rcx, 0x8(%r14)
jl 0x16fa0
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
incq %rax
movq %rcx, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movb (%rbx), %al
cmpb $0x1, %al
jne 0x16fb1
leaq 0x20(%rbx), %rdi
callq 0x8ad0
movb (%rbx), %al
testb $0x1, %al
movq %r15, 0x8(%rsp)
je 0x16fe5
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %eax
cmpl %r14d, %eax
jne 0x1703b
leaq 0x48(%rbx), %r15
leaq 0x20(%rbx), %r13
cmpl $-0x1, %ebp
movl $0xfffffffe, 0x4(%rsp)  # imm = 0xFFFFFFFE
je 0x17024
testl %ebp, %ebp
je 0x170c7
jmp 0x16ffe
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %eax
movl $0xfffffffe, 0x4(%rsp)  # imm = 0xFFFFFFFE
cmpl %r14d, %eax
jne 0x1703b
jmp 0x170cf
leaq 0x18(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x88c0
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %eax
cmpl %r14d, %eax
jne 0x17036
jmp 0x170c7
cmpl $-0x1, %ebp
jne 0x16ffe
movq %r15, %rdi
movq %r13, %rsi
callq 0x8200
movl 0x8(%rbx), %r14d
movl 0xc(%rbx), %eax
cmpl %r14d, %eax
je 0x1701f
cmpl %eax, %r14d
jae 0x1707b
movq 0x8(%rsp), %r15
movl %r14d, %ebp
movl %r14d, %eax
xorl %edx, %edx
movq %r15, %rsi
divl 0x4(%rbx)
movl %edx, %edi
imulq 0x18(%rbx), %rdi
addq 0x78(%rbx), %rdi
callq *%r12
cmpl $-0x2, %eax
je 0x1706d
cmpl $-0x1, %eax
jne 0x17070
jmp 0x1707b
movl %r14d, %ebp
incl %r14d
cmpl 0xc(%rbx), %r14d
jb 0x17048
jmp 0x1707e
movl %r14d, %ebp
movl $0x0, 0x4(%rsp)
movl %ebp, %eax
xorl %edx, %edx
divl 0x4(%rbx)
movq 0x18(%rbx), %rax
movq 0x10(%rsp), %rdi
imulq %rax, %rdx
addq 0x78(%rbx), %rdx
movq %rdx, %rsi
movq %rax, %rdx
callq 0x85c0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x8320
cmpb $0x1, (%rbx)
jne 0x170cf
addq $0x20, %rbx
movl $0x0, 0x4(%rsp)
movq %rbx, %r13
movq %r13, %rdi
callq 0x8630
movl 0x4(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq