// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movl %ecx, 0x14(%rsp)
movq %rsi, %rbx
movq %rdx, 0xb0(%rsp)
movq %rdi, 0x38(%rsp)
addq $0x20, %rdi
movq %rdi, 0x68(%rsp)
callq 0x5550
movq (%rbx), %r14
movq %rbx, 0x40(%rsp)
movq 0x8(%rbx), %rax
subq %r14, %rax
shrq $0x6, %rax
testl %eax, %eax
jle 0x16c6e
movq 0x38(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movl $0x28, %ebp
xorl %ebx, %ebx
leaq 0x70(%rsp), %r12
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r15
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq -0x28(%r14,%rbp), %rsi
movq -0x20(%r14,%rbp), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x6924
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq -0x8(%r14,%rbp), %rsi
movq (%r14,%rbp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x6924
movq %r15, %rdi
movq %r12, %r13
movq %r12, %rsi
callq 0x15ba0
movq 0x38(%rsp), %r14
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x16aeb
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x16b0d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x30(%rax), %ecx
cmpl 0x34(%rax), %ecx
jne 0x16c46
movl $0x17, %edx
movq 0xf48f(%rip), %r15      # 0x25fc0
movq %r15, %rdi
leaq 0x57ea(%rip), %rsi      # 0x1c325
callq 0x5430
movq %r15, %rdi
movl %ebx, %esi
callq 0x5650
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x5370
movsbl %al, %esi
movq %r15, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5280
movl $0x260, %edi            # imm = 0x260
callq 0x5380
movq %rax, %r15
movl 0x48(%r14), %ecx
incl %ecx
movq %rax, %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x1563c
movl $0x18, %edi
callq 0x5380
movq %r15, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x5080
incq 0x10(%r14)
incl 0x48(%r14)
movq %r14, 0x48(%rsp)
leaq 0x18(%rsp), %r14
movq %r14, %rdi
leaq -0x2f7(%rip), %rsi      # 0x168d4
movq 0x8(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x48(%rsp), %r8
callq 0x17324
movq %r14, %rdi
callq 0x5660
cmpq $0x0, 0x18(%rsp)
jne 0x16efd
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq (%rax), %rax
movq %rax, (%rcx)
movl $0x21, %edx
movq 0xf3b0(%rip), %r14      # 0x25fc0
movq %r14, %rdi
leaq 0x56af(%rip), %rsi      # 0x1c2c9
callq 0x5430
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x5370
movsbl %al, %esi
movq %r14, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5280
incq %rbx
movq 0x40(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %rax
subq %r14, %rax
shrq $0x6, %rax
cltq
addq $0x40, %rbp
cmpq %rax, %rbx
movq %r13, %r12
jl 0x16a5c
movq 0x68(%rsp), %rdi
callq 0x52c0
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rbx
subq (%rax), %rbx
sarq $0x6, %rbx
movl $0x1, %esi
cmpq $0xa, %rbx
jb 0x16ce2
movl $0x4, %esi
movabsq $0x346dc5d63886594b, %rdi # imm = 0x346DC5D63886594B
movq %rbx, %rcx
cmpq $0x63, %rcx
jbe 0x16cdb
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x16ce0
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x16ce2
movq %rcx, %rax
mulq %rdi
shrq $0xb, %rdx
addl $0x4, %esi
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x16ca5
addl $-0x3, %esi
jmp 0x16ce2
addl $-0x2, %esi
jmp 0x16ce2
decl %esi
movl %esi, %esi
leaq 0xe8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x5450
movq (%r14), %rdi
movl 0x8(%r14), %esi
movq %rbx, %rdx
callq 0x1740d
leaq 0x5625(%rip), %rcx      # 0x1c33d
movl $0x9, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5400
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x16d52
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x16d5a
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0x48(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x55ca(%rip), %rsi      # 0x1c347
callq 0x56f0
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x16da9
movq %rdx, 0x18(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x28(%rsp)
jmp 0x16db0
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, 0x20(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x16ddf
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0xd8(%rsp), %rdi
cmpq %r15, %rdi
movl 0x14(%rsp), %ebx
je 0x16e00
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0xc8(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x6924
leaq 0xb8(%rsp), %rsi
movl %ebx, %edi
callq 0x1aa0a
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x16e52
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebx, %edi
callq 0x1aa44
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x16e70
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x5390
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x16edf
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x16edf
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x16ec5
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x16ec5
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r15, %rdi
je 0x16f43
movq 0xe8(%rsp), %rsi
jmp 0x16f2c
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x16f43
movq 0x28(%rsp), %rsi
jmp 0x16f2c
cmpq $0x0, 0x18(%rsp)
jne 0x16efd
movq %rax, %rbx
jmp 0x16f43
callq 0x50e0
movq %rax, %rbx
movl $0x260, %esi            # imm = 0x260
movq %r15, %rdi
jmp 0x16f2f
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x16f43
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x16f43
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x7278
movq %rbx, %rdi
callq 0x5670
nop