// File: /workspace/github_cpp_makefile_star2_O2/akinanop[P]mvl-solver/src/Formula.cc
// Function: Formula::WatchedLiterals(int)
int Formula::WatchedLiterals ( int restarts ) {


	int restartCount = restarts;

	// returns 0 if sat, 1 if timeout, 2 if unsat

	if ( LOG ) cout << "Solving with the watched literal algorithm..." << endl;

	while ( true ) {

		//Check if time out
		TIME_E = GetTime();
		if ( ( TIME_E - TIME_S ) > TIMELIMIT )
			return 1;

		if ( CONFLICT ) {
			if ( LOG ) {
				cout << "There is a conflict at level: " << LEVEL << endl;
				cout<<"Conflicting clause: "<<  endl;
				CLAUSELIST[CONFLICTINGCLAUSE] -> Print ();
			}


			if ( LEVEL == 0 ) { cout << "UNSAT" << endl; return 2; }

			Clause* learned = analyzeConflict ( CLAUSELIST[CONFLICTINGCLAUSE], false );
			if (learned->ATOM_LIST.size() > 1)
				LEVEL = VARLIST[learned->WATCHED[1].VAR]->ATOMLEVEL[learned->WATCHED[1].VAL];
			else
				LEVEL = 0;

			BACKTRACKS++;
			if ( LOG ) {
				cout << "We are backtracking to the level: " << LEVEL << endl;
				cout << "# of backtracks so far: " << BACKTRACKS << endl;
			}
			CONFLICT = false;

			if ( BACKTRACKS == restartCount ) {


				//	cout << "restarting" << endl;
				watchedUndoTheory ( 0 );



				restartCount = BACKTRACKS + restarts;
				RESTARTS++;
				// After a restart the learned clause is the only potential unit clause
				// The learned clause is a unit clause at level 0 if the backtrack level would be 0
				UNITLIST.clear();
				if (LEVEL==0)
					UNITLIST.push_back(CLAUSELIST.size()-1);
				LEVEL = 0;
			}
			else {
				watchedUndoTheory ( LEVEL );
				//After backtracking the learned clause is the only unit clause
				UNITLIST.clear();
				UNITLIST.push_back(CLAUSELIST.size()-1);
			}
		}

		// If there is a unit clause, propagate
		if ( ! UNITLIST.empty() )
			WatchedUnitPropagation();

		if ( ! CONFLICT ) {
			Literal atom;
			switch(heuristic) {
			case Heuristic::VSIDS : atom = chooseLiteralVSIDS(); break;
			case Heuristic::VSIDS_pos : atom = chooseLiteralVSIDS_allPos(); break;
			case Heuristic::lazy  : atom = lazyWatchedChooseLiteral (); break;
			}
			if ( atom.VAR != -1 ) {
				DECISIONS++;
				LEVEL++;
				// set REASON for subsequent falsified atoms
				UNITCLAUSE = -1;
				if ( LOG ) cout << "Decision: " << atom . VAR << ( atom . EQUAL ? '=' : '!' ) << atom . VAL << endl;
				watchedReduceTheory ( atom );
			}
			else {
				// The theory is satisfied if all variables are assigned and there is no conflict
				return 0;
			}

		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, %r14
cmpb $0x1, 0xa4(%rdi)
jne 0x8219
movq 0x6dcb(%rip), %rdi      # 0xefd0
leaq 0x2b7b(%rip), %rsi      # 0xad87
callq 0x2210
movq %rax, %rdi
callq 0x2140
leaq 0x60(%r14), %r15
movl 0xc(%rsp), %r12d
callq 0x290a
movsd %xmm0, 0x40(%r14)
subsd 0x38(%r14), %xmm0
cvtsi2sdl 0x30(%r14), %xmm1
ucomisd %xmm1, %xmm0
ja 0x84ca
cmpb $0x1, 0x7c(%r14)
jne 0x83f6
cmpb $0x1, 0xa4(%r14)
jne 0x82ad
movq 0x6d71(%rip), %rbx      # 0xefd0
movq %rbx, %rdi
leaq 0x2b4c(%rip), %rsi      # 0xadb5
callq 0x2210
movl 0x48(%r14), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
callq 0x2140
movq %rbx, %rdi
leaq 0x2b48(%rip), %rsi      # 0xadd4
callq 0x2210
movq %rax, %rdi
callq 0x2140
movslq 0x80(%r14), %rax
movq 0x18(%r14), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x5814
cmpl $0x0, 0x48(%r14)
je 0x84ce
movslq 0x80(%r14), %rax
movq 0x18(%r14), %rcx
movq (%rcx,%rax,8), %rsi
xorl %ebx, %ebx
movq %r14, %rdi
xorl %edx, %edx
callq 0x75f4
movq %rax, %rcx
movq 0x8(%rax), %rax
subq (%rcx), %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
cmpq $0x2, %rax
jb 0x8301
movslq 0x2c(%rcx), %rax
movq (%r14), %rdx
movq (%rdx,%rax,8), %rax
movq 0x18(%rax), %rax
movslq 0x30(%rcx), %rcx
movl (%rax,%rcx,4), %ebx
movl %ebx, 0x48(%r14)
movl 0x4c(%r14), %eax
incl %eax
movl %eax, 0x4c(%r14)
cmpb $0x1, 0xa4(%r14)
jne 0x836a
movq 0x6cb0(%rip), %rbx      # 0xefd0
movq %rbx, %rdi
leaq 0x2ac5(%rip), %rsi      # 0xadef
callq 0x2210
movl 0x48(%r14), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
callq 0x2140
movq %rbx, %rdi
leaq 0x2ac5(%rip), %rsi      # 0xae12
callq 0x2210
movl 0x4c(%r14), %esi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
callq 0x2140
movl 0x4c(%r14), %eax
movb $0x0, 0x7c(%r14)
cmpl %r12d, %eax
jne 0x83c3
movq %r14, %rdi
xorl %esi, %esi
callq 0x7524
movl 0x4c(%r14), %r12d
addl 0xc(%rsp), %r12d
incl 0xa0(%r14)
movq %r15, %rdi
callq 0x902c
cmpl $0x0, 0x48(%r14)
jne 0x83bc
movq 0x20(%r14), %rax
subq 0x18(%r14), %rax
shrq $0x3, %rax
decl %eax
movl %eax, 0x14(%rsp)
movq %r15, %rdi
leaq 0x14(%rsp), %rsi
callq 0x9044
andl $0x0, 0x48(%r14)
jmp 0x83f6
movl 0x48(%r14), %esi
movq %r14, %rdi
callq 0x7524
movq %r15, %rdi
callq 0x902c
movq 0x20(%r14), %rax
subq 0x18(%r14), %rax
shrq $0x3, %rax
decl %eax
movl %eax, 0x10(%rsp)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x9044
cmpq %r15, (%r15)
je 0x8403
movq %r14, %rdi
callq 0x7a10
cmpb $0x0, 0x7c(%r14)
jne 0x8222
movl 0xa8(%r14), %eax
xorl %ebx, %ebx
cmpl $0x3, %eax
je 0x843e
cmpl $0x2, %eax
je 0x8434
cmpl $0x1, %eax
jne 0x84ec
movq %r14, %rdi
callq 0x68ba
jmp 0x8446
movq %r14, %rdi
callq 0x7f32
jmp 0x8446
movq %r14, %rdi
callq 0x6966
movq %rax, %r13
movl %edx, %ebp
cmpl $-0x1, %r13d
je 0x84ec
incl 0x50(%r14)
incl 0x48(%r14)
orl $-0x1, 0x78(%r14)
cmpb $0x1, 0xa4(%r14)
jne 0x84b6
movq 0x6b5d(%rip), %rdi      # 0xefd0
leaq 0x29b1(%rip), %rsi      # 0xae2b
callq 0x2210
movq %rax, %rdi
movl %r13d, %esi
callq 0x2380
testb $0x1, %bpl
pushq $0x21
popq %rsi
pushq $0x3d
popq %rcx
cmovnel %ecx, %esi
movq %rax, %rdi
callq 0x2270
movq %r13, %rsi
shrq $0x20, %rsi
movq %rax, %rdi
callq 0x2380
movq %rax, %rdi
callq 0x2140
movzbl %bpl, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x7ac0
jmp 0x8222
pushq $0x1
jmp 0x84eb
movq 0x6afb(%rip), %rdi      # 0xefd0
leaq 0x290d(%rip), %rsi      # 0xade9
callq 0x2210
movq %rax, %rdi
callq 0x2140
pushq $0x2
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop