// File: reddragon[P]packed-memory-array/impl1.cpp
// Function: PackedMemoryArray<int>::smallest_interval_in_balance(int, int*, int*) const
int PackedMemoryArray<E>::smallest_interval_in_balance(int index, int * node_index, int * node_level) const {
    // If we are trying to insert at the end of the PMA
    if (index == (int)store.size()) {
        index = (int)store.size() - 1;
    }

    int level = -1;
    int start = index;
    int end = index, count = 1;
    unsigned int sz = segment_size;
    bool found = false;
    do {
        // Get the boundaries of the next interval
        int left = start - (start % sz);
        int right = left + sz;

        // Count only the necessary parts
        for(int i = left; i < start; i++)
            if(ELEM_EXISTS_AT(i))
                count++;
        for(int i = end + 1; i <= right; i++)
            if(ELEM_EXISTS_AT(i))
                count++;
        
        start = left;
        end = right;

        ++level;
        bool is_balanced = !is_out_of_balance(count + 1, level);
        // std::cout << "Level: " << level << ", from " << left << " to " << right << ", having " << count+1 << ", elements, is balanced?: " << is_balanced << ", segment_size: " << smallest_window_size << std::endl;
        // Would be able to fit another element?
        if(is_balanced) {
            found = true;
            break;
        }
        sz <<= 1;
        
    } while(sz <= store.size());
    if(!found) {
        // We did not find a balanced interval
        *node_index = *node_level = -1;
        return -1;
    }

    *node_index = start;
    *node_level = level;
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x58(%rbp)
callq 0x1950
movq %rax, %rcx
movl -0x58(%rbp), %eax
cmpl %ecx, %eax
jne 0x4ba3
movq -0x60(%rbp), %rdi
callq 0x1950
subl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x60(%rbp), %rax
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movl $0x1, -0x38(%rbp)
movl 0x5c(%rax), %eax
movl %eax, -0x3c(%rbp)
movb $0x0, -0x3d(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x30(%rbp), %eax
xorl %edx, %edx
divl -0x3c(%rbp)
movl -0x64(%rbp), %eax
subl %edx, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x4c25
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
movslq -0x4c(%rbp), %rsi
callq 0x5430
testb $0x1, %al
jne 0x4c0f
jmp 0x4c18
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4c1a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4bf0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x48(%rbp), %eax
jg 0x4c63
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
movslq -0x50(%rbp), %rsi
callq 0x5430
testb $0x1, %al
jne 0x4c4d
jmp 0x4c56
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x4c58
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x4c2e
movq -0x60(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x38(%rbp), %esi
addl $0x1, %esi
movl -0x2c(%rbp), %edx
callq 0x5480
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x51(%rbp)
testb $0x1, -0x51(%rbp)
je 0x4c9d
movb $0x1, -0x3d(%rbp)
jmp 0x4cc5
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x3c(%rbp)
movq -0x60(%rbp), %rdi
movl -0x3c(%rbp), %eax
movq %rax, -0x70(%rbp)
callq 0x1950
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jbe 0x4bcb
testb $0x1, -0x3d(%rbp)
jne 0x4ce8
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4d01
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)