// File: /workspace/github_cpp_makefile_star2_O3/cpottamus[P]btree_lab/btree.cc
// Function: BTreeIndex::SanityWalk(unsigned int const&) const
ERROR_T BTreeIndex::SanityWalk(const SIZE_T &node/*, std::set<BTreeNode> &allTreeNodes*/) const{
BTreeNode b;
ERROR_T rc;
SIZE_T offset;
KEY_T testkey;
KEY_T tempkey;
SIZE_T ptr;
VALUE_T value;

rc = b.Unserialize(buffercache, node);

  //Check if node is already in our BTree
  // bool is_in = allTreeNodes.find(b) != allTreeNodes.end();
  // if(is_in) {
  //   std::cout<<"node "<<b<<" has already been visited by this BTree"<<std::endl;
  // }
  // allTreeNodes.insert(b);

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

      //Check to see if the nodes have proper lengths
if(b.info.numkeys>(int)(2*maxNumKeys/3)){
  std::cout << "Current Node of type "<<b.info.nodetype<<" has "<<b.info.numkeys<<" keys. Which is over the 2/3 threshold of the maximum of "<<maxNumKeys<<" keys."<<std::endl;
}

switch(b.info.nodetype){
  case BTREE_ROOT_NODE:
  case BTREE_INTERIOR_NODE:
      //Scan through key/ptr pairs
      //and recurse if possible

    //TODO :: Push node onto set, where we can check against other visited nodes.  4, 5.

  for(offset=0; offset<b.info.numkeys; offset++){
    rc = b.GetKey(offset,testkey);
    if(rc) {return rc; }

      //If keys are not in proper size order
    if(offset+1<b.info.numkeys-1){
      rc = b.GetKey(offset+1, tempkey);
      if(tempkey < testkey){
        std::cout<<"The keys are not properly sorted!"<<std::endl;
      }

    }

    rc=b.GetPtr(offset,ptr);
    if(rc){return rc;}

        return SanityWalk(ptr/*, allTreeNodes*/);

//      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;}

        // return SanityWalk(ptr, key);
   //   }
  }

    //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; }

      return SanityWalk(ptr/*, allTreeNodes*/);
  }else{
      //There are no keys at all on this node, so nowhere to go
    std::cout << "The keys on this interior node are nonexistent."<<std::endl;
    return ERROR_NONEXISTENT;
  }
  break;
  case BTREE_LEAF_NODE:

  for(offset=0; offset<b.info.numkeys;offset++){
    rc = b.GetKey(offset, testkey);
    if(rc) { 
      std::cout << "Leaf Node is missing key"<<std::endl;
      return rc;
    }
    rc =b.GetVal(offset, value);
    if(rc){
      std::cout << "leaf node key is missing associated value"<<std::endl;
      return rc;
    }

      //If keys are not in proper size order
    if(offset+1<b.info.numkeys){
      rc = b.GetKey(offset+1, tempkey);
      if(tempkey < testkey){
        std::cout<<"The keys are not properly sorted!"<<std::endl;
      }
    }
  }
  break;
  default:

  return ERROR_INSANE;
  break;
}

