// File: 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
movq %rsp, %rbp
subq $0x320, %rsp            # imm = 0x320
movq %rsi, -0x2c8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x399e0
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x39a40
movq -0x2c8(%rbp), %rsi
movq %rax, %rdi
callq 0x39a10
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2b9(%rbp)
jne 0x38e4a
jmp 0x38e6d
movq -0x2d0(%rbp), %rdi
callq 0x39a90
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x39a60
movb %al, -0x2b9(%rbp)
movb -0x2b9(%rbp), %al
testb $0x1, %al
jne 0x38e79
jmp 0x38e85
movl $0x3, -0x4(%rbp)
jmp 0x39292
leaq -0x28(%rbp), %rdi
callq 0x39a40
movq -0x2c8(%rbp), %rsi
movq %rax, %rdi
callq 0x39a10
testb $0x1, %al
jne 0x38ea3
jmp 0x38eac
leaq -0x28(%rbp), %rdi
callq 0x39ac0
jmp 0x38eae
leaq -0x238(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x65e0
movq -0x2d0(%rbp), %rdi
movq -0x2d8(%rbp), %rsi
callq 0x38d20
jmp 0x38ed6
leaq -0x28(%rbp), %rdi
callq 0x39a40
movq 0x20(%rax), %rsi
leaq -0x238(%rbp), %rdi
movl $0x1, %edx
callq 0x66c0
jmp 0x38ef6
jmp 0x38ef8
leaq -0x238(%rbp), %rdi
callq 0x61d0
movl %eax, -0x2dc(%rbp)
jmp 0x38f0c
movl -0x2dc(%rbp), %eax
cmpl $-0x1, %eax
je 0x39250
leaq -0x238(%rbp), %rdi
leaq -0x245(%rbp), %rsi
callq 0x65d0
jmp 0x38f30
movzbl -0x245(%rbp), %eax
movq %rax, -0x2f0(%rbp)
leaq -0x269(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x68d0
movq -0x2f0(%rbp), %rsi
movq -0x2e8(%rbp), %rcx
leaq -0x268(%rbp), %rdi
xorl %edx, %edx
callq 0x17140
jmp 0x38f6f
leaq -0x269(%rbp), %rdi
callq 0x6560
xorl %eax, %eax
movl %eax, %esi
leaq -0x268(%rbp), %rdi
callq 0x69b0
movq %rax, -0x2f8(%rbp)
jmp 0x38f94
movq -0x2f8(%rbp), %rsi
movzbl -0x245(%rbp), %eax
movl %eax, %edx
leaq -0x238(%rbp), %rdi
callq 0x6660
jmp 0x38fb2
movq -0x2c8(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x39af0
testb $0x1, %al
jne 0x38fcb
jmp 0x3902b
movl $0x3, -0x270(%rbp)
jmp 0x39212
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
jmp 0x39284
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
leaq -0x269(%rbp), %rdi
callq 0x6560
jmp 0x39284
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
jmp 0x39242
leaq -0x238(%rbp), %rdi
leaq -0x245(%rbp), %rsi
callq 0x65d0
jmp 0x39040
movq -0x2c8(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x1b960
testb $0x1, %al
jne 0x3905c
jmp 0x391ec
movzbl -0x245(%rbp), %eax
movq %rax, -0x308(%rbp)
leaq -0x291(%rbp), %rdi
movq %rdi, -0x300(%rbp)
callq 0x68d0
movq -0x308(%rbp), %rsi
movq -0x300(%rbp), %rcx
leaq -0x290(%rbp), %rdi
xorl %edx, %edx
callq 0x17140
jmp 0x3909b
leaq -0x291(%rbp), %rdi
callq 0x6560
xorl %eax, %eax
movl %eax, %esi
leaq -0x290(%rbp), %rdi
callq 0x69b0
movq %rax, -0x310(%rbp)
jmp 0x390c0
movq -0x310(%rbp), %rsi
movzbl -0x245(%rbp), %eax
movl %eax, %edx
leaq -0x238(%rbp), %rdi
callq 0x6660
jmp 0x390de
leaq 0x8599(%rip), %rsi      # 0x4167e
leaq -0x290(%rbp), %rdi
callq 0xaa80
movb %al, -0x311(%rbp)
jmp 0x390f9
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0x39105
jmp 0x39155
movl $0x2, -0x4(%rbp)
movl $0x1, -0x270(%rbp)
jmp 0x391d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
leaq -0x291(%rbp), %rdi
callq 0x6560
jmp 0x39242
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
jmp 0x391de
movl -0x1c(%rbp), %eax
movl %eax, -0x318(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x6210
jmp 0x39173
movl -0x318(%rbp), %edi
leaq -0x2b8(%rbp), %rsi
callq 0x3e3a0
jmp 0x39187
leaq -0x2b8(%rbp), %rdi
callq 0x6340
movl -0x1c(%rbp), %edi
callq 0x3e410
jmp 0x3919d
movl $0x0, -0x4(%rbp)
movl $0x1, -0x270(%rbp)
jmp 0x391d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x240(%rbp)
movl %eax, -0x244(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x6340
jmp 0x391de
leaq -0x290(%rbp), %rdi
callq 0x6340
jmp 0x39212
leaq -0x290(%rbp), %rdi
callq 0x6340
jmp 0x39242
movzbl -0x245(%rbp), %eax
movl %eax, %esi
leaq -0x238(%rbp), %rdi
movl $0x1, %edx
callq 0x66c0
jmp 0x39208
movl $0x0, -0x270(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x6340
movl -0x270(%rbp), %eax
movl %eax, -0x31c(%rbp)
testl %eax, %eax
je 0x3923d
jmp 0x39230
movl -0x31c(%rbp), %eax
subl $0x3, %eax
je 0x39250
jmp 0x39276
jmp 0x38ef8
leaq -0x268(%rbp), %rdi
callq 0x6340
jmp 0x39284
movq -0x2d0(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x38d60
jmp 0x39265
movl $0x3, -0x4(%rbp)
movl $0x1, -0x270(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x6130
jmp 0x39292
leaq -0x238(%rbp), %rdi
callq 0x6130
jmp 0x3929e
movl -0x4(%rbp), %eax
addq $0x320, %rsp            # imm = 0x320
popq %rbp
retq
movq -0x240(%rbp), %rdi
callq 0x68c0
nopw (%rax,%rax)