// File: /workspace/github_cpp_makefile_star2_O2/xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::getValueFromDisk[abi:cxx11](unsigned long)
LsmValue KVStore::getValueFromDisk(LsmKey key) {

    size_t levelNumber = ssTables.size();
    if (levelNumber == 0)   // All data are stored in memTable.
        return "";

    TimeStamp maxTimeStamp = 0;
    LsmValue newestValue;

    auto getNewestValue = [&](const SSTPtr& sst, LsmKey key) {
        LsmValue newValue = sst->get(key);
        TimeStamp newTimeStamp = sst->getTimeStamp();
        if (newValue.length() != 0 && newTimeStamp > maxTimeStamp) {
            newestValue = newValue;
            maxTimeStamp = newTimeStamp;
        }
    };

    // Read from L0.
    vector<SSTPtr> L0SSTs = *ssTables[0];
    uint32_t L0SSTNumber = L0SSTs.size();
    for (int i = L0SSTNumber - 1; i >= 0; --i) {
        SSTPtr sst = L0SSTs[i];
        getNewestValue(sst, key);

        if (newestValue.length() != 0)
            return newestValue;
    }

    // Read from the rest levels.
    for (size_t n = 1; n < levelNumber; n++) {
        vector<SSTPtr> levelSSTs = *ssTables[n];
        if (key < levelSSTs.front()->getMinKey() || key > levelSSTs.back()->getMaxKey())
            continue;
        uint32_t sstIndex = sstBinarySearch(levelSSTs, key, 0, levelSSTs.size() - 1);
        SSTPtr targetSST = levelSSTs[sstIndex];
        getNewestValue(targetSST, key);

        if (newestValue.length() != 0)
            return newestValue;
    }

    return newestValue;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r15
movq 0x38(%rsi), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x7ca7
movq %rdx, %r14
movq %rsi, %rdx
leaq 0x70(%rsp), %rax
andq $0x0, (%rax)
leaq 0x10(%r15), %rcx
movq %rcx, (%r15)
andq $0x0, 0x8(%r15)
addq $0x20, %rdx
movb $0x0, 0x10(%r15)
movq %rax, 0x50(%rsp)
movq %r15, (%rsp)
movq %r15, 0x58(%rsp)
leaq 0x8(%rsp), %rsi
andq $0x0, (%rsi)
movq %rdx, 0x40(%rsp)
movq %rdx, %rdi
callq 0xad0a
movq (%rax), %rsi
leaq 0x28(%rsp), %rdi
callq 0xa5bc
movq 0x30(%rsp), %rax
subq 0x28(%rsp), %rax
shrq $0x4, %rax
leaq 0x10(%rsp), %r12
movl %eax, %r15d
decq %r15
leaq 0x8(%rsp), %r13
leaq 0x50(%rsp), %rbp
testl %r15d, %r15d
js 0x7cc0
movl %r15d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
shlq $0x4, %rsi
addq 0x28(%rsp), %rsi
movq %r13, %rdi
callq 0xaa4e
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x8572
movq (%rsp), %rax
movq 0x8(%rax), %rbx
movq %r12, %rdi
callq 0x6932
decq %r15
testq %rbx, %rbx
je 0x7c4f
leaq 0x28(%rsp), %rdi
callq 0xa552
movq (%rsp), %r15
jmp 0x7d9f
leaq 0x6360(%rip), %rsi      # 0xe00e
leaq 0x28(%rsp), %rdx
movq %r15, %rdi
callq 0x37ce
jmp 0x7d9f
pushq $0x1
popq %rax
leaq 0x20(%rsp), %r13
leaq 0x8(%rsp), %rbp
leaq 0x60(%rsp), %r12
movq (%rsp), %r15
movq %rax, 0x20(%rsp)
cmpq 0x48(%rsp), %rax
jae 0x7d95
movq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0xbc36
movq (%rax), %rsi
movq %rbp, %rdi
callq 0xa5bc
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x57f8
cmpq %r14, %rax
ja 0x7d76
movq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
callq 0x57fe
cmpq %r14, %rax
jb 0x7d76
movq 0x10(%rsp), %rcx
subq 0x8(%rsp), %rcx
shrq $0x4, %rcx
decl %ecx
movq %rbp, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x85e2
movl %eax, %esi
shlq $0x4, %rsi
addq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0xaa4e
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x8572
movq 0x8(%r15), %rbx
leaq 0x68(%rsp), %rdi
callq 0x6932
testq %rbx, %rbx
jne 0x7d8b
movq %rbp, %rdi
callq 0xa552
movq 0x20(%rsp), %rax
incq %rax
jmp 0x7cd6
leaq 0x8(%rsp), %rdi
callq 0xa552
leaq 0x28(%rsp), %rdi
callq 0xa552
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x7df0
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x6932
jmp 0x7dd7
jmp 0x7dd4
movq %rax, %r14
movq %r12, %rdi
callq 0x6932
jmp 0x7de6
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0xa552
jmp 0x7de6
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0xa552
movq (%rsp), %rdi
callq 0x34a8
movq %r14, %rdi
callq 0x3440
nop