// File: cpottamus[P]btree_lab/writebuffer.cc
// Function: main
int main(int argc, char *argv[])
{
  if (argc<5) { 
    usage();
    exit(-1);
  }
  SIZE_T cachesize=atoi(argv[2]);
  SIZE_T blocknum=atoi(argv[3]);
  SIZE_T numblocks=atoi(argv[4]);

  DiskSystem disk(argv[1]);
  BufferCache cache(&disk,cachesize);

  SIZE_T blocksize = disk.GetBlockSize();

  cache.Attach();

  for (unsigned i=blocknum;i<(blocknum+numblocks);i++) { 
    Block block(blocksize);
    ERROR_T rc;
    for (unsigned j=0;j<blocksize;j++) { 
      cin >> block.data[j];
    }
    rc=cache.NotifyAllocateBlock(i);
    if (rc!=ERROR_NOERROR) { 
      cerr << "Error " << rc <<" occured when notifying cache of allocation of block "<< i << endl;
      return -1;
    }
    rc=cache.WriteBlock(i,block);
    if (rc!=ERROR_NOERROR) { 
      cerr << "Error " << rc <<" occured when writing block "<< i << endl;
      return -1;
    }
  }

  cache.Detach();

  cerr << "Your data was successfully written.\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 $0x2a0, %rsp            # imm = 0x2A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
jge 0x26be
callq 0x2670
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22f0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x23a0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x23a0
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x23a0
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x188(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x2400
movq -0x188(%rbp), %rsi
movq -0x180(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x2df0
jmp 0x272c
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 0x3860
jmp 0x276d
leaq -0xc0(%rbp), %rdi
callq 0x2120
leaq -0xc1(%rbp), %rdi
callq 0x2260
movl -0x14(%rbp), %edx
leaq -0x138(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x65d0
jmp 0x279d
leaq -0xa0(%rbp), %rdi
callq 0x5210
movl %eax, -0x18c(%rbp)
jmp 0x27b1
movl -0x18c(%rbp), %eax
movl %eax, -0x13c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x6790
jmp 0x27cb
movl -0x18(%rbp), %eax
movl %eax, -0x140(%rbp)
movl -0x140(%rbp), %eax
movl -0x18(%rbp), %ecx
addl -0x1c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x2ab5
movl -0x13c(%rbp), %esi
leaq -0x168(%rbp), %rdi
callq 0x3240
jmp 0x27fc
movl $0x0, -0x170(%rbp)
movl -0x170(%rbp), %eax
cmpl -0x13c(%rbp), %eax
jae 0x28dd
movq -0x160(%rbp), %rsi
movl -0x170(%rbp), %eax
addq %rax, %rsi
movq 0xb7a9(%rip), %rdi      # 0xdfd8
callq 0x22a0
jmp 0x2836
jmp 0x2838
movl -0x170(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x170(%rbp)
jmp 0x2806
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x287b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x2120
leaq -0xc1(%rbp), %rdi
callq 0x2260
jmp 0x2dde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2dd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2dc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3500
jmp 0x2dc6
movl -0x140(%rbp), %esi
leaq -0x138(%rbp), %rdi
callq 0x6840
movl %eax, -0x190(%rbp)
jmp 0x28f7
movl -0x190(%rbp), %eax
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
je 0x29a9
movq 0xb6d9(%rip), %rdi      # 0xdff0
leaq 0x6727(%rip), %rsi      # 0x9045
callq 0x21e0
movq %rax, -0x198(%rbp)
jmp 0x292c
movq -0x198(%rbp), %rdi
movl -0x16c(%rbp), %esi
callq 0x23e0
movq %rax, -0x1a0(%rbp)
jmp 0x2947
movq -0x1a0(%rbp), %rdi
leaq 0x66f7(%rip), %rsi      # 0x904c
callq 0x21e0
movq %rax, -0x1a8(%rbp)
jmp 0x2963
movq -0x1a8(%rbp), %rdi
movl -0x140(%rbp), %esi
callq 0x2330
movq %rax, -0x1b0(%rbp)
jmp 0x297e
movq -0x1b0(%rbp), %rdi
movq 0xb634(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2993
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x174(%rbp)
jmp 0x2a83
movl -0x140(%rbp), %esi
leaq -0x138(%rbp), %rdi
leaq -0x168(%rbp), %rdx
callq 0x6a70
movl %eax, -0x1b4(%rbp)
jmp 0x29ca
movl -0x1b4(%rbp), %eax
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
je 0x2a79
movq 0xb606(%rip), %rdi      # 0xdff0
leaq 0x6654(%rip), %rsi      # 0x9045
callq 0x21e0
movq %rax, -0x1c0(%rbp)
jmp 0x29ff
movq -0x1c0(%rbp), %rdi
movl -0x16c(%rbp), %esi
callq 0x23e0
movq %rax, -0x1c8(%rbp)
jmp 0x2a1a
movq -0x1c8(%rbp), %rdi
leaq 0x665a(%rip), %rsi      # 0x9082
callq 0x21e0
movq %rax, -0x1d0(%rbp)
jmp 0x2a36
movq -0x1d0(%rbp), %rdi
movl -0x140(%rbp), %esi
callq 0x2330
movq %rax, -0x1d8(%rbp)
jmp 0x2a51
movq -0x1d8(%rbp), %rdi
movq 0xb561(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2a66
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x174(%rbp)
jmp 0x2a83
movl $0x0, -0x174(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3500
movl -0x174(%rbp), %eax
testl %eax, %eax
jne 0x2da2
jmp 0x2a9f
jmp 0x2aa1
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x27d4
leaq -0x138(%rbp), %rdi
callq 0x66a0
jmp 0x2ac3
movq 0xb526(%rip), %rdi      # 0xdff0
leaq 0x65ce(%rip), %rsi      # 0x909f
callq 0x21e0
jmp 0x2ad8
movq 0xb511(%rip), %rdi      # 0xdff0
leaq 0x65de(%rip), %rsi      # 0x90c4
callq 0x21e0
movq %rax, -0x1e0(%rbp)
jmp 0x2af4
leaq -0x138(%rbp), %rdi
callq 0x2ea0
movl %eax, -0x1e4(%rbp)
jmp 0x2b08
movl -0x1e4(%rbp), %esi
movq -0x1e0(%rbp), %rdi
callq 0x2330
movq %rax, -0x1f0(%rbp)
jmp 0x2b23
movq -0x1f0(%rbp), %rdi
movq 0xb48f(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2b38
movq 0xb4b1(%rip), %rdi      # 0xdff0
leaq 0x6591(%rip), %rsi      # 0x90d7
callq 0x21e0
movq %rax, -0x1f8(%rbp)
jmp 0x2b54
leaq -0x138(%rbp), %rdi
callq 0x2ec0
movl %eax, -0x1fc(%rbp)
jmp 0x2b68
movl -0x1fc(%rbp), %esi
movq -0x1f8(%rbp), %rdi
callq 0x2330
movq %rax, -0x208(%rbp)
jmp 0x2b83
movq -0x208(%rbp), %rdi
movq 0xb42f(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2b98
movq 0xb451(%rip), %rdi      # 0xdff0
leaq 0x6544(%rip), %rsi      # 0x90ea
callq 0x21e0
movq %rax, -0x210(%rbp)
jmp 0x2bb4
leaq -0x138(%rbp), %rdi
callq 0x2ee0
movl %eax, -0x214(%rbp)
jmp 0x2bc8
movl -0x214(%rbp), %esi
movq -0x210(%rbp), %rdi
callq 0x2330
movq %rax, -0x220(%rbp)
jmp 0x2be3
movq -0x220(%rbp), %rdi
movq 0xb3cf(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2bf8
movq 0xb3f1(%rip), %rdi      # 0xdff0
leaq 0x64f7(%rip), %rsi      # 0x90fd
callq 0x21e0
movq %rax, -0x228(%rbp)
jmp 0x2c14
leaq -0x138(%rbp), %rdi
callq 0x2f00
movl %eax, -0x22c(%rbp)
jmp 0x2c28
movl -0x22c(%rbp), %esi
movq -0x228(%rbp), %rdi
callq 0x2330
movq %rax, -0x238(%rbp)
jmp 0x2c43
movq -0x238(%rbp), %rdi
movq 0xb36f(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2c58
movq 0xb391(%rip), %rdi      # 0xdff0
leaq 0x64aa(%rip), %rsi      # 0x9110
callq 0x21e0
movq %rax, -0x240(%rbp)
jmp 0x2c74
leaq -0x138(%rbp), %rdi
callq 0x2f20
movl %eax, -0x244(%rbp)
jmp 0x2c88
movl -0x244(%rbp), %esi
movq -0x240(%rbp), %rdi
callq 0x2330
movq %rax, -0x250(%rbp)
jmp 0x2ca3
movq -0x250(%rbp), %rdi
movq 0xb30f(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2cb8
movq 0xb331(%rip), %rdi      # 0xdff0
leaq 0x645d(%rip), %rsi      # 0x9123
callq 0x21e0
movq %rax, -0x258(%rbp)
jmp 0x2cd4
leaq -0x138(%rbp), %rdi
callq 0x2f40
movl %eax, -0x25c(%rbp)
jmp 0x2ce8
movl -0x25c(%rbp), %esi
movq -0x258(%rbp), %rdi
callq 0x2330
movq %rax, -0x268(%rbp)
jmp 0x2d03
movq -0x268(%rbp), %rdi
movq 0xb2af(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2d18
movq 0xb2d1(%rip), %rdi      # 0xdff0
movq 0xb29a(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2d2d
movq 0xb2bc(%rip), %rdi      # 0xdff0
leaq 0x63fb(%rip), %rsi      # 0x9136
callq 0x21e0
movq %rax, -0x270(%rbp)
jmp 0x2d49
leaq -0x138(%rbp), %rdi
callq 0x6820
movsd %xmm0, -0x278(%rbp)
jmp 0x2d5f
movsd -0x278(%rbp), %xmm0
movq -0x270(%rbp), %rdi
callq 0x2430
movq %rax, -0x280(%rbp)
jmp 0x2d7c
movq -0x280(%rbp), %rdi
movq 0xb236(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2d91
movl $0x0, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x6640
leaq -0xa0(%rbp), %rdi
callq 0x40d0
movl -0x4(%rbp), %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
leaq -0x138(%rbp), %rdi
callq 0x6640
leaq -0xa0(%rbp), %rdi
callq 0x40d0
movq -0xd0(%rbp), %rdi
callq 0x23f0
nopw (%rax,%rax)