// File: akinanop[P]mvl-solver/src/QuadraticEncode.cc
// Function: QuadraticEncode(CommandLine*)
void QuadraticEncode(CommandLine * cline)
{
  //local variables
  ifstream infile;
  ofstream outfile;
  char line_buffer[3000];
  char word_buffer[3000];
  int num_clause = 0;
  int num_var = 0;
  int var, val;
  int temp;
  bool isdone = false;
  bool isneg = false;
  Variable * temp_var;
  vector <Variable *> VAR_LIST;
  char ch, ch1;

  //opening infile to read and checking if it opens
  infile.open(cline->FILE, ios::in);
  if(!infile)
    {
      cout<<endl;
      cout<<"**** ERROR ****"<<endl;
      cout<<"Could not open input file : "<<cline->FILE<<endl;
      cout<<endl;
      exit(1);
    }

  //opening outfile to write the new clauses
  outfile.open(cline->MODEL_FILE, ios::out);
  if(!outfile)
    {
      cout<<endl;
      cout<<"**** ERROR ****"<<endl;
      cout<<"Could not open output file : "<<cline->MODEL_FILE<<endl;
      cout<<endl;
      exit(1);
    }

  //now reading each line from the infile and if needed to convert
  //then converting it and writing to outfile or just writing directly
  while(infile.getline(line_buffer, 3000))
    {
      //if line is comment - just write
      if(line_buffer[0] == 'c')
	{
	  //do nothing
	  ;
	}
      //else if line is p cnf var_num clause_num - just write
      else if(line_buffer[0] == 'p')
	{
	  //do nothing
	  temp = sscanf(line_buffer, "p cnf %d %d", &var, &val);
	  num_clause += val;
	}
      //else if line is d var# domsize
      else if(line_buffer[0] == 'd')
	{
	  temp = sscanf(line_buffer, "d %d %d", &var, &val);
	  temp_var = new Variable(var, val);
	  VAR_LIST.push_back(temp_var);
	  num_var += val;
	  num_clause += (Fact(val)/(Fact(val-2)*Fact(2)));
	  num_clause += 1;
	}
      //else its variables
      else
	{
	  if(!isdone)
	    {
	      isdone = true;
	      outfile<<"p cnf "<<num_var<<" "<<num_clause<<endl;
	      int var_count = VAR_LIST.size();
	      for(int x=0; x<var_count; x++)
		{
		  int var_name = VAR_LIST[x]->VAR;
		  int var_domain = VAR_LIST[x]->DOMAINSIZE;
		  for(int y=0; y<var_domain; y++)
		    outfile<<var_name<<"="<<y<<" ";
		  outfile<<"\n";
		  for(int p=0; p<var_domain; p++)
		    {
		      for(int q=p+1; q<var_domain; q++)
			{
			  outfile<<"-"<<var_name<<"="<<p<<" "<<"-"<<var_name<<"="<<q<<""<<endl;
			}
		    }
		}
	    }
	  char * lp = line_buffer;
	  do
	    {
	      char * wp = word_buffer;
	      //erasing all leading space/tabs
	      while (*lp && ((*lp == ' ') || (*lp == '\t')))
		{
		  lp++;
		}
	      //reading each value into word buffer
	      while (*lp && (*lp != ' ') && (*lp != '\t') && (*lp != '\n'))
		{
		  *(wp++) = *(lp++);
		}
	      *wp = '\0';
	      //converting into int and writing to file
	      if(strlen(word_buffer) != 0)
		{
		  temp = sscanf(word_buffer, "%d %c %d", &var, &ch, &val);
		  if(var != 0 )
		    {
		      if(ch == '!')
			{
			  temp = sscanf(word_buffer, "%d %c %c %d", &var, &ch, &ch1, &val);
			  outfile<<"-"<<var<<"="<<val<<" ";
			}
		      else
			{
			  outfile<<var<<"="<<val<<" ";
			}
		    }
		}
	    }while(*lp);
	  outfile<<"0"<<endl;
	}
    }

  //closing file
  infile.close();
  outfile.close();
}

