// File: /workspace/github_cpp_makefile_star2_O2/iu-vail[P]iprimal/main.cpp
// Function: main
int main(int argc, char** argv)
{

  //parse command line and generate an assignment
  CmdParser parser(argc, argv);
  parser.DisplayInput();

  Assignment as;
  if(parser.GetInputFileName().empty()){
    if(parser.GetSeed())
      as.SetSeed(parser.GetSeed());
    else
      as.SetSeed(time(NULL));
    _cout(endl<<"  *Seed for random generator: "<<as.GetSeed()<<endl);
    as.RandomGenerate( parser.GetAssignmentSize(), 
			 parser.GetAssignmentSize(), 
			 MAX_RANDOM, 
			 parser.GetSeed() );
  }
  else{
    ifstream myfile(parser.GetInputFileName().c_str());
    as.ImportMatrix(myfile);
  }
  // negate matrix: uncomment below to convert from Minimization to Maximization
  //as.NegateMatrix();

  as.DisplayMatrix(as.GetMatrix());

  if(as.GetRowSize() != as.GetColSize()){
     cerr<<endl<<"  The input problem must be a SQUARE matrix. You can add dummy rows/columns of 0s to convert a rectangular matrix to a square matrix."<<endl<<endl;
     exit(0);
  }

//  vector<uint> asgn_vec;
//  ifstream fasgn((char*)"asgn.txt");
//  as.ImportVec(fasgn, asgn_vec);

  Utils utils;

  try{

    Primal primal(as.GetMatrix());

    double time_start = utils.GetCurTime();
    double time = 0;
  
    //initiation
#ifdef USE_GREEDY
    vector<uint> asgn_vec;
    primal.GreedyApprox2(asgn_vec);
    primal.InitSolution(asgn_vec);
#else
    primal.RandSolution();
#endif

    primal.PrimalAlgo();

    time += utils.GetCurTime() - time_start;
    cout<<"Time used: "<<time<<endl;

    //utils.WriteVec<uint>(primal.GetAsgnVec(), (char*)"asgn.txt");

    //for testing
    //primal.PrimalTest();
  }
  catch(int e){

    if(e==EXCEPTION_BROKEN){
      cerr<<"I suspect there is something wrong..."<<endl;
      cerr<<"Current seed: "<<as.GetSeed()<<endl;
      cerr<<"The problem has been written in to \"debug.txt\". Send this file to code maintainer?  "<<endl;
      utils.WriteMatrix(as);
    }
    else if(e==EXCEPTION_WRONG){
      cerr<<"!!Wrong solution, double check it. "<<endl;
      cerr<<"Current seed: "<<as.GetSeed()<<endl;
      cerr<<"The problem has been written in to \"debug.txt\"."<<endl;
      utils.WriteMatrix(as);
    }
    else
      cerr<<"Unknown exception caught. "<<endl;
  }

  return 0;

}

