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

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

  filestem=argv[1];
  cachesize=atoi(argv[2]);
  keysize=atoi(argv[3]);
  valuesize=atoi(argv[4]);

  DiskSystem disk(filestem);
  BufferCache cache(&disk,cachesize);
  BTreeIndex btree(keysize,valuesize,&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,true))!=ERROR_NOERROR) { 
    cerr << "Can't attach to index with creation due to error "<<rc<<endl;
    return -1;
  } else {
    cerr << "Index created!"<<endl;
    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 $0x2d0, %rsp            # imm = 0x2D0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x3750
callq 0x3700
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3e67
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), %rdi
callq 0x33a0
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x33a0
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x3410
movq -0x1a0(%rbp), %rsi
movq -0x198(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x3e80
jmp 0x37c6
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 0x4290
jmp 0x3807
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 0x7000
jmp 0x3837
movl -0x20(%rbp), %esi
movl -0x24(%rbp), %edx
leaq -0x188(%rbp), %rdi
leaq -0x140(%rbp), %rcx
movl $0x1, %r8d
callq 0x9500
jmp 0x3858
leaq -0x140(%rbp), %rdi
callq 0x71c0
movl %eax, -0x1a4(%rbp)
jmp 0x386c
movl -0x1a4(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3977
movq 0x12768(%rip), %rdi     # 0x15ff0
leaq 0xc7ad(%rip), %rsi      # 0x1003c
callq 0x31f0
movq %rax, -0x1b0(%rbp)
jmp 0x389d
movq -0x1b0(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x1b8(%rbp)
jmp 0x38b8
movq -0x1b8(%rbp), %rdi
movq 0x126fa(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x38cd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3e27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3915
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 0x3e73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3e59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x3e4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x9650
jmp 0x3e4d
leaq -0x188(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x98d0
movl %eax, -0x1bc(%rbp)
jmp 0x3992
movl -0x1bc(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3a05
movq 0x12646(%rip), %rdi     # 0x15ff0
leaq 0xc6b2(%rip), %rsi      # 0x10063
callq 0x31f0
movq %rax, -0x1c8(%rbp)
jmp 0x39bf
movq -0x1c8(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x1d0(%rbp)
jmp 0x39da
movq -0x1d0(%rbp), %rdi
movq 0x125d8(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x39ef
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3e27
movq 0x125e4(%rip), %rdi     # 0x15ff0
leaq 0xc682(%rip), %rsi      # 0x10095
callq 0x31f0
movq %rax, -0x1d8(%rbp)
jmp 0x3a21
movq -0x1d8(%rbp), %rdi
movq 0x12591(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3a36
leaq -0x188(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x9cb0
movl %eax, -0x1dc(%rbp)
jmp 0x3a4e
movl -0x1dc(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3ac1
movq 0x1258a(%rip), %rdi     # 0x15ff0
leaq 0xc637(%rip), %rsi      # 0x100a4
callq 0x31f0
movq %rax, -0x1e8(%rbp)
jmp 0x3a7b
movq -0x1e8(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x1f0(%rbp)
jmp 0x3a96
movq -0x1f0(%rbp), %rdi
movq 0x1251c(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3aab
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3e27
leaq -0x140(%rbp), %rdi
callq 0x70d0
movl %eax, -0x1f4(%rbp)
jmp 0x3ad5
movl -0x1f4(%rbp), %eax
movl %eax, -0x18c(%rbp)
cmpl $0x0, %eax
je 0x3b48
movq 0x12503(%rip), %rdi     # 0x15ff0
leaq 0xc5d6(%rip), %rsi      # 0x100ca
callq 0x31f0
movq %rax, -0x200(%rbp)
jmp 0x3b02
movq -0x200(%rbp), %rdi
movl -0x18c(%rbp), %esi
callq 0x33f0
movq %rax, -0x208(%rbp)
jmp 0x3b1d
movq -0x208(%rbp), %rdi
movq 0x12495(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3b32
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x190(%rbp)
jmp 0x3e27
movq 0x124a1(%rip), %rdi     # 0x15ff0
leaq 0xc59a(%rip), %rsi      # 0x100f0
callq 0x31f0
jmp 0x3b5d
movq 0x1248c(%rip), %rdi     # 0x15ff0
leaq 0xc59e(%rip), %rsi      # 0x10109
callq 0x31f0
movq %rax, -0x210(%rbp)
jmp 0x3b79
leaq -0x140(%rbp), %rdi
callq 0x3f30
movl %eax, -0x214(%rbp)
jmp 0x3b8d
movl -0x214(%rbp), %esi
movq -0x210(%rbp), %rdi
callq 0x3320
movq %rax, -0x220(%rbp)
jmp 0x3ba8
movq -0x220(%rbp), %rdi
movq 0x1240a(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3bbd
movq 0x1242c(%rip), %rdi     # 0x15ff0
leaq 0xc551(%rip), %rsi      # 0x1011c
callq 0x31f0
movq %rax, -0x228(%rbp)
jmp 0x3bd9
leaq -0x140(%rbp), %rdi
callq 0x3f50
movl %eax, -0x22c(%rbp)
jmp 0x3bed
movl -0x22c(%rbp), %esi
movq -0x228(%rbp), %rdi
callq 0x3320
movq %rax, -0x238(%rbp)
jmp 0x3c08
movq -0x238(%rbp), %rdi
movq 0x123aa(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3c1d
movq 0x123cc(%rip), %rdi     # 0x15ff0
leaq 0xc504(%rip), %rsi      # 0x1012f
callq 0x31f0
movq %rax, -0x240(%rbp)
jmp 0x3c39
leaq -0x140(%rbp), %rdi
callq 0x3f70
movl %eax, -0x244(%rbp)
jmp 0x3c4d
movl -0x244(%rbp), %esi
movq -0x240(%rbp), %rdi
callq 0x3320
movq %rax, -0x250(%rbp)
jmp 0x3c68
movq -0x250(%rbp), %rdi
movq 0x1234a(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3c7d
movq 0x1236c(%rip), %rdi     # 0x15ff0
leaq 0xc4b7(%rip), %rsi      # 0x10142
callq 0x31f0
movq %rax, -0x258(%rbp)
jmp 0x3c99
leaq -0x140(%rbp), %rdi
callq 0x3f90
movl %eax, -0x25c(%rbp)
jmp 0x3cad
movl -0x25c(%rbp), %esi
movq -0x258(%rbp), %rdi
callq 0x3320
movq %rax, -0x268(%rbp)
jmp 0x3cc8
movq -0x268(%rbp), %rdi
movq 0x122ea(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3cdd
movq 0x1230c(%rip), %rdi     # 0x15ff0
leaq 0xc46a(%rip), %rsi      # 0x10155
callq 0x31f0
movq %rax, -0x270(%rbp)
jmp 0x3cf9
leaq -0x140(%rbp), %rdi
callq 0x3fb0
movl %eax, -0x274(%rbp)
jmp 0x3d0d
movl -0x274(%rbp), %esi
movq -0x270(%rbp), %rdi
callq 0x3320
movq %rax, -0x280(%rbp)
jmp 0x3d28
movq -0x280(%rbp), %rdi
movq 0x1228a(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3d3d
movq 0x122ac(%rip), %rdi     # 0x15ff0
leaq 0xc41d(%rip), %rsi      # 0x10168
callq 0x31f0
movq %rax, -0x288(%rbp)
jmp 0x3d59
leaq -0x140(%rbp), %rdi
callq 0x3fd0
movl %eax, -0x28c(%rbp)
jmp 0x3d6d
movl -0x28c(%rbp), %esi
movq -0x288(%rbp), %rdi
callq 0x3320
movq %rax, -0x298(%rbp)
jmp 0x3d88
movq -0x298(%rbp), %rdi
movq 0x1222a(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3d9d
movq 0x1224c(%rip), %rdi     # 0x15ff0
movq 0x12215(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3db2
movq 0x12237(%rip), %rdi     # 0x15ff0
leaq 0xc3bb(%rip), %rsi      # 0x1017b
callq 0x31f0
movq %rax, -0x2a0(%rbp)
jmp 0x3dce
leaq -0x140(%rbp), %rdi
callq 0x7250
movsd %xmm0, -0x2a8(%rbp)
jmp 0x3de4
movsd -0x2a8(%rbp), %xmm0
movq -0x2a0(%rbp), %rdi
callq 0x3440
movq %rax, -0x2b0(%rbp)
jmp 0x3e01
movq -0x2b0(%rbp), %rdi
movq 0x121b1(%rip), %rsi     # 0x15fc0
callq 0x3240
jmp 0x3e16
movl $0x0, -0x4(%rbp)
movl $0x1, -0x190(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x9650
leaq -0x140(%rbp), %rdi
callq 0x7070
leaq -0xa8(%rbp), %rdi
callq 0x4b00
jmp 0x3e67
leaq -0x140(%rbp), %rdi
callq 0x7070
leaq -0xa8(%rbp), %rdi
callq 0x4b00
jmp 0x3e73
movl -0x4(%rbp), %eax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x3400
nop