// File: xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::reconstructLowerLevelMemory(long, long, std::vector<std::shared_ptr<SSTable>, std::allocator<std::shared_ptr<SSTable>>> const&, unsigned long)
void KVStore::reconstructLowerLevelMemory(int64_t minOverlapIndex, int64_t maxOverlapIndex,
                                          const vector<SSTPtr>& newSSTs, size_t lowerLevel) {

    vector<SSTPtr> previousSSTs = *ssTables[lowerLevel];
    uint32_t length = previousSSTs.size();
    vector<SSTPtr> updatedSSTs;

    if (length == 0)
        minOverlapIndex = maxOverlapIndex = 0;

    // If no overlapping, binary search for the insert position.
    if (minOverlapIndex == -1 && maxOverlapIndex == -1) {
        LsmKey insertMinKey = newSSTs.front()->getMinKey();
        if (insertMinKey < previousSSTs.front()->getMinKey())
            minOverlapIndex = maxOverlapIndex = 0;
        else
            minOverlapIndex = maxOverlapIndex =
                    sstBinarySearch(previousSSTs, insertMinKey, 0, length - 1) + 1;
    }

    // Old SSTs left to the overlapping SSTs.
    for (uint32_t i = 0; i < minOverlapIndex; ++i)
        updatedSSTs.push_back(previousSSTs[i]);

    // New SSTs that need insert into the level.
    for (const auto& newSST : newSSTs)
        updatedSSTs.push_back(newSST);

    // Old SSTs right to the overlapping SSTs.
    for (uint32_t i = maxOverlapIndex; i < length; ++i)
        updatedSSTs.push_back(previousSSTs[i]);

    // Save the new layer in the memory.
    ssTables[lowerLevel] = make_shared<vector<SSTPtr>>(updatedSSTs);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
addq $0x20, %rdi
leaq -0x28(%rbp), %rsi
callq 0x16370
movq %rax, %rdi
callq 0x16540
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x16570
leaq -0x40(%rbp), %rdi
callq 0x16120
movl %eax, -0x44(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x15ef0
cmpl $0x0, -0x44(%rbp)
jne 0x12ee6
movq $0x0, -0x18(%rbp)
movq $0x0, -0x10(%rbp)
cmpq $-0x1, -0x10(%rbp)
jne 0x12fbd
cmpq $-0x1, -0x18(%rbp)
jne 0x12fbd
movq -0x20(%rbp), %rdi
callq 0x16f90
movq %rax, %rdi
callq 0xd330
movq %rax, %rdi
callq 0x98e0
movq %rax, -0xc0(%rbp)
jmp 0x12f1e
movq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x16140
movq %rax, %rdi
callq 0xd330
movq %rax, %rdi
callq 0x98e0
movq %rax, -0xc8(%rbp)
jmp 0x12f56
movq -0xd0(%rbp), %rax
movq -0xc8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x12f8c
movq $0x0, -0x18(%rbp)
movq $0x0, -0x10(%rbp)
jmp 0x12fbb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x13134
movq -0x68(%rbp), %rsi
movl -0x44(%rbp), %ecx
decl %ecx
leaq -0x40(%rbp), %rdi
xorl %edx, %edx
callq 0x120b0
movl %eax, -0xd4(%rbp)
jmp 0x12fa8
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq %rax, -0x18(%rbp)
movq %rax, -0x10(%rbp)
jmp 0x12fbd
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpq -0x10(%rbp), %rax
jge 0x12ff6
movl -0x78(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x16170
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x16050
jmp 0x12fe9
jmp 0x12feb
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x12fc4
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x169d0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x16a00
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x16a30
testb $0x1, %al
jne 0x13037
jmp 0x1306c
leaq -0x88(%rbp), %rdi
callq 0x16a70
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x16050
jmp 0x1305c
jmp 0x1305e
leaq -0x88(%rbp), %rdi
callq 0x16ba0
jmp 0x1301e
movq -0x18(%rbp), %rax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x130b3
movl -0x9c(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0x16170
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x16050
jmp 0x130a0
jmp 0x130a2
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x13076
leaq -0xb0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x16300
jmp 0x130c5
movq -0xb8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x28(%rbp), %rsi
callq 0x16370
movq %rax, -0xe0(%rbp)
jmp 0x130e2
movq -0xe0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x15e10
leaq -0xb0(%rbp), %rdi
callq 0x15e40
leaq -0x60(%rbp), %rdi
callq 0x163c0
leaq -0x40(%rbp), %rdi
callq 0x163c0
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x15e40
leaq -0x60(%rbp), %rdi
callq 0x163c0
leaq -0x40(%rbp), %rdi
callq 0x163c0
movq -0x70(%rbp), %rdi
callq 0x4560
nop