// File: /workspace/github_cpp_makefile_star2_O3/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:
movl %edx, %ecx
movl 0x5c(%rdi), %edx
shll %cl, %edx
leal (%rsi,%rdx), %eax
decl %eax
cmpl %esi, %eax
jl 0x2a75
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, -0x8(%rsp)
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq (%rdi), %rbx
movq %rdi, -0x10(%rsp)
movq 0x18(%rdi), %r14
movslq %eax, %rdx
movslq %esi, %r15
decl %esi
xorl %r9d, %r9d
leaq 0x3f(%rcx), %r10
leal 0x3f(%rdx), %ebp
testl %edx, %edx
cmovnsl %edx, %ebp
sarl $0x6, %ebp
movq %rdx, %rcx
andq %r10, %rcx
xorl %r12d, %r12d
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
cmpq %rdi, %rcx
setbe %r12b
movl $0x1, %r13d
movl %edx, %ecx
shlq %cl, %r13
movslq %ebp, %rcx
leaq (%r14,%rcx,8), %rbp
movq -0x8(%rbp,%r12,8), %rcx
btq %rdx, %rcx
jae 0x299b
cmpl %eax, %edx
je 0x2996
movl (%rbx,%rdx,4), %ecx
movslq %eax, %r11
movl %ecx, (%rbx,%r11,4)
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %r8
andq %r10, %r11
xorl %edi, %edi
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
cmpq %rcx, %r11
setbe %dil
movl $0x1, %r11d
movl %eax, %ecx
shlq %cl, %r11
leaq (%r14,%r8,8), %rcx
orq %r11, -0x8(%rcx,%rdi,8)
leaq -0x8(,%r12,8), %rcx
movq -0x10(%rsp), %rdi
incl 0x58(%rdi)
notq %r13
andq %r13, (%rbp,%rcx)
decl %eax
incl %r9d
leaq -0x1(%rdx), %rcx
cmpq %r15, %rdx
movq %rcx, %rdx
jg 0x28f6
testl %r9d, %r9d
movq -0x10(%rsp), %r14
movq -0x8(%rsp), %rcx
jle 0x2a6b
cvtsi2sd %ecx, %xmm0
cvtsi2sd %r9d, %xmm1
divsd %xmm1, %xmm0
movq (%r14), %rdx
movq 0x18(%r14), %r11
cltq
incq %rax
xorpd %xmm1, %xmm1
movabsq $-0x8000000000000000, %r15 # imm = 0x8000000000000000
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
addl %esi, %ecx
cmpl %ecx, %eax
je 0x2a5f
movl (%rdx,%rax,4), %edi
movslq %ecx, %r8
movl %edi, (%rdx,%r8,4)
leal 0x3f(%rcx), %edi
testl %ecx, %ecx
cmovnsl %ecx, %edi
sarl $0x6, %edi
movslq %edi, %rdi
andq %r10, %r8
xorl %ebx, %ebx
cmpq %r15, %r8
setbe %bl
movl $0x1, %r8d
shlq %cl, %r8
leaq (%r11,%rdi,8), %rcx
orq %r8, -0x8(%rcx,%rbx,8)
incl 0x58(%r14)
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rdi
movq %rax, %rcx
andq %r10, %rcx
xorl %r8d, %r8d
cmpq %r15, %rcx
setbe %r8b
movq $-0x2, %rbx
movl %eax, %ecx
rolq %cl, %rbx
leaq (%r11,%rdi,8), %rcx
andq %rbx, -0x8(%rcx,%r8,8)
incq %rax
decl %r9d
jne 0x29e5
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq