// File: /workspace/github_c_makefile_star2_O2/aerospike[P]aerospike-common/src/main/citrusleaf/cf_queue.c
// Function: cf_queue_push_index
int
cf_queue_push_index(cf_queue *q, const void *ptr, uint32_t ix)
{
	cf_queue_lock(q);

	uint32_t size = CF_Q_SZ(q);

	if (size == q->alloc_sz) {
		if (0 != cf_queue_resize(q, q->alloc_sz * 2)) {
			cf_queue_unlock(q);
			return CF_QUEUE_ERR;
		}
	}

	if (ix >= size) { // off the end - put element at tail
		memcpy(CF_Q_ELEM_PTR(q, q->write_offset), ptr, q->element_sz);
		q->write_offset++;
	}
	else if (ix > size / 2) { // nearer tail - move tail
		uint8_t *wp = CF_Q_ELEM_PTR(q, (q->read_offset + ix));
		uint8_t *tp = CF_Q_ELEM_PTR(q, q->write_offset);

		if (tp > wp) {
			memmove(wp + q->element_sz, wp, tp - wp);
		}
		else { // tp < wp - can't be equal
			memmove(q->elements + q->element_sz, q->elements, tp - q->elements);

			uint8_t *ep = q->elements + ((q->alloc_sz - 1) * q->element_sz);

			memcpy(q->elements, ep, q->element_sz);
			memmove(wp + q->element_sz, wp, ep - wp);
		}

		memcpy(wp, ptr, q->element_sz);
		q->write_offset++;
	}
	else { // nearer head - move head
		if (q->read_offset == 0) {
			q->read_offset += q->alloc_sz;
			q->write_offset += q->alloc_sz;
		}

		q->read_offset--;

		uint8_t *wp = CF_Q_ELEM_PTR(q, (q->read_offset + ix));
		uint8_t *hp = CF_Q_ELEM_PTR(q, q->read_offset);

		if (hp <= wp) {
			memmove(hp, hp + q->element_sz, wp - hp);
		}
		else { // hp > wp
			uint8_t *ep = q->elements + ((q->alloc_sz - 1) * q->element_sz);

			memmove(hp, hp + q->element_sz, ep - hp);
			memcpy(ep, q->elements, q->element_sz);
			memmove(q->elements, q->elements + q->element_sz, wp - q->elements);
		}

		memcpy(wp, ptr, q->element_sz);
	}

	q->n_eles++;
	cf_queue_unwrap(q);

	if (q->threadsafe) {
		pthread_cond_signal(&q->CV);
	}

	cf_queue_unlock(q);
	return CF_QUEUE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
callq 0x143df
movl 0xc(%rbx), %r12d
subl 0x8(%rbx), %r12d
cmpl 0x4(%rbx), %r12d
jne 0x145b7
leal (%r12,%r12), %esi
movq %rbx, %rdi
callq 0x143ee
testl %eax, %eax
je 0x145b7
pushq $-0x1
popq %rbp
jmp 0x14730
cmpl %r15d, %r12d
jbe 0x1460f
shrl %r12d
cmpl %r15d, %r12d
jae 0x1462a
addl 0x8(%rbx), %r15d
movl 0x4(%rbx), %r9d
movl 0xc(%rbx), %ecx
xorl %edx, %edx
movq 0x78(%rbx), %rsi
movl %r15d, %eax
divl %r9d
movq 0x18(%rbx), %rdi
movl %ecx, %eax
movl %edx, %r8d
xorl %edx, %edx
divl %r9d
imulq %rdi, %rdx
imulq %rdi, %r8
movq %rdx, %rax
leaq (%rsi,%r8), %r15
subq %r8, %rax
jle 0x146ac
addq %r15, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x146e2
movl 0xc(%rbx), %eax
xorl %edx, %edx
divl 0x4(%rbx)
movl %edx, %edi
movq 0x18(%rbx), %rdx
imulq %rdx, %rdi
addq 0x78(%rbx), %rdi
jmp 0x146ee
movl 0x8(%rbx), %eax
movl 0x4(%rbx), %ecx
testl %eax, %eax
jne 0x14639
addl %ecx, 0xc(%rbx)
movl %ecx, %eax
leal -0x1(%rax), %edi
leal -0x1(%rax,%r15), %eax
xorl %edx, %edx
movl %edi, 0x8(%rbx)
movq 0x18(%rbx), %rsi
movq 0x78(%rbx), %r12
divl %ecx
movl %edi, %eax
movl %edx, %r8d
xorl %edx, %edx
divl %ecx
imulq %rsi, %r8
imulq %rsi, %rdx
leaq (%r12,%r8), %r15
leaq (%r12,%rdx), %rdi
subq %rdx, %r8
jge 0x146fb
decl %ecx
imulq %rsi, %rcx
addq %rdi, %rsi
addq %rcx, %r12
subq %rdx, %rcx
movq %rcx, %rdx
callq 0x88f0
movq 0x18(%rbx), %rdx
movq 0x78(%rbx), %rsi
movq %r12, %rdi
callq 0x8630
movq 0x78(%rbx), %rdi
movq 0x18(%rbx), %rsi
movq %r15, %rdx
addq %rdi, %rsi
subq %rdi, %rdx
jmp 0x14701
addq %rsi, %rdi
callq 0x88f0
movl 0x4(%rbx), %r12d
movq 0x18(%rbx), %rdx
movq 0x78(%rbx), %rdi
decl %r12d
imulq %rdx, %r12
addq %rdi, %r12
movq %r12, %rsi
callq 0x8630
movq 0x18(%rbx), %rdi
subq %r15, %r12
movq %r15, %rsi
movq %r12, %rdx
addq %r15, %rdi
callq 0x88f0
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x8630
incl 0xc(%rbx)
jmp 0x14715
addq %rdi, %rsi
movq %r8, %rdx
callq 0x88f0
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x8630
incl 0x10(%rbx)
movq %rbx, %rdi
callq 0x144cd
xorl %ebp, %ebp
cmpb $0x1, (%rbx)
jne 0x14730
leaq 0x48(%rbx), %rdi
callq 0x8510
movq %rbx, %rdi
callq 0x144be
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq