// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xee80
leaq -0x70(%rbp), %rdi
callq 0x43e0
jmp 0xd542
leaq -0xa8(%rbp), %rdi
callq 0x43e0
jmp 0xd550
leaq -0xd8(%rbp), %rdi
callq 0x43e0
jmp 0xd55e
movq -0xe8(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq -0x40(%rbp), %rdi
callq 0xf1a0
movl %eax, -0xec(%rbp)
jmp 0xd580
movl -0xec(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xd5f4
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xdb29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xdb20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xdb14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x46e0
jmp 0xdb14
movq -0xe8(%rbp), %rax
movl -0x28(%rbp), %ecx
movl %ecx, -0xf0(%rbp)
movl 0x40(%rax), %eax
shll %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movl -0xf0(%rbp), %eax
cmpl %ecx, %eax
jbe 0xd6f8
movq 0x99a7(%rip), %rdi      # 0x16fd0
leaq 0x31ad(%rip), %rsi      # 0x107dd
callq 0x31f0
movq %rax, -0xf8(%rbp)
jmp 0xd63e
movq -0xf8(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x33f0
movq %rax, -0x100(%rbp)
jmp 0xd656
movq -0x100(%rbp), %rdi
leaq 0x318f(%rip), %rsi      # 0x107f3
callq 0x31f0
movq %rax, -0x108(%rbp)
jmp 0xd672
movq -0x108(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x3320
movq %rax, -0x110(%rbp)
jmp 0xd68a
movq -0x110(%rbp), %rdi
leaq 0x3161(%rip), %rsi      # 0x107f9
callq 0x31f0
movq %rax, -0x118(%rbp)
jmp 0xd6a6
movq -0x118(%rbp), %rdi
movq -0xe8(%rbp), %rax
movl 0x40(%rax), %esi
callq 0x3320
movq %rax, -0x120(%rbp)
jmp 0xd6c5
movq -0x120(%rbp), %rdi
leaq 0x3160(%rip), %rsi      # 0x10833
callq 0x31f0
movq %rax, -0x128(%rbp)
jmp 0xd6e1
movq -0x128(%rbp), %rdi
movq 0x98d1(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xd6f6
jmp 0xd6f8
movl -0x40(%rbp), %eax
movl %eax, -0x12c(%rbp)
addl $-0x2, %eax
subl $0x2, %eax
jb 0xd71f
jmp 0xd70b
movl -0x12c(%rbp), %eax
subl $0x4, %eax
je 0xd92e
jmp 0xdaba
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0xd86f
movl -0x48(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x130(%rbp)
jmp 0xd74a
movl -0x130(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xd76e
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xd7fd
movl -0x48(%rbp), %esi
incl %esi
leaq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x134(%rbp)
jmp 0xd79b
movl -0x134(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x47c0
movb %al, -0x135(%rbp)
jmp 0xd7bc
movb -0x135(%rbp), %al
testb $0x1, %al
jne 0xd7c8
jmp 0xd7fb
movq 0x9801(%rip), %rdi      # 0x16fd0
leaq 0x3064(%rip), %rsi      # 0x1083a
callq 0x31f0
movq %rax, -0x140(%rbp)
jmp 0xd7e4
movq -0x140(%rbp), %rdi
movq 0x97ce(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xd7f9
jmp 0xd7fb
jmp 0xd7fd
movl -0x48(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0xac(%rbp), %rdx
callq 0xf650
movl %eax, -0x144(%rbp)
jmp 0xd818
movl -0x144(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xd83c
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movq -0xe8(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0xd510
movl %eax, -0x148(%rbp)
jmp 0xd857
movl -0x148(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
cmpl $0x0, -0x28(%rbp)
jbe 0xd8e7
movl -0x28(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0xac(%rbp), %rdx
callq 0xf650
movl %eax, -0x14c(%rbp)
jmp 0xd890
movl -0x14c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xd8b4
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movq -0xe8(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0xd510
movl %eax, -0x150(%rbp)
jmp 0xd8cf
movl -0x150(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movq 0x96e2(%rip), %rdi      # 0x16fd0
leaq 0x2f67(%rip), %rsi      # 0x1085c
callq 0x31f0
movq %rax, -0x158(%rbp)
jmp 0xd903
movq -0x158(%rbp), %rdi
movq 0x96af(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xd918
movl $0xfffffff7, -0x4(%rbp) # imm = 0xFFFFFFF7
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0xdab8
movl -0x48(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x15c(%rbp)
jmp 0xd959
movl -0x15c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xd9ae
movq 0x9661(%rip), %rdi      # 0x16fd0
leaq 0x2f16(%rip), %rsi      # 0x1088c
callq 0x31f0
movq %rax, -0x168(%rbp)
jmp 0xd984
movq -0x168(%rbp), %rdi
movq 0x962e(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xd999
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movl -0x48(%rbp), %esi
leaq -0x40(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0xf6a0
movl %eax, -0x16c(%rbp)
jmp 0xd9c9
movl -0x16c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xda1e
movq 0x95f1(%rip), %rdi      # 0x16fd0
leaq 0x2ebf(%rip), %rsi      # 0x108a5
callq 0x31f0
movq %rax, -0x178(%rbp)
jmp 0xd9f4
movq -0x178(%rbp), %rdi
movq 0x95be(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xda09
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movl -0x48(%rbp), %eax
addl $0x1, %eax
cmpl -0x28(%rbp), %eax
jae 0xdaa8
movl -0x48(%rbp), %esi
incl %esi
leaq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x17c(%rbp)
jmp 0xda46
movl -0x17c(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x47c0
movb %al, -0x17d(%rbp)
jmp 0xda67
movb -0x17d(%rbp), %al
testb $0x1, %al
jne 0xda73
jmp 0xdaa6
movq 0x9556(%rip), %rdi      # 0x16fd0
leaq 0x2db9(%rip), %rsi      # 0x1083a
callq 0x31f0
movq %rax, -0x188(%rbp)
jmp 0xda8f
movq -0x188(%rbp), %rdi
movq 0x9523(%rip), %rsi      # 0x16fc0
callq 0x3240
jmp 0xdaa4
jmp 0xdaa6
jmp 0xdaa8
jmp 0xdaaa
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xd935
jmp 0xdacd
movl $0xfffffff1, -0x4(%rbp) # imm = 0xFFFFFFF1
movl $0x1, -0xdc(%rbp)
jmp 0xdade
movl $0x0, -0x4(%rbp)
movl $0x1, -0xdc(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x46e0
leaq -0xa8(%rbp), %rdi
callq 0x46e0
leaq -0x70(%rbp), %rdi
callq 0x46e0
leaq -0x40(%rbp), %rdi
callq 0xeea0
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0xa8(%rbp), %rdi
callq 0x46e0
leaq -0x70(%rbp), %rdi
callq 0x46e0
leaq -0x40(%rbp), %rdi
callq 0xeea0
movq -0x78(%rbp), %rdi
callq 0x3400
nopl (%rax,%rax)