// File: /workspace/github_cpp_makefile_star2_O3/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 0x15c04
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x166e2
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x18(%r12), %r13
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x166fe
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
movq (%r13), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
cmpl 0x34(%rax), %ecx
jl 0x167e1
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 0x156a0
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       # 0x16938
leaq 0x8(%rsp), %r12
leaq 0x28(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x17384
movq %r12, %rdi
callq 0x5660
cmpq $0x0, 0x8(%rsp)
jne 0x168bf
movq (%r13), %rax
movq (%rax), %rax
movq %rax, (%r13)
movq 0xf81b(%rip), %r12      # 0x25fc0
leaq 0x5b1d(%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 0x5af2(%rip), %rsi      # 0x1c2eb
leaq 0x5b01(%rip), %rdx      # 0x1c301
leaq 0x8(%rsp), %rdi
callq 0x736c
addq $0x20, %r14
leaq 0x4bcf(%rip), %rsi      # 0x1b3e4
movq %r14, %rdi
callq 0x50f0
testl %eax, %eax
movl 0x4(%rsp), %ebx
jne 0x16842
leaq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x5acd(%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 0x1a9d7
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x16885
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebx, %edi
callq 0x1aa11
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x168a3
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 0x168ed
callq 0x50e0
movq %rax, %rbx
movl $0x260, %esi            # imm = 0x260
movq %rbp, %rdi
jmp 0x1692a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x16918
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x16918
movq %rax, %rbx
jmp 0x1692f
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x72be
jmp 0x1692f
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1692f
movq 0x60(%rsp), %rsi
jmp 0x16927
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1692f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x5670
nop