// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xee80
leaq -0x80(%rbp), %rdi
callq 0x43e0
jmp 0xb49a
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq -0x50(%rbp), %rdi
callq 0xf1a0
movl %eax, -0xa4(%rbp)
jmp 0xb4bc
movl -0xa4(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xb517
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0xb744
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x46e0
jmp 0xb744
movl -0x50(%rbp), %eax
movl %eax, -0xa8(%rbp)
addl $-0x2, %eax
subl $0x2, %eax
jb 0xb53e
jmp 0xb52a
movl -0xa8(%rbp), %eax
subl $0x4, %eax
je 0xb6f3
jmp 0xb715
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0xb64d
movl -0x58(%rbp), %esi
leaq -0x50(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0xf5d0
movl %eax, -0xac(%rbp)
jmp 0xb569
movl -0xac(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xb58d
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movq -0x20(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x47c0
movb %al, -0xad(%rbp)
jmp 0xb5a2
movb -0xad(%rbp), %al
testb $0x1, %al
jne 0xb5b1
jmp 0xb63d
movl -0x58(%rbp), %esi
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0xf650
movl %eax, -0xb4(%rbp)
jmp 0xb5cc
movl -0xb4(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xb5f0
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movq -0x28(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xdb90
jmp 0xb602
movq -0xa0(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x90(%rbp), %rsi
callq 0xb460
movl %eax, -0xb8(%rbp)
jmp 0xb625
movl -0xb8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
jmp 0xb63f
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xb545
cmpl $0x0, -0x38(%rbp)
jbe 0xb6e0
movl -0x38(%rbp), %esi
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0xf650
movl %eax, -0xbc(%rbp)
jmp 0xb672
movl -0xbc(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xb696
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movq -0x28(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xdb90
jmp 0xb6a8
movq -0xa0(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x90(%rbp), %rsi
callq 0xb460
movl %eax, -0xc0(%rbp)
jmp 0xb6cb
movl -0xc0(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movl $0xfffffff7, -0x4(%rbp) # imm = 0xFFFFFFF7
movl $0x1, -0x94(%rbp)
jmp 0xb726
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xdb90
jmp 0xb702
movl $0x0, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0xb726
movl $0xfffffff1, -0x4(%rbp) # imm = 0xFFFFFFF1
movl $0x1, -0x94(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x46e0
leaq -0x50(%rbp), %rdi
callq 0xeea0
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0xeea0
movq -0x88(%rbp), %rdi
callq 0x3400
nopl (%rax)