// File: 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 %rbp
movq %rsp, %rbp
subq $0x580, %rsp            # imm = 0x580
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x68(%rbp), %rdi
movq %rdi, -0x4a0(%rbp)
callq 0x16ce0
movq -0x4a0(%rbp), %rdi
callq 0x164f0
jmp 0x165ae
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
callq 0x16db0
jmp 0x165be
leaq -0xd8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x16df0
jmp 0x165d0
leaq -0xd8(%rbp), %rdi
callq 0x3460
movb %al, -0x4a1(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x3180
movb -0x4a1(%rbp), %al
testb $0x1, %al
jne 0x165fd
jmp 0x166f7
leaq -0x68(%rbp), %rdi
callq 0x16e20
movl %eax, -0x4a8(%rbp)
jmp 0x1660e
movl -0x4a8(%rbp), %eax
cmpl $0x0, %eax
je 0x16662
leaq -0x68(%rbp), %rdi
callq 0x16e20
movl %eax, -0x4ac(%rbp)
jmp 0x1662a
movl -0x4ac(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x16e30
jmp 0x1663e
jmp 0x1667d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x16c9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x16c91
xorl %eax, %eax
movl %eax, %edi
callq 0x3240
movl %eax, %esi
leaq -0xb8(%rbp), %rdi
callq 0x16e30
jmp 0x1667b
jmp 0x1667d
leaq -0x68(%rbp), %rdi
callq 0x16e50
movq %rax, -0x4b8(%rbp)
jmp 0x1668f
movq -0x4b8(%rbp), %rax
movl %eax, -0x4c4(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x16e50
movq %rax, -0x4c0(%rbp)
jmp 0x166ae
movq -0x4c0(%rbp), %rax
movl %eax, -0x4cc(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x16e20
movl %eax, -0x4c8(%rbp)
jmp 0x166cc
movl -0x4c8(%rbp), %r8d
movl -0x4cc(%rbp), %edx
movl -0x4c4(%rbp), %esi
leaq -0xb8(%rbp), %rdi
movl $0x3e8, %ecx            # imm = 0x3E8
callq 0x3840
jmp 0x166f2
jmp 0x16794
leaq -0x300(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x16df0
jmp 0x16709
leaq -0x300(%rbp), %rdi
callq 0x3100
movq %rax, %rsi
leaq -0x2e0(%rbp), %rdi
movl $0x8, %edx
callq 0x34c0
jmp 0x1672b
leaq -0x300(%rbp), %rdi
callq 0x3180
leaq -0xb8(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0x3d70
jmp 0x1674c
leaq -0x2e0(%rbp), %rdi
callq 0x30a0
jmp 0x16794
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x300(%rbp), %rdi
callq 0x3180
jmp 0x16c91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x30a0
jmp 0x16c91
leaq -0xb8(%rbp), %rdi
callq 0x161f0
movq %rax, -0x4d8(%rbp)
jmp 0x167a9
movq -0x4d8(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x41e0
jmp 0x167be
leaq -0xb8(%rbp), %rdi
callq 0x161b0
movl %eax, -0x4dc(%rbp)
jmp 0x167d2
leaq -0xb8(%rbp), %rdi
callq 0x161d0
movl %eax, -0x4e0(%rbp)
jmp 0x167e6
movl -0x4dc(%rbp), %eax
movl -0x4e0(%rbp), %ecx
cmpl %ecx, %eax
je 0x16866
movq 0x107f3(%rip), %rdi     # 0x26ff0
movq 0x107ac(%rip), %rsi     # 0x26fb0
callq 0x32b0
movq %rax, -0x4e8(%rbp)
jmp 0x16812
movq -0x4e8(%rbp), %rdi
leaq 0x3f19(%rip), %rsi      # 0x1a739
callq 0x3270
movq %rax, -0x4f0(%rbp)
jmp 0x1682e
movq -0x4f0(%rbp), %rdi
movq 0x10774(%rip), %rsi     # 0x26fb0
callq 0x32b0
movq %rax, -0x4f8(%rbp)
jmp 0x1684a
movq -0x4f8(%rbp), %rdi
movq 0x10758(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x1685f
xorl %edi, %edi
callq 0x3320
leaq -0x310(%rbp), %rdi
callq 0x15850
jmp 0x16874
leaq -0xb8(%rbp), %rdi
callq 0x161f0
movq %rax, -0x500(%rbp)
jmp 0x16889
movq -0x500(%rbp), %rsi
leaq -0x468(%rbp), %rdi
callq 0x16e70
jmp 0x1689e
leaq -0x310(%rbp), %rdi
callq 0x15880
movsd %xmm0, -0x508(%rbp)
jmp 0x168b4
movsd -0x508(%rbp), %xmm0
movsd %xmm0, -0x470(%rbp)
movq $0x0, -0x478(%rbp)
leaq -0x490(%rbp), %rdi
movq %rdi, -0x510(%rbp)
callq 0x17080
movq -0x510(%rbp), %rsi
leaq -0x468(%rbp), %rdi
callq 0x7100
jmp 0x168f7
leaq -0x468(%rbp), %rdi
leaq -0x490(%rbp), %rsi
callq 0x6ca0
jmp 0x1690c
leaq -0x468(%rbp), %rdi
callq 0x9880
jmp 0x1691a
leaq -0x310(%rbp), %rdi
callq 0x15880
movsd %xmm0, -0x518(%rbp)
jmp 0x16930
movsd -0x518(%rbp), %xmm1
movsd -0x470(%rbp), %xmm0
subsd %xmm0, %xmm1
movsd -0x478(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x478(%rbp)
movq 0x10669(%rip), %rdi     # 0x26fc8
leaq 0x3e59(%rip), %rsi      # 0x1a7bf
callq 0x3270
movq %rax, -0x520(%rbp)
jmp 0x16974
movq -0x520(%rbp), %rdi
movsd -0x478(%rbp), %xmm0
callq 0x34e0
movq %rax, -0x528(%rbp)
jmp 0x16991
movq -0x528(%rbp), %rdi
movq 0x10611(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x169a6
leaq -0x490(%rbp), %rdi
callq 0x170a0
leaq -0x468(%rbp), %rdi
callq 0x17100
jmp 0x16c51
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x16a03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x169f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x490(%rbp), %rdi
callq 0x170a0
leaq -0x468(%rbp), %rdi
callq 0x17100
movl -0x74(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x16c85
movq -0x70(%rbp), %rdi
callq 0x30f0
movl (%rax), %eax
movl %eax, -0x494(%rbp)
cmpl $-0x1, -0x494(%rbp)
jne 0x16b2a
movq 0x105b8(%rip), %rdi     # 0x26ff0
leaq 0x3d8c(%rip), %rsi      # 0x1a7cb
callq 0x3270
movq %rax, -0x530(%rbp)
jmp 0x16a4d
movq -0x530(%rbp), %rdi
movq 0x10555(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16a62
movq 0x10587(%rip), %rdi     # 0x26ff0
leaq 0x3d81(%rip), %rsi      # 0x1a7f1
callq 0x3270
movq %rax, -0x538(%rbp)
jmp 0x16a7e
leaq -0xb8(%rbp), %rdi
callq 0x161a0
movl %eax, -0x53c(%rbp)
jmp 0x16a92
movl -0x53c(%rbp), %esi
movq -0x538(%rbp), %rdi
callq 0x3370
movq %rax, -0x548(%rbp)
jmp 0x16aad
movq -0x548(%rbp), %rdi
movq 0x104f5(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16ac2
movq 0x10527(%rip), %rdi     # 0x26ff0
leaq 0x3d30(%rip), %rsi      # 0x1a800
callq 0x3270
movq %rax, -0x550(%rbp)
jmp 0x16ade
movq -0x550(%rbp), %rdi
movq 0x104c4(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16af3
leaq 0x3d5b(%rip), %rdx      # 0x1a855
leaq -0x310(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x15c60
jmp 0x16b0f
jmp 0x16c4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0x3450
jmp 0x16c85
cmpl $-0x2, -0x494(%rbp)
jne 0x16c17
movq 0x104b2(%rip), %rdi     # 0x26ff0
leaq 0x3d1a(%rip), %rsi      # 0x1a85f
callq 0x3270
movq %rax, -0x558(%rbp)
jmp 0x16b53
movq -0x558(%rbp), %rdi
movq 0x1044f(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16b68
movq 0x10481(%rip), %rdi     # 0x26ff0
leaq 0x3c7b(%rip), %rsi      # 0x1a7f1
callq 0x3270
movq %rax, -0x560(%rbp)
jmp 0x16b84
leaq -0xb8(%rbp), %rdi
callq 0x161a0
movl %eax, -0x564(%rbp)
jmp 0x16b98
movl -0x564(%rbp), %esi
movq -0x560(%rbp), %rdi
callq 0x3370
movq %rax, -0x570(%rbp)
jmp 0x16bb3
movq -0x570(%rbp), %rdi
movq 0x103ef(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16bc8
movq 0x10421(%rip), %rdi     # 0x26ff0
leaq 0x3cad(%rip), %rsi      # 0x1a883
callq 0x3270
movq %rax, -0x578(%rbp)
jmp 0x16be4
movq -0x578(%rbp), %rdi
movq 0x103be(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16bf9
leaq 0x3c55(%rip), %rdx      # 0x1a855
leaq -0x310(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x15c60
jmp 0x16c15
jmp 0x16c4a
movq 0x103d2(%rip), %rdi     # 0x26ff0
leaq 0x3c8e(%rip), %rsi      # 0x1a8b3
callq 0x3270
movq %rax, -0x580(%rbp)
jmp 0x16c33
movq -0x580(%rbp), %rdi
movq 0x1036f(%rip), %rsi     # 0x26fb0
callq 0x32b0
jmp 0x16c48
jmp 0x16c4a
jmp 0x16c4c
callq 0x3450
movl $0x0, -0x4(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x158c0
leaq -0xb8(%rbp), %rdi
callq 0x171d0
leaq -0x68(%rbp), %rdi
callq 0x17200
movl -0x4(%rbp), %eax
addq $0x580, %rsp            # imm = 0x580
popq %rbp
retq
leaq -0x310(%rbp), %rdi
callq 0x158c0
leaq -0xb8(%rbp), %rdi
callq 0x171d0
leaq -0x68(%rbp), %rdi
callq 0x17200
movq -0x70(%rbp), %rdi
callq 0x3490
nop