return ERROR_NOERROR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %rdi
callq 0x7c4a
leaq 0x58(%rsp), %rdi
callq 0x2c58
leaq 0x30(%rsp), %rdi
callq 0x2c58
leaq 0x80(%rsp), %rdi
callq 0x2c58
movq 0x8(%rbx), %rsi
movl (%r14), %edx
leaq 0x8(%rsp), %rdi
callq 0x7e00
movl %eax, %ebp
testl %eax, %eax
je 0x7564
leaq 0x80(%rsp), %rdi
callq 0x2e26
leaq 0x30(%rsp), %rdi
callq 0x2e26
leaq 0x58(%rsp), %rdi
callq 0x2e26
leaq 0x8(%rsp), %rdi
callq 0x7c5a
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x40(%rbx), %eax
addl %eax, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
cmpl %ecx, 0x20(%rsp)
jbe 0x762b
movq 0x6a49(%rip), %rdi      # 0xdfd0
leaq 0x2257(%rip), %rsi      # 0x97e5
movl $0x15, %edx
callq 0x2230
movl 0x8(%rsp), %esi
movq 0x6a2d(%rip), %rdi      # 0xdfd0
callq 0x2360
movq %rax, %r14
leaq 0x2249(%rip), %rsi      # 0x97fb
movl $0x5, %edx
movq %rax, %rdi
callq 0x2230
movl 0x20(%rsp), %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r14
leaq 0x222c(%rip), %rsi      # 0x9801
movl $0x39, %edx
movq %rax, %rdi
callq 0x2230
movl 0x40(%rbx), %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r14
leaq 0x2244(%rip), %rsi      # 0x983b
movl $0x6, %edx
movq %rax, %rdi
callq 0x2230
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movl 0x8(%rsp), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0x76e7
cmpl $0x0, 0x20(%rsp)
je 0x77be
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
xorl %esi, %esi
callq 0x802a
movl %eax, %ebp
testl %eax, %eax
jne 0x7525
movl 0x20(%rsp), %eax
addl $-0x3, %eax
cmpl $-0x3, %eax
ja 0x76b8
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movl $0x1, %esi
callq 0x802a
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2e9e
testb %al, %al
je 0x76b8
movq 0x6935(%rip), %rdi      # 0xdfd0
leaq 0x21a0(%rip), %rsi      # 0x9842
movl $0x21, %edx
callq 0x2230
movq 0x691d(%rip), %rdi      # 0xdfd0
callq 0x20c0
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
xorl %esi, %esi
callq 0x8078
movl %eax, %ebp
testl %eax, %eax
jne 0x7525
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x74cc
movl %eax, %ebp
jmp 0x7525
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x7525
cmpl $0x0, 0x20(%rsp)
je 0x780e
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x58(%rsp), %r15
leaq 0x80(%rsp), %r12
movq 0x68b5(%rip), %r13      # 0xdfd0
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x802a
movl %eax, %ebp
testl %eax, %eax
jne 0x7815
movq %r14, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x8096
movl %eax, %ebp
testl %eax, %eax
jne 0x7860
incl %ebx
cmpl 0x20(%rsp), %ebx
jae 0x77ad
movq %r14, %rdi
movl %ebx, %esi
leaq 0x30(%rsp), %rdx
callq 0x802a
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x2e9e
testb %al, %al
je 0x77ad
movl $0x21, %edx
movq %r13, %rdi
leaq 0x20c2(%rip), %rsi      # 0x9842
callq 0x2230
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
xorl %ebp, %ebp
cmpl 0x20(%rsp), %ebx
jb 0x771b
jmp 0x7525
movq 0x680b(%rip), %rbx      # 0xdfd0
leaq 0x2098(%rip), %rsi      # 0x9864
movl $0x2f, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq 0x67d9(%rip), %rdi      # 0xdfd0
callq 0x2040
movl $0xfffffff7, %ebp       # imm = 0xFFFFFFF7
movq %rax, %rdi
callq 0x2130
jmp 0x7525
xorl %ebp, %ebp
jmp 0x7525
movq 0x67b4(%rip), %rbx      # 0xdfd0
leaq 0x2071(%rip), %rsi      # 0x9894
movl $0x18, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq 0x6782(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2130
jmp 0x7525
movq 0x6769(%rip), %rbx      # 0xdfd0
leaq 0x203f(%rip), %rsi      # 0x98ad
movl $0x29, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq 0x6737(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2130
jmp 0x7525
movq %rax, %rbx
jmp 0x78cc
movq %rax, %rbx
jmp 0x78d6
movq %rax, %rbx
jmp 0x78e0
jmp 0x78bc
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2e26
leaq 0x30(%rsp), %rdi
callq 0x2e26
leaq 0x58(%rsp), %rdi
callq 0x2e26
leaq 0x8(%rsp), %rdi
callq 0x7c5a
movq %rbx, %rdi
callq 0x2370