// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1bd8, %rsp           # imm = 0x1BD8
movq %rdi, %rbx
leaq 0x258(%rsp), %rdi
callq 0x2260
leaq 0x58(%rsp), %rdi
callq 0x2380
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x20(%rbx), %rsi
leaq 0x258(%rsp), %r12
movq %r12, %rdi
movl $0x8, %edx
callq 0x2170
movq 0x258(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x278(%rsp,%rax)
jne 0x6bbb
movq 0x28(%rbx), %rsi
leaq 0x58(%rsp), %rdi
movl $0x10, %edx
callq 0x2060
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0x6c29
leaq 0x1020(%rsp), %r13
movq $0x0, 0x38(%rsp)
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x258(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0xbb8, %edx            # imm = 0xBB8
movq %r12, %rdi
movq %r13, %rsi
callq 0x23c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x6b3c
movzbl 0x1020(%rsp), %eax
cmpl $0x63, %eax
je 0x64be
cmpl $0x64, %eax
je 0x6545
cmpl $0x70, %eax
jne 0x65a0
movq %r13, %rdi
leaq 0x66ea(%rip), %rsi      # 0xcc0b
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
xorl %eax, %eax
callq 0x2210
movq 0x10(%rsp), %rax
addl 0x4(%rsp), %eax
movq %rax, 0x10(%rsp)
jmp 0x64be
movq %r13, %rdi
leaq 0x66c8(%rip), %rsi      # 0xcc17
leaq 0x8(%rsp), %rdx
leaq 0x4(%rsp), %rcx
xorl %eax, %eax
callq 0x2210
movl $0x70, %edi
callq 0x2230
movq %rax, %r14
movl 0x8(%rsp), %esi
movl 0x4(%rsp), %edx
movq %rax, %rdi
callq 0x7332
movq %r14, 0x50(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x6980
movq %r14, (%rsi)
addq $0x8, 0x28(%rsp)
jmp 0x698f
testb $0x1, 0x38(%rsp)
jne 0x67f0
movl $0x6, %edx
leaq 0x58(%rsp), %rdi
leaq 0x655e(%rip), %rsi      # 0xcb1a
callq 0x2270
leaq 0x58(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5c66(%rip), %rsi      # 0xc247
callq 0x2270
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x2390
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2180
movq 0x28(%rsp), %r15
subq 0x20(%rsp), %r15
shrq $0x3, %r15
movb $0x1, %al
movq %rax, 0x38(%rsp)
testl %r15d, %r15d
jle 0x67f0
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movq %r15, 0x40(%rsp)
movq 0x20(%rsp), %rax
movq %rcx, 0x48(%rsp)
movq (%rax,%rcx,8), %rax
movl (%rax), %ebx
movl 0x8(%rax), %r12d
testl %r12d, %r12d
jle 0x66ac
xorl %ebp, %ebp
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x65ff(%rip), %rsi      # 0xcc81
callq 0x2270
movq %r14, %rdi
movl %ebp, %esi
callq 0x2390
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5ba7(%rip), %rsi      # 0xc247
callq 0x2270
incl %ebp
cmpl %ebp, %r12d
jne 0x6664
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x6567(%rip), %rsi      # 0xcc24
callq 0x2270
testl %r12d, %r12d
jle 0x67ca
xorl %r13d, %r13d
movl %r13d, %r15d
leal 0x1(%r15), %r13d
cmpl %r12d, %r13d
jge 0x67c1
movl %r13d, %ebp
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x6549(%rip), %rsi      # 0xcc3b
callq 0x2270
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x656c(%rip), %rsi      # 0xcc81
callq 0x2270
movq %r14, %rdi
movl %r15d, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5b10(%rip), %rsi      # 0xc247
callq 0x2270
movl $0x1, %edx
movq %r14, %rdi
leaq 0x64f0(%rip), %rsi      # 0xcc3b
callq 0x2270
movq %r14, %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6515(%rip), %rsi      # 0xcc81
callq 0x2270
movq %r14, %rdi
movl %ebp, %esi
callq 0x2390
movq %rax, %r14
movq %rax, %rdi
leaq 0x6772(%rip), %rsi      # 0xcefa
xorl %edx, %edx
callq 0x2270
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2180
incl %ebp
cmpl %ebp, %r12d
jne 0x66e1
cmpl %r12d, %r13d
jne 0x66ce
movq 0x48(%rsp), %rcx
incq %rcx
movq 0x40(%rsp), %r15
cmpq %r15, %rcx
leaq 0x1020(%rsp), %r13
jne 0x6649
leaq 0x258(%rsp), %r12
movq %r13, %r14
movzbl (%r14), %eax
cmpl $0x9, %eax
je 0x6801
cmpl $0x20, %eax
jne 0x6806
incq %r14
jmp 0x67f3
xorl %r15d, %r15d
movzbl (%r14,%r15), %eax
cmpq $0x20, %rax
ja 0x6824
movabsq $0x100000601, %rcx      # imm = 0x100000601
btq %rax, %rcx
jb 0x6831
movb %al, 0x460(%rsp,%r15)
incq %r15
jmp 0x6809
movb $0x0, 0x460(%rsp,%r15)
cmpb $0x0, 0x460(%rsp)
je 0x6928
leaq 0x460(%rsp), %rdi
leaq 0x63cf(%rip), %rsi      # 0xcc26
leaq 0x8(%rsp), %rdx
leaq 0x3(%rsp), %rcx
leaq 0x4(%rsp), %r8
xorl %eax, %eax
callq 0x2210
movl 0x8(%rsp), %esi
testl %esi, %esi
je 0x6928
cmpb $0x21, 0x3(%rsp)
jne 0x68e7
leaq 0x460(%rsp), %rdi
leaq 0x63a0(%rip), %rsi      # 0xcc2f
leaq 0x8(%rsp), %rdx
leaq 0x3(%rsp), %rcx
leaq 0xf(%rsp), %r8
leaq 0x4(%rsp), %r9
xorl %eax, %eax
callq 0x2210
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x6380(%rip), %rsi      # 0xcc3b
callq 0x2270
movl 0x8(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x63a1(%rip), %rsi      # 0xcc81
callq 0x2270
jmp 0x6908
leaq 0x58(%rsp), %rdi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x637e(%rip), %rsi      # 0xcc81
callq 0x2270
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x2390
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5924(%rip), %rsi      # 0xc247
callq 0x2270
addq %r15, %r14
cmpb $0x0, (%r14)
jne 0x67f3
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x62da(%rip), %rsi      # 0xcc20
callq 0x2270
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0x58(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
leaq 0x58(%rsp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2180
jmp 0x64be
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x6d28
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
jle 0x6a55
leal 0x3(%rcx), %eax
andl $-0x4, %eax
leal -0x1(%rcx), %edx
movd %edx, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movl $0x4, %edx
movdqa 0x6296(%rip), %xmm3     # 0xcc50
movdqa 0x627e(%rip), %xmm2     # 0xcc40
movdqa 0x6296(%rip), %xmm6     # 0xcc60
movdqa %xmm2, %xmm1
pmuludq %xmm3, %xmm2
pshufd $0xe8, %xmm2, %xmm2     # xmm2 = xmm2[0,2,2,3]
pshufd $0xf5, %xmm3, %xmm4     # xmm4 = xmm3[1,1,3,3]
pshufd $0xf5, %xmm1, %xmm5     # xmm5 = xmm1[1,1,3,3]
pmuludq %xmm4, %xmm5
pshufd $0xe8, %xmm5, %xmm4     # xmm4 = xmm5[0,2,2,3]
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
paddd %xmm6, %xmm3
movl %edx, %esi
addl $-0x4, %edx
addl %eax, %esi
addl $-0x4, %esi
cmpl $0x4, %esi
jne 0x69ca
negl %edx
movd %edx, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
por 0x625c(%rip), %xmm3     # 0xcc70
movdqa 0x6184(%rip), %xmm4     # 0xcba0
pxor %xmm4, %xmm0
pxor %xmm4, %xmm3
pcmpgtd %xmm0, %xmm3
pand %xmm3, %xmm1
pandn %xmm2, %xmm3
por %xmm1, %xmm3
pshufd $0xee, %xmm3, %xmm0     # xmm0 = xmm3[2,3,2,3]
pshufd $0xff, %xmm3, %xmm1     # xmm1 = xmm3[3,3,3,3]
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
pmuludq %xmm1, %xmm2
pmuludq %xmm3, %xmm0
pmuludq %xmm2, %xmm0
movd %xmm0, %eax
jmp 0x6a5a
movl $0x1, %eax
movl $0x2, %esi
cmpl $0x3, %ecx
jl 0x6b22
leal 0x1(%rcx), %edx
andl $-0x4, %edx
leal -0x3(%rcx), %esi
movd %esi, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movl $0x4, %esi
movdqa 0x61c9(%rip), %xmm3     # 0xcc50
movdqa 0x61b1(%rip), %xmm2     # 0xcc40
movdqa 0x61c9(%rip), %xmm6     # 0xcc60
movdqa %xmm2, %xmm1
pmuludq %xmm3, %xmm2
pshufd $0xe8, %xmm2, %xmm2     # xmm2 = xmm2[0,2,2,3]
pshufd $0xf5, %xmm3, %xmm4     # xmm4 = xmm3[1,1,3,3]
pshufd $0xf5, %xmm1, %xmm5     # xmm5 = xmm1[1,1,3,3]
pmuludq %xmm4, %xmm5
pshufd $0xe8, %xmm5, %xmm4     # xmm4 = xmm5[0,2,2,3]
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
paddd %xmm6, %xmm3
movl %esi, %edi
addl $-0x4, %esi
addl %edx, %edi
addl $-0x4, %edi
cmpl $0x4, %edi
jne 0x6a97
negl %esi
movd %esi, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
por 0x618f(%rip), %xmm3     # 0xcc70
movdqa 0x60b7(%rip), %xmm4     # 0xcba0
pxor %xmm4, %xmm0
pxor %xmm4, %xmm3
pcmpgtd %xmm0, %xmm3
pand %xmm3, %xmm1
pandn %xmm2, %xmm3
por %xmm1, %xmm3
pshufd $0xee, %xmm3, %xmm0     # xmm0 = xmm3[2,3,2,3]
pshufd $0xff, %xmm3, %xmm1     # xmm1 = xmm3[3,3,3,3]
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
pmuludq %xmm1, %xmm2
pmuludq %xmm3, %xmm0
pmuludq %xmm2, %xmm0
movd %xmm0, %esi
addl %esi, %esi
addl %ecx, 0x1c(%rsp)
cltd
idivl %esi
movq 0x10(%rsp), %rcx
addl %eax, %ecx
incl %ecx
movq %rcx, 0x10(%rsp)
jmp 0x64be
leaq 0x258(%rsp), %rdi
callq 0x2090
leaq 0x58(%rsp), %rdi
callq 0x22f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6b6a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
movq 0x9457(%rip), %rax      # 0xffc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x60(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x58(%rsp,%rcx)
callq 0x21c0
leaq 0x150(%rsp), %rdi
callq 0x20e0
leaq 0x258(%rsp), %rdi
callq 0x20a0
addq $0x1bd8, %rsp           # imm = 0x1BD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x940e(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x9402(%rip), %rdi      # 0xffd0
leaq 0x5eff(%rip), %rsi      # 0xcad4
movl $0xf, %edx
callq 0x2270
movq 0x93ea(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x93de(%rip), %rdi      # 0xffd0
leaq 0x5fd7(%rip), %rsi      # 0xcbd0
movl $0x1c, %edx
callq 0x2270
movq 0x20(%rbx), %rsi
movq 0x93c2(%rip), %rdi      # 0xffd0
callq 0x2220
movq %rax, %rdi
callq 0x2120
movq 0x93ae(%rip), %rdi      # 0xffd0
callq 0x2120
jmp 0x6c95
movq 0x93a0(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x9394(%rip), %rdi      # 0xffd0
leaq 0x5e91(%rip), %rsi      # 0xcad4
movl $0xf, %edx
callq 0x2270
movq 0x937c(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x9370(%rip), %rdi      # 0xffd0
leaq 0x5f86(%rip), %rsi      # 0xcbed
movl $0x1d, %edx
callq 0x2270
movq 0x28(%rbx), %rsi
movq 0x9354(%rip), %rdi      # 0xffd0
callq 0x2220
movq %rax, %rdi
callq 0x2120
movq 0x9340(%rip), %rdi      # 0xffd0
callq 0x2120
movl $0x1, %edi
callq 0x2290
movq %rax, %rbx
movl $0x70, %esi
movq %r14, %rdi
callq 0x2240
jmp 0x6cc9
movq %rax, %rbx
jmp 0x6d12
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
jmp 0x6cc6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6ce0
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
movq 0x92e1(%rip), %rax      # 0xffc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x60(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x58(%rsp,%rcx)
callq 0x21c0
leaq 0x150(%rsp), %rdi
callq 0x20e0
leaq 0x258(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x23a0
nop