// File: akinanop[P]mvl-solver/src/Boolean2Finite.cc
// Function: Boolean2Finite(CommandLine*)
void Boolean2Finite(CommandLine * cline)
{
  //local variables
  ifstream infile;
  ofstream outfile;
  char line_buffer[MAX_LINE];
  char word_buffer[MAX_LINE];
  int line_num = 0;
  int variable = 0;
  set<int> variables;
  vector <vector<int> > data;

  //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 and checking if it opens
  outfile.open(cline->MODEL_FILE, ios::out);
  if(!outfile)
    {
      cout<<endl;
      cout<<"**** ERROR ****"<<endl;
      cout<<"Could not open input 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, MAX_LINE))
    {
      ++line_num;
      //if line is comment - just write
      if(line_buffer[0] == 'c')
	{
	  outfile.write(line_buffer, strlen(line_buffer));
	  outfile.put('\n');
	}
      //else if line is p cnf var_num clause_num - just write
      else if(line_buffer[0] == 'p')
	{
	  outfile.write(line_buffer, strlen(line_buffer));
	  outfile.put('\n');
	}
      //else its variables
      else
	{
	  char * lp = line_buffer;
	  vector <int> temp;
	  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)
		{
		  variable = atoi(word_buffer);
		  variables.insert(abs(variable));
		  //checking if variable = 0 i.e end of line
		  if(variable != 0)
		    {
		      //checking for sign
		      if(variable < 0)
			{
			  //variable = -variable;
			  //outfile<<variable<<"=0 ";
			  temp.push_back(variable);
			}
		      else
			{
			  //outfile<<variable<<"=1 ";
			  temp.push_back(variable);
			}
		    }
		  //else print newline in outfile
		  else
		    {
		      //outfile.put('\n');
		    }
		}
	    }while(*lp);
	  data.push_back(temp);
	}
    }

  //now writing back the domain information
  set<int>::iterator start = variables.begin();
  set<int>::iterator end = variables.end();
  end--;
  for( ; start != end; )
    {
      ++start;
      outfile<<"d "<<*start<<" 2"<<endl;
    }

  int ovsize = data.size();
  int uvsize = 0;
  int var = 0;
  for(int y=0; y<ovsize; y++)
    {
      uvsize = data[y].size();
      for(int x=0; x<uvsize; x++)
	{
	  var = data[y][x];
	  if(var < 0 )
	    {
	      var = -var;
	      outfile<<var<<"=0 ";
	    }
	  else
	    {
	      outfile<<var<<"=1 ";
	    }
	}
      outfile<<endl;
    }
  //closing file
  infile.close();
  outfile.close();
}

