// File: /workspace/github_cpp_makefile_star2_O1/reddragon[P]packed-memory-array/impl1.cpp
// Function: PackedMemoryArray<int>::rebalance(int, int)
void PackedMemoryArray<E>::rebalance(int index, int level) {
#ifndef OPTIMIZE 
    assert(level <= l);
#endif
    int c = CAPACITY_AT(level);
    // Move all the elements to one side
    int last = index + c - 1, count = 0;
    for(int i = last; i >= index; i--) {
        if(ELEM_EXISTS_AT(i)) {
            if(i != last) {
                // Copy the element to the leftmost position
                insert_element_at(store[i], last);
                // Delete the original copy of the element
                #ifndef OPTIMIZE
                    delete_element_at(i);
                #else
                    exists[i] = 0;
                #endif
               // Update the leftmost pointer, and count of elements moved
            }
            --last;
            count++;
        }
    }

    // Now copy
    double k = (c*1.0)/count, p = 0;
    int actual_index = last, correct_index;
    for(int i = 0; i < count; i++) {
        p += k;
        actual_index++;
        // Now insert the element at the right position
        correct_index = index + (int)p - 1;
        if (correct_index == actual_index)
            continue;
        if(actual_index != correct_index)
            insert_element_at(store[actual_index], correct_index);
        // Remove the left most copy
#ifndef OPTIMIZE
        delete_element_at(actual_index);
#else
        exists[actual_index] = 0;
#endif
    } 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edx, %ecx
movl 0x5c(%rdi), %eax
shll %cl, %eax
movabsq $-0x8000000000000000, %r8 # imm = 0x8000000000000000
movq %rax, -0x10(%rsp)
addl %esi, %eax
decl %eax
xorl %r9d, %r9d
cmpl %esi, %eax
movq %rdi, -0x18(%rsp)
jl 0x29c1
movq (%rdi), %rcx
movq %rcx, -0x8(%rsp)
movq 0x18(%rdi), %rbx
movslq %eax, %rdx
movslq %esi, %r14
xorl %r9d, %r9d
leaq 0x3f(%r8), %r15
leaq 0x3f(%rdx), %rbp
testq %rdx, %rdx
cmovnsq %rdx, %rbp
sarq $0x6, %rbp
movq %rdx, %rcx
andq %r15, %rcx
xorl %r12d, %r12d
cmpq %r8, %rcx
setbe %r12b
movl $0x1, %r13d
movl %edx, %ecx
shlq %cl, %r13
leaq (%rbx,%rbp,8), %rbp
movq -0x8(%rbp,%r12,8), %rcx
btq %rdx, %rcx
jae 0x29b1
cmpl %eax, %edx
je 0x29ac
movq -0x8(%rsp), %rdi
movl (%rdi,%rdx,4), %ecx
movslq %eax, %r10
leaq 0x3f(%r10), %r11
testq %r10, %r10
cmovnsq %r10, %r11
movl %ecx, (%rdi,%r10,4)
sarq $0x6, %r11
andq %r15, %r10
xorl %edi, %edi
cmpq %r8, %r10
setbe %dil
movl $0x1, %r10d
movl %eax, %ecx
shlq %cl, %r10
leaq (%rbx,%r11,8), %rcx
orq %r10, -0x8(%rcx,%rdi,8)
leaq -0x8(,%r12,8), %rcx
movq -0x18(%rsp), %rdi
incl 0x58(%rdi)
notq %r13
andq %r13, (%rbp,%rcx)
decl %eax
incl %r9d
leaq -0x1(%rdx), %rcx
cmpq %r14, %rdx
movq %rcx, %rdx
jg 0x2919
testl %r9d, %r9d
movq -0x18(%rsp), %r15
jle 0x2a79
cvtsi2sdl -0x10(%rsp), %xmm0
cvtsi2sd %r9d, %xmm1
divsd %xmm1, %xmm0
decl %esi
movq (%r15), %rdx
movq 0x18(%r15), %r10
cltq
incq %rax
xorpd %xmm1, %xmm1
leaq 0x3f(%r8), %r11
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
addl %esi, %ecx
cmpl %ecx, %eax
je 0x2a6d
movl (%rdx,%rax,4), %edi
movslq %ecx, %rbx
leaq 0x3f(%rbx), %r14
testq %rbx, %rbx
cmovnsq %rbx, %r14
movl %edi, (%rdx,%rbx,4)
sarq $0x6, %r14
andq %r11, %rbx
xorl %edi, %edi
cmpq %r8, %rbx
setbe %dil
movl $0x1, %ebx
shlq %cl, %rbx
leaq (%r10,%r14,8), %rcx
orq %rbx, -0x8(%rcx,%rdi,8)
incl 0x58(%r15)
leaq 0x3f(%rax), %rdi
testq %rax, %rax
cmovnsq %rax, %rdi
sarq $0x6, %rdi
movq %rax, %rcx
andq %r11, %rcx
xorl %ebx, %ebx
cmpq %r8, %rcx
setbe %bl
movq $-0x2, %r14
movl %eax, %ecx
rolq %cl, %r14
leaq (%r10,%rdi,8), %rcx
andq %r14, -0x8(%rcx,%rbx,8)
incq %rax
decl %r9d
jne 0x29f4
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq