// File: /workspace/github_cpp_makefile_star2_O3/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 0x6b17
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 0x6b85
leaq 0x1020(%rsp), %r13
xorl %ebx, %ebx
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 0x6a98
movzbl 0x1020(%rsp), %eax
cmpl $0x63, %eax
je 0x641b
cmpl $0x64, %eax
je 0x64a2
cmpl $0x70, %eax
jne 0x64fd
movq %r13, %rdi
leaq 0x778d(%rip), %rsi      # 0xdc0b
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 0x641b
movq %r13, %rdi
leaq 0x776b(%rip), %rsi      # 0xdc17
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 0x728e
movq %r14, 0x50(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x68dc
movq %r14, (%rsi)
addq $0x8, 0x28(%rsp)
jmp 0x68eb
testb $0x1, %bl
jne 0x6741
movl $0x6, %edx
leaq 0x58(%rsp), %rdi
leaq 0x7603(%rip), %rsi      # 0xdb1a
callq 0x2270
leaq 0x58(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6d0b(%rip), %rsi      # 0xd247
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), %rax
subq 0x20(%rsp), %rax
shrq $0x3, %rax
movq %rax, 0x40(%rsp)
testl %eax, %eax
jle 0x6741
movq 0x40(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rax
movl (%rax), %ebx
movl 0x8(%rax), %r12d
testl %r12d, %r12d
jle 0x6603
xorl %ebp, %ebp
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x76a8(%rip), %rsi      # 0xdc81
callq 0x2270
movq %r14, %rdi
movl %ebp, %esi
callq 0x2390
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6c50(%rip), %rsi      # 0xd247
callq 0x2270
incl %ebp
cmpl %ebp, %r12d
jne 0x65bb
movq %r15, 0x48(%rsp)
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x760b(%rip), %rsi      # 0xdc24
callq 0x2270
testl %r12d, %r12d
jle 0x6726
xorl %r13d, %r13d
movl %r13d, %r15d
leal 0x1(%r15), %r13d
cmpl %r12d, %r13d
jge 0x671d
movl %r13d, %ebp
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x75ed(%rip), %rsi      # 0xdc3b
callq 0x2270
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7610(%rip), %rsi      # 0xdc81
callq 0x2270
movq %r14, %rdi
movl %r15d, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6bb4(%rip), %rsi      # 0xd247
callq 0x2270
movl $0x1, %edx
movq %r14, %rdi
leaq 0x7594(%rip), %rsi      # 0xdc3b
callq 0x2270
movq %r14, %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x75b9(%rip), %rsi      # 0xdc81
callq 0x2270
movq %r14, %rdi
movl %ebp, %esi
callq 0x2390
movq %rax, %r14
movq %rax, %rdi
leaq 0x7816(%rip), %rsi      # 0xdefa
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 0x663d
cmpl %r12d, %r13d
jne 0x662a
movq 0x48(%rsp), %r15
incq %r15
cmpq 0x40(%rsp), %r15
leaq 0x1020(%rsp), %r13
jne 0x65a5
movq %r13, %r14
leaq 0x258(%rsp), %r12
movzbl (%r14), %eax
cmpl $0x9, %eax
je 0x675a
cmpl $0x20, %eax
jne 0x675f
incq %r14
jmp 0x674c
xorl %r15d, %r15d
cmpb $0x20, %al
ja 0x6779
movzbl %al, %ecx
movabsq $0x100000601, %rdx      # imm = 0x100000601
btq %rcx, %rdx
jb 0x678b
movb %al, 0x460(%rsp,%r15)
movb 0x1(%r14,%r15), %al
incq %r15
jmp 0x6762
movb $0x0, 0x460(%rsp,%r15)
cmpb $0x0, 0x460(%rsp)
je 0x6882
leaq 0x460(%rsp), %rdi
leaq 0x7475(%rip), %rsi      # 0xdc26
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 0x6882
cmpb $0x21, 0x3(%rsp)
jne 0x6841
leaq 0x460(%rsp), %rdi
leaq 0x7446(%rip), %rsi      # 0xdc2f
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 0x7426(%rip), %rsi      # 0xdc3b
callq 0x2270
movl 0x8(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7447(%rip), %rsi      # 0xdc81
callq 0x2270
jmp 0x6862
leaq 0x58(%rsp), %rdi
callq 0x2390
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7424(%rip), %rsi      # 0xdc81
callq 0x2270
movl 0x4(%rsp), %esi
movq %rbx, %rdi
callq 0x2390
movl $0x1, %edx
movq %rax, %rdi
leaq 0x69ca(%rip), %rsi      # 0xd247
callq 0x2270
addq %r15, %r14
cmpb $0x0, (%r14)
jne 0x674c
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0x7380(%rip), %rsi      # 0xdc20
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
movb $0x1, %bl
movq %rax, %rdi
callq 0x2180
jmp 0x641b
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x6c84
movl 0x4(%rsp), %ecx
movl $0x2, %esi
testl %ecx, %ecx
jle 0x6a79
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 0x7335(%rip), %xmm3     # 0xdc50
movdqa 0x731d(%rip), %xmm2     # 0xdc40
movdqa 0x7335(%rip), %xmm6     # 0xdc60
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, %edi
addl $-0x4, %edx
addl %eax, %edi
addl $-0x4, %edi
cmpl $0x4, %edi
jne 0x692b
negl %edx
movd %edx, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
por 0x72fb(%rip), %xmm3     # 0xdc70
movdqa 0x7223(%rip), %xmm4     # 0xdba0
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
cmpl $0x3, %ecx
jl 0x6a7e
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 0x7274(%rip), %xmm3     # 0xdc50
movdqa 0x725c(%rip), %xmm2     # 0xdc40
movdqa 0x7274(%rip), %xmm6     # 0xdc60
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 0x69ec
negl %esi
movd %esi, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
por 0x723a(%rip), %xmm3     # 0xdc70
movdqa 0x7162(%rip), %xmm4     # 0xdba0
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
jmp 0x6a7e
movl $0x1, %eax
addl %ecx, 0x1c(%rsp)
cltd
idivl %esi
movq 0x10(%rsp), %rcx
addl %eax, %ecx
incl %ecx
movq %rcx, 0x10(%rsp)
jmp 0x641b
leaq 0x258(%rsp), %rdi
callq 0x2090
leaq 0x58(%rsp), %rdi
callq 0x22f0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6ac6
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
movq 0xa4fb(%rip), %rax      # 0x10fc8
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 0xa4b2(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0xa4a6(%rip), %rdi      # 0x10fd0
leaq 0x6fa3(%rip), %rsi      # 0xdad4
movl $0xf, %edx
callq 0x2270
movq 0xa48e(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0xa482(%rip), %rdi      # 0x10fd0
leaq 0x707b(%rip), %rsi      # 0xdbd0
movl $0x1c, %edx
callq 0x2270
movq 0x20(%rbx), %rsi
movq 0xa466(%rip), %rdi      # 0x10fd0
callq 0x2220
movq %rax, %rdi
callq 0x2120
movq 0xa452(%rip), %rdi      # 0x10fd0
callq 0x2120
jmp 0x6bf1
movq 0xa444(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0xa438(%rip), %rdi      # 0x10fd0
leaq 0x6f35(%rip), %rsi      # 0xdad4
movl $0xf, %edx
callq 0x2270
movq 0xa420(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0xa414(%rip), %rdi      # 0x10fd0
leaq 0x702a(%rip), %rsi      # 0xdbed
movl $0x1d, %edx
callq 0x2270
movq 0x28(%rbx), %rsi
movq 0xa3f8(%rip), %rdi      # 0x10fd0
callq 0x2220
movq %rax, %rdi
callq 0x2120
movq 0xa3e4(%rip), %rdi      # 0x10fd0
callq 0x2120
movl $0x1, %edi
callq 0x2290
movq %rax, %rbx
movl $0x70, %esi
movq %r14, %rdi
callq 0x2240
jmp 0x6c25
movq %rax, %rbx
jmp 0x6c6e
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
jmp 0x6c22
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x6c3c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
movq 0xa385(%rip), %rax      # 0x10fc8
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