// File: /workspace/github_cpp_makefile_star2_O3/prakhar2808[P]DBEngine/dbengine/sstable.cc
// Function: sstable::getValueFromKey(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
opStatus sstable::getValueFromKey(std::string key, int clientSocket) {
  // Getting the offset of the block using the index.
  std::map<std::string, long long>::iterator iter = index.lower_bound(key);

  // If key is in the index then the iterator points to that map entry.

  // If key is not in the index then the iterator points to the next higher
  // entry in the map. We need to go back 1 block. If we are on the first block
  // then all the keys in this SSTable are greater than the one we are looking
  // for.
  if(iter->first != key && iter == index.begin()) {
    return opStatus::opFail;
  }
  // If iter points to anywhere else
  else if(iter->first != key) {
    --iter;
  }

  std::ifstream readfd;
  openFileToRead(readfd);
  // Directly going to the block to read.
  readfd.seekg(iter->second, std::ios::cur);

  unsigned char ch;
  while(readfd.peek() != EOF) {
    readfd >> ch;
    std::string data((int)ch, '\0');
    readfd.read(&data[0], (int)ch);
    if(data > key) {
      break;
    }
    readfd >> ch;
    if(data == key) {
      std::string val((int)ch, '\0');
      readfd.read(&val[0], (int)ch);
      // If the value has been deleted.
      if(val == "") {
        return opStatus::opKeyNotFound;
      }
      sendToClient(clientSocket, val);
      sendEndMsgToClient(clientSocket);
      return opStatus::opSuccess;
    }
    readfd.seekg(ch, std::ios::cur);
  }
  closeFileAfterRead(readfd);
  return opStatus::opFail;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movl %edx, 0xc(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rdi), %rsi
leaq 0x8(%rdi), %rdx
movq %r14, %rcx
callq 0x18af4
movq %rax, %r12
movq 0x28(%rax), %r13
cmpq 0x8(%r14), %r13
jne 0x17e88
testq %r13, %r13
je 0x17e9d
movq (%r14), %rbx
movq 0x20(%r12), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x53b0
testl %eax, %eax
je 0x17e74
movl $0x3, %eax
cmpq 0x18(%r15), %r12
je 0x180ac
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x53b0
testl %eax, %eax
jne 0x17e92
jmp 0x17e9d
cmpq 0x18(%r15), %r12
je 0x180a7
movq %r12, %rdi
callq 0x53d0
movq %rax, %r12
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x5420
addq $0x38, %r15
movq %rbx, %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x5320
leaq 0x70(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
andl $0xffffefff, 0x88(%rsp,%rax) # imm = 0xFFFFEFFF
movq 0x40(%r12), %rsi
movl $0x1, %edx
callq 0x54e0
leaq 0x60(%rsp), %rbp
leaq 0x70(%rsp), %r15
leaq 0x7(%rsp), %r12
leaq 0x50(%rsp), %r13
movq %r15, %rdi
callq 0x5190
cmpl $-0x1, %eax
je 0x18085
movq %r15, %rdi
movq %r12, %rsi
callq 0x54c0
movzbl 0x7(%rsp), %esi
movq %rbp, 0x50(%rsp)
movq %r13, %rdi
xorl %edx, %edx
callq 0x5450
movq 0x50(%rsp), %rsi
movzbl 0x7(%rsp), %edx
movq %r15, %rdi
callq 0x5490
movq %r13, %rdi
movq %r14, %rsi
callq 0x5060
movl $0x3, %ebx
testl %eax, %eax
jg 0x18061
movq %r15, %rdi
movq %r12, %rsi
callq 0x54c0
movq 0x58(%rsp), %rdx
cmpq 0x8(%r14), %rdx
jne 0x17f7e
testq %rdx, %rdx
je 0x17f97
movq (%r14), %rsi
movq 0x50(%rsp), %rdi
callq 0x53b0
testl %eax, %eax
je 0x17f97
movzbl 0x7(%rsp), %esi
xorl %ebx, %ebx
movq %r15, %rdi
movl $0x1, %edx
callq 0x54e0
jmp 0x18061
movzbl 0x7(%rsp), %esi
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5450
movq 0x10(%rsp), %rsi
movzbl 0x7(%rsp), %edx
movq %r15, %rdi
callq 0x5490
movq %rbx, %rdi
leaq 0x3413(%rip), %rsi      # 0x1b3e4
callq 0x50f0
testl %eax, %eax
je 0x18038
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x6924
movl 0xc(%rsp), %edi
leaq 0x30(%rsp), %rsi
callq 0x1a9d7
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x18025
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x0, 0x8(%rsp)
movl 0xc(%rsp), %edi
callq 0x1aa11
jmp 0x18040
movl $0x2, 0x8(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1805c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x1, %ebx
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x18078
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
testl %ebx, %ebx
je 0x17ef8
cmpl $0x3, %ebx
jne 0x18097
movl $0x3, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x50b0
leaq 0x70(%rsp), %rdi
callq 0x50d0
movl 0x8(%rsp), %eax
jmp 0x180ac
movl $0x3, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1812c
movq %rax, %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x180e8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x180e8
jmp 0x18110
jmp 0x1812c
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x18113
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x18113
movq %rax, %rdi
callq 0x6919
jmp 0x1812c
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1812f
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x1812f
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x50d0
movq %r14, %rdi
callq 0x5670
nop