// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdx, %r12
movq %rdi, %rbx
movq 0x38(%rsi), %rdx
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
movq %rdx, 0x40(%rsp)
testq %rdx, %rdx
je 0x98d8
movq %rsi, %rcx
movq %rax, 0x30(%rsp)
addq $0x20, %rcx
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %rcx, 0x60(%rsp)
movq %rcx, %rdi
callq 0xd740
movq (%rax), %rsi
leaq 0x70(%rsp), %rdi
callq 0xcfe4
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
shrq $0x4, %rax
decl %eax
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movq %r12, 0x68(%rsp)
movl %eax, %r14d
testl %eax, %eax
js 0x976d
movl %r14d, %eax
movq 0x70(%rsp), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rbp
movq 0x8(%rcx,%rax), %r13
testq %r13, %r13
je 0x96e3
movq 0xc8f5(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0x96de
incl 0x8(%r13)
jmp 0x96e3
lock
incl 0x8(%r13)
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x5988
movq %rbp, %rdi
callq 0x690a
cmpq $0x0, 0x18(%rsp)
je 0x971f
movq %rax, %rbp
cmpq 0x8(%rsp), %rax
jbe 0x971f
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3160
movq %rbp, 0x8(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x973b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %r12
movq 0x8(%rbx), %rbp
testq %rbp, %rbp
setne %bl
testq %r13, %r13
je 0x9755
movq %r13, %rdi
callq 0x7d9e
orb %bl, %r15b
leal -0x1(%r14), %eax
testq %rbp, %rbp
movq %r12, %rbx
movq 0x68(%rsp), %r12
je 0x96a7
testl %r14d, %r14d
jns 0x98f6
movq $0x1, 0x38(%rsp)
movb $0x1, %al
cmpq $0x1, 0x40(%rsp)
je 0x98f3
movq %rbx, (%rsp)
leaq 0x38(%rsp), %r14
leaq 0x48(%rsp), %r13
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0xe3ca
movq (%rax), %rsi
movq %r13, %rdi
callq 0xcfe4
movq 0x48(%rsp), %rax
movq (%rax), %rdi
callq 0x6910
movl $0x7, %ebx
cmpq %r12, %rax
ja 0x98a7
movq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
callq 0x6916
cmpq %r12, %rax
jb 0x98a7
movq 0x50(%rsp), %rcx
subq 0x48(%rsp), %rcx
shrq $0x4, %rcx
decl %ecx
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xa3fa
movl %eax, %eax
movq 0x48(%rsp), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rbx
movq 0x8(%rcx,%rax), %rbp
testq %rbp, %rbp
je 0x9830
movq 0xc7a6(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0x982c
incl 0x8(%rbp)
jmp 0x9830
lock
incl 0x8(%rbp)
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x5988
movq %rbx, %rdi
callq 0x690a
cmpq $0x0, 0x18(%rsp)
je 0x986d
movq %rax, %rbx
cmpq 0x8(%rsp), %rax
jbe 0x986d
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3160
movq %rbx, 0x8(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x9889
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
xorl %ebx, %ebx
movq (%rsp), %rax
cmpq $0x0, 0x8(%rax)
setne %bl
orb %bl, %r15b
testq %rbp, %rbp
je 0x98a7
movq %rbp, %rdi
callq 0x7d9e
movq %r13, %rdi
callq 0xcf40
cmpl $0x7, %ebx
je 0x98b8
testl %ebx, %ebx
jne 0x98ec
movq 0x38(%rsp), %rax
incq %rax
movq %rax, 0x38(%rsp)
cmpq 0x40(%rsp), %rax
jb 0x979b
movq (%rsp), %rbx
movb $0x1, %al
jmp 0x98f3
leaq 0x674b(%rip), %rdx      # 0x1002a
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x561a
jmp 0x991e
movl %r15d, %eax
movq (%rsp), %rbx
movl %eax, %r15d
leaq 0x70(%rsp), %rdi
callq 0xcf40
testb $0x1, %r15b
movq 0x30(%rsp), %rax
jne 0x991e
movq (%rbx), %rdi
cmpq %rax, %rdi
je 0x991e
movq (%rax), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x9959
jmp 0x9971
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x9959
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
testq %rbp, %rbp
je 0x9974
movq %rbp, %rdi
callq 0x7d9e
jmp 0x9974
movq %rbx, (%rsp)
movq %rax, %rbx
jmp 0x99c8
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xcf40
jmp 0x99be
movq %rax, %rbx
jmp 0x99be
movq %rbx, (%rsp)
movq %rax, %rbx
jmp 0x99b1
movq %rbx, (%rsp)
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x99b1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
testq %r13, %r13
je 0x99be
movq %r13, %rdi
callq 0x7d9e
leaq 0x70(%rsp), %rdi
callq 0xcf40
movq (%rsp), %rax
movq (%rax), %rdi
cmpq 0x30(%rsp), %rdi
je 0x99e6
movq 0x30(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3460