// File: /workspace/github_cpp_makefile_star2_O3/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 0x7c4a
leaq 0x40(%rsp), %rdi
callq 0x2c58
movq 0x8(%r15), %rsi
movl (%r12), %edx
leaq 0x18(%rsp), %rdi
callq 0x7e00
movl %eax, %ebp
testl %eax, %eax
je 0x6279
leaq 0x40(%rsp), %rdi
callq 0x2e26
leaq 0x18(%rsp), %rdi
callq 0x7c5a
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 0x62e4
movl $0xfffffff7, %ebp       # imm = 0xFFFFFFF7
cmpl $0x0, 0x30(%rsp)
je 0x6254
xorl %r12d, %r12d
leaq 0x18(%rsp), %r13
leaq 0x40(%rsp), %rbx
movq %r13, %rdi
movl %r12d, %esi
movq %rbx, %rdx
callq 0x802a
testl %eax, %eax
jne 0x6387
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2e9e
testb %al, %al
jne 0x6316
incl %r12d
movl 0x30(%rsp), %esi
cmpl %esi, %r12d
jb 0x629e
testl %esi, %esi
je 0x6254
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
callq 0x8078
jmp 0x6328
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x6254
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x6354
movl (%r12), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
xorl %ebp, %ebp
jmp 0x6254
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movl %r12d, %esi
callq 0x8078
movl %eax, %ebp
testl %eax, %eax
jne 0x6254
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x6363
addq $0x8, %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, (%rax)
jmp 0x6372
xorl %ebp, %ebp
movq %r12, %rdx
callq 0x7986
jmp 0x6254
leaq 0xc(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x7986
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
callq 0x620c
movl %eax, %ebp
jmp 0x6254
movq %rax, %rbx
jmp 0x63a2
jmp 0x6395
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2e26
leaq 0x18(%rsp), %rdi
callq 0x7c5a
movq %rbx, %rdi
callq 0x2370