// File: /workspace/github_cpp_makefile_star2_O1/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 0x2260
movl $0x0, 0xc(%rsp)
leaq 0x50(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rax, 0x28(%rsp)
movq %rax, 0x10(%rsp)
movl $0x70, %edi
callq 0x2230
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7332
leaq 0x60(%rsp), %rsi
movq %r14, (%rsi)
movq %rbx, %rdi
callq 0xb9a6
movq (%rbx), %rax
movq (%rax), %rax
movl $0x0, 0x4(%rax)
callq 0x2b7e
movsd %xmm0, 0x38(%rbx)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x68(%rsp), %rdi
movl $0x8, %edx
callq 0x2170
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x88(%rsp,%rax)
jne 0x7e63
leaq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x60(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x8(%rsp), %r12
leaq 0x4(%rsp), %r13
movabsq $0x100000601, %r14      # imm = 0x100000601
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rdi
leaq 0x68(%rsp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %ecx
movl $0xbb8, %edx            # imm = 0xBB8
leaq 0x68(%rsp), %rdi
leaq 0xe30(%rsp), %rsi
callq 0x23c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x7e17
movl $0x38, %edi
callq 0x2230
movq %rax, %rbp
movq 0x20(%rsp), %rax
movl 0xc(%rax), %esi
movq %rbp, %rdi
callq 0x6ed8
movq %rbp, 0x10(%rsp)
movzbl 0xe30(%rsp), %eax
cmpl $0x63, %eax
je 0x79d0
cmpl $0x64, %eax
je 0x7a71
cmpl $0x70, %eax
jne 0x7aca
leaq 0xe30(%rsp), %rdi
leaq 0x51ac(%rip), %rsi      # 0xcc0b
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2210
jmp 0x79d0
leaq 0xe30(%rsp), %rdi
leaq 0x5197(%rip), %rsi      # 0xcc17
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2210
movl $0x70, %edi
callq 0x2230
movq %rax, %rbp
movl 0x8(%rsp), %esi
movl 0x4(%rsp), %edx
movq %rax, %rdi
callq 0x7332
movq %rbp, 0x28(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x7d34
movq %rbp, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x79d0
leaq 0xe30(%rsp), %rbp
movzbl (%rbp), %eax
cmpl $0x9, %eax
je 0x7ae0
cmpl $0x20, %eax
jne 0x7ae5
incq %rbp
jmp 0x7ad2
xorl %r15d, %r15d
movzbl (%rbp,%r15), %eax
cmpq $0x20, %rax
ja 0x7afa
btq %rax, %r14
jb 0x7b07
movb %al, 0x270(%rsp,%r15)
incq %r15
jmp 0x7ae8
movb $0x0, 0x270(%rsp,%r15)
cmpb $0x0, 0x270(%rsp)
je 0x7d0e
leaq 0x270(%rsp), %rdi
leaq 0x50f9(%rip), %rsi      # 0xcc26
movq %r12, %rdx
leaq 0x3(%rsp), %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x2210
cmpb $0x21, 0x3(%rsp)
jne 0x7b6c
leaq 0x270(%rsp), %rdi
leaq 0x50da(%rip), %rsi      # 0xcc2f
movq %r12, %rdx
leaq 0x3(%rsp), %rcx
leaq 0x1f(%rsp), %r8
movq %r13, %r9
xorl %eax, %eax
callq 0x2210
movl 0x8(%rsp), %eax
testq %rax, %rax
je 0x7d0e
xorl %edx, %edx
cmpb $0x3d, 0x3(%rsp)
sete %dl
movl 0x4(%rsp), %esi
shlq $0x20, %rsi
orq %rax, %rsi
movq 0x10(%rsp), %rdi
callq 0x6f6c
cmpb $0x3d, 0x3(%rsp)
jne 0x7be7
movl 0xa8(%rbx), %eax
testl %eax, %eax
je 0x7c70
cmpl $0x3, %eax
je 0x7bbb
cmpl $0x1, %eax
jne 0x7c88
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x38(%rax), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x50c3(%rip), %xmm0     # 0xcca0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x7c88
movl 0xa8(%rbx), %eax
testl %eax, %eax
je 0x7ca3
cmpl $0x1, %eax
je 0x7c47
cmpl $0x3, %eax
jne 0x7cbb
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movslq 0x8(%rax), %rcx
testq %rcx, %rcx
jle 0x7cbb
movl 0x4(%rsp), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x7c3d
movq 0x38(%rax), %rdi
movsd (%rdi,%rsi,8), %xmm0
addsd 0x5068(%rip), %xmm0     # 0xcca0
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x7c22
jmp 0x7cbb
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x40(%rax), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x5037(%rip), %xmm0     # 0xcca0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x7cbb
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x28(%rax), %rax
movslq 0x4(%rsp), %rcx
incl (%rax,%rcx,4)
cmpb $0x0, 0xa5(%rbx)
jne 0x7d0e
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x60(%rax), %rax
jmp 0x7cd4
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x30(%rax), %rax
movslq 0x4(%rsp), %rcx
incl (%rax,%rcx,4)
cmpb $0x0, 0xa5(%rbx)
jne 0x7d0e
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x68(%rax), %rax
movslq 0x4(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %rsi
cmpq 0x10(%rax,%rcx,8), %rsi
je 0x7d00
leaq (%rax,%rcx,8), %rax
addq $0x8, %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, (%rax)
jmp 0x7d0e
leaq (%rax,%rcx,8), %rdi
leaq 0xc(%rsp), %rdx
callq 0x5530
addq %r15, %rbp
cmpb $0x0, (%rbp)
jne 0x7ad2
movq 0x20(%rbx), %rsi
cmpq 0x28(%rbx), %rsi
je 0x7d46
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x20(%rbx)
jmp 0x7d55
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0x6d28
jmp 0x79d0
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0xbab2
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0xc, %rcx
jne 0x7d89
movl $0x18, %edi
callq 0x2230
movl 0xc(%rsp), %ecx
movl %ecx, 0x10(%rax)
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x2070
incq 0x70(%rbx)
cmpb $0x1, 0xa5(%rbx)
jne 0x7e0e
movq 0x10(%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 0xc(%rsp), %esi
andl $0x1, %ecx
callq 0x7604
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq $0x2, %rdx
jb 0x7e0e
movslq 0xc(%rcx), %rsi
movl 0x10(%rcx), %edx
movzbl 0x14(%rcx), %ecx
movl %esi, 0x2c(%rax)
movl %edx, 0x30(%rax)
movb %cl, 0x34(%rax)
movq (%rbx), %rax
movq (%rax,%rsi,8), %rdi
movl 0xc(%rsp), %esi
andl $0x1, %ecx
callq 0x7604
incl 0xc(%rsp)
jmp 0x79d0
leaq 0x68(%rsp), %rdi
callq 0x2090
callq 0x2b7e
movsd %xmm0, 0x40(%rbx)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7e47
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2240
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 0x8166(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x815a(%rip), %rdi      # 0xffd0
leaq 0x4c57(%rip), %rsi      # 0xcad4
movl $0xf, %edx
callq 0x2270
movq 0x8142(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x8136(%rip), %rdi      # 0xffd0
leaq 0x4d2f(%rip), %rsi      # 0xcbd0
movl $0x1c, %edx
callq 0x2270
movq 0x811e(%rip), %rdi      # 0xffd0
callq 0x2120
movq 0x8112(%rip), %rdi      # 0xffd0
callq 0x2120
movl $0x1, %edi
callq 0x2290
movq %rax, %rbx
movl $0x70, %esi
movq %r14, %rdi
jmp 0x7ef9
jmp 0x7f02
jmp 0x7f02
jmp 0x7f02
jmp 0x7f02
jmp 0x7f02
movq %rax, %rbx
movl $0x70, %esi
jmp 0x7ef6
movq %rax, %rbx
movl $0x38, %esi
movq %rbp, %rdi
callq 0x2240
jmp 0x7f05
jmp 0x7f02
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7f21
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x68(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x23a0
nop