// File: /workspace/github_cpp_makefile_star2_O2/akinanop[P]mvl-solver/src/Formula.cc
// Function: Formula::BuildFormula(CommandLine*)
void Formula::BuildFormula ( CommandLine* cline ) {
	//local variables
	ifstream infile;
	char line_buffer[3000];
	char word_buffer[3000];
	int clause_num = 0;
	int atom_num = 0;
	int var, val;
	char ch;
	char ch1;
	string str;
	Variable * temp_var = NULL;
	Literal temp_atom;
	Clause * temp_clause = NULL;

	//to start variable from 1 and not from 0 we need this
	VARLIST.push_back( new Variable(0, 0));
	VARLIST[0]->VAL = 0;

	TIME_S = GetTime();
	//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);
	}

	//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))
	{
		temp_clause = new Clause(cline->CLAUSE_SIZE);
		//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')
		{
			sscanf(line_buffer, "p cnf %d %d", &var, &val);

		}
		//else if line is d var# domsize
		else if(line_buffer[0] == 'd')
		{
			sscanf(line_buffer, "d %d %d", &var, &val);
			temp_var = new Variable(var, val);
			VARLIST.push_back(temp_var);
		}
		//else its clause lines
		else
		{
			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)
				{
					sscanf(word_buffer, "%d %c %d", &var, &ch, &val);
					if(ch == '!')
						sscanf(word_buffer, "%d %c %c %d", &var, &ch, &ch1, &val);
					//checking if variable = 0 i.e end of line
					if(var != 0)
					{
						//add it to clause_list's atom_list
						temp_atom = Literal(var, ch, val);
						temp_clause->addAtom(temp_atom);

						//increment the index representing value so that
						//we can keep track of number of occurrences of each
						//variable and its domain value, if ch is = then increment
						//the value count, else increment others count
						if(ch == '=')
						{
							if (heuristic==Heuristic::VSIDS || heuristic==Heuristic::VSIDS_pos)
								VARLIST[var]->VSIDS_SCORE[val]++;
							else if (heuristic == Heuristic::BK)
								VARLIST[var]->ATOMCNTPOS[val]++;
							if (!WATCH)
								VARLIST[var]->ATOMRECPOS[val].push_back(clause_num);
						}
						else
						{
							if (heuristic==Heuristic::VSIDS) {
								VARLIST[var]->VSIDS_SCORE_NEG[val]++;
							} else if (heuristic == Heuristic::BK)
								VARLIST[var]->ATOMCNTNEG[val]++;
							else if (heuristic == Heuristic::VSIDS_pos) {
								for (int j=0;j<VARLIST[var] -> DOMAINSIZE;j++) {
									if (j!=val)
										VARLIST[var]->VSIDS_SCORE[j]++;
								}
							}
							if (!WATCH)
								VARLIST[var]->ATOMRECNEG[val].push_back(clause_num);
						}
						atom_num++;
					}
				}
			}while(*lp);
			CLAUSELIST.push_back(temp_clause);

			if (temp_clause->ATOM_LIST.size() == 1) {
				UNITLIST.push_back(clause_num);
			}

			// with watched literals option, assign first two literals in the clause to watched1 and watched2


			if ( WATCH ) {

				Literal watched1 = temp_clause -> ATOM_LIST[0];
				temp_clause -> WATCHED[0] = watched1;
				VARLIST[watched1.VAR]->addRecord(clause_num, watched1.VAL, watched1.EQUAL);

				if ( temp_clause -> ATOM_LIST.size() > 1 ) {

					Literal watched2 = temp_clause -> ATOM_LIST[1];
					temp_clause -> WATCHED[1] = watched2;
					VARLIST[watched2.VAR]->addRecord(clause_num, watched2.VAL, watched2.EQUAL);

				}
			}


			atom_num = 0;
			++clause_num;
		}
	}

	//closing file
	infile.close();
	TIME_E = GetTime();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x19e8, %rsp           # imm = 0x19E8
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
leaq 0x68(%rsp), %rdi
callq 0x2250
andl $0x0, 0x10(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
andq $0x0, 0x28(%rsp)
movb $0x0, (%rax)
andq $0x0, 0x18(%rsp)
pushq $0x70
popq %rdi
callq 0x2220
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x5b6c
leaq 0x40(%rsp), %rsi
movq %r14, (%rsi)
movq %rbx, %rdi
callq 0x90ea
movq (%rbx), %rax
movq (%rax), %rax
andl $0x0, 0x4(%rax)
callq 0x290a
movsd %xmm0, 0x38(%rbx)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x68(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x2190
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0x6546
leaq 0x18(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x60(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r12
movabsq $0x100000601, %rbp      # imm = 0x100000601
movl $0xbb8, %edx            # imm = 0xBB8
leaq 0x68(%rsp), %rdi
leaq 0xe30(%rsp), %rsi
callq 0x20e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x650c
pushq $0x38
popq %rdi
callq 0x2220
movq %rax, %r13
movq 0x20(%rsp), %rax
movl 0xc(%rax), %esi
movq %r13, %rdi
callq 0x577e
movq %r13, 0x18(%rsp)
movzbl 0xe30(%rsp), %eax
cmpl $0x63, %eax
je 0x616d
cmpl $0x64, %eax
je 0x61eb
cmpl $0x70, %eax
jne 0x6239
leaq 0xe30(%rsp), %rdi
leaq 0x49de(%rip), %rsi      # 0xabba
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2200
jmp 0x616d
leaq 0xe30(%rsp), %rdi
leaq 0x49cc(%rip), %rsi      # 0xabc6
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2200
pushq $0x70
popq %rdi
callq 0x2220
movq %rax, %r13
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movq %rax, %rdi
callq 0x5b6c
movq %r13, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x556c
jmp 0x616d
leaq 0xe30(%rsp), %r13
cmpb $0x9, %al
je 0x624d
movzbl %al, %ecx
cmpl $0x20, %ecx
jne 0x6256
movb 0x1(%r13), %al
incq %r13
jmp 0x6241
xorl %r14d, %r14d
cmpb $0x20, %al
ja 0x6266
movzbl %al, %ecx
btq %rcx, %rbp
jb 0x6278
movb %al, 0x270(%rsp,%r14)
movb 0x1(%r13,%r14), %al
incq %r14
jmp 0x6259
movb $0x0, 0x270(%rsp,%r14)
cmpb $0x0, 0x270(%rsp)
je 0x6449
leaq 0x270(%rsp), %rdi
leaq 0x4937(%rip), %rsi      # 0xabd5
movq %r15, %rdx
leaq 0x7(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2200
cmpb $0x21, 0x7(%rsp)
jne 0x62dd
leaq 0x270(%rsp), %rdi
leaq 0x4918(%rip), %rsi      # 0xabde
movq %r15, %rdx
leaq 0x7(%rsp), %rcx
leaq 0x17(%rsp), %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x2200
movl 0xc(%rsp), %eax
testq %rax, %rax
je 0x6449
xorl %edx, %edx
cmpb $0x3d, 0x7(%rsp)
sete %dl
movl 0x8(%rsp), %esi
shlq $0x20, %rsi
orq %rax, %rsi
movq 0x18(%rsp), %rdi
callq 0x57f2
cmpb $0x3d, 0x7(%rsp)
movl 0xa8(%rbx), %eax
jne 0x6358
testl %eax, %eax
je 0x63df
cmpl $0x3, %eax
je 0x632c
cmpl $0x1, %eax
jne 0x63f7
movslq 0xc(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x38(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x48c2(%rip), %xmm0     # 0xac10
movsd %xmm0, (%rax,%rcx,8)
jmp 0x63f7
testl %eax, %eax
je 0x6402
cmpl $0x1, %eax
je 0x63b6
cmpl $0x3, %eax
jne 0x641a
movslq 0xc(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x8(%rax), %ecx
movl 0x8(%rsp), %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x641a
cmpq %rsi, %rdx
je 0x63b1
movq 0x38(%rax), %rdi
movsd (%rdi,%rsi,8), %xmm0
addsd 0x4864(%rip), %xmm0     # 0xac10
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
jmp 0x638d
movslq 0xc(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x40(%rax), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x4838(%rip), %xmm0     # 0xac10
movsd %xmm0, (%rax,%rcx,8)
jmp 0x641a
movslq 0xc(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x28(%rax), %rax
movslq 0x8(%rsp), %rcx
incl (%rax,%rcx,4)
cmpb $0x0, 0xa5(%rbx)
pushq $0x60
jmp 0x6423
movslq 0xc(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x30(%rax), %rax
movslq 0x8(%rsp), %rcx
incl (%rax,%rcx,4)
cmpb $0x0, 0xa5(%rbx)
pushq $0x68
popq %rax
jne 0x6449
movslq 0xc(%rsp), %rcx
movq (%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movslq 0x8(%rsp), %rdx
imulq $0x18, %rdx, %rdi
addq (%rcx,%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4170
addq %r14, %r13
movb (%r13), %al
testb %al, %al
jne 0x6241
movq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8b86
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0xc, %rcx
jne 0x6488
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x8ba4
cmpb $0x1, 0xa5(%rbx)
jne 0x6503
movq 0x18(%rsp), %rax
movq (%rax), %rcx
movslq (%rcx), %rsi
movl 0x4(%rcx), %edx
movzbl 0x8(%rcx), %ecx
movl %esi, 0x20(%rax)
movl %edx, 0x24(%rax)
movb %cl, 0x28(%rax)
movq (%rbx), %rax
movq (%rax,%rsi,8), %rdi
movl 0x10(%rsp), %esi
andl $0x1, %ecx
callq 0x5e1a
movq 0x18(%rsp), %rsi
movq (%rsi), %rcx
movq 0x8(%rsi), %rax
subq %rcx, %rax
cqto
pushq $0xc
popq %rdi
idivq %rdi
cmpq $0x2, %rax
jb 0x6503
movslq 0xc(%rcx), %rax
movl 0x10(%rcx), %edx
movzbl 0x14(%rcx), %ecx
movl %eax, 0x2c(%rsi)
movl %edx, 0x30(%rsi)
movb %cl, 0x34(%rsi)
movq (%rbx), %rsi
movq (%rsi,%rax,8), %rdi
movl 0x10(%rsp), %esi
andl $0x1, %ecx
callq 0x5e1a
incl 0x10(%rsp)
jmp 0x616d
leaq 0x68(%rsp), %rdi
callq 0x2090
callq 0x290a
movsd %xmm0, 0x40(%rbx)
leaq 0x48(%rsp), %rdi
callq 0x2130
leaq 0x68(%rsp), %rdi
callq 0x20a0
addq $0x19e8, %rsp           # imm = 0x19E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8a83(%rip), %rdi      # 0xefd0
callq 0x2140
movq 0x8a77(%rip), %rdi      # 0xefd0
leaq 0x4574(%rip), %rsi      # 0xaad4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x8a5c(%rip), %rdi      # 0xefd0
leaq 0x4604(%rip), %rsi      # 0xab7f
callq 0x2210
movq %rax, %rdi
callq 0x2140
movq 0x8a41(%rip), %rdi      # 0xefd0
callq 0x2140
pushq $0x1
popq %rdi
callq 0x2280
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r14, %rdi
jmp 0x65c1
jmp 0x65ca
jmp 0x65ca
jmp 0x65ca
jmp 0x65ca
movq %rax, %rbx
pushq $0x70
jmp 0x65bd
jmp 0x65ca
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r13, %rdi
callq 0x2230
jmp 0x65cd
jmp 0x65ca
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2130
leaq 0x68(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x2390
nop