// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, %r14
movq %rdi, %rbx
movq 0x38(%rsi), %rdx
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
movq %rdx, 0x38(%rsp)
testq %rdx, %rdx
je 0x9618
movq %rsi, %rcx
movq %rax, 0x30(%rsp)
addq $0x20, %rcx
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movq %rbx, (%rsp)
movb $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %rcx, 0x70(%rsp)
movq %rcx, %rdi
callq 0xd658
movq (%rax), %rsi
leaq 0x40(%rsp), %rdi
callq 0xcf10
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
shrq $0x4, %rax
movl %eax, %ebx
decq %rbx
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
testl %ebx, %ebx
js 0x962f
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rbp
movq 0x8(%rcx,%rax), %r12
testq %r12, %r12
je 0x9593
movq 0xca47(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0x958d
incl 0x8(%r12)
jmp 0x9593
lock
incl 0x8(%r12)
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x58e0
movq %rbp, %rdi
callq 0x6860
cmpq $0x0, 0x18(%rsp)
je 0x95c8
movq %rax, %rbp
cmpq %r15, %rax
jbe 0x95c8
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x3160
movq %rbp, %r15
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x95e4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq (%rsp), %rax
movq 0x8(%rax), %rbp
testq %r12, %r12
je 0x95f9
movq %r12, %rdi
callq 0x7c76
decq %rbx
testq %rbp, %rbp
je 0x9554
leaq 0x40(%rsp), %rdi
callq 0xce64
movq (%rsp), %rbx
jmp 0x978a
leaq 0x6a2b(%rip), %rdx      # 0x1004a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x5648
jmp 0x978a
movq $0x1, 0x8(%rsp)
cmpq $0x1, 0x38(%rsp)
movq (%rsp), %rbx
je 0x9780
leaq 0x8(%rsp), %r12
leaq 0x58(%rsp), %rbp
movq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0xe280
movq (%rax), %rsi
movq %rbp, %rdi
callq 0xcf10
movq 0x58(%rsp), %rax
movq (%rax), %rdi
callq 0x6866
cmpq %r14, %rax
ja 0x968f
movq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
callq 0x686c
cmpq %r14, %rax
jae 0x969c
movq %rbp, %rdi
callq 0xce64
jmp 0x9768
movq 0x60(%rsp), %rcx
subq 0x58(%rsp), %rcx
shrq $0x4, %rcx
decl %ecx
movq %rbp, %r13
movq %rbp, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xa254
movl %eax, %eax
movq 0x58(%rsp), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %r12
movq 0x8(%rcx,%rax), %rbp
testq %rbp, %rbp
je 0x96ea
movq 0xc8ec(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0x96e6
incl 0x8(%rbp)
jmp 0x96ea
lock
incl 0x8(%rbp)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x58e0
movq %r12, %rdi
callq 0x6860
cmpq $0x0, 0x18(%rsp)
je 0x9722
movq %rax, %r12
cmpq %r15, %rax
jbe 0x9722
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3160
movq %r12, %r15
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x973e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rbx), %rbx
testq %rbp, %rbp
je 0x974f
movq %rbp, %rdi
callq 0x7c76
movq %r13, %rbp
movq %r13, %rdi
callq 0xce64
testq %rbx, %rbx
movq (%rsp), %rbx
leaq 0x8(%rsp), %r12
jne 0x9780
movq 0x8(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
cmpq 0x38(%rsp), %rax
jb 0x9652
leaq 0x40(%rsp), %rdi
callq 0xce64
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x97c7
jmp 0x97d6
movq %rax, %r14
jmp 0x9825
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x97c7
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
testq %rbp, %rbp
je 0x97d9
movq %rbp, %rdi
callq 0x7c76
jmp 0x97d9
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0xce64
jmp 0x981b
movq %rax, %r14
jmp 0x981b
movq %rax, %r14
jmp 0x980e
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x980e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
testq %r12, %r12
je 0x981b
movq %r12, %rdi
callq 0x7c76
leaq 0x40(%rsp), %rdi
callq 0xce64
movq (%rsp), %rax
movq (%rax), %rdi
cmpq 0x30(%rsp), %rdi
je 0x9843
movq 0x30(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x3280
movq %r14, %rdi
callq 0x3460
nop