// File: /workspace/github_cpp_makefile_star2_O3/prakhar2808[P]DBEngine/dbengine/memtable.cc
// Function: memtable::memtable(int, int, int)
memtable::memtable(int size, int capacity, int memtableID)
  :size(size),
  capacity(capacity),
  memtableID(memtableID) {
  
  journalFilePath = "journalsDir/" + std::to_string(memtableID) + ".txt";
  // Check if the journal file is already present, need to restore it in memory
  struct stat buff;
  if(stat(journalFilePath.c_str(), &buff) != -1) {
    std::ifstream journalReadfd;
    journalReadfd.open(journalFilePath);
    journalReadfd >> std::noskipws;
    unsigned char ch;
    std::cout << "Reading journal : " << journalFilePath << std::endl;
    while(journalReadfd.peek() != EOF) {
      // Length of the key
      journalReadfd >> ch;
      std::string key((int)ch, '\0');
      // The key
      journalReadfd.read(&key[0], (int)ch);
      // Length of the value
      journalReadfd >> ch;
      std::string value((int)ch, '\0');
      // The value
      journalReadfd.read(&value[0], (int)ch);
      // Putting in the in-memory table.
      table[key] = value;
      std::cout << "Read key : " << key << ", value : " << value << std::endl; 
    }
    journalReadfd.close();
  }
  // Opening the journal in append mode.
  journalWritefd.open(journalFilePath, std::ios_base::app);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movl %ecx, %r14d
leaq 0x8(%rdi), %rax
movl $0x0, 0x8(%rdi)
xorl %ecx, %ecx
movq %rcx, 0x10(%rdi)
movq %rax, 0x18(%rdi)
movq %rax, 0x20(%rdi)
movq %rcx, 0x28(%rdi)
movl %esi, 0x30(%rdi)
movl %edx, 0x34(%rdi)
movl %r14d, 0x38(%rdi)
leaq 0x40(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x50(%rdi), %rax
movq %rax, 0x68(%rsp)
movq %rax, 0x40(%rdi)
movq %rcx, 0x48(%rdi)
movb $0x0, 0x50(%rdi)
movq %rdi, 0x38(%rsp)
addq $0x60, %rdi
movq %rdi, 0x40(%rsp)
callq 0x5630
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
movl $0x1, %r15d
cmpl $0xa, %ebp
jb 0x15769
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebp, %ecx
cmpl $0x63, %ecx
jbe 0x15760
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x15766
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x15769
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x1572d
addl $-0x3, %r15d
jmp 0x15769
addl $-0x2, %r15d
jmp 0x15769
decl %r15d
shrl $0x1f, %r14d
leal (%r15,%r14), %esi
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x18(%rsp), %rdi
movl $0x2d, %edx
callq 0x5450
movl %r14d, %edi
leaq 0x18(%rsp), %rbx
addq (%rbx), %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x160d0
leaq 0x6a0c(%rip), %rcx      # 0x1c1b1
movl $0xc, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5400
leaq 0x80(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x157e4
movq %rdx, 0x70(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x80(%rsp)
jmp 0x157eb
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
leaq 0x70(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x69b0(%rip), %rsi      # 0x1c1be
callq 0x56f0
leaq 0x110(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x15843
movq %rdx, 0x100(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x110(%rsp)
jmp 0x1584a
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x100(%rsp), %rbx
movq %rdx, 0x8(%rbx)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5480
movq (%rbx), %rdi
cmpq %r14, %rdi
je 0x1588e
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x70(%rsp), %rdi
cmpq %r15, %rdi
je 0x158a8
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x158c4
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x10(%rsp), %rax
movq (%rax), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5340
cmpl $-0x1, %eax
je 0x15afc
leaq 0x100(%rsp), %rdi
callq 0x5420
leaq 0x100(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x8, %edx
callq 0x5320
movq 0x100(%rsp), %rax
movq -0x18(%rax), %rax
andl $0xffffefff, 0x118(%rsp,%rax) # imm = 0xFFFFEFFF
movq 0x1069f(%rip), %rdi     # 0x25fc0
leaq 0x689b(%rip), %rsi      # 0x1c1c3
movl $0x12, %edx
callq 0x5430
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
movq 0x1067a(%rip), %rdi     # 0x25fc0
callq 0x5430
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5370
movsbl %al, %esi
movq %r14, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5280
leaq 0x100(%rsp), %r12
leaq 0xf(%rsp), %r13
leaq 0x18(%rsp), %rbp
leaq 0x48(%rsp), %r15
movq 0x1062d(%rip), %r14     # 0x25fc0
movq %r12, %rdi
callq 0x5190
cmpl $-0x1, %eax
je 0x15ae2
movq %r12, %rdi
movq %r13, %rsi
callq 0x54c0
movzbl 0xf(%rsp), %esi
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rbp, %rdi
xorl %edx, %edx
callq 0x5450
movq 0x18(%rsp), %rsi
movzbl 0xf(%rsp), %edx
movq %r12, %rdi
callq 0x5490
movq %r12, %rdi
movq %r13, %rsi
callq 0x54c0
movzbl 0xf(%rsp), %esi
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r15, %rdi
xorl %edx, %edx
callq 0x5450
movq 0x48(%rsp), %rsi
movzbl 0xf(%rsp), %edx
movq %r12, %rdi
callq 0x5490
movq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x16054
movq %rax, %rdi
movq %r15, %rsi
callq 0x5220
movl $0xb, %edx
movq %r14, %rdi
leaq 0x679f(%rip), %rsi      # 0x1c1d6
callq 0x5430
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x5430
movq %rax, %rbx
movl $0xa, %edx
movq %rax, %rdi
leaq 0x6782(%rip), %rsi      # 0x1c1e2
callq 0x5430
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %rbx, %rdi
callq 0x5430
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x5370
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5280
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x15abd
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x15993
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x15993
leaq 0x100(%rsp), %rdi
callq 0x50b0
leaq 0x100(%rsp), %rdi
callq 0x50d0
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x51e0
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15b7d
jmp 0x15b68
jmp 0x15b68
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %r15, %rdi
je 0x15b4a
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x15b4a
movq %rax, %r14
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x15bca
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x15bca
movq %rax, %r14
jmp 0x15bca
movq %rax, %r14
jmp 0x15bd4
jmp 0x15b7d
jmp 0x15b78
jmp 0x15b7d
movq %rax, %r14
jmp 0x15ba1
movq %rax, %r14
jmp 0x15bbd
movq %rax, %r14
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x15ba1
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x15bbd
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x100(%rsp), %rdi
callq 0x50d0
movq 0x40(%rsp), %rdi
callq 0x5560
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x15bf1
movq (%rax), %rsi
incq %rsi
callq 0x5390
movq 0x38(%rsp), %rdi
callq 0x1613e
movq %r14, %rdi
callq 0x5670
nop