// File: /workspace/github_cpp_makefile_star2_O2/prakhar2808[P]DBEngine/dbengine/memtableWrapper.cc
// Function: memtableWrapper::putKeyValuePair(keyValuePair_t, sstableWrapper*, int)
opStatus memtableWrapper::putKeyValuePair(keyValuePair_t keyValuePair, 
                                          sstableWrapper* sstableWrapperObjRef,
                                          int clientSocket) {
  // Locking to write
  pthread_mutex_lock(&writeMutex);
  // Put into the memtable
  (*index)->putKeyValuePair(keyValuePair);
  // Check if currect memtable is full
  if((*index)->size >= (*index)->capacity) {
    // New memtable
    memtable* nextMemtableObjPointer = new memtable(0, 
                                                    MAX_MEMTABLE_SIZE,
                                                    lastMemtableID + 1);
    memtableObjPointersList.push_back(nextMemtableObjPointer);
    ++lastMemtableID;
    
    // An async task to put memtable at index to disk and delete entry
    // from list.
    std::thread(writeMemtableToDisk, 
                index, 
                sstableWrapperObjRef, 
                this).detach();
    
    // Make index point to the next memtable.
    ++index;
    std::cout << "Memtable full, switching to next!" << std::endl;
  }
  // Unlocking
  pthread_mutex_unlock(&writeMutex);
  std::string ret = "Inserted successfully!";
  // Check if it was a put operation corresponding to a deletion.
  if(keyValuePair.value == "") {
    ret = "Deleted the key from the database!";
  }
  sendToClient(clientSocket, ret);
  sendEndMsgToClient(clientSocket);
  return opStatus::opSuccess;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %ebx
movq %rsi, %r14
movq %rdi, %r13
movq %rdx, 0x8(%rsp)
leaq 0x20(%rdi), %r15
movq %r15, %rdi
callq 0x55a0
movq 0x18(%r13), %rax
movq 0x10(%rax), %r12
leaq 0x58(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6a76
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1314e
leaq 0x18(%r13), %r12
leaq 0x58(%rsp), %rdi
callq 0x6ab0
movq (%r12), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
cmpl 0x34(%rax), %ecx
jl 0x13b73
movl $0x260, %edi            # imm = 0x260
callq 0x53c0
movq %rax, %rbp
movl 0x48(%r13), %ecx
incl %ecx
movq %rax, %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x12e40
leaq 0x10(%rsp), %rsi
movq %rbp, (%rsi)
movq %r13, %rdi
callq 0x14230
incl 0x48(%r13)
leaq 0x50(%rsp), %r8
movq %r13, (%r8)
leaq 0x12e(%rip), %rsi       # 0x13c58
movq %rsp, %r13
leaq 0x8(%rsp), %rcx
movq %r13, %rdi
movq %r12, %rdx
callq 0x1423c
movq %r13, %rdi
callq 0x56a0
movq %rsp, %rdi
callq 0x142d2
movq (%r12), %rax
movq (%rax), %rax
movq %rax, (%r12)
movq 0x11461(%rip), %rdi     # 0x24fc0
leaq 0x4763(%rip), %rsi      # 0x182c9
callq 0x53b0
movq %rax, %rdi
callq 0x5210
movq %r15, %rdi
callq 0x5300
leaq 0x4769(%rip), %rsi      # 0x182eb
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
callq 0x6ac6
addq $0x20, %r14
leaq 0x384a(%rip), %rsi      # 0x173e4
movq %r14, %rdi
callq 0x7362
testb %al, %al
je 0x13bb7
leaq 0x4755(%rip), %rsi      # 0x18302
leaq 0x10(%rsp), %rdi
callq 0x5740
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5540
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1677c
leaq 0x30(%rsp), %rdi
callq 0x51d0
movl %ebx, %edi
callq 0x167ab
leaq 0x10(%rsp), %rdi
callq 0x51d0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %rsp, %rdi
callq 0x142d2
jmp 0x13c50
movq %rax, %rbx
movl $0x260, %esi            # imm = 0x260
movq %rbp, %rdi
callq 0x53d0
jmp 0x13c50
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x51d0
jmp 0x13c46
movq %rax, %rbx
jmp 0x13c50
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x6ab0
jmp 0x13c50
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x51d0
movq %rbx, %rdi
callq 0x56c0