// File: /workspace/github_cpp_makefile_star2_O3/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 $0x368, %rsp            # imm = 0x368
movq %rdx, 0x80(%rsp)
movq %rsi, %r14
movq %rdi, 0xd0(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x903a(%rip), %rsi      # 0x102a3
leaq 0x9042(%rip), %rdx      # 0x102b2
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x5648
movq (%rbx), %rsi
leaq 0x48(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x20(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xd8(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x3a9c
leaq 0x168(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x18, %edx
callq 0x33c0
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x72f0
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x70(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x38(%rsp), %r15
leaq 0xd8(%rsp), %rbp
leaq 0x8fa3(%rip), %rbx      # 0x102b1
movq %r14, 0x98(%rsp)
leaq 0x168(%rsp), %rdi
movq %r13, %rsi
movl $0x2f, %edx
callq 0x3480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x73eb
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rdi
callq 0x30c0
movq %r12, 0x60(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x55b2
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x3220
testl %eax, %eax
jne 0x73a5
movl 0xf0(%rsp), %eax
movl $0x4000, %ecx           # imm = 0x4000
andl %ecx, %eax
je 0x73a5
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x73db
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x73db
movq 0x38(%rsp), %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x3120
movl %eax, %r14d
movq 0x60(%rsp), %rdi
cmpq %r12, %rdi
je 0x73ce
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3280
testl %r14d, %r14d
movq 0x98(%rsp), %r14
jne 0x73eb
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3490
jmp 0x7316
movq 0xebae(%rip), %rsi      # 0x15fa0
leaq 0x168(%rsp), %rdi
callq 0x31c0
leaq 0x1e8(%rsp), %rdi
callq 0x30d0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7428
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x7444
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
movq %rax, (%rdi)
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
addq %rsi, %rdx
callq 0x55b2
leaq 0x8d4c(%rip), %rsi      # 0x101bc
leaq 0x38(%rsp), %rdi
callq 0x3490
movl $0x1, %esi
cmpq $0xa, 0x80(%rsp)
jb 0x74de
movl $0x4, %esi
movabsq $0x346dc5d63886594b, %rdi # imm = 0x346DC5D63886594B
movq 0x80(%rsp), %rcx
cmpq $0x63, %rcx
jbe 0x74d7
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x74dc
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x74de
movq %rcx, %rax
mulq %rdi
shrq $0xb, %rdx
addl $0x4, %esi
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x74a1
addl $-0x3, %esi
jmp 0x74de
addl $-0x2, %esi
jmp 0x74de
decl %esi
movl %esi, %esi
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
movq %rax, (%rdi)
xorl %edx, %edx
callq 0x32c0
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %esi
movq 0x80(%rsp), %rdx
callq 0x6978
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x18(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
leaq 0x48(%rsp), %rsi
cmpq %rsi, %rcx
je 0x7531
movq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x7554
movl $0xf, %edi
leaq 0x20(%rsp), %r9
cmpq %r9, %rsi
je 0x754f
movq 0x20(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x7560
leaq 0x38(%rsp), %rdi
callq 0x30c0
jmp 0x756e
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32a0
leaq 0x178(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x759b
movq %rsi, 0x168(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x178(%rsp)
jmp 0x75a1
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x170(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq 0x8bfb(%rip), %rsi      # 0x101c3
leaq 0x168(%rsp), %rdi
callq 0x3490
leaq 0xe8(%rsp), %rsi
movq %rsi, 0xd8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x7609
movq %rdx, 0xd8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xe8(%rsp)
jmp 0x760f
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0xe0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x764b
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7667
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x7683
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x168(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
movl $0x14, %edx
callq 0x34a0
leaq 0x1d8(%rsp), %rdi
callq 0x3060
testb %al, %al
je 0x7a51
leaq 0x58(%rsp), %rdi
callq 0x56e0
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movl 0x8(%r14), %ebx
leaq 0x168(%rsp), %rdi
movl $0x2820, %esi           # imm = 0x2820
xorl %edx, %edx
callq 0x3260
leal (%rbx,%rbx,2), %eax
leal 0x2820(,%rax,4), %ecx
movq (%r14), %rax
movq %rax, %r12
movq 0x30(%rax), %rax
testq %rax, %rax
jne 0x76f2
leaq 0x34(%rsp), %rax
movl %ecx, 0x94(%rsp)
movl %ecx, (%rax)
movq 0x28(%r12), %rax
addq $0x28, %r12
leaq 0x168(%rsp), %r15
leaq 0x88(%rsp), %r14
leaq 0x58(%rsp), %rbx
movq %r12, %r13
movq (%rax), %rcx
movq %rcx, 0x88(%rsp)
leaq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x55b2
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movl $0x4, %edx
movq %r15, %rdi
leaq 0x34(%rsp), %rsi
callq 0x3300
movq 0x88(%rsp), %rsi
movq %rbx, %rdi
callq 0x573e
movq 0x88(%rsp), %rcx
movl 0x34(%rsp), %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0xa8(%rsp)
movq 0x68(%rsp), %rsi
cmpq 0x70(%rsp), %rsi
je 0x77c3
movups 0xa0(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x68(%rsp)
jmp 0x77d9
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x8070
movl 0x34(%rsp), %eax
addl 0x18(%rsp), %eax
movl %eax, 0x34(%rsp)
movq (%r13), %rbp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7801
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x28(%rbp), %r13
movq 0x28(%rbp), %rax
testq %rax, %rax
jne 0x772d
movq (%r12), %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq (%rax), %rax
movq (%rbp), %rdx
movq 0x80(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
leaq 0x168(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl $0x20, %edx
callq 0x3300
movq 0x58(%rsp), %rsi
leaq 0x168(%rsp), %rdi
movl $0x2800, %edx           # imm = 0x2800
callq 0x3300
movl 0x94(%rsp), %esi
leaq 0x168(%rsp), %rdi
xorl %edx, %edx
callq 0x3260
movq (%r12), %rax
testq %rax, %rax
je 0x790d
leaq 0x10(%rsp), %rbx
leaq 0x168(%rsp), %r14
leaq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x55b2
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x3300
movq (%r12), %r12
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x78ff
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x28(%r12), %rax
addq $0x28, %r12
testq %rax, %rax
jne 0x78b0
leaq 0x168(%rsp), %rdi
callq 0x3350
leaq 0x10(%rsp), %rcx
movl $0x0, (%rcx)
movq 0xd0(%rsp), %rbx
movq $0x0, (%rbx)
leaq 0x8(%rbx), %r14
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
leaq 0xa0(%rsp), %rdx
leaq 0x38(%rsp), %r8
leaq 0x58(%rsp), %r9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8194
movq (%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x5c48
movq 0xd8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3180
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7999
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x79b0
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x58(%rsp), %rdi
callq 0x573c
movq 0xe5ff(%rip), %rax      # 0x15fc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x170(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x168(%rsp,%rcx)
callq 0x31f0
leaq 0x260(%rsp), %rdi
callq 0x30d0
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x7a17
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7a3c
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe598(%rip), %rdi      # 0x15ff0
leaq 0x8854(%rip), %rsi      # 0x102b3
movl $0x11, %edx
callq 0x32b0
movq 0xe580(%rip), %rdi      # 0x15ff0
callq 0x3140
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x32d0
jmp 0x7a9e
movq %rax, %rbx
movq (%r14), %rdi
testq %rdi, %rdi
je 0x7b8d
callq 0x7c76
jmp 0x7b8d
jmp 0x7b13
jmp 0x7b13
movq %rax, %rbx
jmp 0x7bae
movq %rax, %rbx
jmp 0x7bbb
movq %rax, %rbx
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7b3c
movq 0x178(%rsp), %rsi
jmp 0x7afc
jmp 0x7b06
jmp 0x7ad0
movq %rax, %rbx
jmp 0x7b58
movq %rax, %rbx
jmp 0x7be0
movq %rax, %rbx
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7b3c
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x7b3c
movq %rax, %rbx
jmp 0x7b3c
jmp 0x7b13
jmp 0x7b6e
jmp 0x7b13
jmp 0x7b6e
movq %rax, %rbx
jmp 0x7b8d
movq %rax, %rbx
movq 0xe47e(%rip), %rsi      # 0x15fa0
leaq 0x168(%rsp), %rdi
callq 0x31c0
leaq 0x1e8(%rsp), %rdi
callq 0x30d0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7b58
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x7be0
movq 0x48(%rsp), %rsi
jmp 0x7bd8
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7b8d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7ba4
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x58(%rsp), %rdi
callq 0x573c
leaq 0x168(%rsp), %rdi
callq 0x3380
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x7be0
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7c05
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3460
nop