// File: /workspace/github_cpp_makefile_star2_O3/xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::merge0AndWriteToDisk(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::merge0AndWriteToDisk(const vector<SSTPtr> &SSTs, TimeStamp maxTimeStamp, const KVPair& data) {

    vector<SSTPtr> newSSTs;
    priority_queue<KeyRef, vector<KeyRef>, KeyRefGreaterThan> pq;
    vector<LsmKey> sortedKeys;
    KeyRef currentRef;
    size_t currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;

    for (const auto & SST : SSTs)
        pq.push(make_pair(SST, 0));

    while (!pq.empty()) {
        currentRef = pq.top();
        SSTPtr currentSST = currentRef.first;
        size_t currentIndex = currentRef.second;
        vector<DataIndex> currentDataIndexes = currentSST->getDataIndexes();
        LsmKey currentKey = currentDataIndexes[currentIndex].key;
        LsmValue currentValue = data.at(currentKey);

        if (sortedKeys.empty())
            sortedKeys.push_back(currentKey);

        if ((ssTables.size() != 2 || currentValue != DELETE_SIGN)
            && !sortedKeys.empty() && sortedKeys.back() != currentKey) {
            size_t sizeIncrement = DATA_INDEX_SIZE + currentValue.size();
            if (currentSize + sizeIncrement > MAX_SSTABLE_SIZE) {
                newSSTs.push_back(generateNewSST(sortedKeys, data, 1, maxTimeStamp));
                sortedKeys.clear();
                currentSize = HEADER_SIZE + BLOOM_FILTER_SIZE;
            }
            sortedKeys.push_back(currentKey);
            currentSize += sizeIncrement;
        }

        pq.pop();
        if (currentIndex != currentDataIndexes.size() - 1)
            pq.push(make_pair(currentSST, currentIndex + 1));
    }

    // Pack the remaining data into an SST.
    if (!sortedKeys.empty())
        newSSTs.push_back(generateNewSST(sortedKeys, data, 1, maxTimeStamp));

    return newSSTs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, %r15
movq %rcx, 0x70(%rsp)
movq %rsi, 0xd0(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movq %rdi, 0x48(%rsp)
movups %xmm0, (%rdi)
movq %rax, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rax, 0x10(%rsp)
movaps %xmm0, (%rsp)
movq %rax, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq (%rdx), %r12
movq 0x8(%rdx), %r13
cmpq %r13, %r12
je 0xaa3b
leaq 0x90(%rsp), %rbx
leaq 0x28(%rsp), %r14
movq (%r12), %rax
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0xa749
movq 0xb88d(%rip), %rdx      # 0x15fc8
cmpb $0x0, (%rdx)
je 0xa745
incl 0x8(%rcx)
jmp 0xa749
lock
incl 0x8(%rcx)
movq %rax, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcfae
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa776
callq 0x7c76
addq $0x10, %r12
cmpq %r13, %r12
jne 0xa726
movq 0x90(%rsp), %rbx
cmpq 0x98(%rsp), %rbx
je 0xaa3b
movl $0x2820, %r14d          # imm = 0x2820
leaq 0x38(%rsp), %r13
movq (%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x8(%rbx), %rsi
leaq 0x58(%rsp), %rdi
callq 0xe856
movq 0x10(%rbx), %rbp
movq %rbp, 0x60(%rsp)
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r12
testq %r12, %r12
je 0xa7ec
movq 0xb7f3(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0xa7e1
incl 0x8(%r12)
jmp 0xa7ec
lock
incl 0x8(%r12)
movq 0x60(%rsp), %rbp
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x6878
movq 0xb8(%rsp), %rax
movq %rbp, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0xf438
testq %rax, %rax
je 0xaac7
movq %r13, 0x28(%rsp)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x55b2
movq (%rsp), %rax
movq 0x8(%rsp), %rsi
cmpq %rsi, %rax
jne 0xa879
cmpq 0x10(%rsp), %rax
je 0xa86c
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xa879
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x5216
movq 0xd0(%rsp), %rax
cmpq $0x2, 0x38(%rax)
jne 0xa8a1
leaq 0x28(%rsp), %rdi
leaq 0x5a05(%rip), %rsi      # 0x10299
callq 0x30a0
testl %eax, %eax
je 0xa961
movq 0x8(%rsp), %rsi
cmpq %rsi, (%rsp)
je 0xa961
movq -0x8(%rsi), %rax
cmpq 0x20(%rsp), %rax
je 0xa961
movq 0x30(%rsp), %r13
leaq (%r14,%r13), %rax
addq $0xc, %rax
cmpq $0x200001, %rax         # imm = 0x200001
jb 0xa92e
movl $0x1, %ecx
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rsp, %rsi
movq %r15, %rdx
movq 0x70(%rsp), %r8
callq 0xb2d0
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xe8a0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xa910
callq 0x7c76
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
movl $0x2820, %r14d          # imm = 0x2820
cmpq %rsi, %rax
je 0xa92b
movq %rsi, 0x8(%rsp)
jmp 0xa92e
movq %rax, %rsi
cmpq 0x10(%rsp), %rsi
je 0xa948
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xa955
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x5216
addq $0xc, %r13
addq %r13, %r14
leaq 0x38(%rsp), %r13
leaq 0x90(%rsp), %rdi
callq 0xd030
movq 0xc0(%rsp), %rax
subq 0xb8(%rsp), %rax
sarq $0x4, %rax
decq %rax
cmpq %rax, %rbp
je 0xa9e4
movq %rbx, 0x78(%rsp)
movq %r12, 0x80(%rsp)
testq %r12, %r12
je 0xa9b5
movq 0xb625(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0xa9af
incl 0x8(%r12)
jmp 0xa9b5
lock
incl 0x8(%r12)
incq %rbp
movq %rbp, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xcfae
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xa9e4
callq 0x7c76
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xa9fb
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0xaa18
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
testq %r12, %r12
je 0xaa25
movq %r12, %rdi
callq 0x7c76
movq 0x90(%rsp), %rbx
cmpq 0x98(%rsp), %rbx
jne 0xa7a0
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0xaa7e
leaq 0x28(%rsp), %rdi
movq %rsp, %rsi
movl $0x1, %ecx
movq %r15, %rdx
movq 0x70(%rsp), %r8
callq 0xb2d0
leaq 0x28(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0xe8a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xaa7e
callq 0x7c76
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xaa8d
callq 0x7c76
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xaaa3
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x90(%rsp), %rdi
callq 0xd53e
movq 0x48(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5994(%rip), %rdi      # 0x10462
callq 0x3360
jmp 0xab22
movq %rax, %rbx
jmp 0xab5e
jmp 0xab01
jmp 0xaae3
jmp 0xab01
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xab04
callq 0x7c76
jmp 0xab04
movq %rax, %rbx
jmp 0xab51
jmp 0xab31
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xab34
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0xab34
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
jne 0xab59
jmp 0xab5e
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0xab51
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
testq %r12, %r12
je 0xab5e
movq %r12, %rdi
callq 0x7c76
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xab6d
callq 0x7c76
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xab83
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x90(%rsp), %rdi
callq 0xd53e
movq 0x48(%rsp), %rdi
callq 0xce64
movq %rbx, %rdi
callq 0x3460