// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %r9, %r13
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movq %r8, 0x88(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rax, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq $0x2820, 0xa8(%rsp) # imm = 0x2820
movq (%rdx), %rdi
callq 0x6872
movq %rax, 0x60(%rsp)
movq $0x0, 0x38(%rsp)
movq (%r15), %rdi
callq 0x685c
incq %rax
leaq 0xa0(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, 0x90(%rsp)
movq %r13, 0xb0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r14, 0xc0(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq %r12, (%rsp)
movq %r12, 0xd8(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq (%rbx), %r13
movq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
cmpq %rax, %r13
je 0xc3f6
xorl %r12d, %r12d
leaq 0x58(%rsp), %rbx
leaq 0xb0(%rsp), %rbp
movq $0x0, 0x58(%rsp)
movq (%r13), %rdi
callq 0x6872
movq %rax, %r14
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
jne 0xc2e2
movq (%r15), %rsi
leaq 0x40(%rsp), %rdi
callq 0x6878
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
movq %rax, 0x30(%rsp)
subq %rdi, %rsi
callq 0x3280
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
movq (%r13), %rsi
leaq 0x40(%rsp), %rdi
callq 0x6878
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
movq %rax, 0x30(%rsp)
subq %rdi, %rsi
callq 0x3280
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq %rax, 0x68(%rsp)
jae 0xc288
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc2c9
movq 0x68(%rsp), %rax
jmp 0xc2bb
jne 0xc2a7
movq (%r15), %rdi
callq 0x6860
movq %rax, 0x30(%rsp)
movq (%r13), %rdi
callq 0x6860
cmpq %rax, 0x30(%rsp)
jb 0xc275
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0xc2d0
movq 0x80(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0xc2e2
leaq 0x68(%rsp), %rdx
jmp 0xc2d8
leaq 0x80(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x5216
cmpq 0x60(%rsp), %r12
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0xc3cc
movq (%r15), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0xc328
movq 0x9cb3(%rip), %rcx      # 0x15fc8
cmpb $0x0, (%rcx)
je 0xc31f
incl 0x8(%rax)
jmp 0xc328
lock
incl 0x8(%rax)
movq 0x38(%rsp), %r12
movq %r12, 0x50(%rsp)
movq (%r13), %rax
movq %rax, 0x68(%rsp)
movq 0x8(%r13), %rax
movq %rax, 0x70(%rsp)
testq %rax, %rax
leaq 0x40(%rsp), %rsi
je 0xc35e
movq 0x9c78(%rip), %rcx      # 0x15fc8
cmpb $0x0, (%rcx)
je 0xc35a
incl 0x8(%rax)
jmp 0xc35e
lock
incl 0x8(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0xf(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0xd110
testb %al, %al
movq %r13, %rax
cmovneq %r15, %rax
movq %rbx, %rdx
leaq 0x38(%rsp), %rcx
cmovneq %rcx, %rdx
movq (%rax), %rsi
movq %rbp, %rdi
callq 0xc51e
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0xc3a6
callq 0x7c76
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xc3b5
callq 0x7c76
movq 0x38(%rsp), %r12
cmpq 0x60(%rsp), %r12
je 0xc3cc
cmpq %r14, 0x58(%rsp)
jne 0xc2f8
cmpq %r14, 0x58(%rsp)
je 0xc3e4
movq (%r13), %rsi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0xc51e
jmp 0xc3cc
addq $0x10, %r13
cmpq 0x98(%rsp), %r13
jne 0xc1e3
leaq 0xb0(%rsp), %rbx
leaq 0x38(%rsp), %r14
movq 0x60(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0xc41f
movq (%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xc51e
jmp 0xc403
movq 0x10(%rsp), %rdi
cmpq 0x18(%rsp), %rdi
movq (%rsp), %rbx
je 0xc477
movq 0xa0(%rsp), %rcx
movq 0x88(%rsp), %r8
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x90(%rsp), %rdx
callq 0xb2d0
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xc472
callq 0x7c76
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc489
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq %rbx, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xc4cb
callq 0x7c76
jmp 0xc4cb
jmp 0xc4c8
movq %rax, %rbx
jmp 0xc4f6
movq %rax, %rbx
jmp 0xc50d
jmp 0xc4c8
jmp 0xc4c8
jmp 0xc4c8
jmp 0xc4c8
jmp 0xc4c8
movq %rax, %rbx
movq (%rsp), %r12
jmp 0xc4f6
movq %rax, %rbx
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0xc4e3
callq 0x7c76
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq (%rsp), %r12
je 0xc4f6
callq 0x7c76
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc50d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
movq %r12, %rdi
callq 0xce64
movq %rbx, %rdi
callq 0x3460
nop