// File: /workspace/github_cpp_makefile_star2_O1/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 0x7d2a
leaq 0x58(%rsp), %rdi
callq 0x2c54
leaq 0x30(%rsp), %rdi
callq 0x2c54
leaq 0x80(%rsp), %rdi
callq 0x2c54
movq 0x8(%rbx), %rsi
movl (%r14), %edx
leaq 0x8(%rsp), %rdi
callq 0x7eee
movl %eax, %ebp
testl %eax, %eax
je 0x7644
leaq 0x80(%rsp), %rdi
callq 0x2e22
leaq 0x30(%rsp), %rdi
callq 0x2e22
leaq 0x58(%rsp), %rdi
callq 0x2e22
leaq 0x8(%rsp), %rdi
callq 0x7d3a
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 0x770b
movq 0x6969(%rip), %rdi      # 0xdfd0
leaq 0x215f(%rip), %rsi      # 0x97cd
movl $0x15, %edx
callq 0x2230
movl 0x8(%rsp), %esi
movq 0x694d(%rip), %rdi      # 0xdfd0
callq 0x2360
movq %rax, %r14
leaq 0x2151(%rip), %rsi      # 0x97e3
movl $0x5, %edx
movq %rax, %rdi
callq 0x2230
movl 0x20(%rsp), %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r14
leaq 0x2134(%rip), %rsi      # 0x97e9
movl $0x39, %edx
movq %rax, %rdi
callq 0x2230
movl 0x40(%rbx), %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r14
leaq 0x214c(%rip), %rsi      # 0x9823
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 0x77c7
cmpl $0x0, 0x20(%rsp)
je 0x789e
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
xorl %esi, %esi
callq 0x8114
movl %eax, %ebp
testl %eax, %eax
jne 0x7605
movl 0x20(%rsp), %eax
addl $-0x3, %eax
cmpl $-0x3, %eax
ja 0x7798
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movl $0x1, %esi
callq 0x8114
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2ebc
testb %al, %al
je 0x7798
movq 0x6855(%rip), %rdi      # 0xdfd0
leaq 0x20a8(%rip), %rsi      # 0x982a
movl $0x21, %edx
callq 0x2230
movq 0x683d(%rip), %rdi      # 0xdfd0
callq 0x20c0
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
xorl %esi, %esi
callq 0x8162
movl %eax, %ebp
testl %eax, %eax
jne 0x7605
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x75ac
movl %eax, %ebp
jmp 0x7605
movl $0xfffffff1, %ebp       # imm = 0xFFFFFFF1
cmpl $0x4, %eax
jne 0x7605
cmpl $0x0, 0x20(%rsp)
je 0x78ee
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
leaq 0x58(%rsp), %r15
leaq 0x80(%rsp), %r12
movq 0x67d5(%rip), %r13      # 0xdfd0
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x8114
movl %eax, %ebp
testl %eax, %eax
jne 0x78f5
movq %r14, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x8180
movl %eax, %ebp
testl %eax, %eax
jne 0x7940
incl %ebx
cmpl 0x20(%rsp), %ebx
jae 0x788d
movq %r14, %rdi
movl %ebx, %esi
leaq 0x30(%rsp), %rdx
callq 0x8114
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x2ebc
testb %al, %al
je 0x788d
movl $0x21, %edx
movq %r13, %rdi
leaq 0x1fca(%rip), %rsi      # 0x982a
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 0x77fb
jmp 0x7605
movq 0x672b(%rip), %rbx      # 0xdfd0
leaq 0x1fa0(%rip), %rsi      # 0x984c
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 0x66f9(%rip), %rdi      # 0xdfd0
callq 0x2040
movl $0xfffffff7, %ebp       # imm = 0xFFFFFFF7
movq %rax, %rdi
callq 0x2130
jmp 0x7605
xorl %ebp, %ebp
jmp 0x7605
movq 0x66d4(%rip), %rbx      # 0xdfd0
leaq 0x1f79(%rip), %rsi      # 0x987c
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 0x66a2(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2130
jmp 0x7605
movq 0x6689(%rip), %rbx      # 0xdfd0
leaq 0x1f47(%rip), %rsi      # 0x9895
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 0x6657(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2130
jmp 0x7605
movq %rax, %rbx
jmp 0x79ac
movq %rax, %rbx
jmp 0x79b6
movq %rax, %rbx
jmp 0x79c0
jmp 0x799c
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2e22
leaq 0x30(%rsp), %rdi
callq 0x2e22
leaq 0x58(%rsp), %rdi
callq 0x2e22
leaq 0x8(%rsp), %rdi
callq 0x7d3a
movq %rbx, %rdi
callq 0x2370