// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xc8(%rsp)
movl %esi, 0xc4(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0xc4(%rsp), %ecx
movl %ecx, 0xc0(%rsp)
testb $0x1, 0xa4(%rax)
je 0x11b0c
movq 0xc4df(%rip), %rdi      # 0x1dfd0
leaq 0x626f(%rip), %rsi      # 0x17d67
callq 0x2220
movq %rax, %rdi
movq 0xc4b9(%rip), %rsi      # 0x1dfc0
callq 0x2260
jmp 0x11b0e
callq 0x2ec0
movq 0x20(%rsp), %rax
movsd %xmm0, 0x40(%rax)
movsd 0x40(%rax), %xmm0
subsd 0x38(%rax), %xmm0
cvtsi2sdl 0x30(%rax), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x11b42
movl $0x1, 0xd4(%rsp)
jmp 0x1204d
movq 0x20(%rsp), %rax
testb $0x1, 0x7c(%rax)
je 0x11e09
movq 0x20(%rsp), %rax
testb $0x1, 0xa4(%rax)
je 0x11bd3
movq 0xc46a(%rip), %rdi      # 0x1dfd0
leaq 0x6228(%rip), %rsi      # 0x17d95
callq 0x2220
movq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x48(%rax), %esi
callq 0x2400
movq %rax, %rdi
movq 0xc434(%rip), %rsi      # 0x1dfc0
callq 0x2260
movq 0xc438(%rip), %rdi      # 0x1dfd0
leaq 0x6215(%rip), %rsi      # 0x17db4
callq 0x2220
movq %rax, %rdi
movq 0xc412(%rip), %rsi      # 0x1dfc0
callq 0x2260
movq 0x20(%rsp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movslq 0x80(%rax), %rsi
callq 0x12f20
movq (%rax), %rdi
callq 0xb600
movq 0x20(%rsp), %rax
cmpl $0x0, 0x48(%rax)
jne 0x11c10
movq 0xc3eb(%rip), %rdi      # 0x1dfd0
leaq 0x61dd(%rip), %rsi      # 0x17dc9
callq 0x2220
movq %rax, %rdi
movq 0xc3c5(%rip), %rsi      # 0x1dfc0
callq 0x2260
movl $0x2, 0xd4(%rsp)
jmp 0x1204d
movq 0x20(%rsp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movslq 0x80(%rax), %rsi
callq 0x12f20
movq 0x20(%rsp), %rdi
movq (%rax), %rsi
xorl %edx, %edx
callq 0x10110
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0xb890
cmpq $0x1, %rax
jbe 0x11c8b
movq 0x20(%rsp), %rdi
movq 0xb8(%rsp), %rax
movslq 0x2c(%rax), %rsi
callq 0xaab0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq (%rcx), %rcx
movq 0x18(%rcx), %rcx
movq 0xb8(%rsp), %rdx
movslq 0x30(%rdx), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, 0x48(%rax)
jmp 0x11c97
movq 0x20(%rsp), %rax
movl $0x0, 0x48(%rax)
movq 0x20(%rsp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
testb $0x1, 0xa4(%rax)
je 0x11d12
movq 0xc31b(%rip), %rdi      # 0x1dfd0
leaq 0x6113(%rip), %rsi      # 0x17dcf
callq 0x2220
movq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x48(%rax), %esi
callq 0x2400
movq %rax, %rdi
movq 0xc2e5(%rip), %rsi      # 0x1dfc0
callq 0x2260
movq 0xc2e9(%rip), %rdi      # 0x1dfd0
leaq 0x6104(%rip), %rsi      # 0x17df2
callq 0x2220
movq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x4c(%rax), %esi
callq 0x2400
movq %rax, %rdi
movq 0xc2b3(%rip), %rsi      # 0x1dfc0
callq 0x2260
movq 0x20(%rsp), %rax
movb $0x0, 0x7c(%rax)
movl 0x4c(%rax), %eax
cmpl 0xc0(%rsp), %eax
jne 0x11db5
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0xfed0
movq 0x20(%rsp), %rdi
movl 0x4c(%rdi), %eax
addl 0xc4(%rsp), %eax
movl %eax, 0xc0(%rsp)
movl 0xa0(%rdi), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rdi)
addq $0x60, %rdi
callq 0x141e0
movq 0x20(%rsp), %rax
cmpl $0x0, 0x48(%rax)
jne 0x11da7
movq 0x20(%rsp), %rdi
movq %rdi, %rax
addq $0x60, %rax
movq %rax, 0x18(%rsp)
addq $0x18, %rdi
callq 0x12f00
movq 0x18(%rsp), %rdi
subq $0x1, %rax
movl %eax, 0xb4(%rsp)
leaq 0xb4(%rsp), %rsi
callq 0x14210
movq 0x20(%rsp), %rax
movl $0x0, 0x48(%rax)
jmp 0x11e07
movq 0x20(%rsp), %rdi
movl 0x48(%rdi), %esi
callq 0xfed0
movq 0x20(%rsp), %rdi
addq $0x60, %rdi
callq 0x141e0
movq 0x20(%rsp), %rdi
movq %rdi, %rax
addq $0x60, %rax
movq %rax, 0x10(%rsp)
addq $0x18, %rdi
callq 0x12f00
movq 0x10(%rsp), %rdi
subq $0x1, %rax
movl %eax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rsi
callq 0x14210
jmp 0x11e09
movq 0x20(%rsp), %rdi
addq $0x60, %rdi
callq 0x13e10
testb $0x1, %al
jne 0x11e25
movq 0x20(%rsp), %rdi
callq 0x10a70
movq 0x20(%rsp), %rax
testb $0x1, 0x7c(%rax)
jne 0x12048
leaq 0xa4(%rsp), %rdi
callq 0x59f0
movq 0x20(%rsp), %rax
movl 0xa8(%rax), %eax
movl %eax, 0xc(%rsp)
subl $0x1, %eax
je 0x11e74
jmp 0x11e57
movl 0xc(%rsp), %eax
subl $0x2, %eax
je 0x11f13
jmp 0x11e66
movl 0xc(%rsp), %eax
subl $0x3, %eax
je 0x11ece
jmp 0x11f50
movq 0x20(%rsp), %rdi
callq 0xe0d0
movb %dl, 0x90(%rsp)
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x98(%rsp)
movb 0x90(%rsp), %al
movb %al, 0xa0(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0xa4(%rsp)
movb 0xa0(%rsp), %al
movb %al, 0xac(%rsp)
jmp 0x11f50
movq 0x20(%rsp), %rdi
callq 0xe2d0
movb %dl, 0x70(%rsp)
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x7c(%rsp)
movb 0x70(%rsp), %al
movb %al, 0x84(%rsp)
movq 0x7c(%rsp), %rax
movq %rax, 0xa4(%rsp)
movb 0x84(%rsp), %al
movb %al, 0xac(%rsp)
jmp 0x11f50
movq 0x20(%rsp), %rdi
callq 0x11600
movb %dl, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x5c(%rsp)
movb 0x50(%rsp), %al
movb %al, 0x64(%rsp)
movq 0x5c(%rsp), %rax
movq %rax, 0xa4(%rsp)
movb 0x64(%rsp), %al
movb %al, 0xac(%rsp)
cmpl $-0x1, 0xa4(%rsp)
je 0x12039
movq 0x20(%rsp), %rax
movl 0x50(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x50(%rax)
movl 0x48(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x48(%rax)
movl $0xffffffff, 0x78(%rax) # imm = 0xFFFFFFFF
testb $0x1, 0xa4(%rax)
je 0x11ff9
movq 0xc044(%rip), %rdi      # 0x1dfd0
leaq 0x5e78(%rip), %rsi      # 0x17e0b
callq 0x2220
movq %rax, %rdi
movl 0xa4(%rsp), %esi
callq 0x2400
movq %rax, (%rsp)
movb 0xac(%rsp), %cl
movb $0x3d, %al
movb $0x21, %dl
movb %dl, 0xa(%rsp)
testb $0x1, %cl
movb %al, 0xb(%rsp)
jne 0x11fcb
movb 0xa(%rsp), %al
movb %al, 0xb(%rsp)
movq (%rsp), %rdi
movb 0xb(%rsp), %al
movsbl %al, %esi
callq 0x22d0
movq %rax, %rdi
movl 0xa8(%rsp), %esi
callq 0x2400
movq %rax, %rdi
movq 0xbfcc(%rip), %rsi      # 0x1dfc0
callq 0x2260
movq 0x20(%rsp), %rdi
movq 0xa4(%rsp), %rax
movq %rax, 0x3c(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x3c(%rsp), %rax
movq %rax, 0x2c(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x2c(%rsp), %rsi
movzbl 0x34(%rsp), %edx
callq 0x10c70
jmp 0x12046
movl $0x0, 0xd4(%rsp)
jmp 0x1204d
jmp 0x12048
jmp 0x11b0e
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
nopl (%rax)