// File: prakhar2808[P]DBEngine/dbengine/sstableWrapper.cc
// Function: sstableWrapper::dumpMemtableToSSTable(memtable*)
void sstableWrapper::dumpMemtableToSSTable(memtable* memtableObjRef) {
  // File path
  std::string filePath = 
    "SSTablesDir/" + std::to_string(memtableObjRef->memtableID) + ".txt";
  // Bloom File path
  std::string bloomFilePath =
    "bloomFiltersDir/" + std::to_string(memtableObjRef->memtableID) + ".txt";
  // Creating SSTable
  sstable* sstableObjRef = new sstable(filePath, bloomFilePath);
  // Opening SSTable file to write
  sstableObjRef->openFileToWrite();
  // Writing to the file
  std::map<std::string, std::string>::iterator iter;
  // Bytes array
  std::vector<unsigned char> buffer;
  // To write each key-value pair in the SSTable and index using buffer.
  for(iter = memtableObjRef->table.begin();
      iter != memtableObjRef->table.end();
      ++iter) {
    buffer.clear();
    buffer.push_back((iter->first).length());
    std::copy((iter->first).begin(), 
              (iter->first).end(), 
              std::back_inserter(buffer));
    buffer.push_back((iter->second).length());
    std::copy((iter->second).begin(),
              (iter->second).end(),
              std::back_inserter(buffer));
    // Add the record to file and index.
    sstableObjRef->writeInFile(buffer);
    // Add the key to bloom filter.
    sstableObjRef->filter->add(iter->first);
  }
  
  // Close the file to save the changes. SSTable and index files are dumped.
  sstableObjRef->closeFileAfterWrite();
  // Dump the bloom filter to the disk.
  sstableObjRef->filter->writeToFile();
  
  // Loading the index in memory.
  sstableObjRef->loadIndexInMemory();
  
  // Inserting SSTable to list.
  sstableObjRefList.push_back(sstableObjRef);
  // Increasing the count of number of SSTables.
  ++(*sstable_no);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %esi
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x34330
movq -0x1f8(%rbp), %rdx
leaq 0x58ae(%rip), %rsi      # 0x41581
leaq -0x50(%rbp), %rdi
callq 0x342e0
jmp 0x3bcde
leaq 0x5541(%rip), %rdx      # 0x41226
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x34290
jmp 0x3bcf4
leaq -0x50(%rbp), %rdi
callq 0x6340
leaq -0x70(%rbp), %rdi
callq 0x6340
movq -0x10(%rbp), %rax
movl 0x38(%rax), %esi
leaq -0xe0(%rbp), %rdi
callq 0x34330
jmp 0x3bd1b
leaq 0x586c(%rip), %rsi      # 0x4158e
leaq -0xc0(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0x342e0
jmp 0x3bd37
leaq 0x54e8(%rip), %rdx      # 0x41226
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x34290
jmp 0x3bd53
leaq -0xc0(%rbp), %rdi
callq 0x6340
leaq -0xe0(%rbp), %rdi
callq 0x6340
movl $0x468, %edi            # imm = 0x468
callq 0x64f0
movq %rax, -0x208(%rbp)
jmp 0x3bd7e
movb $0x1, -0x14a(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x6210
jmp 0x3bd97
leaq -0x128(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x6210
jmp 0x3bdac
leaq -0x149(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x68d0
movq -0x210(%rbp), %rdx
leaq 0x58b1(%rip), %rsi      # 0x4167e
leaq -0x148(%rbp), %rdi
callq 0x9970
jmp 0x3bddb
movq -0x208(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x128(%rbp), %rdx
leaq -0x148(%rbp), %rcx
callq 0x38300
jmp 0x3bdfe
movb $0x0, -0x14a(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x6340
leaq -0x149(%rbp), %rdi
callq 0x6560
leaq -0x128(%rbp), %rdi
callq 0x6340
leaq -0x108(%rbp), %rdi
callq 0x6340
movq -0x208(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x38950
jmp 0x3be51
leaq -0x158(%rbp), %rdi
callq 0x34610
leaq -0x170(%rbp), %rdi
callq 0x397b0
movq -0x10(%rbp), %rdi
callq 0x34630
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x10(%rbp), %rdi
callq 0x345e0
movq %rax, -0x180(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x34580
testb $0x1, %al
jne 0x3beb3
jmp 0x3c207
leaq -0x170(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x3c790
leaq -0x158(%rbp), %rdi
callq 0x34730
movq %rax, %rdi
callq 0x6780
movq -0x218(%rbp), %rdi
movb %al, -0x181(%rbp)
leaq -0x181(%rbp), %rsi
callq 0x397d0
jmp 0x3bef5
leaq -0x158(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x34730
movq %rax, %rdi
callq 0x64c0
movq -0x228(%rbp), %rdi
movq %rax, -0x190(%rbp)
callq 0x34730
movq %rax, %rdi
callq 0x6920
movq %rax, -0x198(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x3c820
movq %rax, -0x220(%rbp)
jmp 0x3bf47
movq -0x220(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x190(%rbp), %rdi
movq -0x198(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
callq 0x3c7b0
movq %rax, -0x230(%rbp)
jmp 0x3bf78
movq -0x230(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x34730
movq %rax, %rdi
addq $0x20, %rdi
callq 0x6780
movb %al, -0x1a9(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x1a9(%rbp), %rsi
callq 0x397d0
jmp 0x3bfb9
leaq -0x158(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x34730
movq %rax, %rdi
addq $0x20, %rdi
callq 0x6460
movq -0x240(%rbp), %rdi
movq %rax, -0x1b8(%rbp)
callq 0x34730
movq %rax, %rdi
addq $0x20, %rdi
callq 0x6300
movq %rax, -0x1c0(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x3c820
movq %rax, -0x238(%rbp)
jmp 0x3c013
movq -0x238(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x1b8(%rbp), %rdi
movq -0x1c0(%rbp), %rsi
movq -0x1c8(%rbp), %rdx
callq 0x3c850
movq %rax, -0x248(%rbp)
jmp 0x3c044
movq -0x248(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0xe8(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x389f0
jmp 0x3c067
movq -0xe8(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x250(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x34730
movq %rax, %rsi
leaq -0x1f0(%rbp), %rdi
callq 0x6210
jmp 0x3c096
movq -0x250(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x8040
jmp 0x3c0ab
leaq -0x1f0(%rbp), %rdi
callq 0x6340
leaq -0x158(%rbp), %rdi
callq 0x35b70
jmp 0x3be87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c0eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x6340
leaq -0x70(%rbp), %rdi
callq 0x6340
jmp 0x3c2aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c2a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x6340
leaq -0xe0(%rbp), %rdi
callq 0x6340
jmp 0x3c2a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c295
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c1b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c1ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c194
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x6340
leaq -0x149(%rbp), %rdi
callq 0x6560
leaq -0x128(%rbp), %rdi
callq 0x6340
leaq -0x108(%rbp), %rdi
callq 0x6340
testb $0x1, -0x14a(%rbp)
jne 0x3c1c3
jmp 0x3c1d4
movq -0x208(%rbp), %rdi
movl $0x468, %esi            # imm = 0x468
callq 0x6500
jmp 0x3c295
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x3c289
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x6340
jmp 0x3c289
movq -0xe8(%rbp), %rdi
callq 0x38ce0
jmp 0x3c215
movq -0xe8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x8160
jmp 0x3c227
movq -0xe8(%rbp), %rdi
callq 0x38510
jmp 0x3c235
movq -0x200(%rbp), %rdi
addq $0x8, %rdi
leaq -0xe8(%rbp), %rsi
callq 0x3c730
jmp 0x3c24e
movq -0x200(%rbp), %rax
movq (%rax), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq -0x170(%rbp), %rdi
callq 0x398a0
leaq -0xa0(%rbp), %rdi
callq 0x6340
leaq -0x30(%rbp), %rdi
callq 0x6340
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x170(%rbp), %rdi
callq 0x398a0
leaq -0xa0(%rbp), %rdi
callq 0x6340
leaq -0x30(%rbp), %rdi
callq 0x6340
movq -0x78(%rbp), %rdi
callq 0x68c0
nopw %cs:(%rax,%rax)