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

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

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

  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;
    if ((rc=btree.Update(KEY_T(key),VALUE_T(value)))!=ERROR_NOERROR) { 
      cerr <<"Can't update index due to error "<<rc<<endl;
    } else {
      cerr <<"Update succeeded\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 $0x340, %rsp            # imm = 0x340
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 0x3f7b
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 -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x1f8(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x3410
movq -0x1f8(%rbp), %rsi
movq -0x1f0(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3fa0
jmp 0x37be
movq %rsp, %rax
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
leaq -0xb0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
xorl %r9d, %r9d
xorps %xmm2, %xmm2
movl %r9d, %edx
movl %r9d, %ecx
movl %r9d, %r8d
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4a10
jmp 0x37ff
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0xd1(%rbp), %rdi
callq 0x3270
movl -0x1c(%rbp), %edx
leaq -0x148(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x7780
jmp 0x382f
leaq -0x190(%rbp), %rdi
xorl %edx, %edx
leaq -0x148(%rbp), %rcx
movl $0x1, %r8d
movl %edx, %esi
callq 0x9c80
jmp 0x384e
leaq -0x148(%rbp), %rdi
callq 0x7940
movl %eax, -0x1fc(%rbp)
jmp 0x3862
movl -0x1fc(%rbp), %eax
movl %eax, -0x194(%rbp)
cmpl $0x0, %eax
je 0x3961
movq 0x13772(%rip), %rdi     # 0x16ff0
leaq 0xc7b1(%rip), %rsi      # 0x10036
callq 0x31f0
movq %rax, -0x208(%rbp)
jmp 0x3893
movq -0x208(%rbp), %rdi
movl -0x194(%rbp), %esi
callq 0x33f0
movq %rax, -0x210(%rbp)
jmp 0x38ae
movq -0x210(%rbp), %rdi
movq 0x13704(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x38c3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x198(%rbp)
jmp 0x3f2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x390b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0xd1(%rbp), %rdi
callq 0x3270
jmp 0x3f87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x3f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x3f61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x3f55
leaq -0x190(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0xa050
movl %eax, -0x214(%rbp)
jmp 0x3979
movl -0x214(%rbp), %eax
movl %eax, -0x194(%rbp)
cmpl $0x0, %eax
je 0x39ec
movq 0x1365f(%rip), %rdi     # 0x16ff0
leaq 0xc6c5(%rip), %rsi      # 0x1005d
callq 0x31f0
movq %rax, -0x220(%rbp)
jmp 0x39a6
movq -0x220(%rbp), %rdi
movl -0x194(%rbp), %esi
callq 0x33f0
movq %rax, -0x228(%rbp)
jmp 0x39c1
movq -0x228(%rbp), %rdi
movq 0x135f1(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x39d6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x198(%rbp)
jmp 0x3f2f
movq 0x135fd(%rip), %rdi     # 0x16ff0
leaq 0xc688(%rip), %rsi      # 0x10082
callq 0x31f0
movq %rax, -0x230(%rbp)
jmp 0x3a08
movq -0x230(%rbp), %rdi
movq 0x135aa(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3a1d
movq -0x28(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
callq 0x45f0
jmp 0x3a2f
movq -0x30(%rbp), %rsi
leaq -0x1e8(%rbp), %rdi
callq 0x45f0
jmp 0x3a41
leaq -0x190(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
leaq -0x1e8(%rbp), %rdx
callq 0xca10
movl %eax, -0x234(%rbp)
jmp 0x3a63
movl -0x234(%rbp), %eax
movl %eax, -0x194(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x46b0
leaq -0x1c0(%rbp), %rdi
callq 0x46b0
movl -0x234(%rbp), %eax
cmpl $0x0, %eax
je 0x3b27
movq 0x13553(%rip), %rdi     # 0x16ff0
leaq 0xc5ee(%rip), %rsi      # 0x10092
callq 0x31f0
movq %rax, -0x240(%rbp)
jmp 0x3ab2
movq -0x240(%rbp), %rdi
movl -0x194(%rbp), %esi
callq 0x33f0
movq %rax, -0x248(%rbp)
jmp 0x3acd
movq -0x248(%rbp), %rdi
movq 0x134e5(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3ae2
jmp 0x3b3e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0x3b16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x46b0
leaq -0x1c0(%rbp), %rdi
callq 0x46b0
jmp 0x3f55
movq 0x134c2(%rip), %rdi     # 0x16ff0
leaq 0xc57e(%rip), %rsi      # 0x100b3
callq 0x31f0
jmp 0x3b3c
jmp 0x3b3e
leaq -0x190(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0xa430
movl %eax, -0x24c(%rbp)
jmp 0x3b56
movl -0x24c(%rbp), %eax
movl %eax, -0x194(%rbp)
cmpl $0x0, %eax
je 0x3bc9
movq 0x13482(%rip), %rdi     # 0x16ff0
leaq 0xc550(%rip), %rsi      # 0x100c5
callq 0x31f0
movq %rax, -0x258(%rbp)
jmp 0x3b83
movq -0x258(%rbp), %rdi
movl -0x194(%rbp), %esi
callq 0x33f0
movq %rax, -0x260(%rbp)
jmp 0x3b9e
movq -0x260(%rbp), %rdi
movq 0x13414(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3bb3
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x198(%rbp)
jmp 0x3f2f
leaq -0x148(%rbp), %rdi
callq 0x7850
movl %eax, -0x264(%rbp)
jmp 0x3bdd
movl -0x264(%rbp), %eax
movl %eax, -0x194(%rbp)
cmpl $0x0, %eax
je 0x3c50
movq 0x133fb(%rip), %rdi     # 0x16ff0
leaq 0xc4ef(%rip), %rsi      # 0x100eb
callq 0x31f0
movq %rax, -0x270(%rbp)
jmp 0x3c0a
movq -0x270(%rbp), %rdi
movl -0x194(%rbp), %esi
callq 0x33f0
movq %rax, -0x278(%rbp)
jmp 0x3c25
movq -0x278(%rbp), %rdi
movq 0x1338d(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3c3a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x198(%rbp)
jmp 0x3f2f
movq 0x13399(%rip), %rdi     # 0x16ff0
leaq 0xc4b3(%rip), %rsi      # 0x10111
callq 0x31f0
jmp 0x3c65
movq 0x13384(%rip), %rdi     # 0x16ff0
leaq 0xc4b7(%rip), %rsi      # 0x1012a
callq 0x31f0
movq %rax, -0x280(%rbp)
jmp 0x3c81
leaq -0x148(%rbp), %rdi
callq 0x4050
movl %eax, -0x284(%rbp)
jmp 0x3c95
movl -0x284(%rbp), %esi
movq -0x280(%rbp), %rdi
callq 0x3320
movq %rax, -0x290(%rbp)
jmp 0x3cb0
movq -0x290(%rbp), %rdi
movq 0x13302(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3cc5
movq 0x13324(%rip), %rdi     # 0x16ff0
leaq 0xc46a(%rip), %rsi      # 0x1013d
callq 0x31f0
movq %rax, -0x298(%rbp)
jmp 0x3ce1
leaq -0x148(%rbp), %rdi
callq 0x4070
movl %eax, -0x29c(%rbp)
jmp 0x3cf5
movl -0x29c(%rbp), %esi
movq -0x298(%rbp), %rdi
callq 0x3320
movq %rax, -0x2a8(%rbp)
jmp 0x3d10
movq -0x2a8(%rbp), %rdi
movq 0x132a2(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3d25
movq 0x132c4(%rip), %rdi     # 0x16ff0
leaq 0xc41d(%rip), %rsi      # 0x10150
callq 0x31f0
movq %rax, -0x2b0(%rbp)
jmp 0x3d41
leaq -0x148(%rbp), %rdi
callq 0x4090
movl %eax, -0x2b4(%rbp)
jmp 0x3d55
movl -0x2b4(%rbp), %esi
movq -0x2b0(%rbp), %rdi
callq 0x3320
movq %rax, -0x2c0(%rbp)
jmp 0x3d70
movq -0x2c0(%rbp), %rdi
movq 0x13242(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3d85
movq 0x13264(%rip), %rdi     # 0x16ff0
leaq 0xc3d0(%rip), %rsi      # 0x10163
callq 0x31f0
movq %rax, -0x2c8(%rbp)
jmp 0x3da1
leaq -0x148(%rbp), %rdi
callq 0x40b0
movl %eax, -0x2cc(%rbp)
jmp 0x3db5
movl -0x2cc(%rbp), %esi
movq -0x2c8(%rbp), %rdi
callq 0x3320
movq %rax, -0x2d8(%rbp)
jmp 0x3dd0
movq -0x2d8(%rbp), %rdi
movq 0x131e2(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3de5
movq 0x13204(%rip), %rdi     # 0x16ff0
leaq 0xc383(%rip), %rsi      # 0x10176
callq 0x31f0
movq %rax, -0x2e0(%rbp)
jmp 0x3e01
leaq -0x148(%rbp), %rdi
callq 0x40d0
movl %eax, -0x2e4(%rbp)
jmp 0x3e15
movl -0x2e4(%rbp), %esi
movq -0x2e0(%rbp), %rdi
callq 0x3320
movq %rax, -0x2f0(%rbp)
jmp 0x3e30
movq -0x2f0(%rbp), %rdi
movq 0x13182(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3e45
movq 0x131a4(%rip), %rdi     # 0x16ff0
leaq 0xc336(%rip), %rsi      # 0x10189
callq 0x31f0
movq %rax, -0x2f8(%rbp)
jmp 0x3e61
leaq -0x148(%rbp), %rdi
callq 0x40f0
movl %eax, -0x2fc(%rbp)
jmp 0x3e75
movl -0x2fc(%rbp), %esi
movq -0x2f8(%rbp), %rdi
callq 0x3320
movq %rax, -0x308(%rbp)
jmp 0x3e90
movq -0x308(%rbp), %rdi
movq 0x13122(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3ea5
movq 0x13144(%rip), %rdi     # 0x16ff0
movq 0x1310d(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3eba
movq 0x1312f(%rip), %rdi     # 0x16ff0
leaq 0xc2d4(%rip), %rsi      # 0x1019c
callq 0x31f0
movq %rax, -0x310(%rbp)
jmp 0x3ed6
leaq -0x148(%rbp), %rdi
callq 0x79d0
movsd %xmm0, -0x318(%rbp)
jmp 0x3eec
movsd -0x318(%rbp), %xmm0
movq -0x310(%rbp), %rdi
callq 0x3440
movq %rax, -0x320(%rbp)
jmp 0x3f09
movq -0x320(%rbp), %rdi
movq 0x130a9(%rip), %rsi     # 0x16fc0
callq 0x3240
jmp 0x3f1e
movl $0x0, -0x4(%rbp)
movl $0x1, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x9dd0
leaq -0x148(%rbp), %rdi
callq 0x77f0
leaq -0xb0(%rbp), %rdi
callq 0x5280
jmp 0x3f7b
leaq -0x190(%rbp), %rdi
callq 0x9dd0
leaq -0x148(%rbp), %rdi
callq 0x77f0
leaq -0xb0(%rbp), %rdi
callq 0x5280
jmp 0x3f87
movl -0x4(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq -0xe0(%rbp), %rdi
callq 0x3400
nopw %cs:(%rax,%rax)
nopl (%rax)