// File: /workspace/github_cpp_makefile_star2_O1/reddragon[P]packed-memory-array/impl1.cpp
// Function: PackedMemoryArray<int>::rebalance(int, int, int)
void PackedMemoryArray<E>::rebalance(int index, int level, E e) {
#ifndef OPTIMZE
    assert(level <= l);
#endif
    int c = CAPACITY_AT(level);
    // Move all the elements to one side
    int last = index + c - 1, count = 0;
    bool element_inserted = false;
    std::vector<E> level_copy;
    for(int i = last; i >= index; i--) {
        if(ELEM_EXISTS_AT(i)) {
            if(!element_inserted && store[i] < e) {
                level_copy.push_back(e);
                element_inserted = true;
            }
            level_copy.push_back(store[i]);
            delete_element_at(i);
            --last;
            count++;
        }
    }

    if(!element_inserted)
        level_copy.push_back(e);

    // Now copy
    double k = (c*1.0)/(level_copy.size()), p = 0;
    int correct_index;
    for(int i = level_copy.size()-1; i >= 0; i--) {
        p += k;
        // Now insert the element at the right position
        correct_index = index + (int)p - 1;
        insert_element_at(level_copy[i], correct_index);
    } 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, 0xc(%rsp)
cmpl %edx, 0x54(%rdi)
jl 0x2713
movl %esi, %ebx
movq %rdi, %r14
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
movl 0x5c(%rdi), %r15d
movl %edx, %ecx
shll %cl, %r15d
leal (%rbx,%r15), %eax
decl %eax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl %esi, %eax
jl 0x2618
movq %r15, 0x38(%rsp)
movslq %eax, %r15
movq %rbx, 0x40(%rsp)
movslq %ebx, %rax
movq %rax, 0x48(%rsp)
leaq (,%r15,4), %r13
movl $0x0, 0x8(%rsp)
leaq 0x3f(%rbp), %rax
movq %rax, 0x50(%rsp)
movq %r14, 0x30(%rsp)
leaq 0x3f(%r15), %r12
testq %r15, %r15
cmovnsq %r15, %r12
sarq $0x6, %r12
shlq $0x3, %r12
movq 0x18(%r14), %rax
addq %r12, %rax
movq %r15, %rcx
andq 0x50(%rsp), %rcx
xorl %ebx, %ebx
movq %rbp, %r14
cmpq %rbp, %rcx
setbe %bl
movl $0x1, %ebp
movl %r15d, %ecx
shlq %cl, %rbp
movq -0x8(%rax,%rbx,8), %rax
btq %r15, %rax
jae 0x25e9
testb $0x1, 0x8(%rsp)
jne 0x25a1
movq 0x30(%rsp), %rax
movq (%rax), %rcx
movl 0xc(%rsp), %eax
cmpl %eax, (%rcx,%r15,4)
jge 0x25a1
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x258c
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x25a1
movb $0x1, %al
movl %eax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x2b08
movq 0x30(%rsp), %rax
movq (%rax), %rdx
addq %r13, %rdx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x25c7
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x25d1
leaq 0x10(%rsp), %rdi
callq 0x2b08
leaq -0x8(,%rbx,8), %rax
movq 0x30(%rsp), %rcx
addq 0x18(%rcx), %r12
notq %rbp
andq %rbp, (%rax,%r12)
leaq -0x1(%r15), %rax
addq $-0x4, %r13
cmpq 0x48(%rsp), %r15
movq %rax, %r15
movq %r14, %rbp
movq 0x30(%rsp), %r14
jg 0x250d
testb $0x1, 0x8(%rsp)
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r15
jne 0x2644
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2635
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2644
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x2b08
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
movl %eax, %ecx
decl %ecx
js 0x26f2
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movq %rax, %xmm1
punpckldq 0x9ac(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x9b4(%rip), %xmm1      # 0x3030
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
decl %ebx
movq (%r14), %rax
movq 0x18(%r14), %rdx
movl %ecx, %esi
incq %rsi
xorpd %xmm1, %xmm1
leaq 0x3f(%rbp), %r8
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %ecx
addl %ebx, %ecx
movl -0x4(%rdi,%rsi,4), %r9d
movslq %ecx, %rcx
movl %r9d, (%rax,%rcx,4)
leaq 0x3f(%rcx), %r9
testq %rcx, %rcx
cmovnsq %rcx, %r9
sarq $0x6, %r9
movq %rcx, %r10
andq %r8, %r10
xorl %r11d, %r11d
cmpq %rbp, %r10
setbe %r11b
movl $0x1, %r10d
shlq %cl, %r10
leaq (%rdx,%r9,8), %rcx
orq %r10, -0x8(%rcx,%r11,8)
incl 0x58(%r14)
decq %rsi
jg 0x26a2
testq %rdi, %rdi
je 0x2704
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10f0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x96c(%rip), %rdi       # 0x3086
leaq 0x970(%rip), %rsi       # 0x3091
leaq 0x973(%rip), %rcx       # 0x309b
movl $0x185, %edx            # imm = 0x185
callq 0x1090
jmp 0x2734
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x274e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1160