// File: /workspace/github_cpp_makefile_star2_O2/cpottamus[P]btree_lab/sim.cc
// Function: main
int main(int argc, char *argv[])
{

  // CONFORMS to the interface of ref_impl.pl

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

  char *filestem=argv[1];
  SIZE_T cachesize=atoi(argv[2]);
  SIZE_T superblocknum;

  FILE *file; 
  char line[1024];
  int max = 8192;
  ERROR_T rc;
  
  // We'll connect to the btree only once and then
  // run lots of operations
  // so we need to do this outside the loop
  DiskSystem disk(filestem);
  BufferCache cache(&disk,cachesize);
  // will be set on init
  BTreeIndex *btree;


  if ((rc=cache.Attach())!=ERROR_NOERROR) {
    cerr << "Can't attach cache due to error "<<rc<<"\n";
    return -1;
  }
  
  file=stdin;

  //Now simply read each line and call btree functions corresponding to the same
  while (fgets(line, max, file) != NULL){
    // foreach line read we will refer to a case switch statement
    string line2, action, key, value;
    line2 = line;
    istrstream is(line2.c_str(),line2.size());
    is >> action >> key >> value;

    if (action == "INIT") {
      btree = new BTreeIndex(atoi(key.c_str()),atoi(value.c_str()),&cache);
      if ((rc=btree->Attach(0, true))!=ERROR_NOERROR) {
	cerr << "Can't attach btree with initialization due to error "<<rc<<"\n";
	cout << "FAIL\n";
      } else {
	cout << "OK\n";
      }
    } else if (action == "INSERT"){
      if ((rc=btree->Insert(KEY_T(key.c_str()),VALUE_T(value.c_str())))!=ERROR_NOERROR) { 
        cout <<"FAIL"<<endl;
	cerr <<"Can't insert due to error "<<rc<<"\n";
      } else {
        cout <<"OK\n";
      }
    } else if (action == "UPDATE"){
      if ((rc=btree->Update(KEY_T(key.c_str()),VALUE_T(value.c_str())))!=ERROR_NOERROR) { 
        cout <<"FAIL" <<endl;
	cerr <<"Can't update due to error "<<rc<<"\n";
      } else {
        cout <<"OK\n";
      }
    } else if (action == "DELETE"){
      if ((rc=btree->Delete(KEY_T(key.c_str())))!=ERROR_NOERROR) { 
        cout <<"FAIL"<<endl;
	cerr <<"Can't delete due to error "<<rc<<endl;
      } else {
        cout <<"OK\n";
      }
    } else if (action == "LOOKUP"){
      VALUE_T lookup_value;
      if ((rc=btree->Lookup(KEY_T(key.c_str()),lookup_value))!=ERROR_NOERROR) { 
        cout <<"FAIL"<< endl;
	cerr <<"Can't lookup due to error "<<rc<<endl;
      } else {
        cout <<"OK ";
 	for (unsigned int k=0; k<lookup_value.length; k++) {
 	    cout << lookup_value.data[k];
	}
 	cout << endl;
      }
    } else if (action == "DISPLAY") {
      // This should always be OK
      cout <<"OK BEGIN DISPLAY\n";
      btree->Display(cout,BTREE_SORTED_KEYVAL);
      cout <<"OK END DISPLAY\n";
    } else if (action == "DEINIT"){
      if ((rc=btree->Detach(superblocknum))!=ERROR_NOERROR) { 
	cout << "FAIL"<<endl;
	cerr << "Can't detach btree due to error "<<rc<<endl;
      } else {
	if ((rc=cache.Detach())!=ERROR_NOERROR) { 
	  cout <<"FAIL"<<endl;
	  cerr <<"Can't detach cache due to error "<<rc<<endl;
	} else {
	  delete btree;
	  cout << "OK\n";
	}
      }
    }
  }
    
  fclose(file);

  return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x758, %rsp            # imm = 0x758
cmpl $0x3, %edi
jne 0x26f1
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %rdi
callq 0x2340
movl %eax, %ebx
leaq 0x1e0(%rsp), %rdi
leaq 0x100(%rsp), %rdx
movq %r14, %rsi
callq 0x2e02
andl $0x0, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0x160(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3228
leaq 0x1e0(%rsp), %rdi
callq 0x20d0
leaq 0x100(%rsp), %rdi
leaq 0x160(%rsp), %rsi
movl %ebx, %edx
callq 0x47ec
leaq 0x100(%rsp), %rdi
callq 0x48ce
movl %eax, %ebx
testl %eax, %eax
je 0x26fe
movq 0xa92c(%rip), %rdi      # 0xcff0
leaq 0x5964(%rip), %rsi      # 0x802f
callq 0x21d0
movq %rax, %rdi
movl %ebx, %esi
callq 0x23b0
leaq 0x594c(%rip), %rsi      # 0x802d
movq %rax, %rdi
callq 0x21d0
pushq $-0x1
popq %rbx
jmp 0x2cfb
callq 0x25fc
pushq $0x1
popq %rbx
jmp 0x2d15
movq 0xa8c3(%rip), %rax      # 0xcfc8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
leaq 0x350(%rsp), %r15
leaq 0x68(%rsp), %r13
leaq 0xb0(%rsp), %r14
leaq 0xd8(%rsp), %r12
movq %r15, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq 0x20(%rsp), %rdx
callq 0x2350
testq %rax, %rax
je 0x2cef
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
xorl %ecx, %ecx
movb %cl, 0x98(%rsp)
movb %cl, 0x78(%rsp)
movb %cl, 0x38(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movb %cl, 0x58(%rsp)
leaq 0x88(%rsp), %rdi
movq %r15, %rbx
movq %r15, %rsi
callq 0x23f0
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x1e0(%rsp), %r15
movq %r15, %rdi
callq 0x2060
movq %r15, %rdi
movq %r13, %rsi
callq 0x22a0
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x22a0
movq %rax, %rdi
leaq 0x48(%rsp), %rsi
callq 0x22a0
movq %r13, %rdi
leaq 0x5947(%rip), %rsi      # 0x814c
callq 0x2e3d
testb %al, %al
je 0x28b0
pushq $0x48
popq %rdi
callq 0x21f0
movq %rax, %rbp
movq 0x28(%rsp), %rdi
callq 0x2340
movl %eax, %r15d
movq 0x48(%rsp), %rdi
callq 0x2340
movq %rbp, %rdi
movl %r15d, %esi
movl %eax, %edx
leaq 0x100(%rsp), %rcx
pushq $0x1
popq %r15
movl %r15d, %r8d
callq 0x5298
movq %rbp, %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x550e
movl %eax, %r15d
leaq 0x5824(%rip), %rsi      # 0x808b
testl %eax, %eax
je 0x289f
movq 0xa77e(%rip), %rdi      # 0xcff0
leaq 0x57d7(%rip), %rsi      # 0x8050
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x23b0
movq %rax, %rdi
leaq 0x579a(%rip), %rsi      # 0x802d
callq 0x21d0
leaq 0x57e6(%rip), %rsi      # 0x8085
movq 0xa72a(%rip), %rdi      # 0xcfd0
callq 0x21d0
jmp 0x2a15
movq %r13, %rdi
leaq 0x57d5(%rip), %rsi      # 0x808f
callq 0x2e3d
testb %al, %al
je 0x295a
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x3008
movq 0x48(%rsp), %rsi
movq %r12, %rdi
callq 0x3008
movq %r12, %r13
movq %rbp, %rdi
movq %r14, %r12
movq %r14, %rsi
movq %r13, %rdx
callq 0x592c
movl %eax, %r15d
movq %r13, %rdi
callq 0x308a
movq %r12, %rdi
callq 0x308a
movq 0xa6c1(%rip), %rax      # 0xcfd0
leaq 0x5775(%rip), %rsi      # 0x808b
testl %r15d, %r15d
movq %r13, %r12
leaq 0x68(%rsp), %r13
je 0x2a0d
movq 0xa6a2(%rip), %rdi      # 0xcfd0
leaq 0x5761(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa6a7(%rip), %rdi      # 0xcff0
leaq 0x574b(%rip), %rsi      # 0x809b
callq 0x21d0
jmp 0x29fb
movq %r13, %rdi
leaq 0x5752(%rip), %rsi      # 0x80b6
callq 0x2e3d
testb %al, %al
je 0x2a53
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x3008
movq 0x48(%rsp), %rsi
movq %r12, %rdi
callq 0x3008
movq %r12, %r13
movq %rbp, %rdi
movq %r14, %r12
movq %r14, %rsi
movq %r13, %rdx
callq 0x6a24
movl %eax, %r15d
movq %r13, %rdi
callq 0x308a
movq %r12, %rdi
callq 0x308a
movq 0xa617(%rip), %rax      # 0xcfd0
leaq 0x56cb(%rip), %rsi      # 0x808b
testl %r15d, %r15d
movq %r13, %r12
leaq 0x68(%rsp), %r13
je 0x2a0d
movq 0xa5fc(%rip), %rdi      # 0xcfd0
leaq 0x56bb(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa601(%rip), %rdi      # 0xcff0
leaq 0x56c7(%rip), %rsi      # 0x80bd
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x23b0
leaq 0x5620(%rip), %rsi      # 0x802d
movq %rax, %rdi
callq 0x21d0
leaq 0x1e0(%rsp), %rdi
callq 0x22e0
leaq 0x48(%rsp), %rdi
callq 0x20d0
leaq 0x28(%rsp), %rdi
callq 0x20d0
movq %r13, %rdi
callq 0x20d0
leaq 0x88(%rsp), %rdi
callq 0x20d0
movq %rbx, %r15
jmp 0x272a
movq %r13, %rdi
leaq 0x567b(%rip), %rsi      # 0x80d8
callq 0x2e3d
testb %al, %al
je 0x2af8
movq %r12, %r13
movq 0x28(%rsp), %rsi
movq %r14, %r12
movq %r14, %rdi
callq 0x3008
movq %rbp, %rdi
movq %r12, %rsi
callq 0x6a86
movl %eax, %r15d
movq %r12, %rdi
callq 0x308a
movq 0xa536(%rip), %rax      # 0xcfd0
leaq 0x55ea(%rip), %rsi      # 0x808b
testl %r15d, %r15d
movq %r13, %r12
leaq 0x68(%rsp), %r13
je 0x2a0d
movq 0xa517(%rip), %rdi      # 0xcfd0
leaq 0x55d6(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa51c(%rip), %rdi      # 0xcff0
leaq 0x5604(%rip), %rsi      # 0x80df
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x23b0
movq %rax, %rdi
callq 0x20e0
jmp 0x2a15
movq %r13, %rdi
leaq 0x55f8(%rip), %rsi      # 0x80fa
callq 0x2e3d
testb %al, %al
je 0x2b80
movq %r14, %rdi
callq 0x2ee0
movq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x3008
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x591a
movl %eax, %r15d
movq %r12, %rdi
callq 0x308a
testl %r15d, %r15d
je 0x2c2f
movq 0xa487(%rip), %rdi      # 0xcfd0
leaq 0x5546(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa48c(%rip), %rdi      # 0xcff0
leaq 0x5596(%rip), %rsi      # 0x8101
callq 0x21d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x23b0
jmp 0x2c76
movq %r13, %rdi
leaq 0x5596(%rip), %rsi      # 0x8120
callq 0x2e3d
testb %al, %al
je 0x2bca
movq 0xa436(%rip), %rdi      # 0xcfd0
leaq 0x5587(%rip), %rsi      # 0x8128
callq 0x21d0
movq %rbp, %rdi
movq 0xa420(%rip), %r15      # 0xcfd0
movq %r15, %rsi
pushq $0x2
popq %rdx
callq 0x6f92
movq %r15, %rax
leaq 0x5575(%rip), %rsi      # 0x813a
jmp 0x2a0d
movq %r13, %rdi
leaq 0x5576(%rip), %rsi      # 0x814a
callq 0x2e3d
testb %al, %al
je 0x2a15
movq %rbp, %rdi
leaq 0xac(%rsp), %rsi
callq 0x571e
movl %eax, %r15d
testl %eax, %eax
je 0x2c8b
movq 0xa3cd(%rip), %rdi      # 0xcfd0
leaq 0x548c(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa3d2(%rip), %rdi      # 0xcff0
leaq 0x552c(%rip), %rsi      # 0x8151
callq 0x21d0
jmp 0x2ae0
movq 0xa39a(%rip), %rdi      # 0xcfd0
leaq 0x54df(%rip), %rsi      # 0x811c
callq 0x21d0
xorl %r15d, %r15d
movl 0xc0(%rsp), %eax
cmpq %rax, %r15
jae 0x2c6f
movq 0xb8(%rsp), %rax
movsbl (%rax,%r15), %esi
movq 0xa36b(%rip), %rdi      # 0xcfd0
callq 0x2260
incq %r15
jmp 0x2c45
movq 0xa35a(%rip), %rax      # 0xcfd0
movq %rax, %rdi
callq 0x20e0
movq %r14, %rdi
callq 0x308a
jmp 0x2a15
leaq 0x100(%rsp), %rdi
callq 0x484c
movl %eax, %r15d
testl %eax, %eax
je 0x2cd2
movq 0xa32a(%rip), %rdi      # 0xcfd0
leaq 0x53e9(%rip), %rsi      # 0x8096
callq 0x21d0
movq %rax, %rdi
callq 0x20e0
movq 0xa32f(%rip), %rdi      # 0xcff0
leaq 0x54aa(%rip), %rsi      # 0x8172
callq 0x21d0
jmp 0x2ae0
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movq 0xa2ed(%rip), %rax      # 0xcfd0
leaq 0x53a1(%rip), %rsi      # 0x808b
jmp 0x2a0d
movq 0x20(%rsp), %rdi
callq 0x21e0
xorl %ebx, %ebx
leaq 0x100(%rsp), %rdi
callq 0x4818
leaq 0x160(%rsp), %rdi
callq 0x3636
movl %ebx, %eax
addq $0x758, %rsp            # imm = 0x758
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2d65
jmp 0x2d7b
jmp 0x2da4
jmp 0x2d7b
jmp 0x2da4
movq %rax, %rbx
jmp 0x2dec
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0x20d0
jmp 0x2df9
movq %rax, %rbx
jmp 0x2df9
jmp 0x2d7b
jmp 0x2d65
jmp 0x2d7b
jmp 0x2da4
movq %rax, %rbx
jmp 0x2ddf
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x308a
jmp 0x2d7e
jmp 0x2d7b
jmp 0x2da4
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x308a
jmp 0x2da7
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %rbp, %rdi
callq 0x2200
jmp 0x2da7
jmp 0x2d9f
movq %rax, %rbx
jmp 0x2db4
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0x22e0
leaq 0x48(%rsp), %rdi
callq 0x20d0
leaq 0x28(%rsp), %rdi
callq 0x20d0
leaq 0x68(%rsp), %rdi
callq 0x20d0
leaq 0x88(%rsp), %rdi
callq 0x20d0
leaq 0x100(%rsp), %rdi
callq 0x4818
leaq 0x160(%rsp), %rdi
callq 0x3636
movq %rbx, %rdi
callq 0x23c0
nop