// File: cpottamus[P]btree_lab/btree_sane.cc
// Function: main
int main(int argc, char **argv)
{
  char *filestem;
  SIZE_T cachesize;
  SIZE_T superblocknum;

  if (argc!=3) { 
    usage();
    return -1;
  }

  filestem=argv[1];
  cachesize=atoi(argv[2]);

  DiskSystem disk(filestem);
  BufferCache cache(&disk,cachesize);
  BTreeIndex btree(0,0,&cache);
  
  ERROR_T rc;


  if ((rc=cache.Attach())!=ERROR_NOERROR) { 
    cerr << "Can't attach buffer cache due to error"<<rc<<endl;
    return -1;
  }

  if ((rc=btree.Attach(0))!=ERROR_NOERROR) { 
    cerr << "Can't attach to index  due to error "<<rc<<endl;
    return -1;
  } else {
    cerr << "Index attached!"<<endl;
    // Your Implementation should do the right thing here
    if ((rc=btree.SanityCheck())!=ERROR_NOERROR) { 
      cerr <<"Sanity failed: error "<<rc<<endl;
    } else {
      cerr <<"Sanity check succeded\n";
    }
    if ((rc=btree.Detach(superblocknum))!=ERROR_NOERROR) { 
      cerr <<"Can't detach from index due to error "<<rc<<endl;
      return -1;
    }
    if ((rc=cache.Detach())!=ERROR_NOERROR) { 
      cerr <<"Can't detach from cache due to error "<<rc<<endl;
      return -1;
    }
    cerr << "Performance statistics:\n";
    
    cerr << "numallocs       = "<<cache.GetNumAllocs()<<endl;
    cerr << "numdeallocs     = "<<cache.GetNumDeallocs()<<endl;
    cerr << "numreads        = "<<cache.GetNumReads()<<endl;
    cerr << "numdiskreads    = "<<cache.GetNumDiskReads()<<endl;
    cerr << "numwrites       = "<<cache.GetNumWrites()<<endl;
    cerr << "numdiskwrites   = "<<cache.GetNumDiskWrites()<<endl;
    cerr << endl;
    
    cerr << "total time      = "<<cache.GetCurrentTime()<<endl;

    return 0;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x3750
callq 0x3700
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3ecc
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x33a0
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x3410
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x3ef0
jmp 0x37a6
movq %rsp, %rax
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
xorl %r9d, %r9d
xorps %xmm2, %xmm2
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4300
jmp 0x37e7
leaq -0xc0(%rbp), %rdi
callq 0x3120
leaq -0xc1(%rbp), %rdi
callq 0x3270
movl -0x1c(%rbp), %edx
leaq -0x138(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x7070
jmp 0x3817
leaq -0x180(%rbp), %rdi
xorl %edx, %edx
leaq -0x138(%rbp), %rcx
movl $0x1, %r8d
movl %edx, %esi
callq 0x9570
jmp 0x3836
leaq -0x138(%rbp), %rdi
callq 0x7230
movl %eax, -0x19c(%rbp)
jmp 0x384a
movl -0x19c(%rbp), %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, %eax
je 0x3955
movq 0x1278a(%rip), %rdi     # 0x15ff0
leaq 0xc7bd(%rip), %rsi      # 0x1002a
callq 0x31f0
movq %rax, -0x1a8(%rbp)
jmp 0x387b
movq -0x1a8(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x33f0
movq %rax, -0x1b0(%rbp)
jmp 0x3896
movq -0x1b0(%rbp), %rdi
movq 0x1271c(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x38ab
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x188(%rbp)
jmp 0x3e8c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x38f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x3120
leaq -0xc1(%rbp), %rdi
callq 0x3270
jmp 0x3ed8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x3ebe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x3eb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x96c0
jmp 0x3eb2
leaq -0x180(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x9940
movl %eax, -0x1b4(%rbp)
jmp 0x396d
movl -0x1b4(%rbp), %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, %eax
je 0x39e0
movq 0x1266b(%rip), %rdi     # 0x15ff0
leaq 0xc6c5(%rip), %rsi      # 0x10051
callq 0x31f0
movq %rax, -0x1c0(%rbp)
jmp 0x399a
movq -0x1c0(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x33f0
movq %rax, -0x1c8(%rbp)
jmp 0x39b5
movq -0x1c8(%rbp), %rdi
movq 0x125fd(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x39ca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x188(%rbp)
jmp 0x3e8c
movq 0x12609(%rip), %rdi     # 0x15ff0
leaq 0xc688(%rip), %rsi      # 0x10076
callq 0x31f0
movq %rax, -0x1d0(%rbp)
jmp 0x39fc
movq -0x1d0(%rbp), %rdi
movq 0x125b6(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3a11
leaq -0x180(%rbp), %rdi
callq 0xcda0
movl %eax, -0x1d4(%rbp)
jmp 0x3a25
movl -0x1d4(%rbp), %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, %eax
je 0x3a84
movq 0x125b3(%rip), %rdi     # 0x15ff0
leaq 0xc642(%rip), %rsi      # 0x10086
callq 0x31f0
movq %rax, -0x1e0(%rbp)
jmp 0x3a52
movq -0x1e0(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x33f0
movq %rax, -0x1e8(%rbp)
jmp 0x3a6d
movq -0x1e8(%rbp), %rdi
movq 0x12545(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3a82
jmp 0x3a9b
movq 0x12565(%rip), %rdi     # 0x15ff0
leaq 0xc60a(%rip), %rsi      # 0x1009c
callq 0x31f0
jmp 0x3a99
jmp 0x3a9b
leaq -0x180(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x9d20
movl %eax, -0x1ec(%rbp)
jmp 0x3ab3
movl -0x1ec(%rbp), %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, %eax
je 0x3b26
movq 0x12525(%rip), %rdi     # 0x15ff0
leaq 0xc5e1(%rip), %rsi      # 0x100b3
callq 0x31f0
movq %rax, -0x1f8(%rbp)
jmp 0x3ae0
movq -0x1f8(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x33f0
movq %rax, -0x200(%rbp)
jmp 0x3afb
movq -0x200(%rbp), %rdi
movq 0x124b7(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3b10
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x188(%rbp)
jmp 0x3e8c
leaq -0x138(%rbp), %rdi
callq 0x7140
movl %eax, -0x204(%rbp)
jmp 0x3b3a
movl -0x204(%rbp), %eax
movl %eax, -0x184(%rbp)
cmpl $0x0, %eax
je 0x3bad
movq 0x1249e(%rip), %rdi     # 0x15ff0
leaq 0xc580(%rip), %rsi      # 0x100d9
callq 0x31f0
movq %rax, -0x210(%rbp)
jmp 0x3b67
movq -0x210(%rbp), %rdi
movl -0x184(%rbp), %esi
callq 0x33f0
movq %rax, -0x218(%rbp)
jmp 0x3b82
movq -0x218(%rbp), %rdi
movq 0x12430(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3b97
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x188(%rbp)
jmp 0x3e8c
movq 0x1243c(%rip), %rdi     # 0x15ff0
leaq 0xc544(%rip), %rsi      # 0x100ff
callq 0x31f0
jmp 0x3bc2
movq 0x12427(%rip), %rdi     # 0x15ff0
leaq 0xc548(%rip), %rsi      # 0x10118
callq 0x31f0
movq %rax, -0x220(%rbp)
jmp 0x3bde
leaq -0x138(%rbp), %rdi
callq 0x3fa0
movl %eax, -0x224(%rbp)
jmp 0x3bf2
movl -0x224(%rbp), %esi
movq -0x220(%rbp), %rdi
callq 0x3320
movq %rax, -0x230(%rbp)
jmp 0x3c0d
movq -0x230(%rbp), %rdi
movq 0x123a5(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3c22
movq 0x123c7(%rip), %rdi     # 0x15ff0
leaq 0xc4fb(%rip), %rsi      # 0x1012b
callq 0x31f0
movq %rax, -0x238(%rbp)
jmp 0x3c3e
leaq -0x138(%rbp), %rdi
callq 0x3fc0
movl %eax, -0x23c(%rbp)
jmp 0x3c52
movl -0x23c(%rbp), %esi
movq -0x238(%rbp), %rdi
callq 0x3320
movq %rax, -0x248(%rbp)
jmp 0x3c6d
movq -0x248(%rbp), %rdi
movq 0x12345(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3c82
movq 0x12367(%rip), %rdi     # 0x15ff0
leaq 0xc4ae(%rip), %rsi      # 0x1013e
callq 0x31f0
movq %rax, -0x250(%rbp)
jmp 0x3c9e
leaq -0x138(%rbp), %rdi
callq 0x3fe0
movl %eax, -0x254(%rbp)
jmp 0x3cb2
movl -0x254(%rbp), %esi
movq -0x250(%rbp), %rdi
callq 0x3320
movq %rax, -0x260(%rbp)
jmp 0x3ccd
movq -0x260(%rbp), %rdi
movq 0x122e5(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3ce2
movq 0x12307(%rip), %rdi     # 0x15ff0
leaq 0xc461(%rip), %rsi      # 0x10151
callq 0x31f0
movq %rax, -0x268(%rbp)
jmp 0x3cfe
leaq -0x138(%rbp), %rdi
callq 0x4000
movl %eax, -0x26c(%rbp)
jmp 0x3d12
movl -0x26c(%rbp), %esi
movq -0x268(%rbp), %rdi
callq 0x3320
movq %rax, -0x278(%rbp)
jmp 0x3d2d
movq -0x278(%rbp), %rdi
movq 0x12285(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3d42
movq 0x122a7(%rip), %rdi     # 0x15ff0
leaq 0xc414(%rip), %rsi      # 0x10164
callq 0x31f0
movq %rax, -0x280(%rbp)
jmp 0x3d5e
leaq -0x138(%rbp), %rdi
callq 0x4020
movl %eax, -0x284(%rbp)
jmp 0x3d72
movl -0x284(%rbp), %esi
movq -0x280(%rbp), %rdi
callq 0x3320
movq %rax, -0x290(%rbp)
jmp 0x3d8d
movq -0x290(%rbp), %rdi
movq 0x12225(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3da2
movq 0x12247(%rip), %rdi     # 0x15ff0
leaq 0xc3c7(%rip), %rsi      # 0x10177
callq 0x31f0
movq %rax, -0x298(%rbp)
jmp 0x3dbe
leaq -0x138(%rbp), %rdi
callq 0x4040
movl %eax, -0x29c(%rbp)
jmp 0x3dd2
movl -0x29c(%rbp), %esi
movq -0x298(%rbp), %rdi
callq 0x3320
movq %rax, -0x2a8(%rbp)
jmp 0x3ded
movq -0x2a8(%rbp), %rdi
movq 0x121c5(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3e02
movq 0x121e7(%rip), %rdi     # 0x15ff0
movq 0x121b0(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3e17
movq 0x121d2(%rip), %rdi     # 0x15ff0
leaq 0xc365(%rip), %rsi      # 0x1018a
callq 0x31f0
movq %rax, -0x2b0(%rbp)
jmp 0x3e33
leaq -0x138(%rbp), %rdi
callq 0x72c0
movsd %xmm0, -0x2b8(%rbp)
jmp 0x3e49
movsd -0x2b8(%rbp), %xmm0
movq -0x2b0(%rbp), %rdi
callq 0x3440
movq %rax, -0x2c0(%rbp)
jmp 0x3e66
movq -0x2c0(%rbp), %rdi
movq 0x1214c(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3e7b
movl $0x0, -0x4(%rbp)
movl $0x1, -0x188(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x96c0
leaq -0x138(%rbp), %rdi
callq 0x70e0
leaq -0xa0(%rbp), %rdi
callq 0x4b70
jmp 0x3ecc
leaq -0x138(%rbp), %rdi
callq 0x70e0
leaq -0xa0(%rbp), %rdi
callq 0x4b70
jmp 0x3ed8
movl -0x4(%rbp), %eax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x3400
nopw %cs:(%rax,%rax)
nop