// Assembly:
subq $0x1d38, %rsp           # imm = 0x1D38
movq %rdi, 0x1d30(%rsp)
leaq 0x1b28(%rsp), %rdi
callq 0x22b0
leaq 0x1928(%rsp), %rdi
callq 0x23f0
jmp 0x9e2b
movl $0x0, 0x19c(%rsp)
movl $0x0, 0x198(%rsp)
movb $0x0, 0x18b(%rsp)
movb $0x0, 0x18a(%rsp)
leaq 0x168(%rsp), %rdi
callq 0xa9c0
movq 0x1d30(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x1b28(%rsp), %rdi
movl $0x8, %edx
callq 0x2180
jmp 0x9e7e
movq 0x1b28(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x1b28(%rsp,%rax), %rdi
callq 0x2130
movb %al, 0x137(%rsp)
jmp 0x9ea0
movb 0x137(%rsp), %al
testb $0x1, %al
jne 0x9eb0
jmp 0x9f9f
movq 0x14119(%rip), %rdi     # 0x1dfd0
movq 0x14102(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x9ec5
movq 0x14104(%rip), %rdi     # 0x1dfd0
leaq 0xdc01(%rip), %rsi      # 0x17ad4
callq 0x2220
movq %rax, 0x128(%rsp)
jmp 0x9ee2
movq 0x128(%rsp), %rdi
movq 0x140cf(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x9ef8
movq 0x140d1(%rip), %rdi     # 0x1dfd0
leaq 0xdc5d(%rip), %rsi      # 0x17b63
callq 0x2220
movq %rax, 0x120(%rsp)
jmp 0x9f15
movq 0x120(%rsp), %rdi
movq 0x1d30(%rsp), %rax
movq 0x20(%rax), %rsi
callq 0x2220
movq %rax, 0x118(%rsp)
jmp 0x9f38
movq 0x118(%rsp), %rdi
movq 0x14079(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x9f4e
movq 0x1407b(%rip), %rdi     # 0x1dfd0
movq 0x14064(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x9f63
movl $0x1, %edi
callq 0x22e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1920(%rsp)
movl %eax, 0x191c(%rsp)
jmp 0xa99b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1920(%rsp)
movl %eax, 0x191c(%rsp)
jmp 0xa981
movq 0x1d30(%rsp), %rax
movq 0x28(%rax), %rsi
leaq 0x1928(%rsp), %rdi
movl $0x10, %edx
callq 0x2060
jmp 0x9fbf
movq 0x1928(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x1928(%rsp,%rax), %rdi
callq 0x2130
movb %al, 0x117(%rsp)
jmp 0x9fe1
movb 0x117(%rsp), %al
testb $0x1, %al
jne 0x9ff1
jmp 0xa0ae
movq 0x13fd8(%rip), %rdi     # 0x1dfd0
movq 0x13fc1(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa006
movq 0x13fc3(%rip), %rdi     # 0x1dfd0
leaq 0xdac0(%rip), %rsi      # 0x17ad4
callq 0x2220
movq %rax, 0x108(%rsp)
jmp 0xa023
movq 0x108(%rsp), %rdi
movq 0x13f8e(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa039
movq 0x13f90(%rip), %rdi     # 0x1dfd0
leaq 0xdb39(%rip), %rsi      # 0x17b80
callq 0x2220
movq %rax, 0x100(%rsp)
jmp 0xa056
movq 0x100(%rsp), %rdi
movq 0x1d30(%rsp), %rax
movq 0x28(%rax), %rsi
callq 0x2220
movq %rax, 0xf8(%rsp)
jmp 0xa079
movq 0xf8(%rsp), %rdi
movq 0x13f38(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa08f
movq 0x13f3a(%rip), %rdi     # 0x1dfd0
movq 0x13f23(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa0a4
movl $0x1, %edi
callq 0x22e0
jmp 0xa0b0
leaq 0x1b28(%rsp), %rdi
leaq 0xd60(%rsp), %rsi
movl $0xbb8, %edx            # imm = 0xBB8
callq 0x20f0
movq %rax, 0xf0(%rsp)
jmp 0xa0d4
movq 0xf0(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2300
movb %al, 0xef(%rsp)
jmp 0xa0f4
movb 0xef(%rsp), %al
testb $0x1, %al
jne 0xa104
jmp 0xa934
movsbl 0xd60(%rsp), %eax
cmpl $0x63, %eax
jne 0xa116
jmp 0xa92f
movsbl 0xd60(%rsp), %eax
cmpl $0x70, %eax
jne 0xa16a
leaq 0xd60(%rsp), %rdi
leaq 0xda6c(%rip), %rsi      # 0x17b9e
leaq 0x194(%rsp), %rdx
leaq 0x190(%rsp), %rcx
movb $0x0, %al
callq 0x2210
movl %eax, 0x18c(%rsp)
movl 0x190(%rsp), %eax
addl 0x19c(%rsp), %eax
movl %eax, 0x19c(%rsp)
jmp 0xa92d
movsbl 0xd60(%rsp), %eax
cmpl $0x64, %eax
jne 0xa2c1
leaq 0xda28(%rip), %rsi      # 0x17baa
xorl %eax, %eax
leaq 0xd60(%rsp), %rdi
leaq 0x194(%rsp), %rdx
leaq 0x190(%rsp), %rcx
callq 0x2210
movl %eax, 0x18c(%rsp)
movl $0x70, %edi
callq 0x2230
movq %rax, 0xe0(%rsp)
jmp 0xa1bc
movq 0xe0(%rsp), %rdi
movl 0x194(%rsp), %esi
movl 0x190(%rsp), %edx
callq 0xc4a0
jmp 0xa1d9
movq 0xe0(%rsp), %rax
movq %rax, 0x180(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0xa9d0
jmp 0xa200
movl 0x190(%rsp), %ecx
movl 0x198(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x198(%rsp)
movl 0x190(%rsp), %edi
callq 0xaa40
movl %eax, 0xdc(%rsp)
jmp 0xa22c
movl 0x190(%rsp), %edi
addl $-0x2, %edi
callq 0xaa40
movl %eax, 0xd8(%rsp)
jmp 0xa244
movl $0x2, %edi
callq 0xaa40
movl %eax, 0xd4(%rsp)
jmp 0xa257
movl 0xdc(%rsp), %eax
movl 0xd4(%rsp), %edx
movl 0xd8(%rsp), %ecx
imull %edx, %ecx
cltd
idivl %ecx
addl 0x19c(%rsp), %eax
movl %eax, 0x19c(%rsp)
movl 0x19c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x19c(%rsp)
jmp 0xa92b
movq 0xe0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1920(%rsp)
movl %eax, 0x191c(%rsp)
movl $0x70, %esi
callq 0x2240
jmp 0xa981
testb $0x1, 0x18b(%rsp)
jne 0xa63e
movb $0x1, 0x18b(%rsp)
leaq 0xd83c(%rip), %rsi      # 0x17b1a
leaq 0x1928(%rsp), %rdi
callq 0x2220
movq %rax, 0xc8(%rsp)
jmp 0xa2f5
movq 0xc8(%rsp), %rdi
movl 0x198(%rsp), %esi
callq 0x2400
movq %rax, 0xc0(%rsp)
jmp 0xa313
movq 0xc0(%rsp), %rdi
leaq 0xcf25(%rip), %rsi      # 0x17247
callq 0x2220
movq %rax, 0xb8(%rsp)
jmp 0xa331
movq 0xb8(%rsp), %rdi
movl 0x19c(%rsp), %esi
callq 0x2400
movq %rax, 0xb0(%rsp)
jmp 0xa34f
movq 0xb0(%rsp), %rdi
movq 0x13c62(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa365
leaq 0x168(%rsp), %rdi
callq 0xaa90
movl %eax, 0x160(%rsp)
movl $0x0, 0x15c(%rsp)
movl 0x15c(%rsp), %eax
cmpl 0x160(%rsp), %eax
jge 0xa63c
movslq 0x15c(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0xaab0
movq (%rax), %rax
movl (%rax), %eax
movl %eax, 0x158(%rsp)
movslq 0x15c(%rsp), %rsi
leaq 0x168(%rsp), %rdi
callq 0xaab0
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, 0x154(%rsp)
movl $0x0, 0x150(%rsp)
movl 0x150(%rsp), %eax
cmpl 0x154(%rsp), %eax
jge 0xa482
movl 0x158(%rsp), %esi
leaq 0x1928(%rsp), %rdi
callq 0x2400
movq %rax, 0xa8(%rsp)
jmp 0xa418
movq 0xa8(%rsp), %rdi
leaq 0xd7aa(%rip), %rsi      # 0x17bd1
callq 0x2220
movq %rax, 0xa0(%rsp)
jmp 0xa436
movq 0xa0(%rsp), %rdi
movl 0x150(%rsp), %esi
callq 0x2400
movq %rax, 0x98(%rsp)
jmp 0xa454
movq 0x98(%rsp), %rdi
leaq 0xcde4(%rip), %rsi      # 0x17247
callq 0x2220
jmp 0xa46a
jmp 0xa46c
movl 0x150(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
jmp 0xa3e6
leaq 0xd72e(%rip), %rsi      # 0x17bb7
leaq 0x1928(%rsp), %rdi
callq 0x2220
jmp 0xa498
movl $0x0, 0x14c(%rsp)
movl 0x14c(%rsp), %eax
cmpl 0x154(%rsp), %eax
jge 0xa624
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x148(%rsp)
movl 0x148(%rsp), %eax
cmpl 0x154(%rsp), %eax
jge 0xa60c
leaq 0xd6eb(%rip), %rsi      # 0x17bce
leaq 0x1928(%rsp), %rdi
callq 0x2220
movq %rax, 0x90(%rsp)
jmp 0xa4fa
movq 0x90(%rsp), %rdi
movl 0x158(%rsp), %esi
callq 0x2400
movq %rax, 0x88(%rsp)
jmp 0xa518
movq 0x88(%rsp), %rdi
leaq 0xd6aa(%rip), %rsi      # 0x17bd1
callq 0x2220
movq %rax, 0x80(%rsp)
jmp 0xa536
movq 0x80(%rsp), %rdi
movl 0x14c(%rsp), %esi
callq 0x2400
movq %rax, 0x78(%rsp)
jmp 0xa551
movq 0x78(%rsp), %rdi
leaq 0xccea(%rip), %rsi      # 0x17247
callq 0x2220
movq %rax, 0x70(%rsp)
jmp 0xa569
movq 0x70(%rsp), %rdi
leaq 0xd659(%rip), %rsi      # 0x17bce
callq 0x2220
movq %rax, 0x68(%rsp)
jmp 0xa581
movq 0x68(%rsp), %rdi
movl 0x158(%rsp), %esi
callq 0x2400
movq %rax, 0x60(%rsp)
jmp 0xa599
movq 0x60(%rsp), %rdi
leaq 0xd62c(%rip), %rsi      # 0x17bd1
callq 0x2220
movq %rax, 0x58(%rsp)
jmp 0xa5b1
movq 0x58(%rsp), %rdi
movl 0x148(%rsp), %esi
callq 0x2400
movq %rax, 0x50(%rsp)
jmp 0xa5c9
movq 0x50(%rsp), %rdi
leaq 0xd875(%rip), %rsi      # 0x17e4a
callq 0x2220
movq %rax, 0x48(%rsp)
jmp 0xa5e1
movq 0x48(%rsp), %rdi
movq 0x139d3(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa5f4
jmp 0xa5f6
movl 0x148(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x148(%rsp)
jmp 0xa4c8
jmp 0xa60e
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0xa4a3
jmp 0xa626
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x15c(%rsp)
jmp 0xa384
jmp 0xa63e
leaq 0xd60(%rsp), %rax
movq %rax, 0x140(%rsp)
leaq 0x1a0(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x140(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x47(%rsp)
je 0xa6a4
movq 0x140(%rsp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, 0x46(%rsp)
je 0xa69c
movq 0x140(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x9, %eax
sete %al
movb %al, 0x46(%rsp)
movb 0x46(%rsp), %al
movb %al, 0x47(%rsp)
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0xa6ae
jmp 0xa6c4
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
jmp 0xa65e
jmp 0xa6c6
movq 0x140(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x45(%rsp)
je 0xa71a
movq 0x140(%rsp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x20, %ecx
movb %al, 0x45(%rsp)
je 0xa71a
movq 0x140(%rsp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x9, %ecx
movb %al, 0x45(%rsp)
je 0xa71a
movq 0x140(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
setne %al
movb %al, 0x45(%rsp)
movb 0x45(%rsp), %al
testb $0x1, %al
jne 0xa724
jmp 0xa75b
movq 0x140(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x140(%rsp)
movb (%rax), %cl
movq 0x138(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x138(%rsp)
movb %cl, (%rax)
jmp 0xa6c6
movq 0x138(%rsp), %rax
movb $0x0, (%rax)
leaq 0x1a0(%rsp), %rdi
callq 0x2120
cmpq $0x0, %rax
je 0xa8ea
leaq 0x1a0(%rsp), %rdi
leaq 0xd42d(%rip), %rsi      # 0x17bb9
leaq 0x194(%rsp), %rdx
leaq 0x167(%rsp), %rcx
leaq 0x190(%rsp), %r8
movb $0x0, %al
callq 0x2210
movl %eax, 0x18c(%rsp)
cmpl $0x0, 0x194(%rsp)
je 0xa8e8
movsbl 0x167(%rsp), %eax
cmpl $0x21, %eax
jne 0xa886
leaq 0xd3ea(%rip), %rsi      # 0x17bc2
xorl %eax, %eax
leaq 0x1a0(%rsp), %rdi
leaq 0x194(%rsp), %rdx
leaq 0x167(%rsp), %rcx
leaq 0x166(%rsp), %r8
leaq 0x190(%rsp), %r9
callq 0x2210
movl %eax, 0x18c(%rsp)
leaq 0xd3b9(%rip), %rsi      # 0x17bce
leaq 0x1928(%rsp), %rdi
callq 0x2220
movq %rax, 0x38(%rsp)
jmp 0xa829
movq 0x38(%rsp), %rdi
movl 0x194(%rsp), %esi
callq 0x2400
movq %rax, 0x30(%rsp)
jmp 0xa841
movq 0x30(%rsp), %rdi
leaq 0xd384(%rip), %rsi      # 0x17bd1
callq 0x2220
movq %rax, 0x28(%rsp)
jmp 0xa859
movq 0x28(%rsp), %rdi
movl 0x190(%rsp), %esi
callq 0x2400
movq %rax, 0x20(%rsp)
jmp 0xa871
movq 0x20(%rsp), %rdi
leaq 0xc9ca(%rip), %rsi      # 0x17247
callq 0x2220
jmp 0xa884
jmp 0xa8e6
movl 0x194(%rsp), %esi
leaq 0x1928(%rsp), %rdi
callq 0x2400
movq %rax, 0x18(%rsp)
jmp 0xa8a1
movq 0x18(%rsp), %rdi
leaq 0xd324(%rip), %rsi      # 0x17bd1
callq 0x2220
movq %rax, 0x10(%rsp)
jmp 0xa8b9
movq 0x10(%rsp), %rdi
movl 0x190(%rsp), %esi
callq 0x2400
movq %rax, 0x8(%rsp)
jmp 0xa8d1
movq 0x8(%rsp), %rdi
leaq 0xc96a(%rip), %rsi      # 0x17247
callq 0x2220
jmp 0xa8e4
jmp 0xa8e6
jmp 0xa8e8
jmp 0xa8ea
jmp 0xa8ec
movq 0x140(%rsp), %rax
cmpb $0x0, (%rax)
jne 0xa64e
leaq 0xd2af(%rip), %rsi      # 0x17bb3
leaq 0x1928(%rsp), %rdi
callq 0x2220
movq %rax, (%rsp)
jmp 0xa917
movq (%rsp), %rdi
movq 0x1369e(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0xa929
jmp 0xa92b
jmp 0xa92d
jmp 0xa92f
jmp 0xa0b0
leaq 0x1b28(%rsp), %rdi
callq 0x20a0
jmp 0xa943
leaq 0x1928(%rsp), %rdi
callq 0x2360
jmp 0xa952
leaq 0x168(%rsp), %rdi
callq 0xaad0
leaq 0x1928(%rsp), %rdi
callq 0x2380
leaq 0x1b28(%rsp), %rdi
callq 0x20b0
addq $0x1d38, %rsp           # imm = 0x1D38
retq
leaq 0x168(%rsp), %rdi
callq 0xaad0
leaq 0x1928(%rsp), %rdi
callq 0x2380
leaq 0x1b28(%rsp), %rdi
callq 0x20b0
movq 0x1920(%rsp), %rdi
callq 0x2410
nopw %cs:(%rax,%rax)
nop