// File: /workspace/github_cpp_makefile_star2_O1/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 0xa90c
leaq 0x90(%rsp), %rbx
leaq 0x28(%rsp), %r14
movq 0xb70f(%rip), %rbp      # 0x15fc8
movq (%r12), %rax
movq 0x8(%r12), %rcx
testq %rcx, %rcx
je 0xa8d6
cmpb $0x0, (%rbp)
je 0xa8d2
incl 0x8(%rcx)
jmp 0xa8d6
lock
incl 0x8(%rcx)
movq %rax, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd082
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xa903
callq 0x7d9e
addq $0x10, %r12
cmpq %r13, %r12
jne 0xa8b9
movq 0x90(%rsp), %rbx
cmpq 0x98(%rsp), %rbx
je 0xabb4
movl $0x2820, %r14d          # imm = 0x2820
leaq 0x38(%rsp), %rbp
movq (%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x8(%rbx), %rsi
leaq 0x58(%rsp), %rdi
callq 0xe974
movq 0x10(%rbx), %rax
movq %rax, 0x60(%rsp)
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r13
testq %r13, %r13
je 0xa972
movq 0xb666(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0xa96d
incl 0x8(%r13)
jmp 0xa972
lock
incl 0x8(%r13)
movq 0x60(%rsp), %r12
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x6922
movq 0xb8(%rsp), %rax
movq %r12, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0xf5ae
movq %rbp, 0x28(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x558a
movq 0x8(%rsp), %rsi
cmpq %rsi, (%rsp)
jne 0xa9f7
cmpq 0x10(%rsp), %rsi
je 0xa9ea
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xa9f7
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x5214
movq 0xd0(%rsp), %rax
cmpq $0x2, 0x38(%rax)
jne 0xaa1f
leaq 0x28(%rsp), %rdi
leaq 0x5867(%rip), %rsi      # 0x10279
callq 0x30a0
testl %eax, %eax
je 0xaadc
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
je 0xaadc
movq -0x8(%rax), %rax
cmpq 0x20(%rsp), %rax
je 0xaadc
movq 0x30(%rsp), %rbp
leaq (%r14,%rbp), %rax
addq $0xc, %rax
cmpq $0x200001, %rax         # imm = 0x200001
jb 0xaaa4
movl $0x1, %ecx
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rsp, %rsi
movq %r15, %rdx
movq 0x70(%rsp), %r8
callq 0xb444
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xe9ba
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xaa8e
callq 0x7d9e
movq (%rsp), %rax
movl $0x2820, %r14d          # imm = 0x2820
cmpq %rax, 0x8(%rsp)
je 0xaaa4
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xaac3
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xaad0
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x5214
addq $0xc, %rbp
addq %rbp, %r14
leaq 0x38(%rsp), %rbp
leaq 0x90(%rsp), %rdi
callq 0xd104
movq 0xc0(%rsp), %rax
subq 0xb8(%rsp), %rax
sarq $0x4, %rax
decq %rax
cmpq %rax, %r12
je 0xab5d
movq %rbx, 0x78(%rsp)
movq %r13, 0x80(%rsp)
testq %r13, %r13
je 0xab2e
movq 0xb4aa(%rip), %rax      # 0x15fc8
cmpb $0x0, (%rax)
je 0xab29
incl 0x8(%r13)
jmp 0xab2e
lock
incl 0x8(%r13)
incq %r12
movq %r12, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xd082
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xab5d
callq 0x7d9e
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0xab74
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0xab91
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
testq %r13, %r13
je 0xab9e
movq %r13, %rdi
callq 0x7d9e
movq 0x90(%rsp), %rbx
cmpq 0x98(%rsp), %rbx
jne 0xa92d
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0xabf7
leaq 0x28(%rsp), %rdi
movq %rsp, %rsi
movl $0x1, %ecx
movq %r15, %rdx
movq 0x70(%rsp), %r8
callq 0xb444
leaq 0x28(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0xe9ba
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xabf7
callq 0x7d9e
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xac06
callq 0x7d9e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xac1c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x90(%rsp), %rdi
callq 0xd626
movq 0x48(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xac69
movq %rax, %rbx
jmp 0xacc6
jmp 0xac78
jmp 0xac4d
jmp 0xac78
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0xac7b
callq 0x7d9e
jmp 0xac7b
movq %rax, %rbx
jmp 0xacb9
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
jne 0xacc1
jmp 0xacc6
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xac9c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0xac9c
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0xacb9
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
testq %r13, %r13
je 0xacc6
movq %r13, %rdi
callq 0x7d9e
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xacd5
callq 0x7d9e
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xaceb
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x90(%rsp), %rdi
callq 0xd626
movq 0x48(%rsp), %rdi
callq 0xcf40
movq %rbx, %rdi
callq 0x3460