// Assembly:
subq $0x1ce8, %rsp           # imm = 0x1CE8
movq %rdi, 0x1ce0(%rsp)
leaq 0x1ad8(%rsp), %rdi
callq 0x22b0
leaq 0x18d8(%rsp), %rdi
callq 0x23f0
jmp 0x5a3b
movl $0x0, 0x14c(%rsp)
movl $0x0, 0x148(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x62b0
leaq 0x100(%rsp), %rdi
callq 0x62c0
movq 0x1ce0(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x1ad8(%rsp), %rdi
movl $0x8, %edx
callq 0x2180
jmp 0x5a8b
movq 0x1ad8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x1ad8(%rsp,%rax), %rdi
callq 0x2130
movb %al, 0x93(%rsp)
jmp 0x5aad
movb 0x93(%rsp), %al
testb $0x1, %al
jne 0x5abd
jmp 0x5b89
movq 0x1850c(%rip), %rdi     # 0x1dfd0
movq 0x184f5(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5ad2
movq 0x184f7(%rip), %rdi     # 0x1dfd0
leaq 0x11ff4(%rip), %rsi     # 0x17ad4
callq 0x2220
movq %rax, 0x88(%rsp)
jmp 0x5aef
movq 0x88(%rsp), %rdi
movq 0x184c2(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5b05
movq 0x184c4(%rip), %rdi     # 0x1dfd0
leaq 0x12011(%rip), %rsi     # 0x17b24
callq 0x2220
movq %rax, 0x80(%rsp)
jmp 0x5b22
movq 0x80(%rsp), %rdi
movq 0x1848f(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5b38
movq 0x18491(%rip), %rdi     # 0x1dfd0
movq 0x1847a(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5b4d
movl $0x1, %edi
callq 0x22e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18d0(%rsp)
movl %eax, 0x18cc(%rsp)
jmp 0x6288
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18d0(%rsp)
movl %eax, 0x18cc(%rsp)
jmp 0x6261
movq 0x1ce0(%rsp), %rax
movq 0x28(%rax), %rsi
leaq 0x18d8(%rsp), %rdi
movl $0x10, %edx
callq 0x2060
jmp 0x5ba9
movq 0x18d8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x18d8(%rsp,%rax), %rdi
callq 0x2130
movb %al, 0x7f(%rsp)
jmp 0x5bc8
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x5bd5
jmp 0x5c63
movq 0x183f4(%rip), %rdi     # 0x1dfd0
movq 0x183dd(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5bea
movq 0x183df(%rip), %rdi     # 0x1dfd0
leaq 0x11edc(%rip), %rsi     # 0x17ad4
callq 0x2220
movq %rax, 0x70(%rsp)
jmp 0x5c04
movq 0x70(%rsp), %rdi
movq 0x183b0(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5c17
movq 0x183b2(%rip), %rdi     # 0x1dfd0
leaq 0x11eff(%rip), %rsi     # 0x17b24
callq 0x2220
movq %rax, 0x68(%rsp)
jmp 0x5c31
movq 0x68(%rsp), %rdi
movq 0x18383(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5c44
movq 0x18385(%rip), %rdi     # 0x1dfd0
movq 0x1836e(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x5c59
movl $0x1, %edi
callq 0x22e0
jmp 0x5c65
leaq 0x1ad8(%rsp), %rdi
leaq 0xd10(%rsp), %rsi
movl $0xbb8, %edx            # imm = 0xBB8
callq 0x20f0
movq %rax, 0x60(%rsp)
jmp 0x5c86
movq 0x60(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2300
movb %al, 0x5f(%rsp)
jmp 0x5ca0
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x5cad
jmp 0x5fb9
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
movsbl 0xd10(%rsp), %eax
cmpl $0x63, %eax
jne 0x5d0d
leaq 0xd10(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x2120
movq 0x50(%rsp), %rsi
movq %rax, %rdx
leaq 0x18d8(%rsp), %rdi
callq 0x2320
jmp 0x5cf4
leaq 0x18d8(%rsp), %rdi
movl $0xa, %esi
callq 0x2040
jmp 0x5d08
jmp 0x5fb4
movsbl 0xd10(%rsp), %eax
cmpl $0x70, %eax
jne 0x5d5c
leaq 0xd10(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2120
movq 0x48(%rsp), %rsi
movq %rax, %rdx
leaq 0x18d8(%rsp), %rdi
callq 0x2320
jmp 0x5d43
leaq 0x18d8(%rsp), %rdi
movl $0xa, %esi
callq 0x2040
jmp 0x5d57
jmp 0x5fb2
leaq 0xd10(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x62d0
leaq 0x150(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0xf8(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x47(%rsp)
je 0x5dcf
movq 0xf8(%rsp), %rax
movsbl (%rax), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, 0x46(%rsp)
je 0x5dc7
movq 0xf8(%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 0x5dd9
jmp 0x5def
movq 0xf8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf8(%rsp)
jmp 0x5d89
jmp 0x5df1
movq 0xf8(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x45(%rsp)
je 0x5e45
movq 0xf8(%rsp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x20, %ecx
movb %al, 0x45(%rsp)
je 0x5e45
movq 0xf8(%rsp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x9, %ecx
movb %al, 0x45(%rsp)
je 0x5e45
movq 0xf8(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
setne %al
movb %al, 0x45(%rsp)
movb 0x45(%rsp), %al
testb $0x1, %al
jne 0x5e4f
jmp 0x5e86
movq 0xf8(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0xf8(%rsp)
movb (%rax), %cl
movq 0xd8(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0xd8(%rsp)
movb %cl, (%rax)
jmp 0x5df1
movq 0xd8(%rsp), %rax
movb $0x0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0x2120
cmpq $0x0, %rax
je 0x5f7b
leaq 0x150(%rsp), %rdi
callq 0x23a0
movl %eax, 0x148(%rsp)
movl 0x148(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, 0xd4(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0xd4(%rsp), %rsi
callq 0x62e0
movb %dl, 0x37(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x5ef1
movb 0x37(%rsp), %al
movq 0x38(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movb %al, 0xc8(%rsp)
cmpl $0x0, 0x148(%rsp)
je 0x5f77
cmpl $0x0, 0x148(%rsp)
jge 0x5f5c
leaq 0xe0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6340
jmp 0x5f34
jmp 0x5f75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x18d0(%rsp)
movl %eax, 0x18cc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x6420
jmp 0x6261
leaq 0xe0(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x6340
jmp 0x5f73
jmp 0x5f75
jmp 0x5f79
jmp 0x5f79
jmp 0x5f7b
jmp 0x5f7d
movq 0xf8(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x5d79
leaq 0x100(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x63b0
jmp 0x5fa5
leaq 0xe0(%rsp), %rdi
callq 0x6420
jmp 0x5fb4
jmp 0x5c65
leaq 0x118(%rsp), %rdi
callq 0x6480
movq %rax, 0xb8(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x64b0
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x64e0
movq %rax, 0xa8(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x6520
testb $0x1, %al
jne 0x6015
jmp 0x608d
leaq 0xb8(%rsp), %rdi
callq 0x6540
leaq 0x11af8(%rip), %rsi     # 0x17b21
leaq 0x18d8(%rsp), %rdi
callq 0x2220
movq %rax, 0x28(%rsp)
jmp 0x603d
leaq 0xb8(%rsp), %rdi
callq 0x6570
movq 0x28(%rsp), %rdi
movl (%rax), %esi
callq 0x2400
movq %rax, 0x20(%rsp)
jmp 0x605d
movq 0x20(%rsp), %rdi
leaq 0x11ad5(%rip), %rsi     # 0x17b3e
callq 0x2220
movq %rax, 0x18(%rsp)
jmp 0x6075
movq 0x18(%rsp), %rdi
movq 0x17f3f(%rip), %rsi     # 0x1dfc0
callq 0x2260
jmp 0x6088
jmp 0x5ffa
leaq 0x100(%rsp), %rdi
callq 0x6590
movl %eax, 0xa4(%rsp)
movl $0x0, 0xa0(%rsp)
movl $0x0, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jge 0x6207
movslq 0x98(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x65b0
movq %rax, %rdi
callq 0x65d0
movl %eax, 0xa0(%rsp)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jge 0x61d9
movslq 0x98(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x65b0
movq %rax, %rdi
movslq 0x94(%rsp), %rsi
callq 0x65f0
movl (%rax), %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, 0x9c(%rsp)
jge 0x6191
movl 0x9c(%rsp), %eax
negl %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %esi
leaq 0x18d8(%rsp), %rdi
callq 0x2400
movq %rax, 0x10(%rsp)
jmp 0x617c
movq 0x10(%rsp), %rdi
leaq 0x119b9(%rip), %rsi     # 0x17b41
callq 0x2220
jmp 0x618f
jmp 0x61c1
movl 0x9c(%rsp), %esi
leaq 0x18d8(%rsp), %rdi
callq 0x2400
movq %rax, 0x8(%rsp)
jmp 0x61ac
movq 0x8(%rsp), %rdi
leaq 0x1198d(%rip), %rsi     # 0x17b45
callq 0x2220
jmp 0x61bf
jmp 0x61c1
jmp 0x61c3
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x6105
movq 0x17de0(%rip), %rsi     # 0x1dfc0
leaq 0x18d8(%rsp), %rdi
callq 0x2260
jmp 0x61ef
jmp 0x61f1
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x60c2
leaq 0x1ad8(%rsp), %rdi
callq 0x20a0
jmp 0x6216
leaq 0x18d8(%rsp), %rdi
callq 0x2360
jmp 0x6225
leaq 0x100(%rsp), %rdi
callq 0x6610
leaq 0x118(%rsp), %rdi
callq 0x6670
leaq 0x18d8(%rsp), %rdi
callq 0x2380
leaq 0x1ad8(%rsp), %rdi
callq 0x20b0
addq $0x1ce8, %rsp           # imm = 0x1CE8
retq
leaq 0x100(%rsp), %rdi
callq 0x6610
leaq 0x118(%rsp), %rdi
callq 0x6670
leaq 0x18d8(%rsp), %rdi
callq 0x2380
leaq 0x1ad8(%rsp), %rdi
callq 0x20b0
movq 0x18d0(%rsp), %rdi
callq 0x2410
nopw %cs:(%rax,%rax)
nopl (%rax)