// File: /workspace/github_c_makefile_star2_O1/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
cmpb $0x1, (%rdi)
jne 0x1679a
leaq 0x20(%rbx), %rdi
callq 0x8ad0
movl 0x4(%rbx), %esi
movl 0xc(%rbx), %ebp
subl 0x8(%rbx), %ebp
cmpl %esi, %ebp
jne 0x167c8
addl %esi, %esi
movq %rbx, %rdi
callq 0x165f5
testl %eax, %eax
je 0x167c8
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x0, (%rbx)
jne 0x16960
jmp 0x1696c
cmpl %r15d, %ebp
jbe 0x1681f
shrl %ebp
cmpl %r15d, %ebp
jae 0x1683a
movq 0x78(%rbx), %rsi
addl 0x8(%rbx), %r15d
movl 0x4(%rbx), %r9d
movl 0xc(%rbx), %ecx
movl %r15d, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %r8d
movq 0x18(%rbx), %rdi
imulq %rdi, %r8
leaq (%rsi,%r8), %r15
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
imulq %rdi, %rdx
movq %rdx, %rax
subq %r8, %rax
jle 0x168c2
addq %r15, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x168f8
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 0x16904
cmpl $0x0, 0x8(%rbx)
jne 0x16849
movl 0x4(%rbx), %eax
movl %eax, 0x8(%rbx)
addl %eax, 0xc(%rbx)
movl 0x4(%rbx), %ecx
movl 0x8(%rbx), %eax
leal -0x1(%rax), %edi
movl %edi, 0x8(%rbx)
movq 0x78(%rbx), %r12
leal -0x1(%rax,%r15), %eax
xorl %edx, %edx
divl %ecx
movl %edx, %r8d
movq 0x18(%rbx), %rsi
imulq %rsi, %r8
leaq (%r12,%r8), %r15
movl %edi, %eax
xorl %edx, %edx
divl %ecx
imulq %rsi, %rdx
leaq (%r12,%rdx), %rdi
subq %rdx, %r8
jge 0x16911
decl %ecx
imulq %rsi, %rcx
addq %rcx, %r12
addq %rdi, %rsi
subq %rdx, %rcx
movq %rcx, %rdx
callq 0x8850
movq 0x18(%rbx), %rdx
movq 0x78(%rbx), %rsi
movq %r12, %rdi
callq 0x85c0
movq 0x78(%rbx), %rdi
movq 0x18(%rbx), %rsi
addq %rdi, %rsi
movq %r15, %rdx
subq %rdi, %rdx
jmp 0x16917
addq %rsi, %rdi
callq 0x8850
movq 0x78(%rbx), %rdi
movl 0x4(%rbx), %r12d
decl %r12d
movq 0x18(%rbx), %rdx
imulq %rdx, %r12
addq %rdi, %r12
movq %r12, %rsi
callq 0x85c0
movq 0x18(%rbx), %rdi
addq %r15, %rdi
subq %r15, %r12
movq %r15, %rsi
movq %r12, %rdx
callq 0x8850
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x85c0
incl 0xc(%rbx)
jmp 0x1692b
addq %rdi, %rsi
movq %r8, %rdx
callq 0x8850
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x85c0
incl 0x10(%rbx)
movl 0xc(%rbx), %ecx
cmpl $0x40000000, %ecx       # imm = 0x40000000
jb 0x1694b
movl 0x8(%rbx), %eax
subl %eax, %ecx
xorl %edx, %edx
divl 0x4(%rbx)
movl %edx, 0x8(%rbx)
addl %edx, %ecx
movl %ecx, 0xc(%rbx)
cmpb $0x1, (%rbx)
jne 0x16959
leaq 0x48(%rbx), %rdi
callq 0x84e0
xorl %ebp, %ebp
cmpb $0x1, (%rbx)
jne 0x1696c
addq $0x20, %rbx
movq %rbx, %rdi
callq 0x8630
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq