// File: /workspace/github_cpp_makefile_star2_O3/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 0x26cd
movl %esi, %ebx
movq %rdi, %r14
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
movl 0x5c(%rdi), %r15d
movl %edx, %ecx
shll %cl, %r15d
leal (%rbx,%r15), %r12d
decl %r12d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl %esi, %r12d
jl 0x25cb
movq %r15, 0x38(%rsp)
movslq %r12d, %r15
movq %rbx, 0x40(%rsp)
movslq %ebx, %rax
movq %rax, 0x48(%rsp)
movq %r14, 0x30(%rsp)
movq 0x18(%r14), %rax
leaq (,%r15,4), %rbp
movl $0x0, 0x8(%rsp)
leaq 0x3f(%r13), %rcx
movq %rcx, 0x50(%rsp)
leal 0x3f(%r12), %edx
testl %r12d, %r12d
cmovnsl %r12d, %edx
sarl $0x6, %edx
movq %r15, %rcx
andq 0x50(%rsp), %rcx
xorl %r13d, %r13d
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
cmpq %rsi, %rcx
setbe %r13b
movl $0x1, %ebx
movl %r15d, %ecx
shlq %cl, %rbx
movslq %edx, %r14
leaq (%rax,%r14,8), %rcx
movq -0x8(%rcx,%r13,8), %rcx
btq %r15, %rcx
jae 0x2592
movq 0x30(%rsp), %rax
movq (%rax), %rdx
testb $0x1, 0x8(%rsp)
jne 0x254e
movl 0xc(%rsp), %eax
cmpl %eax, (%rdx,%r15,4)
jge 0x254e
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2531
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2548
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x2afa
movq 0x30(%rsp), %rax
movq (%rax), %rdx
movb $0x1, %al
movl %eax, 0x8(%rsp)
addq %rbp, %rdx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x256c
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2576
leaq 0x10(%rsp), %rdi
callq 0x2afa
leaq -0x8(,%r13,8), %rcx
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
leaq (%rax,%r14,8), %rdx
notq %rbx
andq %rbx, (%rcx,%rdx)
leaq -0x1(%r15), %rcx
addq $-0x4, %rbp
decl %r12d
cmpq 0x48(%rsp), %r15
movq %rcx, %r15
jg 0x24b3
testb $0x1, 0x8(%rsp)
movq 0x40(%rsp), %rbx
movq 0x30(%rsp), %r14
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
movq 0x38(%rsp), %r15
jne 0x25f7
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x25e8
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x25f7
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x2afa
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
movl %eax, %ecx
decl %ecx
js 0x26ac
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movq %rax, %xmm1
punpckldq 0xa09(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
decl %ebx
subpd 0xa0f(%rip), %xmm1      # 0x3040
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movq (%r14), %rax
movq 0x18(%r14), %rdx
movl %ecx, %esi
xorpd %xmm1, %xmm1
leaq 0x3f(%r13), %r8
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
leal (%rbx,%r9), %ecx
movl (%rdi,%rsi,4), %r10d
movslq %ecx, %rcx
movl %r10d, (%rax,%rcx,4)
addl %ebx, %r9d
addl $0x3f, %r9d
testl %ecx, %ecx
cmovnsl %ecx, %r9d
sarl $0x6, %r9d
movslq %r9d, %r9
movq %rcx, %r10
andq %r8, %r10
xorl %r11d, %r11d
cmpq %r13, %r10
setbe %r11b
movl $0x1, %r10d
shlq %cl, %r10
leaq (%rdx,%r9,8), %rcx
orq %r10, -0x8(%rcx,%r11,8)
incl 0x58(%r14)
addq $-0x1, %rsi
jb 0x2652
jmp 0x26b1
testq %rdi, %rdi
je 0x26be
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 0x9c2(%rip), %rdi       # 0x3096
leaq 0x9c6(%rip), %rsi       # 0x30a1
leaq 0x9c9(%rip), %rcx       # 0x30ab
movl $0x185, %edx            # imm = 0x185
callq 0x1090
jmp 0x26ee
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2708
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1160