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

  DiskSystem disk(argv[2]);
  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;
    rc=cache.ReadBlock(i,block);
    if (rc!=ERROR_NOERROR) { 
      cerr << "Error " << rc <<" occured when reading block "<< i << endl;
      return -1;
    }
    for (SIZE_T j=0;j<block.length;j++) { 
      cout << block.data[j];
    }
  }

  cache.Detach();

  cerr << "Your data was successfully read.\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 $0x270, %rsp            # imm = 0x270
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 0x22e0
movq -0x10(%rbp), %rax
movq 0x8(%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 0x10(%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 0x2d20
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 0x3790
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 0x6500
jmp 0x279d
leaq -0xa0(%rbp), %rdi
callq 0x5140
movl %eax, -0x18c(%rbp)
jmp 0x27b1
movl -0x18c(%rbp), %eax
movl %eax, -0x13c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x66c0
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 0x29e9
movl -0x13c(%rbp), %esi
leaq -0x168(%rbp), %rdi
callq 0x3170
jmp 0x27fc
movl -0x140(%rbp), %esi
leaq -0x138(%rbp), %rdi
leaq -0x168(%rbp), %rdx
callq 0x6820
movl %eax, -0x190(%rbp)
jmp 0x281d
movl -0x190(%rbp), %eax
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
je 0x2963
movq 0xb7b3(%rip), %rdi      # 0xdff0
leaq 0x6800(%rip), %rsi      # 0x9044
callq 0x21e0
movq %rax, -0x198(%rbp)
jmp 0x2852
movq -0x198(%rbp), %rdi
movl -0x16c(%rbp), %esi
callq 0x23e0
movq %rax, -0x1a0(%rbp)
jmp 0x286d
movq -0x1a0(%rbp), %rdi
leaq 0x67d0(%rip), %rsi      # 0x904b
callq 0x21e0
movq %rax, -0x1a8(%rbp)
jmp 0x2889
movq -0x1a8(%rbp), %rdi
movl -0x140(%rbp), %esi
callq 0x2320
movq %rax, -0x1b0(%rbp)
jmp 0x28a4
movq -0x1b0(%rbp), %rdi
movq 0xb70e(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x28b9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x170(%rbp)
jmp 0x29b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2901
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 0x2d12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2d06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x2cfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3430
jmp 0x2cfa
movl $0x0, -0x174(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x158(%rbp), %eax
jae 0x29ad
movq -0x160(%rbp), %rax
movl -0x174(%rbp), %ecx
movzbl (%rax,%rcx), %esi
movq 0xb63d(%rip), %rdi      # 0xdfd0
callq 0x2370
jmp 0x299a
jmp 0x299c
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x296d
movl $0x0, -0x170(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3430
movl -0x170(%rbp), %eax
testl %eax, %eax
jne 0x2cd6
jmp 0x29d3
jmp 0x29d5
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x27d4
leaq -0x138(%rbp), %rdi
callq 0x65d0
jmp 0x29f7
movq 0xb5f2(%rip), %rdi      # 0xdff0
leaq 0x6663(%rip), %rsi      # 0x9068
callq 0x21e0
jmp 0x2a0c
movq 0xb5dd(%rip), %rdi      # 0xdff0
leaq 0x6670(%rip), %rsi      # 0x908a
callq 0x21e0
movq %rax, -0x1b8(%rbp)
jmp 0x2a28
leaq -0x138(%rbp), %rdi
callq 0x2dd0
movl %eax, -0x1bc(%rbp)
jmp 0x2a3c
movl -0x1bc(%rbp), %esi
movq -0x1b8(%rbp), %rdi
callq 0x2320
movq %rax, -0x1c8(%rbp)
jmp 0x2a57
movq -0x1c8(%rbp), %rdi
movq 0xb55b(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2a6c
movq 0xb57d(%rip), %rdi      # 0xdff0
leaq 0x6623(%rip), %rsi      # 0x909d
callq 0x21e0
movq %rax, -0x1d0(%rbp)
jmp 0x2a88
leaq -0x138(%rbp), %rdi
callq 0x2df0
movl %eax, -0x1d4(%rbp)
jmp 0x2a9c
movl -0x1d4(%rbp), %esi
movq -0x1d0(%rbp), %rdi
callq 0x2320
movq %rax, -0x1e0(%rbp)
jmp 0x2ab7
movq -0x1e0(%rbp), %rdi
movq 0xb4fb(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2acc
movq 0xb51d(%rip), %rdi      # 0xdff0
leaq 0x65d6(%rip), %rsi      # 0x90b0
callq 0x21e0
movq %rax, -0x1e8(%rbp)
jmp 0x2ae8
leaq -0x138(%rbp), %rdi
callq 0x2e10
movl %eax, -0x1ec(%rbp)
jmp 0x2afc
movl -0x1ec(%rbp), %esi
movq -0x1e8(%rbp), %rdi
callq 0x2320
movq %rax, -0x1f8(%rbp)
jmp 0x2b17
movq -0x1f8(%rbp), %rdi
movq 0xb49b(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2b2c
movq 0xb4bd(%rip), %rdi      # 0xdff0
leaq 0x6589(%rip), %rsi      # 0x90c3
callq 0x21e0
movq %rax, -0x200(%rbp)
jmp 0x2b48
leaq -0x138(%rbp), %rdi
callq 0x2e30
movl %eax, -0x204(%rbp)
jmp 0x2b5c
movl -0x204(%rbp), %esi
movq -0x200(%rbp), %rdi
callq 0x2320
movq %rax, -0x210(%rbp)
jmp 0x2b77
movq -0x210(%rbp), %rdi
movq 0xb43b(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2b8c
movq 0xb45d(%rip), %rdi      # 0xdff0
leaq 0x653c(%rip), %rsi      # 0x90d6
callq 0x21e0
movq %rax, -0x218(%rbp)
jmp 0x2ba8
leaq -0x138(%rbp), %rdi
callq 0x2e50
movl %eax, -0x21c(%rbp)
jmp 0x2bbc
movl -0x21c(%rbp), %esi
movq -0x218(%rbp), %rdi
callq 0x2320
movq %rax, -0x228(%rbp)
jmp 0x2bd7
movq -0x228(%rbp), %rdi
movq 0xb3db(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2bec
movq 0xb3fd(%rip), %rdi      # 0xdff0
leaq 0x64ef(%rip), %rsi      # 0x90e9
callq 0x21e0
movq %rax, -0x230(%rbp)
jmp 0x2c08
leaq -0x138(%rbp), %rdi
callq 0x2e70
movl %eax, -0x234(%rbp)
jmp 0x2c1c
movl -0x234(%rbp), %esi
movq -0x230(%rbp), %rdi
callq 0x2320
movq %rax, -0x240(%rbp)
jmp 0x2c37
movq -0x240(%rbp), %rdi
movq 0xb37b(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2c4c
movq 0xb39d(%rip), %rdi      # 0xdff0
movq 0xb366(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2c61
movq 0xb388(%rip), %rdi      # 0xdff0
leaq 0x648d(%rip), %rsi      # 0x90fc
callq 0x21e0
movq %rax, -0x248(%rbp)
jmp 0x2c7d
leaq -0x138(%rbp), %rdi
callq 0x6750
movsd %xmm0, -0x250(%rbp)
jmp 0x2c93
movsd -0x250(%rbp), %xmm0
movq -0x248(%rbp), %rdi
callq 0x2430
movq %rax, -0x258(%rbp)
jmp 0x2cb0
movq -0x258(%rbp), %rdi
movq 0xb302(%rip), %rsi      # 0xdfc0
callq 0x2230
jmp 0x2cc5
movl $0x0, -0x4(%rbp)
movl $0x1, -0x170(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x6570
leaq -0xa0(%rbp), %rdi
callq 0x4000
movl -0x4(%rbp), %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
leaq -0x138(%rbp), %rdi
callq 0x6570
leaq -0xa0(%rbp), %rdi
callq 0x4000
movq -0xd0(%rbp), %rdi
callq 0x23f0
nop