// File: /workspace/github_cpp_makefile_star2_O2/xx01cyx[P]LSM-Tree/MemTable.cc
// Function: MemTable::writeToDisk(unsigned long)
SSTPtr MemTable::writeToDisk(TimeStamp timeStamp) {

    // Create the directory.
    string pathname = "./data/level-0/";
    utils::mkdir(pathname.c_str());

    // Open the output file.
    string filename = pathname + "table-" + to_string(timeStamp) + ".sst";
    ofstream out(filename, ios::out | ios::binary);
    if (!out.is_open()) {
        cerr << "Open file failed." << endl;
        exit(-1);
    }

    // Initialize.
    SSTHeader sstHeader;
    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;

    // Set the file position to tha start of data index.
    out.seekp(dataIndexStart, ios::beg);

    // Write data indexes into the file.
    Node* q = getLowestHead();
    Node* p = q;
    uint32_t offset = dataStart;
    while (p->next) {
        LsmKey k = p->next->key;
        LsmValue v = p->next->value;

        out.write((char*)&k, sizeof(k));
        out.write((char*)&offset, sizeof(offset));

        bloomFilter.insert(k);
        DataIndex dataIndex = DataIndex(k, offset);
        dataIndexes.push_back(dataIndex);

        offset += v.size();
        p = p->next;
    }

    // After the loop, p now points to the max key.
    sstHeader = SSTHeader(timeStamp, keyNumber, q->next->key, p->key);

    // Set the file position to the beginning.
    out.seekp(0, ios::beg);

    // Write header and bloom filter into the file.
    out.write((char*)&sstHeader, HEADER_SIZE);
    out.write((char*)(bloomFilter.byteArray), BLOOM_FILTER_SIZE);

    // Set the file position to the start of data.
    out.seekp(dataStart, ios::beg);

    // Write data into the file.
    while (q->next) {
        LsmValue v = q->next->value;
        out.write((char*)&v[0], v.size());
        q = q->next;
    }

    // Close the file.
    out.close();

    // Create an SST in the memory.
    SSTPtr sst = make_shared<SSTable>(0, sstHeader, bloomFilter, dataIndexes);

    // Rename the file.
    string newFilename = sst->getFilename();
    rename(filename.c_str(), newFilename.c_str());

    return sst;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rdx, 0x60(%rsp)
movq %rsi, %r13
movq %rdi, %r14
leaq 0x7fdf(%rip), %rsi      # 0xe2a4
leaq 0xa8(%rsp), %rbx
leaq 0x158(%rsp), %rdx
movq %rbx, %rdi
callq 0x37ce
movq (%rbx), %rbx
leaq 0x7d27(%rip), %rsi      # 0xe00e
leaq 0x40(%rsp), %rdi
leaq 0x158(%rsp), %rdx
callq 0x37ce
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xc8(%rsp), %rdi
leaq 0x70(%rsp), %rdx
movq %rbx, %rsi
callq 0x37ce
movq %r14, 0x90(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
pushq $0x18
popq %rdx
callq 0x33a0
leaq 0xc8(%rsp), %rdi
callq 0x34a8
leaq 0x10(%rsp), %rbp
pushq $0x2f
popq %rbx
leaq 0x40(%rsp), %r14
leaq 0x70(%rsp), %r15
leaq 0x7f4d(%rip), %r12      # 0xe2b2
movq %r13, 0x68(%rsp)
leaq 0x158(%rsp), %rdi
movq %rbp, %rsi
movl %ebx, %edx
callq 0x3460
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x6404
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3490
movq %r15, %rdi
movq %r14, %rsi
callq 0x32f0
movq 0x70(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3200
testl %eax, %eax
jne 0x63d0
movl 0xe0(%rsp), %eax
movl $0x4000, %ecx           # imm = 0x4000
andl %ecx, %eax
je 0x63d0
movq %r15, %rdi
callq 0x34a8
jmp 0x63f4
movq 0x40(%rsp), %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x3100
movl %eax, %r13d
movq %r15, %rdi
callq 0x34a8
testl %r13d, %r13d
movq 0x68(%rsp), %r13
jne 0x6404
movq %r14, %rdi
movq %r12, %rsi
callq 0x3470
jmp 0x636a
leaq 0x158(%rsp), %rdi
callq 0x31a0
leaq 0x10(%rsp), %rdi
callq 0x34a8
leaq 0x40(%rsp), %rdi
callq 0x34a8
leaq 0x7d95(%rip), %rdx      # 0xe1c1
leaq 0x40(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x685d
leaq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x5964
leaq 0x158(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x58d9
leaq 0x7d5d(%rip), %rdx      # 0xe1c8
leaq 0xc8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x58ba
leaq 0x158(%rsp), %rdi
callq 0x34a8
leaq 0x10(%rsp), %rdi
callq 0x34a8
leaq 0x40(%rsp), %rdi
callq 0x34a8
leaq 0x158(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x3480
leaq 0x1c8(%rsp), %rdi
callq 0x3050
testb %al, %al
je 0x6734
leaq 0x8(%rsp), %rdi
callq 0x4c98
andq $0x0, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
imull $0xc, 0x8(%r13), %ebx
leaq 0x158(%rsp), %rdi
movl $0x2820, %esi           # imm = 0x2820
xorl %edx, %edx
callq 0x3240
addl $0x2820, %ebx           # imm = 0x2820
movq %r13, %rdi
callq 0x6832
movq %rax, %r13
leaq 0x4(%rsp), %rax
movl %ebx, 0x3c(%rsp)
movl %ebx, (%rax)
leaq 0x10(%rsp), %r14
leaq 0x158(%rsp), %r15
leaq 0x70(%rsp), %rbp
leaq 0x98(%rsp), %rbx
movq %r13, %r12
movq 0x28(%r12), %rsi
testq %rsi, %rsi
je 0x65c9
movq (%rsi), %rax
movq %rax, 0x30(%rsp)
addq $0x8, %rsi
movq %r14, %rdi
callq 0x32f0
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x32c0
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x32c0
movq 0x30(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x4cf6
movq 0x30(%rsp), %rax
movl 0x4(%rsp), %ecx
movq %rax, 0x98(%rsp)
movl %ecx, 0xa0(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x6896
movl 0x18(%rsp), %eax
addl %eax, 0x4(%rsp)
movq 0x28(%r12), %r12
movq %r14, %rdi
callq 0x34a8
jmp 0x653b
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x28(%r13), %rcx
movq (%rcx), %rcx
movq (%r12), %rdx
movq 0x60(%rsp), %rsi
movq %rsi, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3240
movq 0x90(%rsp), %r15
leaq 0x158(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x32c0
movq 0x8(%rsp), %rsi
leaq 0x158(%rsp), %rdi
movl $0x2800, %edx           # imm = 0x2800
callq 0x32c0
movl 0x3c(%rsp), %esi
leaq 0x158(%rsp), %rdi
xorl %edx, %edx
callq 0x3240
leaq 0x10(%rsp), %rbx
leaq 0x158(%rsp), %r14
movq 0x28(%r13), %rsi
testq %rsi, %rsi
je 0x6690
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x32f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x32c0
movq 0x28(%r13), %r13
movq %rbx, %rdi
callq 0x34a8
jmp 0x665b
leaq 0x158(%rsp), %rdi
callq 0x3330
leaq 0x10(%rsp), %rsi
andl $0x0, (%rsi)
leaq 0x40(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x70(%rsp), %r8
movq %r15, %rdi
callq 0x68b3
movq (%r15), %rsi
leaq 0x10(%rsp), %rdi
callq 0x533e
leaq 0xc8(%rsp), %rbx
movq (%rbx), %rdi
leaq 0x10(%rsp), %r14
movq (%r14), %rsi
callq 0x3160
movq %r14, %rdi
callq 0x34a8
leaq 0x70(%rsp), %rdi
callq 0x5a44
leaq 0x8(%rsp), %rdi
callq 0x4cf4
leaq 0x158(%rsp), %rdi
callq 0x3360
movq %rbx, %rdi
callq 0x34a8
leaq 0xa8(%rsp), %rdi
callq 0x34a8
movq %r15, %rax
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe8b5(%rip), %rdi      # 0x14ff0
leaq 0x7b72(%rip), %rsi      # 0xe2b4
callq 0x3230
movq %rax, %rdi
callq 0x3120
pushq $-0x1
popq %rdi
callq 0x3280
jmp 0x6771
movq %rax, %r14
addq $0x8, %r15
movq %r15, %rdi
callq 0x6932
jmp 0x67ee
jmp 0x67bb
jmp 0x67bb
movq %rax, %r14
jmp 0x6802
movq %rax, %r14
jmp 0x680f
movq %rax, %r14
leaq 0x158(%rsp), %rdi
jmp 0x67a2
jmp 0x67a9
movq %rax, %r14
jmp 0x67da
jmp 0x67ae
movq %rax, %r14
leaq 0xc8(%rsp), %rdi
callq 0x34a8
jmp 0x67d0
movq %rax, %r14
jmp 0x67d0
movq %rax, %r14
jmp 0x681c
jmp 0x67bb
jmp 0x67bb
jmp 0x67e1
jmp 0x67e1
movq %rax, %r14
jmp 0x67ee
movq %rax, %r14
leaq 0x158(%rsp), %rdi
callq 0x31a0
leaq 0x10(%rsp), %rdi
callq 0x34a8
leaq 0x40(%rsp), %rdi
jmp 0x6817
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0x34a8
leaq 0x70(%rsp), %rdi
callq 0x5a44
leaq 0x8(%rsp), %rdi
callq 0x4cf4
leaq 0x158(%rsp), %rdi
callq 0x3360
leaq 0xc8(%rsp), %rdi
callq 0x34a8
leaq 0xa8(%rsp), %rdi
callq 0x34a8
movq %r14, %rdi
callq 0x3440
nop