// File: /workspace/github_cpp_makefile_star2_O2/akinanop[P]mvl-solver/src/LinearEncode.cc
// Function: LinearEncode(CommandLine*)
void LinearEncode(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 new_num_clause = 0;
  int new_num_var = 0;
  int * domain;
  int * index;
  int var, val;
  int temp;
  bool isdone = false;
  int count = 0;
  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 : "<<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 : "<<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_var = var;
	  num_clause = val;
	  domain = new int[var+1];
	  index = new int[var+1];
	  //default initialization to domain value 2
	  for(int i=0; i<var+1; i++)
	    domain[i] = 2;
	}
      //else if line is d var# domsize
      else if(line_buffer[0] == 'd')
	{
	  temp = sscanf(line_buffer, "d %d %d", &var, &val);
	  count++;
	  new_num_var += 2*val-1;
	  new_num_clause += 4*(val-2)+3+1;
	  domain[var] = val;
	}
      //else its variables
      else
	{
	  if(!isdone)
	    {
	      isdone = true;
	      new_num_clause += num_clause;
	      new_num_var += (num_var - count);
	      outfile<<"p cnf "<<new_num_var<<" "<<new_num_clause<<endl;
	      //compute the index values for each variable=domainvalue
	      index[1] = 1;
	      for(int i=2; i<num_var+1; i++)
		{
		  if(domain[i-1] == 2)
		    index[i] = index[i-1]+1;
		  else
		    index[i] = 2*domain[i-1] -1 + index[i-1];
		}
	      int var_t1 = 0;
	      int var_t2 = 0;
	      //add the extra clauses
	      for(int i=0; i<num_var+1; i++)
		{
		  if(domain[i] > 2)
		    {
		      //write the ALO condition
		      for(int j=0; j<domain[i]; j++)
			{
			  outfile<<index[i]+j<<" ";
			}
		      outfile<<" 0"<<endl;
		      var_t1 = index[i]+domain[i];
		      var_t2 = index[i];
		      outfile<<-(var_t1)<<" "<<(var_t2)<<" 0"<<endl;
		      outfile<<var_t1<<" "<<-(var_t2)<<" 0"<<endl;
		      for(int j=0; j<domain[i]-2; j++)
			{
			  outfile<<-(var_t1+1+j)<<" "<<(var_t1+j)<<" "<<var_t2+1+j<<" 0"<<endl;
			  outfile<<var_t1+1+j<<" "<<-(var_t1+j)<<" 0"<<endl;
			  outfile<<var_t1+1+j<<" "<<-(var_t2+1+j)<<" 0"<<endl;
			}
		      for(int j=0; j<domain[i]-1; j++)
			{
			  var_t1=-(index[i]+domain[i]+j);
			  var_t2=-(index[i]+1+j);
			  outfile<<var_t1<<" "<<var_t2<<" 0\n";
			}
		    }
		  else
		    {
		      //do nothing
		      ;
		    }
		}
	    }

	  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(ch == '!')
		    temp = sscanf(word_buffer, "%d %c %c %d", &var, &ch, &ch1, &val);
		  //checking if variable = 0 i.e end of line
		  if(var != 0)
		    {
		      if(domain[var] > 2)
			{
			  var = index[var] + val;
			  if(ch == '!')
			    outfile<<"-"<<var<<" ";
			  else
			    outfile<<var<<" ";
			}
		      else
			{
			  if(val == 1)
			    {
			      var = index[var];
			      outfile<<var<<" ";
			    }
			  else
			    {
			      var = -index[var];
			      outfile<<var<<" ";
			    }
			}
		    }
		}
	    }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 0x2250
