// File: xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::compact0()
void KVStore::compact0() {

    vector<SSTPtr> SSTs = *ssTables[0];
    if (SSTs.size() != 3) {
        cerr << "Number of SSTs in L0 is not 3. L0-compaction error." << endl;
        exit(-1);
    }

    if (!ssTables.count(1))
        ssTables[1] = make_shared<vector<SSTPtr>>();

    // Get the overall interval of SSTs in L0.
    LsmKey minKey, maxKey;
    getCompact0Range(minKey, maxKey);

    // Get all the SSTs in L0 and L1 that need merge.
    int64_t minOverlapIndex = -1;
    int64_t maxOverlapIndex = -1;
    vector<SSTPtr> overlapSSTs = getOverlapSSTs(minKey, maxKey, 1,
                                                minOverlapIndex, maxOverlapIndex);

    // Get all k-v pairs from the disk.
    SSTs.insert(SSTs.end(), overlapSSTs.begin(), overlapSSTs.end());
    KVPair data = getCompactionData(SSTs);

    // Remove the overlapping SST files in the disk.
    reconstructLowerLevelDisk( minOverlapIndex, maxOverlapIndex, 1);

    // Sort the keys and write the data into the disk.
    TimeStamp maxTimeStamp = getMaxTimeStamp(SSTs);
    vector<SSTPtr> mergedSSTs = merge0AndWriteToDisk(SSTs, maxTimeStamp, data);

    // Reconstruct L1 in memory.
    reconstructLowerLevelMemory(minOverlapIndex, maxOverlapIndex, mergedSSTs, 1);

    // Clear L0 in memory and delete corresponding files in the disk.
    reconstructL0();

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x118(%rbp)
addq $0x20, %rdi
movq $0x0, -0x28(%rbp)
leaq -0x28(%rbp), %rsi
callq 0x15de0
movq %rax, %rdi
callq 0x16540
movq %rax, %rsi
leaq -0x20(%rbp), %rdi
callq 0x16570
leaq -0x20(%rbp), %rdi
callq 0x16120
cmpq $0x3, %rax
je 0x11bce
movq 0x20467(%rip), %rdi     # 0x31ff0
leaq 0x11874(%rip), %rsi     # 0x23404
callq 0x42c0
movq %rax, -0x120(%rbp)
jmp 0x11b9e
movq -0x120(%rbp), %rdi
movq 0x20404(%rip), %rsi     # 0x31fb0
callq 0x4300
jmp 0x11bb3
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4360
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x11e7b
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
movq $0x1, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x16790
movq %rax, -0x128(%rbp)
jmp 0x11bf3
movq -0x128(%rbp), %rax
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x11c09
jmp 0x11c6e
leaq -0x50(%rbp), %rdi
callq 0x15d70
jmp 0x11c14
movq -0x118(%rbp), %rdi
addq $0x20, %rdi
movq $0x1, -0x58(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x15de0
movq %rax, -0x130(%rbp)
jmp 0x11c39
movq -0x130(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x15e10
leaq -0x50(%rbp), %rdi
callq 0x15e40
jmp 0x11c6e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x15e40
jmp 0x11e7b
movq -0x118(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x121d0
jmp 0x11c84
movq -0x118(%rbp), %rsi
movq $-0x1, -0x70(%rbp)
movq $-0x1, -0x78(%rbp)
movq -0x60(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq %rsp, %rax
leaq -0x78(%rbp), %rdi
movq %rdi, (%rax)
leaq -0x90(%rbp), %rdi
movl $0x1, %r8d
leaq -0x70(%rbp), %r9
callq 0x12400
jmp 0x11cc5
leaq -0x20(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x162d0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x16870
leaq -0x90(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x162a0
movq -0x148(%rbp), %rdi
movq %rax, -0xa8(%rbp)
callq 0x162d0
movq -0x140(%rbp), %rdi
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rcx
callq 0x167c0
movq %rax, -0x138(%rbp)
jmp 0x11d46
movq -0x138(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x12680
jmp 0x11d66
movq -0x118(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdx
movl $0x1, %ecx
callq 0x127c0
jmp 0x11d81
leaq -0x20(%rbp), %rdi
callq 0x119d0
movq %rax, -0x150(%rbp)
jmp 0x11d93
movq -0x118(%rbp), %rsi
movq -0x150(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rcx
leaq -0x110(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq -0xf0(%rbp), %r8
callq 0x12880
jmp 0x11dc8
movq -0x118(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdx
leaq -0x110(%rbp), %rcx
movl $0x1, %r8d
callq 0x12e70
jmp 0x11deb
movq -0x118(%rbp), %rdi
callq 0x13150
jmp 0x11df9
leaq -0x110(%rbp), %rdi
callq 0x163c0
leaq -0xf0(%rbp), %rdi
callq 0x168b0
leaq -0x90(%rbp), %rdi
callq 0x163c0
leaq -0x20(%rbp), %rdi
callq 0x163c0
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x11e6f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x11e63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x163c0
leaq -0xf0(%rbp), %rdi
callq 0x168b0
leaq -0x90(%rbp), %rdi
callq 0x163c0
leaq -0x20(%rbp), %rdi
callq 0x163c0
movq -0x30(%rbp), %rdi
callq 0x4560
nopl (%rax)