// File: 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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rsi, -0xe8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
addq $0x20, %rdi
callq 0x6750
movq -0xf0(%rbp), %rdi
addq $0x18, %rdi
callq 0x36c80
movq -0xe8(%rbp), %rsi
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x9830
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x33e60
jmp 0x35e9b
leaq -0x60(%rbp), %rdi
callq 0x9890
movq -0xf0(%rbp), %rdi
addq $0x18, %rdi
callq 0x36c80
movq -0xf0(%rbp), %rdi
movq (%rax), %rax
movl 0x30(%rax), %eax
movl %eax, -0xf4(%rbp)
addq $0x18, %rdi
callq 0x36c80
movq %rax, %rcx
movl -0xf4(%rbp), %eax
movq (%rcx), %rcx
cmpl 0x34(%rcx), %eax
jl 0x36011
movl $0x260, %edi            # imm = 0x260
callq 0x64f0
movq %rax, %rdi
movq -0xf0(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x100(%rbp)
movl 0x48(%rax), %ecx
incl %ecx
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x33940
jmp 0x35f16
movq -0xf0(%rbp), %rdi
movq -0x100(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x36bf0
movq -0xf0(%rbp), %rax
movl 0x48(%rax), %ecx
incl %ecx
movl %ecx, 0x48(%rax)
movq %rax, %rdx
addq $0x18, %rdx
movq %rax, -0x88(%rbp)
leaq 0x20b(%rip), %rsi       # 0x36160
leaq -0x80(%rbp), %rdi
movq %rdi, -0x108(%rbp)
leaq -0x18(%rbp), %rcx
leaq -0x88(%rbp), %r8
callq 0x36ca0
movq -0x108(%rbp), %rdi
callq 0x68a0
jmp 0x35f7e
leaq -0x80(%rbp), %rdi
callq 0x36d70
movq -0xf0(%rbp), %rdi
addq $0x18, %rdi
callq 0x36da0
movq 0x29022(%rip), %rdi     # 0x5efc0
leaq 0xb384(%rip), %rsi      # 0x41329
callq 0x64e0
movq %rax, %rdi
movq 0x28fdc(%rip), %rsi     # 0x5ef90
callq 0x6540
jmp 0x36011
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x9890
jmp 0x3614c
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x260, %esi            # imm = 0x260
callq 0x6500
jmp 0x3614c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x36d70
jmp 0x3614c
movq -0xf0(%rbp), %rdi
addq $0x20, %rdi
callq 0x63f0
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x68d0
movq -0x110(%rbp), %rdx
leaq 0xb309(%rip), %rsi      # 0x4134b
leaq -0xa8(%rbp), %rdi
callq 0x9970
jmp 0x36050
leaq -0xa9(%rbp), %rdi
callq 0x6560
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
leaq 0xb610(%rip), %rsi      # 0x4167e
callq 0xaa80
movb %al, -0x111(%rbp)
jmp 0x3607b
movb -0x111(%rbp), %al
testb $0x1, %al
jne 0x36087
jmp 0x360c9
leaq 0xb2d4(%rip), %rsi      # 0x41362
leaq -0xa8(%rbp), %rdi
callq 0x66d0
jmp 0x3609c
jmp 0x360c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xa9(%rbp), %rdi
callq 0x6560
jmp 0x3614c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x36140
movl -0x1c(%rbp), %eax
movl %eax, -0x118(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x6210
jmp 0x360e7
movl -0x118(%rbp), %edi
leaq -0xd0(%rbp), %rsi
callq 0x3e3a0
jmp 0x360fb
leaq -0xd0(%rbp), %rdi
callq 0x6340
movl -0x1c(%rbp), %edi
callq 0x3e410
jmp 0x36111
leaq -0xa8(%rbp), %rdi
callq 0x6340
xorl %eax, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x6340
leaq -0xa8(%rbp), %rdi
callq 0x6340
movq -0x68(%rbp), %rdi
callq 0x68c0
nopw %cs:(%rax,%rax)