// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, 0xd0(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x8f08(%rip), %rsi      # 0x10283
leaq 0x8f10(%rip), %rdx      # 0x10292
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x561a
movq (%rbx), %rsi
leaq 0x40(%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 0x3abc
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 0x7402
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x70(%rsp), %r13
leaq 0x10(%rsp), %rbp
leaq 0x30(%rsp), %r12
leaq 0x60(%rsp), %r14
leaq 0xd8(%rsp), %rbx
movq %r15, 0x98(%rsp)
leaq 0x168(%rsp), %rdi
movq %rbp, %rsi
movl $0x2f, %edx
callq 0x3480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x7503
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rdi
callq 0x30c0
movq %r13, 0x60(%rsp)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x558a
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x3220
testl %eax, %eax
jne 0x74b3
movl 0xf0(%rsp), %eax
movl $0x4000, %ecx           # imm = 0x4000
andl %ecx, %eax
je 0x74b3
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
je 0x74ef
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x74ef
movq %r14, %r15
movq 0x30(%rsp), %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x3120
movl %eax, %r14d
movq 0x60(%rsp), %rdi
cmpq %r13, %rdi
je 0x74df
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3280
testl %r14d, %r14d
movq %r15, %r14
movq 0x98(%rsp), %r15
jne 0x7503
movq %r12, %rdi
leaq 0x8d98(%rip), %rsi      # 0x10291
callq 0x3490
jmp 0x7426
movq 0xea96(%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 0x7540
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x755c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
movq %rax, (%rdi)
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
addq %rsi, %rdx
callq 0x558a
leaq 0x8c14(%rip), %rsi      # 0x1019c
leaq 0x30(%rsp), %rdi
callq 0x3490
movl $0x1, %esi
cmpq $0xa, 0x80(%rsp)
jb 0x75f6
movl $0x4, %esi
movabsq $0x346dc5d63886594b, %rdi # imm = 0x346DC5D63886594B
movq 0x80(%rsp), %rcx
cmpq $0x63, %rcx
jbe 0x75ef
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x75f4
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x75f6
movq %rcx, %rax
mulq %rdi
shrq $0xb, %rdx
addl $0x4, %esi
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x75b9
addl $-0x3, %esi
jmp 0x75f6
addl $-0x2, %esi
jmp 0x75f6
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 0x6a22
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x18(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
leaq 0x40(%rsp), %rdi
cmpq %rdi, %rcx
je 0x7649
movq 0x40(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x7669
movl $0xf, %esi
leaq 0x20(%rsp), %rdi
cmpq %rdi, 0x10(%rsp)
je 0x7664
movq 0x20(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x767a
movq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x30c0
jmp 0x7688
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 0x76b5
movq %rsi, 0x168(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x178(%rsp)
jmp 0x76bb
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 0x8ac1(%rip), %rsi      # 0x101a3
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 0x7723
movq %rdx, 0xd8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xe8(%rsp)
jmp 0x7729
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 0x7765
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7781
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x779d
movq 0x40(%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 0x7b6d
leaq 0x58(%rsp), %rdi
callq 0x56ac
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movl 0x8(%r15), %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 (%r15), %rax
movq %rax, %r13
movq 0x30(%rax), %rax
testq %rax, %rax
jne 0x780c
movl %ecx, 0x94(%rsp)
movl %ecx, 0x54(%rsp)
movq 0x28(%r13), %rax
movq %r13, %r14
testq %rax, %rax
je 0x792c
leaq 0x28(%r13), %rbx
leaq 0x168(%rsp), %r12
leaq 0x54(%rsp), %r15
leaq 0x58(%rsp), %rbp
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 0x558a
movl $0x8, %edx
movq %r12, %rdi
leaq 0x88(%rsp), %rsi
callq 0x3300
movl $0x4, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x3300
movq 0x88(%rsp), %rsi
movq %rbp, %rdi
callq 0x570a
movq 0x88(%rsp), %rax
movl 0x54(%rsp), %ecx
movq %rax, 0xa0(%rsp)
movl %ecx, 0xa8(%rsp)
movq 0x68(%rsp), %rsi
cmpq 0x70(%rsp), %rsi
je 0x78e2
movups 0xa0(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x68(%rsp)
jmp 0x78f4
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x8198
movl 0x18(%rsp), %eax
addl %eax, 0x54(%rsp)
movq (%rbx), %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x791b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0x28(%r14), %rbx
movq 0x28(%r14), %rax
testq %rax, %rax
jne 0x7849
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x28(%r13), %rcx
movq (%rcx), %rcx
movq (%r14), %rdx
movq 0x80(%rsp), %rsi
movq %rsi, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3260
leaq 0x168(%rsp), %rdi
leaq 0x30(%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 0x28(%r13), %rax
testq %rax, %rax
je 0x7a29
addq $0x28, %r13
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 0x558a
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x3300
movq (%r13), %r13
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7a1c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x28(%r13), %rax
addq $0x28, %r13
testq %rax, %rax
jne 0x79cd
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 0x30(%rsp), %r8
leaq 0x58(%rsp), %r9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x82bc
movq (%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x5cf4
movq 0xd8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3180
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7ab5
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7acc
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x58(%rsp), %rdi
callq 0x5708
movq 0xe4e3(%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 0x7b33
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7b58
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 0xe47c(%rip), %rdi      # 0x15ff0
leaq 0x8718(%rip), %rsi      # 0x10293
movl $0x11, %edx
callq 0x32b0
movq 0xe464(%rip), %rdi      # 0x15ff0
callq 0x3140
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x32d0
jmp 0x7bc0
movq %rax, %rbx
movq (%r14), %rdi
testq %rdi, %rdi
je 0x7c67
callq 0x7d9e
jmp 0x7c67
jmp 0x7c43
jmp 0x7c43
movq %rax, %rbx
jmp 0x7c88
movq %rax, %rbx
jmp 0x7c95
movq %rax, %rbx
leaq 0x178(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7cd8
movq 0x178(%rsp), %rsi
jmp 0x7c26
jmp 0x7c33
jmp 0x7bf6
movq %rax, %rbx
jmp 0x7cf4
movq %rax, %rbx
jmp 0x7d10
movq %rax, %rbx
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7cd8
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x7cd8
movq %rax, %rbx
jmp 0x7cd8
jmp 0x7c43
jmp 0x7c48
jmp 0x7c43
jmp 0x7c48
movq %rax, %rbx
jmp 0x7c67
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x7c67
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7c7e
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x3280
leaq 0x58(%rsp), %rdi
callq 0x5708
leaq 0x168(%rsp), %rdi
callq 0x3380
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x7d10
movq 0xe8(%rsp), %rsi
jmp 0x7d08
movq %rax, %rbx
movq 0xe2e2(%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 0x7cf4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x7d10
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x7d35
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3460
nop