// File: /workspace/github_c_makefile_star2_O3/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
cmpb $0x1, (%rdi)
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
jne 0x16860
leaq 0x20(%rbx), %rdi
callq 0x8ad0
movl 0xc(%rbx), %r12d
subl 0x8(%rbx), %r12d
cmpl 0x4(%rbx), %r12d
jne 0x16891
leal (%r12,%r12), %esi
movq %rbx, %rdi
callq 0x166aa
testl %eax, %eax
je 0x16891
cmpb $0x0, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x16a2b
jmp 0x16a37
cmpl %r15d, %r12d
jbe 0x168e9
shrl %r12d
cmpl %r15d, %r12d
jae 0x16904
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 0x16986
addq %r15, %rdi
movq %r15, %rsi
movq %rax, %rdx
jmp 0x169bc
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 0x169c8
movl 0x8(%rbx), %eax
movl 0x4(%rbx), %ecx
testl %eax, %eax
jne 0x16913
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 0x169da
decl %ecx
imulq %rsi, %rcx
addq %rdi, %rsi
addq %rcx, %r12
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
movq %r15, %rdx
addq %rdi, %rsi
subq %rdi, %rdx
jmp 0x169e0
addq %rsi, %rdi
callq 0x8850
movl 0x4(%rbx), %r12d
movq 0x18(%rbx), %rdx
movq 0x78(%rbx), %rdi
decl %r12d
imulq %rdx, %r12
addq %rdi, %r12
movq %r12, %rsi
callq 0x85c0
movq 0x18(%rbx), %rdi
subq %r15, %r12
movq %r15, %rsi
movq %r12, %rdx
addq %r15, %rdi
callq 0x8850
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x85c0
movl 0xc(%rbx), %ecx
incl %ecx
movl %ecx, 0xc(%rbx)
jmp 0x169f7
addq %rdi, %rsi
movq %r8, %rdx
callq 0x8850
movq 0x18(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x85c0
movl 0xc(%rbx), %ecx
incl 0x10(%rbx)
cmpl $0x40000000, %ecx       # imm = 0x40000000
jb 0x16a14
movl 0x8(%rbx), %eax
xorl %edx, %edx
subl %eax, %ecx
divl 0x4(%rbx)
addl %edx, %ecx
movl %edx, 0x8(%rbx)
movl %ecx, 0xc(%rbx)
movb (%rbx), %al
cmpb $0x1, %al
jne 0x16a25
leaq 0x48(%rbx), %rdi
callq 0x84e0
movb (%rbx), %al
xorl %ebp, %ebp
testb $0x1, %al
je 0x16a37
addq $0x20, %rbx
movq %rbx, %rdi
callq 0x8630
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq