// File: reddragon[P]packed-memory-array/impl2.cpp
// Function: PMA::rebalance_interval(int, int)
void
    rebalance_interval(int left, int level) {
        dprintf("rebalance_interval(%d, %d)\n", left, level);
        int w = (1 << level) * this->chunk_size;
        tmp.clear();
        tmp.reserve(w);
        for (int i = left; i < left + w; ++i) {
            if (this->present[i]) {
                tmp.push_back(this->impl[i]);
                this->present[i] = false;
            }
        }
        double m = (double)(1<<level)*chunk_size / (double)tmp.size();
        dprintf("m: %f, tmp.size(): %d\n", m, tmp.size());
        assert(m >= 1.0);
        for (int i = 0; i < tmp.size(); ++i) {
            int k = i * m + left;
            if (k >= left + w) {
                dprintf("k: %d, left+w: %d\n", k, left + w);
            }
            assert(k < left + w);
            this->present[k] = true;
            this->impl[k] = tmp[i];
        }
        nmoves += w;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x60(%rbp)
movl -0x10(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
imull 0x48(%rdi), %eax
movl %eax, -0x14(%rbp)
addq $0x58, %rdi
callq 0x2850
movq -0x60(%rbp), %rdi
addq $0x58, %rdi
movslq -0x14(%rbp), %rsi
callq 0x2870
movl -0xc(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jge 0x22ff
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x18(%rbp), %rsi
callq 0x1b70
movq %rax, -0x28(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1bd0
testb $0x1, %al
jne 0x22a7
jmp 0x22ef
movq -0x60(%rbp), %rdi
movq %rdi, %rax
addq $0x58, %rax
movq %rax, -0x68(%rbp)
movslq -0x18(%rbp), %rsi
callq 0x2530
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x29c0
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x18(%rbp), %rsi
callq 0x1b70
movq %rax, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x2970
jmp 0x22f1
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x226e
movq -0x60(%rbp), %rdi
movb -0x10(%rbp), %cl
movl $0x1, %eax
shll %cl, %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x48(%rdi), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
addq $0x58, %rdi
callq 0x1a90
movsd -0x70(%rbp), %xmm0
movq %rax, %xmm1
movaps 0x4ce7(%rip), %xmm2     # 0x7020
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x4ceb(%rip), %xmm2     # 0x7030
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movsd 0x4c9e(%rip), %xmm1     # 0x7008
ucomisd %xmm1, %xmm0
jb 0x2372
jmp 0x2391
leaq 0x4ed1(%rip), %rdi      # 0x724a
leaq 0x4ce9(%rip), %rsi      # 0x7069
movl $0x10e, %edx            # imm = 0x10E
leaq 0x4ec7(%rip), %rcx      # 0x7253
callq 0x1090
movl $0x0, -0x44(%rbp)
movq -0x60(%rbp), %rdi
movslq -0x44(%rbp), %rax
movq %rax, -0x78(%rbp)
addq $0x58, %rdi
callq 0x1a90
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
jae 0x2475
cvtsi2sdl -0x44(%rbp), %xmm0
movsd -0x40(%rbp), %xmm2
cvtsi2sdl -0xc(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jl 0x23ea
jmp 0x23ea
movl -0x48(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jge 0x23f9
jmp 0x2418
leaq 0x4e7a(%rip), %rdi      # 0x727a
leaq 0x4c62(%rip), %rsi      # 0x7069
movl $0x114, %edx            # imm = 0x114
leaq 0x4e40(%rip), %rcx      # 0x7253
callq 0x1090
movq -0x60(%rbp), %rdi
addq $0x20, %rdi
movslq -0x48(%rbp), %rsi
callq 0x1b70
movq %rax, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x2970
movq -0x60(%rbp), %rdi
addq $0x58, %rdi
movslq -0x44(%rbp), %rsi
callq 0x2530
movq -0x60(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
movslq -0x48(%rbp), %rsi
callq 0x2530
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2398
movslq -0x14(%rbp), %rax
addq 0x8c50(%rip), %rax      # 0xb0d0
movq %rax, 0x8c49(%rip)      # 0xb0d0
addq $0x80, %rsp
popq %rbp
retq