// File: /workspace/github_cpp_makefile_star2_O1/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 $0x738, %rsp            # imm = 0x738
cmpl $0x3, %edi
jne 0x2739
movq 0x8(%rsi), %r14
movq 0x10(%rsi), %rdi
callq 0x2340
movl %eax, %ebx
leaq 0x1c0(%rsp), %rdi
leaq 0xe0(%rsp), %rdx
movq %r14, %rsi
callq 0x3164
subq $0x8, %rsp
xorl %eax, %eax
leaq 0x148(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %rax
pushq %rax
callq 0x361c
addq $0x20, %rsp
leaq 0x1d0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26cb
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0xe0(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movl %ebx, %edx
callq 0x5010
leaq 0xe0(%rsp), %rdi
callq 0x5108
movl %eax, %ebx
testl %eax, %eax
je 0x275b
movq 0xb8f4(%rip), %rdi      # 0xdff0
leaq 0x692c(%rip), %rsi      # 0x902f
movl $0x20, %edx
callq 0x2250
movq 0xb8dc(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x6906(%rip), %rsi      # 0x902d
movl $0x1, %edx
movq %rax, %rdi
callq 0x2250
jmp 0x2fd7
movq 0xb8b0(%rip), %rdi      # 0xdff0
leaq 0x68bd(%rip), %rsi      # 0x9004
movl $0x2a, %edx
callq 0x2250
movl $0x1, %ebx
jmp 0x2ff1
movq 0xb866(%rip), %rax      # 0xdfc8
movq (%rax), %rdx
leaq 0x330(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %rdx, 0x60(%rsp)
callq 0x2350
testq %rax, %rax
je 0x2fcb
xorl %r15d, %r15d
leaq 0x90(%rsp), %r13
leaq 0xb0(%rsp), %r14
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movb %r15b, 0x50(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %r15, 0x98(%rsp)
movb %r15b, 0xa0(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %r15, 0x8(%rsp)
movb %r15b, 0x10(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movb %r15b, 0x30(%rsp)
leaq 0x330(%rsp), %rbx
movq %rbx, %rdi
callq 0x20c0
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2320
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x1c0(%rsp), %rdi
callq 0x2070
leaq 0x1c0(%rsp), %rdi
movq %r13, %rsi
callq 0x22a0
movq %rax, %rdi
movq %rsp, %rsi
callq 0x22a0
movq %rax, %rdi
leaq 0x20(%rsp), %rsi
callq 0x22a0
movq %r13, %rdi
leaq 0x68ec(%rip), %rsi      # 0x914c
callq 0x2090
testl %eax, %eax
je 0x299d
movq %r13, %rdi
leaq 0x6818(%rip), %rsi      # 0x908f
callq 0x2090
testl %eax, %eax
je 0x2a3d
movq %r13, %rdi
leaq 0x6828(%rip), %rsi      # 0x90b6
callq 0x2090
testl %eax, %eax
je 0x2afa
movq %r13, %rdi
leaq 0x6833(%rip), %rsi      # 0x90d8
callq 0x2090
testl %eax, %eax
je 0x2bf1
movq %r13, %rdi
leaq 0x683e(%rip), %rsi      # 0x90fa
callq 0x2090
testl %eax, %eax
je 0x2d9c
movq %r13, %rdi
leaq 0x684d(%rip), %rsi      # 0x9120
callq 0x2090
testl %eax, %eax
je 0x2e60
movq %r13, %rdi
leaq 0x6860(%rip), %rsi      # 0x914a
callq 0x2090
testl %eax, %eax
jne 0x2cf2
movq %rbp, %rdi
leaq 0xdc(%rsp), %rsi
callq 0x5f96
movl %eax, %ebx
testl %eax, %eax
je 0x2f3f
movl $0x4, %edx
movq 0xb6b3(%rip), %rdi      # 0xdfd0
leaq 0x6772(%rip), %rsi      # 0x9096
callq 0x2250
movq 0xb6a0(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq 0xb682(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl $0x20, %edx
movq 0xb689(%rip), %rdi      # 0xdff0
leaq 0x67e3(%rip), %rsi      # 0x9151
callq 0x2250
movq 0xb676(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
jmp 0x2cb4
movl $0x48, %edi
callq 0x21f0
movq %rax, %rbp
movq (%rsp), %rdi
callq 0x2340
movl %eax, %ebx
movq 0x20(%rsp), %rdi
callq 0x2340
movq %rbp, %rdi
movl %ebx, %esi
movl %eax, %edx
leaq 0xe0(%rsp), %rcx
movl $0x1, %r8d
callq 0x5aec
movq %rbp, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x5d7a
movl %eax, %ebx
testl %eax, %eax
je 0x2bd1
movl $0x34, %edx
movq 0xb5f2(%rip), %rdi      # 0xdff0
leaq 0x664b(%rip), %rsi      # 0x9050
callq 0x2250
movq 0xb5df(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movl $0x5, %ebx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6601(%rip), %rsi      # 0x902d
callq 0x2250
leaq 0x664d(%rip), %rsi      # 0x9085
jmp 0x2bdd
movq (%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x3376
movq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3376
movq %rbp, %r12
movq %rbp, %rdi
leaq 0x68(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x61c0
movl %eax, %ebx
movq %r14, %rdi
callq 0x3402
movq %rbp, %rdi
callq 0x3402
testl %ebx, %ebx
je 0x2cc9
movl $0x4, %edx
movq 0xb53c(%rip), %rdi      # 0xdfd0
leaq 0x65fb(%rip), %rsi      # 0x9096
callq 0x2250
leaq 0xb0(%rsp), %r14
movq %r12, %rbp
movq 0xb51e(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq 0xb500(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl $0x1a, %edx
movq 0xb507(%rip), %rdi      # 0xdff0
leaq 0x65ab(%rip), %rsi      # 0x909b
callq 0x2250
jmp 0x2bb2
movq (%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x3376
movq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x3376
movq %rbp, %r12
movq %rbp, %rdi
leaq 0x68(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x74ae
movl %eax, %ebx
movq %r14, %rdi
callq 0x3402
movq %rbp, %rdi
callq 0x3402
testl %ebx, %ebx
je 0x2cc9
movl $0x4, %edx
movq 0xb47f(%rip), %rdi      # 0xdfd0
leaq 0x653e(%rip), %rsi      # 0x9096
callq 0x2250
leaq 0xb0(%rsp), %r14
movq %r12, %rbp
movq 0xb461(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq 0xb443(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl $0x1a, %edx
movq 0xb44a(%rip), %rdi      # 0xdff0
leaq 0x6510(%rip), %rsi      # 0x90bd
callq 0x2250
movq 0xb437(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movl $0x1, %ebx
leaq 0x6461(%rip), %rsi      # 0x902d
jmp 0x2ce7
movl $0x3, %ebx
leaq 0x64ae(%rip), %rsi      # 0x908b
movq 0xb3ec(%rip), %rdi      # 0xdfd0
movq %rbx, %rdx
callq 0x2250
jmp 0x2cf2
movq %rbp, %r12
movq (%rsp), %rsi
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
callq 0x3376
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7512
movl %eax, %ebx
movq %rbp, %rdi
callq 0x3402
testl %ebx, %ebx
je 0x2cc9
movl $0x4, %edx
movq 0xb3a2(%rip), %rdi      # 0xdfd0
leaq 0x6461(%rip), %rsi      # 0x9096
callq 0x2250
leaq 0xb0(%rsp), %r14
movq %r12, %rbp
movq 0xb384(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq 0xb366(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl $0x1a, %edx
movq 0xb36d(%rip), %rdi      # 0xdff0
leaq 0x6455(%rip), %rsi      # 0x90df
callq 0x2250
movq 0xb35a(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
jmp 0x2cf2
movl $0x3, %ebx
movq 0xb2fb(%rip), %rax      # 0xdfd0
leaq 0x63af(%rip), %rsi      # 0x908b
leaq 0xb0(%rsp), %r14
movq %r12, %rbp
movq %rax, %rdi
movq %rbx, %rdx
callq 0x2250
leaq 0x1c0(%rsp), %rdi
callq 0x22d0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2d1b
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2d36
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x2d5b
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2d77
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x330(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movq 0x60(%rsp), %rdx
callq 0x2350
testq %rax, %rax
jne 0x2798
jmp 0x2fcb
leaq 0x68(%rsp), %rdi
callq 0x3234
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x3376
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x68(%rsp), %rdx
callq 0x61ac
movl %eax, %ebx
movq %r14, %rdi
callq 0x3402
testl %ebx, %ebx
je 0x2eab
movl $0x4, %edx
movq 0xb1f0(%rip), %rdi      # 0xdfd0
leaq 0x62af(%rip), %rsi      # 0x9096
callq 0x2250
movq 0xb1dd(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq 0xb1bf(%rip), %rdi      # 0xdfd0
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl $0x1a, %edx
movq 0xb1c6(%rip), %rdi      # 0xdff0
leaq 0x62d0(%rip), %rsi      # 0x9101
callq 0x2250
movq 0xb1b3(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
jmp 0x2f1d
movl $0x11, %edx
movq 0xb164(%rip), %rdi      # 0xdfd0
leaq 0x62b5(%rip), %rsi      # 0x9128
callq 0x2250
movl $0xf, %ebx
movq %rbp, %rdi
movq 0xb149(%rip), %r14      # 0xdfd0
movq %r14, %rsi
movl $0x2, %edx
callq 0x7b1e
movq %r14, %rax
leaq 0xb0(%rsp), %r14
leaq 0x6294(%rip), %rsi      # 0x913a
jmp 0x2ce7
movl $0x3, %edx
movq 0xb119(%rip), %rdi      # 0xdfd0
leaq 0x625e(%rip), %rsi      # 0x911c
callq 0x2250
cmpl $0x0, 0x78(%rsp)
je 0x2efb
xorl %ebx, %ebx
movq 0x70(%rsp), %rax
movb (%rax,%rbx), %al
movb %al, 0xb0(%rsp)
movl $0x1, %edx
movq 0xb0e9(%rip), %rdi      # 0xdfd0
movq %r14, %rsi
callq 0x2250
incq %rbx
movl 0x78(%rsp), %eax
cmpq %rax, %rbx
jb 0x2ecc
movq 0xb0ce(%rip), %rcx      # 0xdfd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21c0
movq 0xb0b3(%rip), %rbx      # 0xdfd0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq 0x68(%rsp), %rdi
callq 0x3402
jmp 0x2cf2
leaq 0xe0(%rsp), %rdi
callq 0x5078
movl %eax, %ebx
testl %eax, %eax
je 0x2fa9
movl $0x4, %edx
movq 0xb072(%rip), %rdi      # 0xdfd0
leaq 0x6131(%rip), %rsi      # 0x9096
callq 0x2250
movq 0xb05f(%rip), %rdi      # 0xdfd0
callq 0x20e0
movl $0x20, %edx
movq 0xb06e(%rip), %rdi      # 0xdff0
leaq 0x61e9(%rip), %rsi      # 0x9172
callq 0x2250
movq 0xb05b(%rip), %rdi      # 0xdff0
movl %ebx, %esi
callq 0x23b0
movq %rax, %rdi
callq 0x20e0
jmp 0x2cf2
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movl $0x3, %ebx
movq 0xb011(%rip), %rax      # 0xdfd0
leaq 0x60c5(%rip), %rsi      # 0x908b
jmp 0x2ce7
movq 0x60(%rsp), %rdi
callq 0x21e0
xorl %ebx, %ebx
leaq 0xe0(%rsp), %rdi
callq 0x5040
leaq 0x140(%rsp), %rdi
callq 0x3bbc
movl %ebx, %eax
addq $0x738, %rsp            # imm = 0x738
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x305a
jmp 0x30a3
jmp 0x30b9
jmp 0x30a3
jmp 0x30b9
movq %rax, %rbx
jmp 0x314e
movq %rax, %rbx
leaq 0x1d0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x315b
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x315b
movq %rax, %rbx
jmp 0x315b
jmp 0x30b9
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3402
jmp 0x30a6
jmp 0x30a3
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3402
jmp 0x3083
movq %rax, %rbx
jmp 0x30a6
jmp 0x30b9
movq %rax, %rbx
movl $0x48, %esi
movq %rbp, %rdi
callq 0x2200
jmp 0x30bc
movq %rax, %rbx
jmp 0x3141
jmp 0x30a3
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3402
jmp 0x30bc
jmp 0x30b4
movq %rax, %rbx
jmp 0x30c9
movq %rax, %rbx
leaq 0x1c0(%rsp), %rdi
callq 0x22d0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x30e5
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x3100
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x3125
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3141
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0xe0(%rsp), %rdi
callq 0x5040
leaq 0x140(%rsp), %rdi
callq 0x3bbc
movq %rbx, %rdi
callq 0x23c0
nop