// File: /workspace/github_cpp_makefile_star2_O3/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 0x728e
leaq 0x60(%rsp), %rsi
movq %r14, (%rsi)
movq %rbx, %rdi
callq 0xbb0a
movq (%rbx), %rax
movq (%rax), %rax
movl $0x0, 0x4(%rax)
callq 0x2b92
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 0x7db3
leaq 0x18(%rbx), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x60(%rbx), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x8(%rsp), %r15
leaq 0x4(%rsp), %r12
movabsq $0x100000601, %rbp      # imm = 0x100000601
leaq (%rsp,%rax), %rdi
addq $0x68, %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 0x7d67
movl $0x38, %edi
callq 0x2230
movq %rax, %r13
movq 0x20(%rsp), %rax
movl 0xc(%rax), %esi
movq %r13, %rdi
callq 0x6e34
movq %r13, 0x10(%rsp)
movzbl 0xe30(%rsp), %eax
cmpl $0x63, %eax
je 0x7d59
cmpl $0x64, %eax
je 0x79b4
cmpl $0x70, %eax
jne 0x7a0d
leaq 0xe30(%rsp), %rdi
leaq 0x6269(%rip), %rsi      # 0xdc0b
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2210
jmp 0x7d59
leaq 0xe30(%rsp), %rdi
leaq 0x6254(%rip), %rsi      # 0xdc17
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2210
movl $0x70, %edi
callq 0x2230
movq %rax, %r13
movl 0x8(%rsp), %esi
movl 0x4(%rsp), %edx
movq %rax, %rdi
callq 0x728e
movq %r13, 0x28(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x7c75
movq %r13, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x7d59
leaq 0xe30(%rsp), %r13
cmpb $0x9, %al
je 0x7a21
movzbl %al, %ecx
cmpl $0x20, %ecx
jne 0x7a2a
movb 0x1(%r13), %al
incq %r13
jmp 0x7a15
xorl %r14d, %r14d
cmpb $0x20, %al
ja 0x7a3a
movzbl %al, %ecx
btq %rcx, %rbp
jb 0x7a4c
movb %al, 0x270(%rsp,%r14)
movb 0x1(%r13,%r14), %al
incq %r14
jmp 0x7a2d
movb $0x0, 0x270(%rsp,%r14)
cmpb $0x0, 0x270(%rsp)
je 0x7c4d
leaq 0x270(%rsp), %rdi
leaq 0x61b4(%rip), %rsi      # 0xdc26
movq %r15, %rdx
leaq 0x3(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2210
cmpb $0x21, 0x3(%rsp)
jne 0x7ab1
leaq 0x270(%rsp), %rdi
leaq 0x6195(%rip), %rsi      # 0xdc2f
movq %r15, %rdx
leaq 0x3(%rsp), %rcx
leaq 0x1f(%rsp), %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x2210
movl 0x8(%rsp), %eax
testq %rax, %rax
je 0x7c4d
xorl %edx, %edx
cmpb $0x3d, 0x3(%rsp)
sete %dl
movl 0x4(%rsp), %esi
shlq $0x20, %rsi
orq %rax, %rsi
movq 0x10(%rsp), %rdi
callq 0x6ec8
cmpb $0x3d, 0x3(%rsp)
movl 0xa8(%rbx), %eax
jne 0x7b2c
testl %eax, %eax
je 0x7baf
cmpl $0x3, %eax
je 0x7b00
cmpl $0x1, %eax
jne 0x7bc7
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 0x617e(%rip), %xmm0     # 0xdca0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x7bc7
testl %eax, %eax
je 0x7be2
cmpl $0x1, %eax
je 0x7b86
cmpl $0x3, %eax
jne 0x7bfa
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movslq 0x8(%rax), %rcx
testq %rcx, %rcx
jle 0x7bfa
movl 0x4(%rsp), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x7b7c
movq 0x38(%rax), %rdi
movsd (%rdi,%rsi,8), %xmm0
addsd 0x6129(%rip), %xmm0     # 0xdca0
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x7b61
jmp 0x7bfa
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 0x60f8(%rip), %xmm0     # 0xdca0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x7bfa
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 0x7c4d
movslq 0x8(%rsp), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq 0x60(%rax), %rax
jmp 0x7c13
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 0x7c4d
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 0x7c3f
leaq (%rax,%rcx,8), %rax
addq $0x8, %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, (%rax)
jmp 0x7c4d
leaq (%rax,%rcx,8), %rdi
leaq 0xc(%rsp), %rdx
callq 0x54e4
addq %r14, %r13
movb (%r13), %al
testb %al, %al
jne 0x7a15
movq 0x20(%rbx), %rsi
cmpq 0x28(%rbx), %rsi
je 0x7c87
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x20(%rbx)
jmp 0x7c9b
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0x6c84
jmp 0x7d59
movq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0xbc1e
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
cmpq $0xc, %rcx
jne 0x7cca
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)
movl 0xc(%rsp), %esi
cmpb $0x1, 0xa5(%rbx)
jne 0x7d53
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movslq (%rcx), %rdi
movl 0x4(%rcx), %edx
movzbl 0x8(%rcx), %ecx
movl %edi, 0x20(%rax)
movl %edx, 0x24(%rax)
movb %cl, 0x28(%rax)
movq (%rbx), %rax
movq (%rax,%rdi,8), %rdi
andl $0x1, %ecx
callq 0x7558
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
movl 0xc(%rsp), %esi
cmpq $0x2, %rdx
jb 0x7d53
movslq 0xc(%rcx), %rdi
movl 0x10(%rcx), %edx
movzbl 0x14(%rcx), %ecx
movl %edi, 0x2c(%rax)
movl %edx, 0x30(%rax)
movb %cl, 0x34(%rax)
movq (%rbx), %rax
movq (%rax,%rdi,8), %rdi
andl $0x1, %ecx
callq 0x7558
movl 0xc(%rsp), %esi
incl %esi
movl %esi, 0xc(%rsp)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
jmp 0x7918
leaq 0x68(%rsp), %rdi
callq 0x2090
callq 0x2b92
movsd %xmm0, 0x40(%rbx)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7d97
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 0x9216(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0x920a(%rip), %rdi      # 0x10fd0
leaq 0x5d07(%rip), %rsi      # 0xdad4
movl $0xf, %edx
callq 0x2270
movq 0x91f2(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0x91e6(%rip), %rdi      # 0x10fd0
leaq 0x5ddf(%rip), %rsi      # 0xdbd0
movl $0x1c, %edx
callq 0x2270
movq 0x91ce(%rip), %rdi      # 0x10fd0
callq 0x2120
movq 0x91c2(%rip), %rdi      # 0x10fd0
callq 0x2120
movl $0x1, %edi
callq 0x2290
movq %rax, %rbx
movl $0x70, %esi
movq %r14, %rdi
jmp 0x7e49
jmp 0x7e52
jmp 0x7e52
jmp 0x7e52
jmp 0x7e52
jmp 0x7e52
movq %rax, %rbx
movl $0x70, %esi
jmp 0x7e46
movq %rax, %rbx
movl $0x38, %esi
movq %r13, %rdi
callq 0x2240
jmp 0x7e55
jmp 0x7e52
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x7e71
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2240
leaq 0x68(%rsp), %rdi
callq 0x20a0
movq %rbx, %rdi
callq 0x23a0
nop