// File: /workspace/github_cpp_makefile_star2_O1/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 0x18a94
movq %rax, %r12
movq 0x28(%rax), %r13
movq 0x8(%r14), %rbx
cmpq %rbx, %r13
jne 0x17e13
testq %r13, %r13
je 0x17e1d
movq (%r14), %rsi
movq 0x20(%r12), %rdi
movq %r13, %rdx
callq 0x53b0
testl %eax, %eax
je 0x17e1d
movl $0x3, %eax
cmpq 0x18(%r15), %r12
je 0x18055
jmp 0x17e1d
cmpq 0x18(%r15), %r12
je 0x18050
cmpq %rbx, %r13
jne 0x17e3b
testq %r13, %r13
je 0x17e46
movq (%r14), %rsi
movq 0x20(%r12), %rdi
movq %r13, %rdx
callq 0x53b0
testl %eax, %eax
je 0x17e46
movq %r12, %rdi
callq 0x53d0
movq %rax, %r12
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x5420
addq $0x38, %r15
movq %r13, %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), %rbx
leaq 0x70(%rsp), %r15
leaq 0x7(%rsp), %r12
leaq 0x50(%rsp), %r13
movq %r15, %rdi
callq 0x5190
cmpl $-0x1, %eax
je 0x1802e
movq %r15, %rdi
movq %r12, %rsi
callq 0x54c0
movzbl 0x7(%rsp), %esi
movq %rbx, 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, %ebp
testl %eax, %eax
jg 0x1800a
movq %r15, %rdi
movq %r12, %rsi
callq 0x54c0
movq 0x58(%rsp), %rdx
cmpq 0x8(%r14), %rdx
jne 0x17f27
testq %rdx, %rdx
je 0x17f40
movq (%r14), %rsi
movq 0x50(%rsp), %rdi
callq 0x53b0
testl %eax, %eax
je 0x17f40
movzbl 0x7(%rsp), %esi
xorl %ebp, %ebp
movq %r15, %rdi
movl $0x1, %edx
callq 0x54e0
jmp 0x1800a
movzbl 0x7(%rsp), %esi
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x5450
movq 0x10(%rsp), %rsi
movzbl 0x7(%rsp), %edx
movq %r15, %rdi
callq 0x5490
movq %rbp, %rdi
leaq 0x346a(%rip), %rsi      # 0x1b3e4
callq 0x50f0
testl %eax, %eax
je 0x17fe1
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 0x1aa0a
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x17fce
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x0, 0x8(%rsp)
movl 0xc(%rsp), %edi
callq 0x1aa44
jmp 0x17fe9
movl $0x2, 0x8(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x18005
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x1, %ebp
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x18021
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
testl %ebp, %ebp
je 0x17ea1
cmpl $0x3, %ebp
jne 0x18040
movl $0x3, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x50b0
leaq 0x70(%rsp), %rdi
callq 0x50d0
movl 0x8(%rsp), %eax
jmp 0x18055
movl $0x3, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x180d5
movq %rax, %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x18091
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x18091
jmp 0x180b9
jmp 0x180d5
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x180bc
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x180bc
movq %rax, %rdi
callq 0x6919
jmp 0x180d5
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x180d8
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x180d8
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x50d0
movq %r14, %rdi
callq 0x5670