// File: /workspace/github_cpp_makefile_star2_O2/xx01cyx[P]LSM-Tree/kvstore.cc
// Function: KVStore::generateNewSST(std::vector<unsigned long, std::allocator<unsigned long>> const&, 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&, unsigned long, unsigned long)
SSTPtr KVStore::generateNewSST(const vector<LsmKey> &keys, const KVPair& data,
                               size_t level, TimeStamp maxTimeStamp) {

    // Create the directory.
    string pathname = "./data/level-" + to_string(level) + "/";
    utils::mkdir(pathname.c_str());

    // Open the output file.
    string filename = pathname + "table-" + to_string(maxTimeStamp)
                      + "-" + to_string(keys.front())
                      + "-" + to_string(keys.back())
                      + ".sst";
    ofstream out(filename, ios::out | ios::binary);
    if (!out.is_open()) {
        cerr << "Open file failed." << endl;
        exit(-1);
    }

    // Initialize.
    size_t keyNumber = keys.size();
    SSTHeader sstHeader = SSTHeader(maxTimeStamp, keyNumber, keys.front(), keys.back());
    BloomFilter bloomFilter;
    vector<DataIndex> dataIndexes = vector<DataIndex>();
    uint32_t dataIndexStart = HEADER_SIZE + BLOOM_FILTER_SIZE;
    uint32_t dataStart = HEADER_SIZE + BLOOM_FILTER_SIZE + DATA_INDEX_SIZE * keyNumber;

    // Write header into the file.
    out.write((char*)&sstHeader, HEADER_SIZE);

    // Set the file position to tha start of data index.
    out.seekp(dataIndexStart, ios::beg);

    // Write data indexes into the file.
    uint32_t offset = dataStart;
    for (const auto& key : keys) {
        out.write((char*)&key, sizeof(key));
        out.write((char*)&offset, sizeof(offset));

        bloomFilter.insert(key);
        DataIndex dataIndex = DataIndex(key, offset);
        dataIndexes.push_back(dataIndex);

        offset += data.at(key).size();
    }

    // Write data into the file.
    for (const auto& key: keys) {
        LsmValue value = data.at(key);
        out.write((char*)&value[0], value.size());
    }

    // Write bloom filter into the file.
    out.seekp(HEADER_SIZE, ios::beg);
    out.write((char*)(bloomFilter.byteArray), BLOOM_FILTER_SIZE);

    // Close the file.
    out.close();

    // Return an SST.
    SSTPtr sst = make_shared<SSTable>(level, sstHeader, bloomFilter, dataIndexes);
    return sst;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movq %r8, %r12
movq %rdx, 0x18(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq %rcx, 0xc0(%rsp)
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
movq %rcx, %rsi
callq 0x5964
leaq 0x4d23(%rip), %rsi      # 0xe1b2
leaq 0x168(%rsp), %rdi
movq %rbx, %rdx
callq 0x5940
leaq 0x4e0c(%rip), %rdx      # 0xe2b2
leaq 0xe8(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x58ba
leaq 0x168(%rsp), %rdi
callq 0x34a8
leaq 0xc8(%rsp), %rdi
callq 0x34a8
movq 0xe8(%rsp), %rdi
callq 0x732e
leaq 0x4cd8(%rip), %rdx      # 0xe1c1
leaq 0xa0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x685d
leaq 0x148(%rsp), %rdi
movq %r12, %rsi
callq 0x5964
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x148(%rsp), %rdx
callq 0x58d9
leaq 0x4c97(%rip), %rdx      # 0xe1c6
leaq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x58ba
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
leaq 0x128(%rsp), %rdi
callq 0x5964
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x58d9
leaq 0x4c52(%rip), %rdx      # 0xe1c6
leaq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x58ba
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x108(%rsp), %rdi
callq 0x5964
leaq 0x168(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x58d9
leaq 0x4c04(%rip), %rdx      # 0xe1c8
leaq 0xc8(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x58ba
leaq 0x168(%rsp), %rdi
callq 0x34a8
leaq 0x108(%rsp), %rdi
callq 0x34a8
leaq 0x80(%rsp), %rdi
callq 0x34a8
leaq 0x20(%rsp), %rdi
callq 0x34a8
leaq 0x128(%rsp), %rdi
callq 0x34a8
leaq 0x40(%rsp), %rdi
callq 0x34a8
leaq 0x60(%rsp), %rdi
callq 0x34a8
leaq 0x148(%rsp), %rdi
callq 0x34a8
leaq 0xa0(%rsp), %rdi
callq 0x34a8
leaq 0x168(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x3480
leaq 0x1d8(%rsp), %rdi
callq 0x3050
testb %al, %al
je 0x9895
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rbx
movq -0x8(%rbx), %rcx
subq %rax, %rbx
sarq $0x3, %rbx
movq (%rax), %rax
movq %r12, 0x80(%rsp)
movq %rbx, 0x88(%rsp)
movq %rax, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x4c98
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x80(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x32c0
leaq 0x168(%rsp), %rdi
movl $0x2820, %esi           # imm = 0x2820
xorl %edx, %edx
callq 0x3240
imull $0xc, %ebx, %eax
addl $0x2820, %eax           # imm = 0x2820
leaq 0xa0(%rsp), %rcx
movl %eax, (%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %r13
movq 0x8(%rax), %r15
leaq 0x168(%rsp), %rbp
leaq 0x60(%rsp), %r14
leaq 0x40(%rsp), %r12
leaq 0x20(%rsp), %rbx
cmpq %r15, %r13
je 0x9793
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x8
popq %rdx
callq 0x32c0
movq %rbp, %rdi
leaq 0xa0(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x32c0
movq (%r13), %rsi
movq %r14, %rdi
callq 0x4cf6
movq (%r13), %rax
movl 0xa0(%rsp), %ecx
movq %rax, 0x20(%rsp)
movl %ecx, 0x28(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6896
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xcf9a
movl 0x8(%rax), %eax
addl %eax, 0xa0(%rsp)
addq $0x8, %r13
jmp 0x9725
movq 0x8(%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r12
leaq 0x20(%rsp), %r14
leaq 0x168(%rsp), %r15
cmpq %r12, %rbx
je 0x97e9
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0xcf9a
movq %r14, %rdi
movq %rax, %rsi
callq 0x32f0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r15, %rdi
callq 0x32c0
movq %r14, %rdi
callq 0x34a8
addq $0x8, %rbx
jmp 0x97ac
leaq 0x168(%rsp), %rdi
pushq $0x20
popq %rsi
xorl %edx, %edx
callq 0x3240
movq 0x60(%rsp), %rsi
leaq 0x168(%rsp), %rdi
movl $0x2800, %edx           # imm = 0x2800
callq 0x32c0
leaq 0x168(%rsp), %rdi
callq 0x3330
leaq 0xc0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x40(%rsp), %r8
movq 0x10(%rsp), %rdi
callq 0xa595
leaq 0x40(%rsp), %rdi
callq 0x5a44
leaq 0x60(%rsp), %rdi
callq 0x4cf4
leaq 0x168(%rsp), %rdi
callq 0x3360
leaq 0xc8(%rsp), %rdi
callq 0x34a8
leaq 0xe8(%rsp), %rdi
callq 0x34a8
movq 0x10(%rsp), %rax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb754(%rip), %rdi      # 0x14ff0
leaq 0x4a11(%rip), %rsi      # 0xe2b4
callq 0x3230
movq %rax, %rdi
callq 0x3120
pushq $-0x1
popq %rdi
callq 0x3280
jmp 0x98ba
movq %rax, %rbx
jmp 0x99b2
movq %rax, %rbx
jmp 0x99bf
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x34a8
jmp 0x98df
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x34a8
jmp 0x98f1
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x34a8
jmp 0x9903
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x34a8
jmp 0x9912
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x34a8
leaq 0x40(%rsp), %rdi
callq 0x34a8
leaq 0x60(%rsp), %rdi
callq 0x34a8
leaq 0x148(%rsp), %rdi
callq 0x34a8
leaq 0xa0(%rsp), %rdi
jmp 0x99c7
movq %rax, %rbx
jmp 0x991f
movq %rax, %rbx
jmp 0x9929
movq %rax, %rbx
jmp 0x9933
movq %rax, %rbx
jmp 0x9940
jmp 0x9960
movq %rax, %rbx
jmp 0x99cc
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x34a8
jmp 0x997a
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x99d4
jmp 0x999b
jmp 0x999b
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x34a8
jmp 0x999e
jmp 0x999b
jmp 0x999b
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5a44
leaq 0x60(%rsp), %rdi
callq 0x4cf4
leaq 0x168(%rsp), %rdi
callq 0x3360
leaq 0xc8(%rsp), %rdi
callq 0x34a8
leaq 0xe8(%rsp), %rdi
callq 0x34a8
movq %rbx, %rdi
callq 0x3440
nop