// File: /workspace/github_cpp_makefile_star2_O2/prakhar2808[P]DBEngine/dbengine/memtableWrapper.cc
// Function: memtableWrapper::putKeyValuePair(std::vector<keyValuePair_t, std::allocator<keyValuePair_t>>&, sstableWrapper*, int)
opStatus memtableWrapper::putKeyValuePair(
    std::vector<keyValuePair_t>& keyValuePairVec, 
    sstableWrapper* sstableWrapperObjRef,
    int clientSocket) {
  // Locking to write
  pthread_mutex_lock(&writeMutex);
  // Iterating over the vector and writing.
  for(int i = 0; i < (int)keyValuePairVec.size(); i++) {
    // Put into the memtable
    (*index)->putKeyValuePair(keyValuePairVec[i]);
    // Check if currect memtable is full
    if((*index)->size == (*index)->capacity) {
      std::cout << "Creating new memtable! " << i << std::endl; 
      // 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 " + std::to_string(keyValuePairVec.size()) + " key-value pairs!";
  sendToClient(clientSocket, ret);
  sendEndMsgToClient(clientSocket);
  return opStatus::opSuccess;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %ecx, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
leaq 0x20(%rsp), %rax
movq %rdx, (%rax)
addq $0x20, %rdi
movq %rdi, 0x18(%rsp)
callq 0x55a0
leaq 0x18(%r15), %rbp
xorl %ebx, %ebx
leaq 0xa8(%rsp), %r13
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
subq %rsi, %rax
shrq $0x6, %rax
cltq
cmpq %rax, %r12
jge 0x13e7b
movq (%rbp), %rax
movq %r13, %r14
movq 0x10(%rax), %r13
addq %rbx, %rsi
movq %r14, %rdi
callq 0x6a76
movq %r13, %rdi
movq %r14, %r13
movq %r14, %rsi
callq 0x1314e
movq %r13, %rdi
callq 0x6ab0
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
cmpl 0x34(%rax), %ecx
jne 0x13e6f
movq 0x11207(%rip), %rdi     # 0x24fc0
leaq 0x4565(%rip), %rsi      # 0x18325
callq 0x53b0
movq %rax, %rdi
movl %r12d, %esi
callq 0x5690
movq %rax, %rdi
callq 0x5210
movl $0x260, %edi            # imm = 0x260
callq 0x53c0
movq %rax, %r13
movl 0x48(%r15), %ecx
incl %ecx
movq %rax, %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x12e40
movq %r13, 0x48(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x14230
incl 0x48(%r15)
movq %r15, 0x68(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
leaq -0x1cc(%rip), %rsi      # 0x13c58
movq %rbp, %rdx
leaq 0x20(%rsp), %rcx
leaq 0x68(%rsp), %r8
callq 0x1423c
movq %r13, %rdi
callq 0x56a0
movq %r13, %rdi
callq 0x142d2
movq (%rbp), %rax
movq (%rax), %rax
movq %rax, (%rbp)
movq 0x11168(%rip), %rdi     # 0x24fc0
leaq 0x446a(%rip), %rsi      # 0x182c9
callq 0x53b0
movq %rax, %rdi
callq 0x5210
movq %r14, %r13
incq %r12
addq $0x40, %rbx
jmp 0x13d54
movq 0x18(%rsp), %rdi
callq 0x5300
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x6, %rsi
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0x142df
leaq 0x4494(%rip), %rsi      # 0x1833d
leaq 0x28(%rsp), %rdi
movq %rbx, %rdx
callq 0x13394
leaq 0x448a(%rip), %rdx      # 0x18347
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x13375
movl 0xc(%rsp), %ebx
leaq 0x28(%rsp), %rdi
callq 0x51d0
leaq 0x68(%rsp), %rdi
callq 0x51d0
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5540
leaq 0x88(%rsp), %rsi
movl %ebx, %edi
callq 0x1677c
leaq 0x88(%rsp), %rdi
callq 0x51d0
movl %ebx, %edi
callq 0x167ab
leaq 0x48(%rsp), %rdi
callq 0x51d0
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x51d0
jmp 0x13f65
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x51d0
jmp 0x13f5b
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x13f6a
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x51d0
jmp 0x13fa2
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x142d2
jmp 0x13fa2
movq %rax, %rbx
movl $0x260, %esi            # imm = 0x260
movq %r13, %rdi
callq 0x53d0
jmp 0x13fa2
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x6ab0
movq %rbx, %rdi
callq 0x56c0