// Assembly:
pushq %r14
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movq %rsi, %rdx
movl %edi, %esi
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x7a3c
movq %rbx, %rdi
callq 0x7640
andl $0x0, 0x48(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%rsp)
movupd %xmm0, 0x68(%rsp)
movupd %xmm0, 0x78(%rsp)
leaq 0x90(%rsp), %rbx
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x22a0
leaq 0xe0(%rsp), %rdi
movq 0x8(%rdi), %r14
callq 0x2110
testq %r14, %r14
je 0x7725
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x22a0
movq 0x10(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x23b0
leaq 0x10(%rsp), %rdi
callq 0x2110
leaq 0x48(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x2986
leaq 0xe0(%rsp), %rdi
callq 0x20a0
jmp 0x775e
movl 0x88(%rsp), %r8d
movl %r8d, %eax
testl %r8d, %r8d
jne 0x7744
xorl %edi, %edi
callq 0x21f0
movl 0x88(%rsp), %r8d
leaq 0x48(%rsp), %rdi
movl %eax, (%rdi)
movl 0xb0(%rsp), %edx
movl %edx, %esi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x2650
leaq 0x58(%rsp), %rbx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x2bb8
movl 0x4c(%rsp), %eax
cmpl 0x50(%rsp), %eax
jne 0x786a
movq %rsp, %rdi
callq 0x6ff0
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x7ac2
movq %rsp, %rdi
callq 0x7004
movsd %xmm0, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
leaq 0xe0(%rsp), %rdi
callq 0x363a
leaq 0xe0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x33ea
leaq 0xe0(%rsp), %rdi
callq 0x474a
movq %rsp, %rdi
callq 0x7004
movsd %xmm0, 0x38(%rsp)
movq 0x67d1(%rip), %rdi      # 0xdfc8
leaq 0x1f2d(%rip), %rsi      # 0x972b
callq 0x2210
movsd 0x38(%rsp), %xmm1
subsd 0x40(%rsp), %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x2330
movq %rax, %rdi
callq 0x2120
leaq 0x10(%rsp), %rdi
callq 0x832e
leaq 0xe0(%rsp), %rdi
callq 0x7cc8
movq %rsp, %rdi
callq 0x7034
leaq 0x48(%rsp), %rdi
callq 0x7d54
leaq 0x88(%rsp), %rdi
callq 0x7d6e
xorl %eax, %eax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r14
retq
movq 0x677f(%rip), %rdi      # 0xdff0
callq 0x2120
leaq 0x1e28(%rip), %rsi      # 0x96a5
movq %rax, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x2120
xorl %edi, %edi
callq 0x2260
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x20a0
jmp 0x78db
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2110
jmp 0x78db
jmp 0x78d8
movq %rdx, %r14
movq %rax, %rbx
jmp 0x790a
movq %rdx, %r14
movq %rax, %rbx
jmp 0x7917
jmp 0x78d8
jmp 0x78d8
movq %rax, %rbx
jmp 0x78e5
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x7d54
leaq 0x88(%rsp), %rdi
callq 0x7d6e
movq %rbx, %rdi
callq 0x2390
movq %rdx, %r14
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x832e
leaq 0xe0(%rsp), %rdi
callq 0x7cc8
cmpl $0x1, %r14d
jne 0x7a24
movq %rbx, %rdi
callq 0x20f0
movl (%rax), %eax
cmpl $-0x2, %eax
je 0x7984
cmpl $-0x1, %eax
jne 0x79f7
movq 0x66b0(%rip), %rdi      # 0xdff0
leaq 0x1df0(%rip), %rsi      # 0x9737
callq 0x2210
movq %rax, %rdi
callq 0x2120
movq 0x6695(%rip), %rdi      # 0xdff0
leaq 0x1dfb(%rip), %rsi      # 0x975d
callq 0x2210
movl 0x48(%rsp), %esi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x2120
leaq 0x1dea(%rip), %rsi      # 0x976c
jmp 0x79cd
movq 0x6665(%rip), %rdi      # 0xdff0
leaq 0x1e39(%rip), %rsi      # 0x97cb
callq 0x2210
movq %rax, %rdi
callq 0x2120
movq 0x664a(%rip), %rdi      # 0xdff0
leaq 0x1db0(%rip), %rsi      # 0x975d
callq 0x2210
movl 0x48(%rsp), %esi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x2120
leaq 0x1e22(%rip), %rsi      # 0x97ef
movq 0x661c(%rip), %rdi      # 0xdff0
callq 0x2210
movq %rax, %rdi
callq 0x2120
leaq 0x1dd9(%rip), %rdx      # 0x97c1
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
callq 0x71be
jmp 0x7a12
movq 0x65f2(%rip), %rdi      # 0xdff0
leaq 0x1e1a(%rip), %rsi      # 0x981f
callq 0x2210
movq %rax, %rdi
callq 0x2120
callq 0x2360
jmp 0x783e
movq %rax, %rbx
callq 0x2360
movq %rsp, %rdi
callq 0x7034
jmp 0x78db