// File: /workspace/github_cpp_makefile_star2_O3/SeanChao[P]KV-Store/kvstore.cc
// Function: KVStore::compaction(int)
void KVStore::compaction(int level) {
    std::clog << "run compaction on level " << level << std::endl;
    // range statistics
    int nextLv = level + 1;
    int nextLvPos = -1;        // index of insertion point of indexTable in next
                               // level, -1 indicates no assignment yet
    std::vector<Location> id;  // location of tables to be merged
    uint64_t min = UINT64_MAX;
    uint64_t max = 0;
    if (level == 0) {
        uint64_t t0Min = indexTableList[0].front().key;
        uint64_t t1Min = indexTableList[1].front().key;
        uint64_t t0Max = indexTableList[0].back().key;
        uint64_t t1Max = indexTableList[1].back().key;
        uint64_t t2Min = indexTableList[2].front().key;
        uint64_t t2Max = indexTableList[2].back().key;
        min = std::min({t0Min, t1Min, t2Min});
        max = std::max({t0Max, t1Max, t2Max});
        // std::clog << "[lv0]compaction range " << min << " " << max <<
        // std::endl;
        id.push_back(Location(0, 2));
        id.push_back(Location(0, 1));
        id.push_back(Location(0, 0));
    } else {
        // selects exceeding files and merges them into next level
        int more = fileNum[level] - levelSizeLim(level);
        // counts the range of keys in the last file of current level
        for (int i = 0; i < more; i++) {
            int tabId = getIndex(level, levelSizeLim(level) + i);
            uint64_t tMin = indexTableList[tabId].front().key;
            uint64_t tMax = indexTableList[tabId].back().key;
            if (tMin < min) min = tMin;
            max = tMax > max ? tMax : max;
            id.push_back(Location(level, levelSizeLim(level) + i));
        }
    }
    // looks for key range overlapped files
    int delFiles = 0;
    for (int i = 0; fileNum.size() > (size_t)nextLv && i < fileNum[nextLv];
         i++) {
        int tabId = getIndex(nextLv, i);
        uint64_t tMin = indexTableList[tabId].front().key;
        uint64_t tMax = indexTableList[tabId].back().key;
        if (!(tMax < min || max < tMin)) {
            id.push_back(Location(nextLv, i));
            delFiles++;
            // if (nextLvPos == -1) nextLvPos = i;
        }
        if (tMax < min) nextLvPos = i + 1;
    }
    // merge
    std::vector<Pair> all;
    for (size_t i = 0; i < id.size(); i++) {
        // reads ssTable and build pair vector
        std::vector<Pair> t = readSsTable(id[i].level, id[i].id);
        all = merge(all, t);
    }
    // std::clog << "Merged all data: " << std::endl;
    // for (auto &i : all)
    // std::clog << "\t" << i.key << ":\t" << std::string(i.val, 0, 50)
    // << std::endl;
    // update state: removes indexTable from memory, updates fileNum
    for (int i = (int)id.size() - 1; i >= 0; i--) {
        indexTableList.erase(indexTableList.begin() + getIndex(id[i]));
        fileNum[id[i].level]--;
        if (!std::filesystem::remove(resolvePath(id[i])))
            std::clog << "error deleting " << resolvePath(id[i]) << std::endl;
    }
    // slice merged data and write to disk
    // program state is updated in call to writeSsTable
    uint64_t size = 0;
    std::vector<Pair> tmp;
    if (nextLvPos == -1) nextLvPos = 0;
    // renames all file in nextLv to avoid rename hazard
    renameLevel(nextLv, 0);
    for (auto &i : all) {
        size += i.val.length() + DATA_CONST_SIZE;
        tmp.push_back(i);
        if (size >= MEM_TABLE_SIZE_MAX || i.key == all.back().key) {
            writeSsTable(tmp, Location(nextLv, nextLvPos++), true);
            size = 0;
            tmp.clear();
        }
    }
    renameLevel(nextLv, 1);
    // call compaction on next level if necessary
    if (fileNum[nextLv] > levelSizeLim(nextLv)) compaction(nextLv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %esi, %r15d
movq %rdi, %rbx
movq 0x7f60(%rip), %r14      # 0xefa8
leaq 0x4315(%rip), %rsi      # 0xb364
movl $0x18, %edx
movq %r14, %rdi
callq 0x32b0
movq %r14, %rdi
movl %r15d, %esi
callq 0x33d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
leal 0x1(%r15), %ecx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testl %r15d, %r15d
movl %ecx, 0x4(%rsp)
movq %r15, 0x88(%rsp)
je 0x7189
movslq %r15d, %rdx
movl $0x1, %esi
shll %cl, %esi
movq 0x60(%rbx), %rax
movq %rdx, 0x8(%rsp)
movl (%rax,%rdx,4), %ecx
xorl %r15d, %r15d
movq %rsi, 0x58(%rsp)
subl %esi, %ecx
movl %ecx, 0x78(%rsp)
movq $-0x1, %r13
jle 0x7225
xorl %ebp, %ebp
movq 0x60(%rbx), %rax
movq 0x68(%rbx), %rdx
movq %rdx, %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x8(%rsp), %rsi
cmpq %rcx, %rsi
cmovbq %rsi, %rcx
cmpq %rax, %rdx
je 0x7122
xorl %esi, %esi
xorl %edx, %edx
addl (%rax,%rsi,4), %edx
incq %rsi
cmpq %rsi, %rcx
jne 0x7115
jmp 0x7124
xorl %edx, %edx
movq 0x58(%rsp), %rax
addl %r15d, %eax
addl %eax, %edx
movslq %edx, %rcx
movq 0x40(%rbx), %rdx
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx,8), %rsi
movq 0x8(%rdx,%rcx,8), %rcx
movq (%rsi), %r12
movq -0x10(%rcx), %r14
movq 0x88(%rsp), %rcx
movl %ecx, 0x10(%rsp)
movl %eax, 0x14(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa8b6
cmpq %r13, %r12
cmovbq %r12, %r13
cmpq %rbp, %r14
cmovaq %r14, %rbp
incl %r15d
cmpl 0x78(%rsp), %r15d
jne 0x70ee
jmp 0x7227
movq 0x40(%rbx), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdx
movq (%rcx), %r13
movq 0x18(%rax), %rcx
movq (%rcx), %rcx
movq -0x10(%rdx), %rdx
movq 0x20(%rax), %rsi
movq -0x10(%rsi), %rbp
movq 0x30(%rax), %rsi
movq (%rsi), %rsi
cmpq %r13, %rcx
cmovbq %rcx, %r13
movq 0x38(%rax), %rax
cmpq %r13, %rsi
cmovbq %rsi, %r13
cmpq %rbp, %rdx
cmovaq %rdx, %rbp
movq -0x10(%rax), %rax
cmpq %rax, %rbp
cmovbq %rax, %rbp
movabsq $0x200000000, %rax      # imm = 0x200000000
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x40(%rsp), %rdi
callq 0xa8b6
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x40(%rsp), %rdi
callq 0xa8b6
leaq 0x10(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x40(%rsp), %rdi
callq 0xa8b6
jmp 0x7227
xorl %ebp, %ebp
movslq 0x4(%rsp), %rsi
movq 0x60(%rbx), %rax
movq 0x68(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rdi, 0x8(%rsp)
cmpq %rsi, %rdx
movq %rsi, 0x80(%rsp)
jbe 0x7314
xorl %r14d, %r14d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, 0x8(%rsp)
leaq 0x10(%rsp), %r12
cmpl (%rax,%rsi,4), %r14d
jge 0x7314
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rdx, %rsi
cmovbq %rsi, %rdx
testq %rdx, %rdx
je 0x729c
xorl %edi, %edi
xorl %esi, %esi
addl (%rax,%rdi,4), %esi
incq %rdi
cmpq %rdi, %rdx
jne 0x728f
jmp 0x729e
xorl %esi, %esi
addl %r14d, %esi
movslq %esi, %rsi
movq 0x40(%rbx), %rdx
leaq (%rsi,%rsi,2), %rsi
movq 0x8(%rdx,%rsi,8), %rdi
movq -0x10(%rdi), %r15
cmpq %r13, %r15
jb 0x72e5
movq (%rdx,%rsi,8), %rdx
cmpq (%rdx), %rbp
jb 0x72e5
movl 0x4(%rsp), %eax
movl %eax, 0x10(%rsp)
movl %r14d, 0x14(%rsp)
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0xa8b6
movq 0x60(%rbx), %rax
movq 0x68(%rbx), %rcx
incl %r14d
cmpq %r13, %r15
movq 0x8(%rsp), %rdx
cmovbl %r14d, %edx
movq %rdx, 0x8(%rsp)
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
movq 0x80(%rsp), %rsi
cmpq %rsi, %rdx
ja 0x726b
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
subq %rax, %r13
je 0x7423
xorl %r14d, %r14d
leaq 0x90(%rsp), %r15
leaq 0x100(%rsp), %r12
leaq 0xe8(%rsp), %rbp
movl (%rax,%r14,8), %edx
movl 0x4(%rax,%r14,8), %ecx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x79de
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
callq 0x9318
leaq 0xb0(%rsp), %r13
movq %rbp, %rdi
movq %r15, %rsi
callq 0x9318
movq %r13, %rdi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x82b8
movq 0x70(%rsp), %rax
movaps 0xb0(%rsp), %xmm0
movq 0xc0(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movaps 0x60(%rsp), %xmm1
movaps %xmm0, 0x60(%rsp)
movaps %xmm1, 0x10(%rsp)
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movq $0x0, 0xc0(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x9166
movq %r13, %rdi
callq 0x9166
movq %rbp, %rdi
callq 0x9166
movq %r12, %rdi
callq 0x9166
movq %r15, %rdi
callq 0x9166
incq %r14
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %r13
subq %rax, %r13
sarq $0x3, %r13
cmpq %r13, %r14
jb 0x7353
jmp 0x7427
sarq $0x3, %r13
testl %r13d, %r13d
jle 0x75b9
leaq 0x40(%rbx), %rax
movq %rax, 0x58(%rsp)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq 0x90(%rsp), %r12
leaq 0x10(%rsp), %rbp
movq 0x60(%rbx), %rcx
movq 0x40(%rsp), %rax
movslq -0x8(%rax,%r13,8), %rsi
movq 0x68(%rbx), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rdx, %rsi
cmovbq %rsi, %rdx
testq %rdx, %rdx
je 0x7483
xorl %edi, %edi
xorl %esi, %esi
addl (%rcx,%rdi,4), %esi
incq %rdi
cmpq %rdi, %rdx
jne 0x7476
jmp 0x7485
xorl %esi, %esi
movq 0x40(%rbx), %rcx
movslq -0x4(%rax,%r13,8), %rax
movslq %esi, %rdx
addq %rax, %rdx
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %rsi
movq 0x58(%rsp), %rdi
callq 0xaacc
leaq -0x1(%r13), %r15
movq 0x40(%rsp), %rax
movslq (%rax,%r15,8), %rcx
movq 0x60(%rbx), %rdx
decl (%rdx,%rcx,4)
movl (%rax,%r15,8), %edx
movl 0x4(%rax,%r15,8), %ecx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5f1a
movq %rbp, %rdi
movq %r12, %rsi
movl $0x2, %edx
callq 0x923c
movq %rbp, %rdi
callq 0x3200
movl %eax, %r14d
movq %rbp, %rdi
callq 0x8fe4
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x7516
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3280
testb %r14b, %r14b
jne 0x75ac
movl $0xf, %edx
movq 0x7a7d(%rip), %rdi      # 0xefa8
leaq 0x3e4b(%rip), %rsi      # 0xb37d
callq 0x32b0
movq 0x40(%rsp), %rax
movl (%rax,%r15,8), %edx
movl 0x4(%rax,%r15,8), %ecx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5f1a
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x7a47(%rip), %rdi      # 0xefa8
callq 0x32b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3250
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31b0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x75ac
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
cmpq $0x1, %r13
movq %r15, %r13
jg 0x744d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, %rdi
movl 0x4(%rsp), %esi
xorl %edx, %edx
callq 0x6900
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq %rax, %r15
movq 0x8(%rsp), %r12
je 0x76d1
xorl %ebp, %ebp
cmpl $-0x1, %r12d
cmovel %ebp, %r12d
movl 0x4(%rsp), %eax
movq %rax, 0x78(%rsp)
leaq 0x10(%rsp), %r13
movq 0x18(%r15), %r14
movq %r13, %rdi
movq %r15, %rsi
callq 0x93b4
addq %r14, %rbp
addq $0x28, %rbp
cmpq $0x1fffff, %rbp         # imm = 0x1FFFFF
ja 0x763e
movq (%r15), %rax
movq 0x68(%rsp), %rcx
cmpq -0x30(%rcx), %rax
jne 0x76c2
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
callq 0x9318
movq %r12, %rdx
shlq $0x20, %rdx
addq 0x78(%rsp), %rdx
movq %rbx, %rdi
leaq 0xd0(%rsp), %r14
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x7d28
incl %r12d
movq %r12, 0x8(%rsp)
movq %r14, %rdi
callq 0x9166
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r12
cmpq %rbp, %r12
je 0x76bb
leaq 0x20(%rbp), %r14
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x76a6
movq (%r14), %rsi
incq %rsi
callq 0x3280
leaq 0x30(%r14), %rax
addq $0x10, %r14
cmpq %r12, %r14
movq %rax, %r14
jne 0x7692
movq %rbp, 0x18(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
addq $0x30, %r15
cmpq 0x58(%rsp), %r15
jne 0x760d
movl $0x1, %ebp
movq %rbx, %rdi
movl 0x4(%rsp), %r14d
movl %r14d, %esi
movl $0x1, %edx
callq 0x6900
movq 0x88(%rsp), %rcx
addb $0x2, %cl
shll %cl, %ebp
movq 0x60(%rbx), %rax
movq 0x80(%rsp), %rcx
cmpl %ebp, (%rax,%rcx,4)
jle 0x7714
movq %rbx, %rdi
movl %r14d, %esi
callq 0x702a
leaq 0x10(%rsp), %rdi
callq 0x9166
leaq 0x60(%rsp), %rdi
callq 0x9166
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x773f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7762
jmp 0x7762
jmp 0x7762
jmp 0x77e7
jmp 0x7762
jmp 0x77b1
jmp 0x77b1
movq %rax, %rbx
jmp 0x781f
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x9166
jmp 0x77ea
jmp 0x77b1
jmp 0x77b1
movq %rax, %rbx
jmp 0x7792
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8fe4
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x7815
movq 0xa0(%rsp), %rsi
jmp 0x780d
movq %rax, %rbx
jmp 0x7815
movq %rax, %rbx
jmp 0x77dd
movq %rax, %rbx
jmp 0x77d0
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x9166
leaq 0x100(%rsp), %rdi
callq 0x9166
leaq 0x90(%rsp), %rdi
jmp 0x77ef
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9166
jmp 0x7815
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7815
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x60(%rsp), %rdi
callq 0x9166
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7836
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x33f0