// File: /workspace/github_cpp_makefile_star2_O1/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, 0x4(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq %rdx, 0x28(%rsp)
leaq 0x20(%rdi), %r15
movq %r15, %rdi
callq 0x5550
movq 0x18(%r12), %rax
movq 0x10(%rax), %r13
leaq 0x60(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x6924
leaq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
addq %rsi, %rdx
callq 0x6924
leaq 0x50(%rsp), %rsi
movq %r13, %rdi
callq 0x15ba0
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x1667e
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x18(%r12), %r13
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1669a
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
movq (%r13), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
cmpl 0x34(%rax), %ecx
jl 0x1677d
movl $0x260, %edi            # imm = 0x260
callq 0x5380
movq %rax, %rbp
movl 0x48(%r12), %ecx
incl %ecx
movq %rax, %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x1563c
movl $0x18, %edi
callq 0x5380
movq %rbp, 0x10(%rax)
movq %rax, %rdi
movq %r12, %rsi
callq 0x5080
incq 0x10(%r12)
incl 0x48(%r12)
leaq 0x90(%rsp), %r8
movq %r12, (%r8)
leaq 0x1ce(%rip), %rsi       # 0x168d4
leaq 0x8(%rsp), %r12
leaq 0x28(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x17324
movq %r12, %rdi
callq 0x5660
cmpq $0x0, 0x8(%rsp)
jne 0x1685b
movq (%r13), %rax
movq (%rax), %rax
movq %rax, (%r13)
movq 0xf87f(%rip), %r12      # 0x25fc0
leaq 0x5b81(%rip), %rsi      # 0x1c2c9
movl $0x21, %edx
movq %r12, %rdi
callq 0x5430
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x5370
movsbl %al, %esi
movq %r12, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5280
movq %r15, %rdi
callq 0x52c0
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5b56(%rip), %rsi      # 0x1c2eb
leaq 0x5b65(%rip), %rdx      # 0x1c301
leaq 0x8(%rsp), %rdi
callq 0x7326
addq $0x20, %r14
leaq 0x4c33(%rip), %rsi      # 0x1b3e4
movq %r14, %rdi
callq 0x50f0
testl %eax, %eax
movl 0x4(%rsp), %ebx
jne 0x167de
leaq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x5b31(%rip), %rcx      # 0x1c302
movl $0x22, %r8d
xorl %esi, %esi
callq 0x55b0
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x6924
leaq 0x30(%rsp), %rsi
movl %ebx, %edi
callq 0x1aa0a
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x16821
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebx, %edi
callq 0x1aa44
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1683f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5390
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x8(%rsp)
je 0x16889
callq 0x50e0
movq %rax, %rbx
movl $0x260, %esi            # imm = 0x260
movq %rbp, %rdi
jmp 0x168c6
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x168b4
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x168b4
movq %rax, %rbx
jmp 0x168cb
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x7278
jmp 0x168cb
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x168cb
movq 0x60(%rsp), %rsi
jmp 0x168c3
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x168cb
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x5670
nop