// File: /workspace/github_cpp_makefile_star2_O2/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 0x725a
leaq 0x58(%rsp), %rdi
callq 0x2ac4
leaq 0x30(%rsp), %rdi
callq 0x2ac4
leaq 0x80(%rsp), %rdi
callq 0x2ac4
movq 0x8(%rbx), %rsi
movl (%r14), %edx
leaq 0x8(%rsp), %rdi
callq 0x73fc
movl %eax, %r12d
testl %eax, %eax
jne 0x6e74
movl 0x20(%rsp), %ecx
movl 0x40(%rbx), %eax
addl %eax, %eax
pushq $0x3
popq %rsi
xorl %edx, %edx
divl %esi
cmpl %eax, %ecx
jbe 0x6cb9
movq 0x637b(%rip), %rdi      # 0xcfd0
leaq 0x1b89(%rip), %rsi      # 0x87e5
callq 0x21b0
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x2370
leaq 0x1b87(%rip), %rsi      # 0x87fb
movq %rax, %rdi
callq 0x21b0
movl 0x20(%rsp), %esi
movq %rax, %rdi
callq 0x2120
leaq 0x1b72(%rip), %rsi      # 0x8801
movq %rax, %rdi
callq 0x21b0
movl 0x40(%rbx), %esi
movq %rax, %rdi
callq 0x2120
leaq 0x1b92(%rip), %rsi      # 0x883b
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
movl 0x8(%rsp), %eax
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0x6d6d
cmpl $0x0, 0x20(%rsp)
je 0x6e18
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
xorl %esi, %esi
callq 0x7622
movl %eax, %r12d
testl %eax, %eax
jne 0x6e74
movl 0x20(%rsp), %eax
addl $-0x3, %eax
cmpl $-0x3, %eax
ja 0x6d3c
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
leaq 0x30(%rsp), %rdx
callq 0x7622
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2ccc
testb %al, %al
je 0x6d3c
movq 0x62a8(%rip), %rdi      # 0xcfd0
leaq 0x1b13(%rip), %rsi      # 0x8842
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
xorl %esi, %esi
callq 0x766e
movl %eax, %r12d
testl %eax, %eax
jne 0x6e74
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x6bdc
movl %eax, %r12d
jmp 0x6e74
cmpl $0x4, %eax
jne 0x6e14
xorl %r12d, %r12d
leaq 0x8(%rsp), %rbx
leaq 0x58(%rsp), %r14
leaq 0x80(%rsp), %r15
xorl %r13d, %r13d
cmpl 0x20(%rsp), %r13d
jae 0x6e74
movq %rbx, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x7622
movl %eax, %ebp
testl %eax, %eax
jne 0x6e39
movq %rbx, %rdi
movl %r13d, %esi
movq %r15, %rdx
callq 0x768a
movl %eax, %ebp
testl %eax, %eax
jne 0x6e56
incl %r13d
cmpl 0x20(%rsp), %r13d
jae 0x6d8e
movq %rbx, %rdi
movl %r13d, %esi
leaq 0x30(%rsp), %rdx
callq 0x7622
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x2ccc
testb %al, %al
je 0x6d8e
movq 0x61d5(%rip), %rdi      # 0xcfd0
leaq 0x1a40(%rip), %rsi      # 0x8842
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
jmp 0x6d8e
pushq $-0xf
jmp 0x6e35
movq 0x61b1(%rip), %rdi      # 0xcfd0
leaq 0x1a3e(%rip), %rsi      # 0x8864
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
pushq $-0x9
popq %r12
jmp 0x6e74
movq 0x6190(%rip), %rdi      # 0xcfd0
leaq 0x1a4d(%rip), %rsi      # 0x8894
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
jmp 0x6e71
movq 0x6173(%rip), %rdi      # 0xcfd0
leaq 0x1a49(%rip), %rsi      # 0x88ad
callq 0x21b0
movq %rax, %rdi
callq 0x20c0
movl %ebp, %r12d
leaq 0x80(%rsp), %rdi
callq 0x2c6e
leaq 0x30(%rsp), %rdi
callq 0x2c6e
leaq 0x58(%rsp), %rdi
callq 0x2c6e
leaq 0x8(%rsp), %rdi
callq 0x7264
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6ed5
movq %rax, %rbx
jmp 0x6edf
movq %rax, %rbx
jmp 0x6ee9
jmp 0x6ec5
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2c6e
leaq 0x30(%rsp), %rdi
callq 0x2c6e
leaq 0x58(%rsp), %rdi
callq 0x2c6e
leaq 0x8(%rsp), %rdi
callq 0x7264
movq %rbx, %rdi
callq 0x2380
nop