// File: /workspace/github_cpp_makefile_star2_O2/akinanop[P]mvl-solver/src/GenBenFunc.cc
// Function: GenBenBool(CommandLine*)
void GenBenBool(CommandLine * cline)
{
  //local variables
  int x = 0;
  int y = 0;
  int z = 0;
  int * BSOLN;
  int ** BCLAUSE_LIST;
  srand(time(NULL));

  //Assign Memory to each global variable
  BSOLN = new int[cline->NUM_VAR+1];

  BCLAUSE_LIST = new int *[cline->NUM_CLAUSE];
  for(y=0; y<cline->NUM_CLAUSE; y++)
    BCLAUSE_LIST[y] = new int[cline->CLAUSE_SIZE];

  //Generating solution
  for(x=1; x<cline->NUM_VAR+1; x++)
    {
      if(rand() % 2)
	BSOLN[x] = x;
      else
	BSOLN[x] = -x;
    }

  //Generating clauses
  bool already_exist = false;
  for(y=0; y<cline->NUM_CLAUSE; y++)
    {
      for(x=0; x<cline->CLAUSE_SIZE; x++)
	{
	  //checking if problem needs to be solvable or random
	  //if satisfiable
	  if(cline->SAT_UNSAT && x==0)
	    {
	      //assigning variable to clause
	      if(rand()%2)
		{
		  BCLAUSE_LIST[y][x] = BSOLN[(rand() % cline->NUM_VAR)+1];
		}
	      else
		{
		  BCLAUSE_LIST[y][x] = -1 * BSOLN[(rand() % cline->NUM_VAR)+1];
		}
	    }

	  //else unsatisfiable
	  else
	    {
	      //assigning variable to clause
	      if(rand()%2)
		{
		  BCLAUSE_LIST[y][x] = (rand() % cline->NUM_VAR) + 1;
		}
	      else
		{
		  BCLAUSE_LIST[y][x] = -1 * ((rand() % cline->NUM_VAR) + 1);
		}
	      //checking if variable already exist
	      do
		{
		  already_exist = false;
		  for(z=0; z<x && !already_exist; z++)
		    {
		      if(BCLAUSE_LIST[y][x] == BCLAUSE_LIST[y][z])
			{
			  BCLAUSE_LIST[y][x] = (rand() % cline->NUM_VAR) + 1;
			  already_exist = true;
			}
		      else
			{
			  already_exist = false;
			}
		    }
		}while(already_exist);
	    }
	}
    }

  //opening and writing to outputfile
  ofstream outfile;
  outfile.open(cline->FILE, ios::out);
  if(!outfile)
    {
      cout<<endl;
      cout<<"**** ERROR ****"<<endl;
      cout<<" Could not open output file to write "<<endl;
      cout<<"****       ****"<<endl;
      cout<<endl;
    }
  else
    {
      outfile<<"p cnf "<<cline->NUM_VAR<<" "<<cline->NUM_CLAUSE<<endl;
      for(y=0; y<cline->NUM_CLAUSE; y++)
	{
	  for(x=0; x<cline->CLAUSE_SIZE; x++)
	    {
	      outfile<<BCLAUSE_LIST[y][x]<<" ";
	    }
	  outfile<<"0"<<endl;
	}
    }
  outfile.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
xorl %r13d, %r13d
xorl %edi, %edi
callq 0x21e0
movl %eax, %edi
callq 0x21f0
movslq 0x4(%rbx), %r12
pushq $-0x1
popq %r14
cmpq %r14, %r12
leaq 0x4(,%r12,4), %rdi
cmovlq %r14, %rdi
callq 0x2030
movq %rax, 0x10(%rsp)
movslq 0x8(%rbx), %rbp
leaq (,%rbp,8), %rdi
testq %rbp, %rbp
cmovsq %r14, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
movl $0x0, %r15d
cmovgl %ebp, %r15d
cmpq %r13, %r15
je 0x3a76
movslq 0xc(%rbx), %rax
leaq (,%rax,4), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0x2030
movq 0x8(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
jmp 0x3a4b
pushq $0x1
popq %r14
pushq $-0x1
popq %rbp
movslq %r12d, %rax
cmpq %rax, %r14
jg 0x3aa6
callq 0x20b0
testb $0x1, %al
movl %r14d, %eax
cmovel %ebp, %eax
movq 0x10(%rsp), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movl 0x4(%rbx), %r12d
decl %ebp
jmp 0x3a7d
xorl %r12d, %r12d
movslq 0x8(%rbx), %rax
cmpq %rax, %r12
jge 0x3b6f
xorl %r13d, %r13d
movslq 0xc(%rbx), %rax
cmpq %rax, %r13
jge 0x3b67
movb 0x10(%rbx), %r14b
callq 0x20b0
movl %eax, %ebp
callq 0x20b0
cltd
idivl 0x4(%rbx)
testq %r13, %r13
jne 0x3b0b
testb %r14b, %r14b
je 0x3b0b
movslq %edx, %rax
movq 0x10(%rsp), %rcx
movl 0x4(%rcx,%rax,4), %eax
movl %eax, %ecx
negl %ecx
testb $0x1, %bpl
cmovnel %eax, %ecx
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rax
movl %ecx, (%rax)
incq %r13
jmp 0x3ab9
leal 0x1(%rdx), %eax
testb $0x1, %bpl
notl %edx
cmovnel %eax, %edx
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rbp
movl %edx, (%rbp,%r13,4)
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq %r13, %r15
jae 0x3b36
testb $0x1, %r14b
je 0x3b47
xorl %r15d, %r15d
testb $0x1, %r14b
movl $0x0, %r14d
jne 0x3b2b
jmp 0x3b06
cmpl (%rbp,%r15,4), %edx
sete %r14b
jne 0x3b62
callq 0x20b0
cltd
idivl 0x4(%rbx)
incl %edx
movl %edx, (%rbp,%r13,4)
incq %r15
jmp 0x3b2b
incq %r12
jmp 0x3aa9
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x2370
movq 0x20(%rbx), %rsi
pushq $0x10
popq %rdx
movq %r15, %rdi
callq 0x2060
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x38(%rsp,%rax)
je 0x3c2e
movq 0xb42a(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0xb41e(%rip), %rdi      # 0xefd0
leaq 0x6f1b(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xb403(%rip), %rdi      # 0xefd0
leaq 0x6f10(%rip), %rsi      # 0xaae4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xb3e8(%rip), %rdi      # 0xefd0
leaq 0x6f1b(%rip), %rsi      # 0xab0a
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0xb3cd(%rip), %rdi      # 0xefd0
callq 0x2140
leaq 0x18(%rsp), %rdi
callq 0x22e0
leaq 0x18(%rsp), %rdi
callq 0x2300
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6ee5(%rip), %rsi      # 0xab1a
leaq 0x18(%rsp), %rdi
callq 0x2210
movl 0x4(%rbx), %esi
movq %rax, %rdi
callq 0x2380
leaq 0x65f6(%rip), %rsi      # 0xa247
movq %rax, %rdi
callq 0x2210
movl 0x8(%rbx), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
callq 0x2140
xorl %ebp, %ebp
leaq 0x18(%rsp), %r15
leaq 0x65cd(%rip), %r12      # 0xa247
leaq 0x6f4e(%rip), %r13      # 0xabcf
movslq 0x8(%rbx), %rax
cmpq %rax, %rbp
jge 0x3c08
xorl %r14d, %r14d
movslq 0xc(%rbx), %rax
cmpq %rax, %r14
jge 0x3cbf
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%r14,4), %esi
movq %r15, %rdi
callq 0x2380
movq %rax, %rdi
movq %r12, %rsi
callq 0x2210
incq %r14
jmp 0x3c91
movq %r15, %rdi
movq %r13, %rsi
callq 0x2210
movq %rax, %rdi
callq 0x2140
incq %rbp
jmp 0x3c81
jmp 0x3cdb
jmp 0x3cdb
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2300
movq %rbx, %rdi
callq 0x2390