// File: /workspace/github_cpp_makefile_star2_O2/cpottamus[P]btree_lab/btree.cc
// Function: BTreeIndex::LookupLeaf(unsigned int const&, Block const&, std::vector<unsigned int, std::allocator<unsigned int>>&)
ERROR_T BTreeIndex::LookupLeaf(const SIZE_T &node, const KEY_T &key, std::vector<SIZE_T> &pointerPath){
  BTreeNode b;
  ERROR_T rc;
  SIZE_T offset;
  KEY_T testkey;
  SIZE_T ptr;

  rc = b.Unserialize(buffercache, node);

  if(rc!=ERROR_NOERROR){
    return rc;
  }

  switch(b.info.nodetype){
    case BTREE_ROOT_NODE:
    case BTREE_INTERIOR_NODE:
      // Scan through key/ptr pairs
      //and recurse if possible
    for(offset=0;offset<b.info.numkeys; offset++){
      rc=b.GetKey(offset,testkey);
      if(rc) { return rc; }
      if(key < testkey){
            // OK, so we now have the first key that's larger
            // so we ned to recurse on the ptr immediately previous to 
            // this one, if it exists
        rc=b.GetPtr(offset,ptr);
        if (rc) { return rc; }
          //If there is no error on finding the appropriate pointer, push it onto our stack. 
        pointerPath.push_back(ptr);
        //cout << "PointerPath has: " << pointerPath[0] << endl;
        return LookupLeaf(ptr, key, pointerPath);
      }
    }

      //if we get here, we need to go to the next pointer, if it exists.
    if(b.info.numkeys>0){
      rc=b.GetPtr(b.info.numkeys,ptr);
      if (rc) { return rc; }
        //If there is no error on finding the appropriate pointer, push it onto our stack. 
      pointerPath.push_back(ptr);
      return LookupLeaf(ptr, key, pointerPath);
    } else {
        // There are no keys at all on this node, so nowhere to go
      return ERROR_NONEXISTENT;
    }
    break;
    case BTREE_LEAF_NODE:
    pointerPath.push_back(node);
    return ERROR_NOERROR;
    break;
    default:
        // We can't be looking at anything other than a root, internal, or leaf
    return ERROR_INSANE;
    break;
  }

  return ERROR_INSANE;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
leaq 0x18(%rsp), %rdi
callq 0x725a
leaq 0x40(%rsp), %rdi
callq 0x2ac4
movq 0x8(%r15), %rsi
movl (%r12), %edx
leaq 0x18(%rsp), %rdi
callq 0x73fc
movl %eax, %ebp
testl %eax, %eax
je 0x5b65
leaq 0x40(%rsp), %rdi
callq 0x2c6e
leaq 0x18(%rsp), %rdi
callq 0x7264
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rsp), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0x5baf
xorl %r12d, %r12d
leaq 0x18(%rsp), %r13
leaq 0x40(%rsp), %rbx
movl 0x30(%rsp), %esi
cmpl %esi, %r12d
jae 0x5bcc
movq %r13, %rdi
movl %r12d, %esi
movq %rbx, %rdx
callq 0x7622
movl %eax, %ebp
testl %eax, %eax
jne 0x5b40
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2ccc
testb %al, %al
jne 0x5be1
incl %r12d
jmp 0x5b7e
cmpl $0x4, %eax
jne 0x5bc8
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x6f0e
jmp 0x5b40
pushq $-0xf
jmp 0x5c2a
testl %esi, %esi
je 0x5c28
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x766e
jmp 0x5bf3
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r12d, %esi
callq 0x766e
movl %eax, %ebp
testl %eax, %eax
jne 0x5b40
leaq 0xc(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x6f0e
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
callq 0x5af8
movl %eax, %ebp
jmp 0x5b40
pushq $-0x9
popq %rbp
jmp 0x5b40
movq %rax, %rbx
jmp 0x5c44
jmp 0x5c37
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2c6e
leaq 0x18(%rsp), %rdi
callq 0x7264
movq %rbx, %rdi
callq 0x2380