// File: /workspace/github_cpp_makefile_star2_O1/xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::mergeAndWriteToDisk(std::shared_ptr<SSTable> const&, std::vector<std::shared_ptr<SSTable>, std::allocator<std::shared_ptr<SSTable>>> const&, unsigned long, std::unordered_map<unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>> const&)
vector<SSTPtr> KVStore::mergeAndWriteToDisk(const SSTPtr& upperLevelSST, const vector<SSTPtr>& lowerLevelSSTs,
                                            TimeStamp maxTimeStamp, const KVPair& data) {

    // Initialize.

    vector<SSTPtr> newSSTs;
    vector<LsmKey> sortedKeys;
    size_t currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
    size_t upperKeyNumber = upperLevelSST->getKeyNumber();
    size_t upperIndex = 0;
    size_t lowerKeyNumber;
    size_t lowerIndex;
    size_t lowerLevel = upperLevelSST->getLevel() + 1;
    KeyRefLessThan keyRefLessThan;

    // Function for merging and writing data to the disk. Used in 2-way merge below.

    auto merge = [&](const SSTPtr& sst, size_t& index) {
        LsmKey key = sst->getDataIndexes()[index].key;
        const LsmValue& value = data.at(key);
        if ((lowerLevel != ssTables.size() || value != DELETE_SIGN)
            && !sortedKeys.empty() && sortedKeys.back() != key) {
            size_t sizeIncrement = DATA_INDEX_SIZE + value.size();
            if (currentSize + sizeIncrement > MAX_SSTABLE_SIZE) {
                newSSTs.push_back(generateNewSST(sortedKeys, data, lowerLevel, maxTimeStamp));
                sortedKeys.clear();
                currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
            }
            sortedKeys.push_back(key);
            currentSize += sizeIncrement;
        }
        index++;
    };

    // 2-way merge sort and write data to the disk.

    for (const auto& lowerLevelSST : lowerLevelSSTs) {

        lowerIndex = 0;
        lowerKeyNumber = lowerLevelSST->getKeyNumber();

        if (sortedKeys.empty()) {
            LsmKey upperKey = upperLevelSST->getDataIndexes()[0].key;
            LsmKey lowerKey = lowerLevelSST->getDataIndexes()[0].key;
            if ((upperKey < lowerKey) ||
                (upperKey == lowerKey && upperLevelSST->getTimeStamp() < lowerLevelSST->getTimeStamp()))
                sortedKeys.push_back(upperKey);
            else
                sortedKeys.push_back(lowerKey);
        }

        while (upperIndex != upperKeyNumber && lowerIndex != lowerKeyNumber) {
            KeyRef upperKeyRef = make_pair(upperLevelSST, upperIndex);
            KeyRef lowerKeyRef = make_pair(lowerLevelSST, lowerIndex);

            if (keyRefLessThan(upperKeyRef, lowerKeyRef))
                merge(upperLevelSST, upperIndex);
            else
                merge(lowerLevelSST, lowerIndex);
        }

        while (lowerIndex != lowerKeyNumber)
            merge(lowerLevelSST, lowerIndex);
    }

    while (upperIndex != upperKeyNumber)
        merge(upperLevelSST, upperIndex);

    // Pack the remaining data into an SST.

    if (!sortedKeys.empty())
        newSSTs.push_back(generateNewSST(sortedKeys, data, lowerLevel, maxTimeStamp));

    return newSSTs;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %r13
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %r8, 0x78(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movq %rdi, 0x50(%rsp)
movups %xmm0, (%rdi)
movq %rax, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x2820, 0x98(%rsp) # imm = 0x2820
movq (%rdx), %rdi
callq 0x691c
movq %rax, %r12
movq $0x0, 0x8(%rsp)
movq (%r15), %rdi
callq 0x6906
incq %rax
leaq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, 0x80(%rsp)
movq %r13, 0xa0(%rsp)
movq %rcx, 0xa8(%rsp)
movq %r14, 0xb0(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq (%rbx), %r13
movq 0x8(%rbx), %rax
movq %rax, 0x88(%rsp)
cmpq %rax, %r13
je 0xc509
leaq 0x30(%rsp), %r14
leaq 0xa0(%rsp), %rbx
movq $0x0, 0x30(%rsp)
movq (%r13), %rdi
callq 0x691c
movq %rax, %rbp
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
jne 0xc409
movq (%r15), %rsi
leaq 0x38(%rsp), %rdi
callq 0x6922
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq (%rdi), %rax
movq %rax, 0x28(%rsp)
subq %rdi, %rsi
callq 0x3280
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
movq (%r13), %rsi
leaq 0x38(%rsp), %rdi
callq 0x6922
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq (%rdi), %rax
movq %rax, 0x28(%rsp)
subq %rdi, %rsi
callq 0x3280
movq 0x28(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, 0x58(%rsp)
jae 0xc3b5
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc3f3
movq 0x58(%rsp), %rax
jmp 0xc3e5
jne 0xc3d4
movq (%r15), %rdi
callq 0x690a
movq %rax, 0x28(%rsp)
movq (%r13), %rdi
callq 0x690a
cmpq %rax, 0x28(%rsp)
jb 0xc3a2
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc3fa
movq 0x70(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xc409
leaq 0x58(%rsp), %rdx
jmp 0xc3ff
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x5214
cmpq %r12, 0x8(%rsp)
je 0xc4f0
cmpq %rbp, 0x30(%rsp)
je 0xc4f0
movq (%r15), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0xc44a
movq 0x9b8c(%rip), %rcx      # 0x15fc8
cmpb $0x0, (%rcx)
je 0xc446
incl 0x8(%rax)
jmp 0xc44a
lock
incl 0x8(%rax)
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq (%r13), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%r13), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
je 0xc480
movq 0x9b56(%rip), %rcx      # 0x15fc8
cmpb $0x0, (%rcx)
je 0xc47c
incl 0x8(%rax)
jmp 0xc480
lock
incl 0x8(%rax)
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x7(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0xd1f2
testb %al, %al
movq %r13, %rsi
cmovneq %r15, %rsi
movq %r14, %rdx
leaq 0x8(%rsp), %rax
cmovneq %rax, %rdx
movq %rbx, %rdi
callq 0xc610
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xc4ca
callq 0x7d9e
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0xc409
callq 0x7d9e
jmp 0xc409
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xc610
cmpq %rbp, 0x30(%rsp)
jne 0xc4e2
addq $0x10, %r13
cmpq 0x88(%rsp), %r13
jne 0xc313
leaq 0xa0(%rsp), %rbx
leaq 0x8(%rsp), %r14
movq 0x80(%rsp), %r13
cmpq %r12, 0x8(%rsp)
je 0xc535
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xc610
jmp 0xc51e
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0xc57e
movq 0x90(%rsp), %rcx
movq 0x78(%rsp), %r8
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0xb444
leaq 0x38(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0xe9ba
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0xc57e
callq 0x7d9e
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc595
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq 0x50(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xc5d8
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
jmp 0xc5c1
movq %rax, %rbx
jmp 0xc5e7
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0xc5d8
callq 0x7d9e
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0xc5e7
callq 0x7d9e
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc5fe
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq 0x50(%rsp), %rdi
callq 0xcf40
movq %rbx, %rdi
callq 0x3460