// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
leaq 0x18(%rsp), %rdi
callq 0x7d2a
leaq 0x40(%rsp), %rdi
callq 0x2c54
movq 0x8(%r15), %rsi
movl (%r12), %edx
leaq 0x18(%rsp), %rdi
callq 0x7eee
movl %eax, %ebp
testl %eax, %eax
je 0x630f
leaq 0x40(%rsp), %rdi
callq 0x2e22
leaq 0x18(%rsp), %rdi
callq 0x7d3a
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 0x637e
movq %rbx, 0x10(%rsp)
movl 0x30(%rsp), %esi
testl %esi, %esi
je 0x6364
xorl %r12d, %r12d
leaq 0x18(%rsp), %r13
leaq 0x40(%rsp), %rbx
movq %r13, %rdi
movl %r12d, %esi
movq %rbx, %rdx
callq 0x8114
movl %eax, %ebp
testl %eax, %eax
jne 0x62ea
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2ebc
testb %al, %al
jne 0x63b5
incl %r12d
movl 0x30(%rsp), %esi
cmpl %esi, %r12d
jb 0x6335
testl %esi, %esi
movq 0x10(%rsp), %rbx
je 0x63ab
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x8162
jmp 0x63cc
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x62ea
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x63f3
movl (%r12), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0x62ea
movl $0xfffffff7, %ebp       # imm = 0xFFFFFFF7
jmp 0x62ea
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r12d, %esi
callq 0x8162
movq 0x10(%rsp), %rbx
movl %eax, %ebp
testl %eax, %eax
jne 0x62ea
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6405
leaq 0x8(%rbx), %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, (%rax)
jmp 0x6412
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rdx
callq 0x7a66
jmp 0x62ea
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
callq 0x7a66
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x62a4
movl %eax, %ebp
jmp 0x62ea
movq %rax, %rbx
jmp 0x6440
jmp 0x6433
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2e22
leaq 0x18(%rsp), %rdi
callq 0x7d3a
movq %rbx, %rdi
callq 0x2370