// File: /workspace/github_cpp_makefile_star2_O2/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, %ebx
movq %rsi, %r14
movq %rdi, %r15
callq 0x155f0
movq %rax, %r12
leaq 0x20(%rax), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x15072
testb %al, %al
je 0x14bb2
cmpq 0x18(%r15), %r12
jne 0x14bb2
pushq $0x3
popq %rbp
jmp 0x14d45
movq %r13, %rdi
movq %r14, %rsi
callq 0x15072
testb %al, %al
je 0x14bcc
movq %r12, %rdi
callq 0x5410
movq %rax, %r12
movl %ebx, 0xc(%rsp)
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x5450
movq %r15, %rdi
movq %r13, %rsi
callq 0x14af6
movq 0x40(%r12), %rsi
leaq 0x70(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x5530
leaq 0x40(%rsp), %rbx
leaq 0x70(%rsp), %r15
leaq 0xb(%rsp), %r12
leaq 0x30(%rsp), %r13
pushq $0x1
popq %rbp
movq %r15, %rdi
callq 0x5180
cmpl $-0x1, %eax
je 0x14c9b
movq %r15, %rdi
movq %r12, %rsi
callq 0x5510
movzbl 0xb(%rsp), %esi
movq %rbx, 0x30(%rsp)
movq %r13, %rdi
xorl %edx, %edx
callq 0x5470
movq 0x30(%rsp), %rsi
movzbl 0xb(%rsp), %edx
movq %r15, %rdi
callq 0x54d0
movq %r13, %rdi
movq %r14, %rsi
callq 0x1507c
testb %al, %al
jne 0x14c91
movq %r15, %rdi
movq %r12, %rsi
callq 0x5510
movq %r13, %rdi
movq %r14, %rsi
callq 0xc061
testb %al, %al
jne 0x14cad
movzbl 0xb(%rsp), %esi
movq %r15, %rdi
movl %ebp, %edx
callq 0x5530
movq %r13, %rdi
callq 0x51d0
jmp 0x14c11
leaq 0x30(%rsp), %rdi
callq 0x51d0
leaq 0x70(%rsp), %rdi
callq 0x50a0
pushq $0x3
popq %rbp
jmp 0x14d3b
movzbl 0xb(%rsp), %esi
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x5470
movq 0x10(%rsp), %rsi
movzbl 0xb(%rsp), %edx
leaq 0x70(%rsp), %rdi
callq 0x54d0
movl 0xc(%rsp), %ebx
leaq 0x26fe(%rip), %rsi      # 0x173e4
leaq 0x10(%rsp), %rdi
callq 0x7362
testb %al, %al
je 0x14cf9
pushq $0x2
popq %rbp
jmp 0x14d27
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5540
leaq 0x50(%rsp), %rsi
movl %ebx, %edi
callq 0x1677c
leaq 0x50(%rsp), %rdi
callq 0x51d0
xorl %ebp, %ebp
movl %ebx, %edi
callq 0x167ab
leaq 0x10(%rsp), %rdi
callq 0x51d0
leaq 0x30(%rsp), %rdi
callq 0x51d0
leaq 0x70(%rsp), %rdi
callq 0x50e0
movl %ebp, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x51d0
jmp 0x14d6d
jmp 0x14d84
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x51d0
jmp 0x14d87
jmp 0x14d7f
jmp 0x14d7f
jmp 0x14d7f
movq %rax, %rbx
jmp 0x14d91
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x51d0
leaq 0x70(%rsp), %rdi
callq 0x50e0
movq %rbx, %rdi
callq 0x56c0
nop