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

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

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

  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;
    VALUE_T val;
    if ((rc=btree.Lookup(KEY_T(key),val))!=ERROR_NOERROR) { 
      cerr <<"Lookup failed: error "<<rc<<endl;
    } else {
      cerr <<"Lookup succeeded\n";
      cout << val;
    }
    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 $0x330, %rsp            # imm = 0x330
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x3750
callq 0x3700
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3f87
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 -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1f0(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x3410
movq -0x1f0(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x3fa0
jmp 0x37b2
movq %rsp, %rax
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
xorl %r9d, %r9d
xorps %xmm2, %xmm2
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4a40
jmp 0x37f3
leaq -0xc8(%rbp), %rdi
callq 0x3120
leaq -0xc9(%rbp), %rdi
callq 0x3270
movl -0x1c(%rbp), %edx
leaq -0x140(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x77b0
jmp 0x3823
leaq -0x188(%rbp), %rdi
xorl %edx, %edx
leaq -0x140(%rbp), %rcx
movl $0x1, %r8d
movl %edx, %esi
callq 0x9cb0
jmp 0x3842
leaq -0x140(%rbp), %rdi
callq 0x7970
movl %eax, -0x1f4(%rbp)
jmp 0x3856
movl -0x1f4(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3955
movq 0x1377e(%rip), %rdi     # 0x16ff0
leaq 0xc7b7(%rip), %rsi      # 0x10030
callq 0x31f0
movq %rax, -0x200(%rbp)
jmp 0x3887
movq -0x200(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x208(%rbp)
jmp 0x38a2
movq -0x208(%rbp), %rdi
movq 0x13710(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x38b7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3f3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x38ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3120
leaq -0xc9(%rbp), %rdi
callq 0x3270
jmp 0x3f93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3f79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3f61
leaq -0x188(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0xa080
movl %eax, -0x20c(%rbp)
jmp 0x396d
movl -0x20c(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x39e0
movq 0x1366b(%rip), %rdi     # 0x16ff0
leaq 0xc6cb(%rip), %rsi      # 0x10057
callq 0x31f0
movq %rax, -0x218(%rbp)
jmp 0x399a
movq -0x218(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x220(%rbp)
jmp 0x39b5
movq -0x220(%rbp), %rdi
movq 0x135fd(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x39ca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3f3b
movq 0x13609(%rip), %rdi     # 0x16ff0
leaq 0xc68e(%rip), %rsi      # 0x1007c
callq 0x31f0
movq %rax, -0x228(%rbp)
jmp 0x39fc
movq -0x228(%rbp), %rdi
movq 0x135b6(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3a11
leaq -0x1b8(%rbp), %rdi
callq 0x43e0
jmp 0x3a1f
movq -0x28(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x4620
jmp 0x3a31
leaq -0x188(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
leaq -0x1b8(%rbp), %rdx
callq 0xa8d0
movl %eax, -0x22c(%rbp)
jmp 0x3a53
movl -0x22c(%rbp), %eax
movl %eax, -0x18c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x46e0
movl -0x22c(%rbp), %eax
cmpl $0x0, %eax
je 0x3b02
movq 0x1356f(%rip), %rdi     # 0x16ff0
leaq 0xc604(%rip), %rsi      # 0x1008c
callq 0x31f0
movq %rax, -0x238(%rbp)
jmp 0x3a96
movq -0x238(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x240(%rbp)
jmp 0x3ab1
movq -0x240(%rbp), %rdi
movq 0x13501(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3ac6
jmp 0x3b2e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3f2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x46e0
jmp 0x3f2d
movq 0x134e7(%rip), %rdi     # 0x16ff0
leaq 0xc592(%rip), %rsi      # 0x100a2
callq 0x31f0
jmp 0x3b17
movq 0x134b2(%rip), %rdi     # 0x16fd0
leaq -0x1b8(%rbp), %rsi
callq 0x4050
jmp 0x3b2c
jmp 0x3b2e
leaq -0x188(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0xa460
movl %eax, -0x244(%rbp)
jmp 0x3b46
movl -0x244(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3bb9
movq 0x13492(%rip), %rdi     # 0x16ff0
leaq 0xc54f(%rip), %rsi      # 0x100b4
callq 0x31f0
movq %rax, -0x250(%rbp)
jmp 0x3b73
movq -0x250(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x258(%rbp)
jmp 0x3b8e
movq -0x258(%rbp), %rdi
movq 0x13424(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3ba3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3f1f
leaq -0x140(%rbp), %rdi
callq 0x7880
movl %eax, -0x25c(%rbp)
jmp 0x3bcd
movl -0x25c(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3c40
movq 0x1340b(%rip), %rdi     # 0x16ff0
leaq 0xc4ee(%rip), %rsi      # 0x100da
callq 0x31f0
movq %rax, -0x268(%rbp)
jmp 0x3bfa
movq -0x268(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x270(%rbp)
jmp 0x3c15
movq -0x270(%rbp), %rdi
movq 0x1339d(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3c2a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3f1f
movq 0x133a9(%rip), %rdi     # 0x16ff0
leaq 0xc4b2(%rip), %rsi      # 0x10100
callq 0x31f0
jmp 0x3c55
movq 0x13394(%rip), %rdi     # 0x16ff0
leaq 0xc4b6(%rip), %rsi      # 0x10119
callq 0x31f0
movq %rax, -0x278(%rbp)
jmp 0x3c71
leaq -0x140(%rbp), %rdi
callq 0x4080
movl %eax, -0x27c(%rbp)
jmp 0x3c85
movl -0x27c(%rbp), %esi
movq -0x278(%rbp), %rdi
callq 0x3320
movq %rax, -0x288(%rbp)
jmp 0x3ca0
movq -0x288(%rbp), %rdi
movq 0x13312(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3cb5
movq 0x13334(%rip), %rdi     # 0x16ff0
leaq 0xc469(%rip), %rsi      # 0x1012c
callq 0x31f0
movq %rax, -0x290(%rbp)
jmp 0x3cd1
leaq -0x140(%rbp), %rdi
callq 0x40a0
movl %eax, -0x294(%rbp)
jmp 0x3ce5
movl -0x294(%rbp), %esi
movq -0x290(%rbp), %rdi
callq 0x3320
movq %rax, -0x2a0(%rbp)
jmp 0x3d00
movq -0x2a0(%rbp), %rdi
movq 0x132b2(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3d15
movq 0x132d4(%rip), %rdi     # 0x16ff0
leaq 0xc41c(%rip), %rsi      # 0x1013f
callq 0x31f0
movq %rax, -0x2a8(%rbp)
jmp 0x3d31
leaq -0x140(%rbp), %rdi
callq 0x40c0
movl %eax, -0x2ac(%rbp)
jmp 0x3d45
movl -0x2ac(%rbp), %esi
movq -0x2a8(%rbp), %rdi
callq 0x3320
movq %rax, -0x2b8(%rbp)
jmp 0x3d60
movq -0x2b8(%rbp), %rdi
movq 0x13252(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3d75
movq 0x13274(%rip), %rdi     # 0x16ff0
leaq 0xc3cf(%rip), %rsi      # 0x10152
callq 0x31f0
movq %rax, -0x2c0(%rbp)
jmp 0x3d91
leaq -0x140(%rbp), %rdi
callq 0x40e0
movl %eax, -0x2c4(%rbp)
jmp 0x3da5
movl -0x2c4(%rbp), %esi
movq -0x2c0(%rbp), %rdi
callq 0x3320
movq %rax, -0x2d0(%rbp)
jmp 0x3dc0
movq -0x2d0(%rbp), %rdi
movq 0x131f2(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3dd5
movq 0x13214(%rip), %rdi     # 0x16ff0
leaq 0xc382(%rip), %rsi      # 0x10165
callq 0x31f0
movq %rax, -0x2d8(%rbp)
jmp 0x3df1
leaq -0x140(%rbp), %rdi
callq 0x4100
movl %eax, -0x2dc(%rbp)
jmp 0x3e05
movl -0x2dc(%rbp), %esi
movq -0x2d8(%rbp), %rdi
callq 0x3320
movq %rax, -0x2e8(%rbp)
jmp 0x3e20
movq -0x2e8(%rbp), %rdi
movq 0x13192(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3e35
movq 0x131b4(%rip), %rdi     # 0x16ff0
leaq 0xc335(%rip), %rsi      # 0x10178
callq 0x31f0
movq %rax, -0x2f0(%rbp)
jmp 0x3e51
leaq -0x140(%rbp), %rdi
callq 0x4120
movl %eax, -0x2f4(%rbp)
jmp 0x3e65
movl -0x2f4(%rbp), %esi
movq -0x2f0(%rbp), %rdi
callq 0x3320
movq %rax, -0x300(%rbp)
jmp 0x3e80
movq -0x300(%rbp), %rdi
movq 0x13132(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3e95
movq 0x13154(%rip), %rdi     # 0x16ff0
movq 0x1311d(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3eaa
movq 0x1313f(%rip), %rdi     # 0x16ff0
leaq 0xc2d3(%rip), %rsi      # 0x1018b
callq 0x31f0
movq %rax, -0x308(%rbp)
jmp 0x3ec6
leaq -0x140(%rbp), %rdi
callq 0x7a00
movsd %xmm0, -0x310(%rbp)
jmp 0x3edc
movsd -0x310(%rbp), %xmm0
movq -0x308(%rbp), %rdi
callq 0x3440
movq %rax, -0x318(%rbp)
jmp 0x3ef9
movq -0x318(%rbp), %rdi
movq 0x130b9(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3f0e
movl $0x0, -0x4(%rbp)
movl $0x1, -0x190(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x46e0
jmp 0x3f3b
leaq -0x1b8(%rbp), %rdi
callq 0x46e0
jmp 0x3f61
leaq -0x188(%rbp), %rdi
callq 0x9e00
leaq -0x140(%rbp), %rdi
callq 0x7820
leaq -0xa8(%rbp), %rdi
callq 0x52b0
jmp 0x3f87
leaq -0x188(%rbp), %rdi
callq 0x9e00
leaq -0x140(%rbp), %rdi
callq 0x7820
leaq -0xa8(%rbp), %rdi
callq 0x52b0
jmp 0x3f93
movl -0x4(%rbp), %eax
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x3400
nop