// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %r8, %r15
movq %rcx, %r12
movl %edx, %ebp
movq %rsi, (%rsp)
movq %rdi, %rbx
testl %edx, %edx
jle 0x16efd
leaq 0x8(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x81d0
movl %ebp, %eax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
movq (%r14), %rax
addq %rcx, %rax
movq %rax, (%r14)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
movl %ebp, %edx
subl %ecx, %edx
imull $0xf4240, %edx, %ecx    # imm = 0xF4240
addq 0x8(%r14), %rcx
movq %rcx, 0x8(%r14)
cmpq $0x3b9aca01, %rcx       # imm = 0x3B9ACA01
jl 0x16efd
addq $-0x3b9aca00, %rcx      # imm = 0xC4653600
movq %rcx, 0x10(%rsp)
incq %rax
movq %rax, 0x8(%rsp)
cmpb $0x1, (%rbx)
jne 0x16f0b
leaq 0x20(%rbx), %rdi
callq 0x8ad0
cmpb $0x1, (%rbx)
jne 0x16f2b
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
jne 0x16f7b
leaq 0x48(%rbx), %r13
leaq 0x20(%rbx), %r14
cmpl $-0x1, %ebp
je 0x16f68
testl %ebp, %ebp
je 0x16f3e
jmp 0x16f4e
movl 0xc(%rbx), %eax
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
cmpl 0x8(%rbx), %eax
jne 0x16f7b
jmp 0x17023
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
jmp 0x1701b
cmpl $-0x1, %ebp
je 0x16f68
leaq 0x8(%rsp), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x88c0
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
jne 0x16f73
jmp 0x16f3e
movq %r13, %rdi
movq %r14, %rsi
callq 0x8200
movl 0xc(%rbx), %eax
cmpl 0x8(%rbx), %eax
je 0x16f49
movl 0x8(%rbx), %r13d
cmpl 0xc(%rbx), %r13d
jae 0x16fda
movl %r13d, %ebp
movl %r13d, %eax
xorl %edx, %edx
divl 0x4(%rbx)
movl %edx, %edi
imulq 0x18(%rbx), %rdi
addq 0x78(%rbx), %rdi
movq %r15, %rsi
callq *%r12
cmpl $-0x2, %eax
je 0x16fbd
testl %eax, %eax
je 0x16fb6
cmpl $-0x1, %eax
jne 0x16fc4
movl $0x4, %eax
jmp 0x16fbf
movl $0x6, %eax
jmp 0x16fc6
xorl %eax, %eax
movl %r13d, %ebp
jmp 0x16fc6
xorl %eax, %eax
cmpl $0x6, %eax
je 0x16fcf
testl %eax, %eax
jne 0x16fdd
incl %r13d
cmpl 0xc(%rbx), %r13d
jb 0x16f88
jmp 0x16fdd
movl %r13d, %ebp
xorl %r13d, %r13d
movl %ebp, %eax
xorl %edx, %edx
divl 0x4(%rbx)
movq 0x18(%rbx), %rax
imulq %rax, %rdx
addq 0x78(%rbx), %rdx
movq (%rsp), %rdi
movq %rdx, %rsi
movq %rax, %rdx
callq 0x85c0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x8320
cmpb $0x1, (%rbx)
jne 0x17023
addq $0x20, %rbx
xorl %r13d, %r13d
movq %rbx, %r14
movq %r14, %rdi
callq 0x8630
movl %r13d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq