// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %ecx, 0x4(%rsp)
cmpl %edx, 0x54(%rdi)
jl 0x2487
movq %rdi, %rbx
movl 0x5c(%rdi), %edi
movl %edx, %ecx
shll %cl, %edi
leal -0x1(%rsi), %eax
movl %eax, 0x1c(%rsp)
movq %rdi, 0x10(%rsp)
leal (%rsi,%rdi), %ebp
decl %ebp
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
leaq 0x18(%rbx), %r15
movslq %ebp, %r12
movslq %esi, %r14
leaq (,%r12,4), %r13
movl $0x0, 0x8(%rsp)
cmpq %r14, %r12
jl 0x23cd
movq %r15, %rdi
movq %r12, %rsi
callq 0x1e08
testq %rdx, (%rax)
je 0x23c2
movq (%rbx), %rsi
testb $0x1, 0x8(%rsp)
jne 0x23ab
movl (%rsi,%r12,4), %eax
cmpl 0x4(%rsp), %eax
jge 0x23ab
leaq 0x20(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x2892
movq (%rbx), %rsi
movb $0x1, %al
movl %eax, 0x8(%rsp)
addq %r13, %rsi
leaq 0x20(%rsp), %rdi
callq 0x2892
movq %rbx, %rdi
movl %ebp, %esi
callq 0x28b0
decq %r12
addq $-0x4, %r13
decl %ebp
jmp 0x236a
testb $0x1, 0x8(%rsp)
jne 0x23e3
leaq 0x20(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x2892
cvtsi2sdl 0x10(%rsp), %xmm2
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0xc2c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xc34(%rip), %xmm0      # 0x3040
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
movl %eax, %r14d
decq %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
testl %r14d, %r14d
js 0x246e
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
cvttsd2si %xmm0, %edx
addl 0x1c(%rsp), %edx
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rcx
movl (%rcx,%rax,4), %esi
decq %r14
movq %rbx, %rdi
callq 0x15a8
jmp 0x2432
leaq 0x20(%rsp), %rdi
callq 0x1e36
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc08(%rip), %rdi       # 0x3096
leaq 0xc0c(%rip), %rsi       # 0x30a1
leaq 0xc0f(%rip), %rcx       # 0x30ab
movl $0x185, %edx            # imm = 0x185
callq 0x1090
jmp 0x24ac
jmp 0x24ac
jmp 0x24ac
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x1e36
movq %rbx, %rdi
callq 0x1160
nop