// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1be8, %rsp           # imm = 0x1BE8
movq %rdi, %r14
leaq 0x268(%rsp), %rdi
callq 0x2250
leaq 0x68(%rsp), %rdi
callq 0x2370
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movq 0x20(%r14), %rsi
leaq 0x268(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2190
movq 0x268(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x288(%rsp,%rax)
jne 0x545d
movq 0x28(%r14), %rsi
leaq 0x68(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2060
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0x54b9
leaq 0x268(%rsp), %rbp
leaq 0x1030(%rsp), %rbx
leaq 0x5bd8(%rip), %r12      # 0xabea
leaq 0x5bd4(%rip), %r13      # 0xabed
xorl %r15d, %r15d
movl $0x0, 0x24(%rsp)
movq $0x0, 0x18(%rsp)
movl $0xbb8, %edx            # imm = 0xBB8
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x20e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x5413
movzbl 0x1030(%rsp), %eax
cmpl $0x63, %eax
je 0x502d
cmpl $0x64, %eax
je 0x5095
cmpl $0x70, %eax
jne 0x512b
movq %rbx, %rdi
leaq 0x5b46(%rip), %rsi      # 0xabba
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x2200
movq 0x18(%rsp), %rax
addl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
jmp 0x502d
movq %rbx, %rdi
leaq 0x5b27(%rip), %rsi      # 0xabc6
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x2200
pushq $0x70
popq %rdi
callq 0x2220
movq %rax, %r14
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
movq %rax, %rdi
callq 0x5b6c
movq %r14, 0x60(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x556c
movl 0xc(%rsp), %edi
addl %edi, 0x24(%rsp)
callq 0x5589
movl %eax, %ebx
movl 0xc(%rsp), %edi
addl $-0x2, %edi
callq 0x5589
movl %eax, %r14d
pushq $0x2
popq %rdi
callq 0x5589
movl %eax, %ecx
imull %r14d, %ecx
movl %ebx, %eax
leaq 0x1030(%rsp), %rbx
cltd
idivl %ecx
movq 0x18(%rsp), %rcx
addl %eax, %ecx
incl %ecx
movq %rcx, 0x18(%rsp)
jmp 0x502d
testb $0x1, %r15b
movabsq $0x100000601, %r15      # imm = 0x100000601
je 0x5261
movzbl (%rbx), %eax
cmpl $0x9, %eax
je 0x514c
cmpl $0x20, %eax
jne 0x5151
incq %rbx
jmp 0x513f
xorl %r14d, %r14d
cmpb $0x20, %al
ja 0x5161
movzbl %al, %ecx
btq %rcx, %r15
jb 0x5173
movb %al, 0x470(%rsp,%r14)
movb 0x1(%rbx,%r14), %al
incq %r14
jmp 0x5154
movb $0x0, 0x470(%rsp,%r14)
cmpb $0x0, 0x470(%rsp)
je 0x522c
leaq 0x470(%rsp), %rdi
leaq 0x5a3c(%rip), %rsi      # 0xabd5
leaq 0x10(%rsp), %rdx
leaq 0xb(%rsp), %rcx
leaq 0xc(%rsp), %r8
xorl %eax, %eax
callq 0x2200
movl 0x10(%rsp), %esi
testl %esi, %esi
je 0x522c
cmpb $0x21, 0xb(%rsp)
leaq 0x68(%rsp), %rax
jne 0x51fe
leaq 0x470(%rsp), %rdi
leaq 0x5a0c(%rip), %rsi      # 0xabde
leaq 0x10(%rsp), %rdx
leaq 0xb(%rsp), %rcx
leaq 0x17(%rsp), %r8
leaq 0xc(%rsp), %r9
xorl %eax, %eax
callq 0x2200
leaq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x2210
movl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
movq %r13, %rsi
callq 0x2210
movl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
leaq 0x5020(%rip), %rsi      # 0xa247
callq 0x2210
addq %r14, %rbx
cmpb $0x0, (%rbx)
jne 0x513f
leaq 0x68(%rsp), %rdi
leaq 0x598b(%rip), %rsi      # 0xabcf
callq 0x2210
leaq 0x1030(%rsp), %rbx
movb $0x1, %r15b
movq %rax, %rdi
callq 0x2140
jmp 0x502d
leaq 0x68(%rsp), %rdi
leaq 0x58ad(%rip), %rsi      # 0xab1a
callq 0x2210
movq %rax, %rdi
movl 0x24(%rsp), %esi
callq 0x2380
movq %rax, %rdi
leaq 0x4fbf(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x2380
movq %rax, %rdi
callq 0x2140
movq 0x38(%rsp), %rcx
subq 0x30(%rsp), %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movq %rcx, 0x50(%rsp)
xorl %ecx, %ecx
cmpq 0x50(%rsp), %rcx
je 0x513f
movq 0x30(%rsp), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %rax
movl (%rax), %r15d
movl 0x8(%rax), %ebp
xorl %ebx, %ebx
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movl %eax, 0x28(%rsp)
cmpl %ebx, 0x28(%rsp)
je 0x532b
leaq 0x68(%rsp), %rdi
movl %r15d, %esi
callq 0x2380
movq %rax, %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x4f25(%rip), %rsi      # 0xa247
callq 0x2210
incl %ebx
jmp 0x52f0
leaq 0x68(%rsp), %rdi
leaq 0x589c(%rip), %rsi      # 0xabd3
callq 0x2210
xorl %ebx, %ebx
cmpl 0x28(%rsp), %ebx
je 0x53ec
leal 0x1(%rbx), %r14d
movl %r14d, 0x2c(%rsp)
cmpl %ebp, %r14d
jge 0x53e1
leaq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x2210
movq %rax, %rdi
movl %r15d, %esi
callq 0x2380
movq %rax, %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x4eb6(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movq %r12, %rsi
callq 0x2210
movq %rax, %rdi
movl %r15d, %esi
callq 0x2380
movq %rax, %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %rdi
movl %r14d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5a9e(%rip), %rsi      # 0xae6a
callq 0x2210
movq %rax, %rdi
callq 0x2140
incl %r14d
jmp 0x5351
movl 0x2c(%rsp), %eax
movl %eax, %ebx
jmp 0x533e
movq 0x58(%rsp), %rcx
incq %rcx
leaq 0x268(%rsp), %rbp
leaq 0x1030(%rsp), %rbx
movabsq $0x100000601, %r15      # imm = 0x100000601
jmp 0x52c1
leaq 0x268(%rsp), %rdi
callq 0x2090
leaq 0x68(%rsp), %rdi
callq 0x22e0
leaq 0x30(%rsp), %rdi
callq 0x55a8
leaq 0x68(%rsp), %rdi
callq 0x2300
leaq 0x268(%rsp), %rdi
callq 0x20a0
addq $0x1be8, %rsp           # imm = 0x1BE8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9b6c(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0x9b60(%rip), %rdi      # 0xefd0
leaq 0x565d(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x9b45(%rip), %rdi      # 0xefd0
leaq 0x56ed(%rip), %rsi      # 0xab7f
callq 0x2210
movq 0x20(%r14), %rsi
movq %rax, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x9b1e(%rip), %rdi      # 0xefd0
callq 0x2140
jmp 0x5513
movq 0x9b10(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0x9b04(%rip), %rdi      # 0xefd0
leaq 0x5601(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x9ae9(%rip), %rdi      # 0xefd0
leaq 0x56ae(%rip), %rsi      # 0xab9c
callq 0x2210
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x9ac2(%rip), %rdi      # 0xefd0
callq 0x2140
pushq $0x1
popq %rdi
callq 0x2280
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r14, %rdi
callq 0x2230
jmp 0x5543
movq %rax, %rbx
jmp 0x5557
jmp 0x5540
jmp 0x5540
jmp 0x5540
jmp 0x5540
jmp 0x5540
jmp 0x5540
jmp 0x5540
jmp 0x5540
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x55a8
leaq 0x68(%rsp), %rdi
callq 0x2300
leaq 0x268(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2390