// File: /workspace/github_cpp_makefile_star2_O2/Laakeri[P]maxpre/src/./BVE.cpp
// Function: maxPreprocessor::Preprocessor::tryBVE2(int)
int Preprocessor::tryBVE2(int var) {
	int sizeLimit = pi.litClauses[posLit(var)].size() + pi.litClauses[negLit(var)].size();

  sizeLimit += min(BVElocalGrow, BVEglobalGrow);
	
	vector<int> isPosDef, isNegDef;
	vector<int> defClauses;
	if (BVEgate) {
		defClauses = tryBVEGE(var);
	}
	bool defFound = false;
	if (defClauses.size() > 0) {
		defFound = true;
		rLog.gatesExtracted++;
		isPosDef.resize(pi.litClauses[posLit(var)].size());
		isNegDef.resize(pi.litClauses[negLit(var)].size());
		sort(defClauses.begin(), defClauses.end());
		for (int i = 0; i < (int)pi.litClauses[posLit(var)].size(); i++) {
			if (binary_search(defClauses.begin(), defClauses.end(), pi.litClauses[posLit(var)][i])) {
				isPosDef[i] = 1;
			}
		}
		for (int i = 0; i < (int)pi.litClauses[negLit(var)].size(); i++) {
			if (binary_search(defClauses.begin(), defClauses.end(), pi.litClauses[negLit(var)][i])) {
				isNegDef[i] = 1;
			}
		}
	}
	
	//the condition sizelimit >= 6 doesnt really help
	if (sizeLimit >= 6) { // magic constant
		vector<uint64_t> h1=getBVEHash(pi.litClauses[posLit(var)], var, 0);
		vector<uint64_t> h2=getBVEHash(pi.litClauses[negLit(var)], var, 1);
		int hcnt = 0;
		for (int i = 0; i < (int)pi.litClauses[posLit(var)].size(); i++) {
			for (int ii = 0; ii < (int)pi.litClauses[negLit(var)].size(); ii++) {
				if (defFound && (isPosDef[i] == isNegDef[ii])) continue;
				if ((h1[i] & h2[ii]) == 0) {
					hcnt++;
					if (hcnt > sizeLimit) return 0;
				}
			}
		}
	}
	
	vector<vector<int> > newClauses;
	for (int i = 0; i < (int)pi.litClauses[posLit(var)].size(); i++) {
		for (int ii = 0; ii < (int)pi.litClauses[negLit(var)].size(); ii++) {
			if (defFound && (isPosDef[i] == isNegDef[ii])) continue;
			const vector<int>& c1 = pi.clauses[pi.litClauses[posLit(var)][i]].lit;
			const vector<int>& c2 = pi.clauses[pi.litClauses[negLit(var)][ii]].lit;
			unsigned j2 = 0;
			bool f = false;
			for (unsigned j = 0; j < c1.size(); j++) {
				while (j2 < c2.size() && c2[j2] <= c1[j]) {
					if (litNegation(c2[j2]) == c1[j] && litVariable(c1[j]) != var) {
						f = true;
						break;
					}
					j2++;
				}
				if (f) break;
				if (j2 < c2.size() && litNegation(c2[j2]) == c1[j] && litVariable(c1[j]) != var) {
					f = true;
					break;
				}
			}
			
			if (f) continue;
			if ((int)newClauses.size() >= sizeLimit) return 0;
			newClauses.push_back(vector<int>());
			j2 = 0;
			for (unsigned j = 0; j < c1.size(); j++) {
				while (j2 < c2.size() && c2[j2] <= c1[j]) {
					if (litVariable(c2[j2]) != var) {
						newClauses.back().push_back(c2[j2]);
					}
					j2++;
				}
				if (!(newClauses.back().size() > 0 && newClauses.back().back() == c1[j]) && litVariable(c1[j]) != var) {
					newClauses.back().push_back(c1[j]);
				}
			}
			while (j2 < c2.size()) {
				if (litVariable(c2[j2]) != var) {
					newClauses.back().push_back(c2[j2]);
				}
				j2++;
			}
		}
	}
	
	vector<vector<int> > nClauses;
	vector<int> toRemove;
	for (int c : pi.litClauses[posLit(var)]) {
		toRemove.push_back(c);
	}
	for (int c : pi.litClauses[negLit(var)]) {
		toRemove.push_back(c);
		nClauses.push_back(pi.clauses[c].lit);
	}
	for (int c : toRemove) {
		pi.removeClause(c);
	}
	for (auto& nc : newClauses) {
		pi.addClause(nc);
	}
	trace.BVE(var, nClauses);
	
	rLog.removeVariable(1);
	rLog.removeClause((int)toRemove.size() - (int)newClauses.size());

  BVEglobalGrow += (int)toRemove.size() - (int)newClauses.size();
	
	assert(pi.litClauses[posLit(var)].size() == 0 && pi.litClauses[negLit(var)].size() == 0);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %esi, %r14d
leal (%r14,%r14), %eax
cltq
movq 0x18(%rdi), %rdx
imulq $0x18, %rax, %rax
movq 0x8(%rdx,%rax), %rbx
movq %rax, 0x18(%rsp)
subq (%rdx,%rax), %rbx
leal 0x1(,%r14,2), %eax
cltq
imulq $0x18, %rax, %r13
movq 0x8(%rdx,%r13), %r15
movl 0x3a8(%rdi), %eax
movb $0x1, 0x7(%rsp)
subq (%rdx,%r13), %r15
movl 0x3a4(%rdi), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, 0x10(%rsp)
andq $0x0, 0xd0(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
andq $0x0, 0x70(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x60(%rsp)
cmpb $0x1, 0x3a0(%rdi)
movq %rdi, 0x8(%rsp)
movq %r13, 0x38(%rsp)
jne 0xbb85
movq %rdi, %r12
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %edx
callq 0xb6b2
leaq 0x60(%rsp), %r13
leaq 0x20(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1c6d2
movq %rbp, %rdi
callq 0x7512
movq 0x8(%r13), %rax
movq 0x18(%r12), %rdx
cmpq (%r13), %rax
sete %al
je 0xbb80
movb %al, 0x7(%rsp)
incl 0x2c8(%r12)
movq 0x18(%rsp), %rax
movq 0x8(%rdx,%rax), %rsi
subq (%rdx,%rax), %rsi
sarq $0x2, %rsi
leaq 0xc0(%rsp), %rdi
callq 0x68c2
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x38(%rsp), %rcx
movq 0x8(%rax,%rcx), %rsi
subq (%rax,%rcx), %rsi
sarq $0x2, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x68c2
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1c706
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x18(%rsp), %rcx
movq (%rdx,%rcx), %rax
movq 0x8(%rdx,%rcx), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jge 0xbb22
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq %r12, %rax
movq %rax, %rdx
callq 0x18f6e
testb %al, %al
je 0xbb19
movq 0xc0(%rsp), %rax
movl $0x1, (%rax,%r12)
incq %r13
addq $0x4, %r12
jmp 0xbaca
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x38(%rsp), %rcx
movq (%rdx,%rcx), %rax
movq 0x8(%rdx,%rcx), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jge 0xbb80
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq %r12, %rax
movq %rax, %rdx
callq 0x18f6e
testb %al, %al
je 0xbb6e
movq 0xa0(%rsp), %rax
movl $0x1, (%rax,%r12)
incq %r13
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdx
addq $0x4, %r12
jmp 0xbb28
movq 0x38(%rsp), %r13
shrq $0x2, %rbx
shrq $0x2, %r15
addl %r15d, %ebx
movl 0x10(%rsp), %ebp
addl %ebx, %ebp
movl %ebp, 0x10(%rsp)
cmpl $0x6, %ebp
jl 0xbc77
addq 0x18(%rsp), %rdx
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movl %r14d, %ecx
xorl %r8d, %r8d
callq 0x9fc2
movq 0x18(%rbx), %rdx
addq %r13, %rdx
leaq 0x40(%rsp), %rdi
pushq $0x1
popq %r8
movq %rbx, %rsi
movl %r14d, %ecx
callq 0x9fc2
movq 0x18(%rbx), %rax
movq 0x18(%rsp), %rdx
movq 0x8(%rax,%rdx), %rcx
subq (%rax,%rdx), %rcx
shrq $0x2, %rcx
addq %r13, %rax
movq 0xc0(%rsp), %rdx
movq 0xa0(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
testl %ecx, %ecx
cmovlel %r9d, %ecx
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpq %rcx, %r10
je 0xc0fe
movq 0x8(%rax), %rbx
subq (%rax), %rbx
shrq $0x2, %rbx
testl %ebx, %ebx
cmovlel %r9d, %ebx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0xbc6d
cmpb $0x0, 0x7(%rsp)
jne 0xbc50
movl (%rdx,%r10,4), %ebp
cmpl (%rsi,%r15,4), %ebp
je 0xbc68
movq (%r8,%r15,8), %r12
testq %r12, (%rdi,%r10,8)
jne 0xbc68
cmpl 0x10(%rsp), %r11d
jge 0xbec6
incl %r11d
incq %r15
jmp 0xbc3a
incq %r10
movq 0x8(%rsp), %rbx
jmp 0xbc1d
movq 0x8(%rsp), %rbx
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
xorl %edi, %edi
movq 0x18(%rsp), %rcx
movq 0x8(%rdx,%rcx), %rax
subq (%rdx,%rcx), %rax
shrq $0x2, %rax
cltq
cmpq %rax, %rdi
jge 0xbeea
xorl %ebp, %ebp
movq %rdi, 0x58(%rsp)
movq 0x38(%rsp), %rcx
movq (%rdx,%rcx), %rax
movq 0x8(%rdx,%rcx), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jge 0xbebe
cmpb $0x0, 0x7(%rsp)
jne 0xbcf6
movq 0xc0(%rsp), %rcx
movl (%rcx,%rdi,4), %ecx
movq 0xa0(%rsp), %rsi
cmpl (%rsi,%rbp,4), %ecx
je 0xbd91
movq 0x18(%rsp), %rcx
movq (%rdx,%rcx), %rcx
movslq (%rcx,%rdi,4), %rcx
movq (%rbx), %rsi
imulq $0x28, %rcx, %rcx
leaq (%rsi,%rcx), %r12
movslq (%rax,%rbp,4), %rax
imulq $0x28, %rax, %rbx
addq %rsi, %rbx
movq (%rsi,%rcx), %rax
movq 0x8(%rsi,%rcx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
xorl %esi, %esi
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rcx
jbe 0xbd99
movq (%rbx), %r8
movq 0x8(%rbx), %r9
subq %r8, %r9
sarq $0x2, %r9
movl %esi, %r10d
cmpq %r10, %r9
jbe 0xbd83
movl (%r8,%r10,4), %r11d
movl (%rax,%rdi,4), %r15d
movl %r15d, %r10d
xorl %r11d, %r10d
cmpl $0x1, %r10d
setne %r13b
movl %r15d, %r10d
sarl %r10d
cmpl %r14d, %r10d
sete %r10b
orb %r13b, %r10b
cmpl %r15d, %r11d
jg 0xbd7e
testb %r10b, %r10b
je 0xbd87
incl %esi
jmp 0xbd42
testb %r10b, %r10b
je 0xbd87
incl %edi
jmp 0xbd2d
movq 0x8(%rsp), %rbx
movq 0x58(%rsp), %rdi
incq %rbp
jmp 0xbcb2
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpl %eax, 0x10(%rsp)
jle 0xbee2
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0x1cb40
movq %r15, %rdi
callq 0x7512
xorl %r13d, %r13d
xorl %r15d, %r15d
movl %r15d, %r15d
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
jbe 0xbe9a
movl %r13d, %ecx
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
subq %rdx, %rsi
sarq $0x2, %rsi
cmpq %rcx, %rsi
jbe 0xbe40
movl (%rdx,%rcx,4), %esi
cmpl (%rax,%r15,4), %esi
jg 0xbe40
sarl %esi
cmpl %r14d, %esi
je 0xbe3b
leaq (%rdx,%rcx,4), %rsi
movq 0x28(%rsp), %rdi
addq $-0x18, %rdi
callq 0x68a4
movq (%r12), %rax
incl %r13d
jmp 0xbdff
movq 0x28(%rsp), %rdi
movq -0x10(%rdi), %rdx
cmpq -0x18(%rdi), %rdx
je 0xbe5a
movl (%rax,%r15,4), %ecx
cmpl %ecx, -0x4(%rdx)
jne 0xbe5e
jmp 0xbe72
movl (%rax,%r15,4), %ecx
sarl %ecx
cmpl %r14d, %ecx
je 0xbe72
addq $-0x18, %rdi
leaq (%rax,%r15,4), %rsi
callq 0x68a4
incl %r15d
jmp 0xbde3
movl (%rax,%r13,4), %ecx
sarl %ecx
cmpl %r14d, %ecx
je 0xbe97
leaq (%rax,%r13,4), %rsi
movq 0x28(%rsp), %rdi
addq $-0x18, %rdi
callq 0x68a4
incl %r13d
movl %r13d, %r13d
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r13, %rcx
ja 0xbe7a
movq 0x8(%rsp), %rbx
movq 0x18(%rbx), %rdx
jmp 0xbd8c
incq %rdi
jmp 0xbc8e
leaq 0x40(%rsp), %rdi
callq 0x6bf4
leaq 0x20(%rsp), %rdi
callq 0x6bf4
xorl %r15d, %r15d
jmp 0xc0c5
xorl %r15d, %r15d
jmp 0xc0bb
addq 0x18(%rsp), %rdx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
leaq 0x80(%rsp), %r15
andq $0x0, 0x10(%r15)
movaps %xmm0, (%r15)
movq (%rdx), %rbx
movq 0x8(%rdx), %r13
leaq 0x14(%rsp), %r12
movq 0x38(%rsp), %rbp
cmpq %r13, %rbx
je 0xbf3b
movl (%rbx), %eax
movl %eax, 0x14(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x68a4
addq $0x4, %rbx
jmp 0xbf1f
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rbp), %rbx
movq 0x8(%rax,%rbp), %r13
leaq 0x14(%rsp), %r12
leaq 0x40(%rsp), %r15
cmpq %r13, %rbx
je 0xbf91
movl (%rbx), %eax
movl %eax, 0x14(%rsp)
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x68a4
movslq 0x14(%rsp), %rax
imulq $0x28, %rax, %rsi
movq 0x8(%rsp), %rax
addq (%rax), %rsi
movq %r15, %rdi
callq 0x18f96
addq $0x4, %rbx
jmp 0xbf57
movq 0x80(%rsp), %rbx
movq 0x88(%rsp), %r15
cmpq %r15, %rbx
je 0xbfb8
movl (%rbx), %esi
movq 0x8(%rsp), %rdi
callq 0x21490
addq $0x4, %rbx
jmp 0xbfa1
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbx
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
cmpq %rbx, %r15
je 0xbfe7
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x214e8
addq $0x18, %r15
jmp 0xbfcc
movq 0x8(%rsp), %rax
leaq 0x2f0(%rax), %rdi
leaq 0x40(%rsp), %rdx
movl %r14d, %esi
callq 0x2034a
movq 0x8(%rsp), %rax
leaq 0x218(%rax), %r14
pushq $0x1
popq %r15
movq %r14, %rdi
movl %r15d, %esi
callq 0x21ebe
movq 0x88(%rsp), %rsi
subq 0x80(%rsp), %rsi
shrq $0x2, %rsi
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
pushq $-0x18
popq %rbx
cqto
idivq %rbx
addl %eax, %esi
movq %r14, %rdi
callq 0x21e84
movq 0x88(%rsp), %rcx
subq 0x80(%rsp), %rcx
shrq $0x2, %rcx
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
cqto
idivq %rbx
movq 0x8(%rsp), %rdx
addl 0x3a8(%rdx), %ecx
addl %eax, %ecx
movl %ecx, 0x3a8(%rdx)
movq 0x18(%rdx), %rax
movq 0x18(%rsp), %rdx
movq 0x8(%rax,%rdx), %rcx
cmpq (%rax,%rdx), %rcx
jne 0xc11b
movq 0x8(%rax,%rbp), %rcx
cmpq (%rax,%rbp), %rcx
jne 0xc11b
leaq 0x80(%rsp), %rdi
callq 0x7512
leaq 0x40(%rsp), %rdi
callq 0x6aa2
leaq 0x20(%rsp), %rdi
callq 0x6aa2
leaq 0x60(%rsp), %rdi
callq 0x7512
leaq 0xa0(%rsp), %rdi
callq 0x7512
leaq 0xc0(%rsp), %rdi
callq 0x7512
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rdi
callq 0x6bf4
leaq 0x20(%rsp), %rdi
callq 0x6bf4
movq 0x18(%rbx), %rdx
jmp 0xbc7c
leaq 0x23527(%rip), %rdi     # 0x2f649
leaq 0x233a6(%rip), %rsi     # 0x2f4cf
leaq 0x2356a(%rip), %rcx     # 0x2f69a
movl $0x1cf, %edx            # imm = 0x1CF
callq 0x31c0
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x6bf4
jmp 0xc19a
jmp 0xc159
jmp 0xc159
jmp 0xc16d
jmp 0xc159
jmp 0xc16d
jmp 0xc16d
jmp 0xc16d
jmp 0xc159
movq %rax, %rbx
jmp 0xc19a
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x7512
jmp 0xc190
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x7512
leaq 0x40(%rsp), %rdi
callq 0x6aa2
jmp 0xc190
jmp 0xc18d
jmp 0xc18d
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x6aa2
leaq 0x60(%rsp), %rdi
callq 0x7512
leaq 0xa0(%rsp), %rdi
callq 0x7512
leaq 0xc0(%rsp), %rdi
callq 0x7512
movq %rbx, %rdi
callq 0x3490