leaq 0x58(%rsp), %rdi
callq 0x2370
movq 0x20(%rbx), %rsi
leaq 0x258(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2190
movq 0x258(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x278(%rsp,%rax)
jne 0x4e8c
movq 0x28(%rbx), %rsi
leaq 0x58(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2060
movq 0x58(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
jne 0x4edc
leaq 0x258(%rsp), %r12
leaq 0x1020(%rsp), %r13
leaq 0x8(%rsp), %rbp
movl $0x0, 0x30(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x34(%rsp)
movq %r14, 0x50(%rsp)
movslq %r14d, %rbx
movl $0xbb8, %edx            # imm = 0xBB8
movq %r12, %rdi
movq %r13, %rsi
callq 0x20e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4e4c
movzbl 0x1020(%rsp), %eax
cmpl $0x63, %eax
je 0x48ab
cmpl $0x64, %eax
je 0x499f
cmpl $0x70, %eax
je 0x4dda
testb $0x1, %r15b
movq %rbx, 0x48(%rsp)
jne 0x4c8a
leaq 0x58(%rsp), %rdi
leaq 0x6213(%rip), %rsi      # 0xab1a
callq 0x2210
movq 0x50(%rsp), %rcx
movl %ecx, %r14d
subl 0x34(%rsp), %r14d
addl 0x20(%rsp), %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5914(%rip), %rsi      # 0xa247
callq 0x2210
movq 0x38(%rsp), %rsi
addl 0x30(%rsp), %esi
movq %rax, %rdi
movq %rsi, 0x38(%rsp)
callq 0x2380
movq %rax, %rdi
callq 0x2140
movq 0x10(%rsp), %rax
movl $0x1, 0x4(%rax)
pushq $0x2
popq %rax
cmpq %rbx, %rax
jg 0x49ef
movq 0x18(%rsp), %rcx
movl -0x4(%rcx,%rax,4), %ecx
cmpl $0x2, %ecx
jne 0x4989
movq 0x10(%rsp), %rsi
movl -0x4(%rsi,%rax,4), %ecx
incl %ecx
jmp 0x4997
movq 0x10(%rsp), %rsi
movl -0x4(%rsi,%rax,4), %edx
leal (%rdx,%rcx,2), %ecx
decl %ecx
movl %ecx, (%rsi,%rax,4)
incq %rax
jmp 0x4965
movq %r13, %rdi
leaq 0x621d(%rip), %rsi      # 0xabc6
movq %rbp, %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x2200
incl 0x34(%rsp)
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
leal (%rcx,%rax,2), %ecx
decl %ecx
movq %rcx, 0x20(%rsp)
movq 0x38(%rsp), %rcx
leal -0x4(%rcx,%rax,4), %ecx
movq %rcx, 0x38(%rsp)
movslq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
jmp 0x48ab
movl %r14d, 0x44(%rsp)
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x4c81
movq 0x18(%rsp), %r15
movl (%r15,%r14,4), %eax
cmpl $0x3, %eax
jl 0x4c5f
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
cmpl %eax, %ebx
jge 0x4a44
movl (%r12,%r14,4), %esi
addl %ebx, %esi
leaq 0x58(%rsp), %rdi
callq 0x2380
movq %rax, %rdi
leaq 0x5810(%rip), %rsi      # 0xa247
callq 0x2210
incl %ebx
movl (%r15,%r14,4), %eax
jmp 0x4a19
leaq 0x58(%rsp), %rdi
leaq 0x617e(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
movl (%r12,%r14,4), %eax
movl (%r15,%r14,4), %ebp
movq %rax, %rbx
leal (%rax,%rbp), %r13d
movl %r13d, %esi
negl %esi
leaq 0x58(%rsp), %rdi
movl %esi, 0x20(%rsp)
callq 0x2380
movq %rax, %rdi
leaq 0x57be(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x612c(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
leaq 0x58(%rsp), %rdi
movl %r13d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5781(%rip), %rsi      # 0xa247
callq 0x2210
movl %ebx, %esi
negl %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
leaq 0x60ed(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq %rbx, %rax
incl %ebx
notl %eax
movq %rax, %r15
movl %eax, %r12d
subl %ebp, %r12d
xorl %ebp, %ebp
movq 0x18(%rsp), %rax
movl (%rax,%r14,4), %eax
leal -0x2(%rax), %ecx
cmpl %ecx, %ebp
jge 0x4bfe
leaq 0x58(%rsp), %rdi
movl %r12d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x571c(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl %r13d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5702(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x6070(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
incl %r13d
leaq 0x58(%rsp), %rdi
movl %r13d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x56c2(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl 0x20(%rsp), %esi
callq 0x2380
movq %rax, %rdi
leaq 0x602e(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
leaq 0x58(%rsp), %rdi
movl %r13d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5683(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl %r15d, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5ff0(%rip), %rsi      # 0xabce
callq 0x2210
movq %rax, %rdi
callq 0x2140
incl %ebp
decl %r12d
decl 0x20(%rsp)
decl %r15d
incl %ebx
jmp 0x4b00
xorl %ebp, %ebp
pushq $-0x1
popq %r15
movq 0x18(%rsp), %r12
leal -0x1(%rax), %ecx
cmpl %ecx, %ebp
jge 0x4c5f
movl %r15d, %ebx
movq 0x10(%rsp), %rcx
subl (%rcx,%r14,4), %ebx
movl %ebx, %esi
subl %eax, %esi
incl %esi
leaq 0x58(%rsp), %rdi
callq 0x2380
movq %rax, %rdi
leaq 0x5611(%rip), %rsi      # 0xa247
callq 0x2210
movq %rax, %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rdi
leaq 0x5f82(%rip), %rsi      # 0xabd1
callq 0x2210
incl %ebp
movl (%r12,%r14,4), %eax
decl %r15d
jmp 0x4c09
incq %r14
leaq 0x258(%rsp), %r12
leaq 0x1020(%rsp), %r13
leaq 0x8(%rsp), %rbp
movq 0x48(%rsp), %rbx
jmp 0x49f7
movl 0x44(%rsp), %eax
movq %rax, 0x20(%rsp)
movq %r13, %rbx
movzbl (%rbx), %eax
cmpl $0x9, %eax
je 0x4c9a
cmpl $0x20, %eax
jne 0x4c9f
incq %rbx
jmp 0x4c8d
xorl %r15d, %r15d
cmpb $0x20, %al
ja 0x4cb9
movzbl %al, %ecx
movabsq $0x100000601, %rdx      # imm = 0x100000601
btq %rcx, %rdx
jb 0x4ccb
movb %al, 0x460(%rsp,%r15)
movb 0x1(%rbx,%r15), %al
incq %r15
jmp 0x4ca2
movb $0x0, 0x460(%rsp,%r15)
cmpb $0x0, 0x460(%rsp)
je 0x4da8
leaq 0x460(%rsp), %rdi
leaq 0x5ee4(%rip), %rsi      # 0xabd5
movq %rbp, %rdx
leaq 0x7(%rsp), %rcx
leaq 0xc(%rsp), %r8
xorl %eax, %eax
callq 0x2200
cmpb $0x21, 0x7(%rsp)
jne 0x4d34
leaq 0x460(%rsp), %rdi
leaq 0x5ec3(%rip), %rsi      # 0xabde
movq %rbp, %rdx
leaq 0x7(%rsp), %rcx
leaq 0x2f(%rsp), %r8
leaq 0xc(%rsp), %r9
xorl %eax, %eax
callq 0x2200
movslq 0x8(%rsp), %rax
testq %rax, %rax
je 0x4da8
movq 0x18(%rsp), %rcx
cmpl $0x3, (%rcx,%rax,4)
jl 0x4d7a
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rcx
addl (%rcx,%rax,4), %esi
movl %esi, 0x8(%rsp)
cmpb $0x21, 0x7(%rsp)
leaq 0x58(%rsp), %rdi
jne 0x4d94
leaq 0x5e7e(%rip), %rsi      # 0xabea
callq 0x2210
movq %rax, %rdi
movl 0x8(%rsp), %esi
jmp 0x4d94
cmpl $0x1, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movl (%rcx,%rax,4), %esi
je 0x4d8b
negl %esi
movl %esi, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x2380
movq %rax, %rdi
leaq 0x54a4(%rip), %rsi      # 0xa247
callq 0x2210
addq %r15, %rbx
cmpb $0x0, (%rbx)
jne 0x4c8d
leaq 0x58(%rsp), %rdi
leaq 0x5e0f(%rip), %rsi      # 0xabcf
callq 0x2210
movq 0x48(%rsp), %rbx
movb $0x1, %r15b
movq %rax, %rdi
callq 0x2140
jmp 0x48ab
movq %r13, %rdi
leaq 0x5dd6(%rip), %rsi      # 0xabba
movq %rbp, %rdx
leaq 0xc(%rsp), %rcx
xorl %eax, %eax
callq 0x2200
movslq 0x8(%rsp), %rax
cmpq $-0x1, %rax
movl 0xc(%rsp), %ecx
movl %ecx, 0x30(%rsp)
movq %rax, %r14
leaq 0x4(,%rax,4), %rbx
pushq $-0x1
popq %rax
cmovlq %rax, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x2030
movq %rax, 0x10(%rsp)
xorl %eax, %eax
cmpq %r14, %rax
jg 0x48a3
movq 0x18(%rsp), %rcx
movl $0x2, (%rcx,%rax,4)
incq %rax
jmp 0x4e32
leaq 0x258(%rsp), %rdi
callq 0x2090
leaq 0x58(%rsp), %rdi
callq 0x22e0
leaq 0x58(%rsp), %rdi
callq 0x2300
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 0xa13d(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0xa131(%rip), %rdi      # 0xefd0
leaq 0x5c2e(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xa116(%rip), %rdi      # 0xefd0
leaq 0x5cbe(%rip), %rsi      # 0xab7f
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xa0fb(%rip), %rdi      # 0xefd0
callq 0x2140
jmp 0x4f2a
movq 0xa0ed(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0xa0e1(%rip), %rdi      # 0xefd0
leaq 0x5bde(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xa0c6(%rip), %rdi      # 0xefd0
leaq 0x5c8b(%rip), %rsi      # 0xab9c
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xa0ab(%rip), %rdi      # 0xefd0
callq 0x2140
pushq $0x1
popq %rdi
callq 0x2280
movq %rax, %rbx
jmp 0x4f56
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
jmp 0x4f49
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2300
leaq 0x258(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2390
nop