// File: biotomas[P]ipasir/app/genipabones/genipabones.cpp
// Function: main
int main(int argc, char **argv) {
	printf("c This program finds back-bones of a CNF SAT formula. A back-bone is assignment fixed in all solutions.\n");
	printf("c You can specify backbone candidates by adding a line starting with 'cvip' followed by variables names separated by space to the cnf file.\n");
	printf("c You can specify a time limit (in seconds) for checking one backbone using the -t parameter.\n");
	printf("c Using the incremental SAT solver %s.\n", ipasir_signature());


	if (argc < 2) {
		puts("USAGE: ./bbones <dimacs.cnf> [-t=<time-limit-per-check>]");
		return 0;
	}

	void *solver = ipasir_init();
	vector<int> boundary;
	int maxVar;
	bool loaded = loadFormula(solver, argv[1], boundary, maxVar);

	if (!loaded) {
		printf("The input formula \"%s\" could not be loaded.\n", argv[1]);
		return 0;
	}

	if (boundary.size() == 0) {
		boundary.clear();
		for (int var = 1; var <= maxVar; var++) {
			boundary.push_back(var);
		}
		printf("c Will check all the variables (%d) for backbones.\n", maxVar);
	} else {
		printf("c Will check %lu candidates for backbones.\n", boundary.size());
	}

	int timelimit = 0;
	if ((argc > 2 && argv[2][1] == 't')) {
		timelimit = atoi(argv[2]+3);
		printf("c Time limit per check is %d seconds\n", timelimit);
		ipasir_set_terminate(solver, NULL, terminateCallback);
		signal(SIGALRM, watchdog);
	}

	int bbonesFound = 0;

	for (size_t i = 0; i < boundary.size(); i++) {
		int candidate = boundary[i];
		printf("c Checking canidate %d, (nr. %lu of %lu)\n", candidate, i+1, boundary.size());
		// check positive case
		ipasir_assume(solver, candidate);
		terminateFlag = 0;
		alarm(timelimit);
		int res = ipasir_solve(solver);
		if (res == 20) {
			bbonesFound++;
			printf("%d 0\n", -candidate);
			continue;
		}
		alarm(0);

		// check negative case
		ipasir_assume(solver, -candidate);
		terminateFlag = 0;
		alarm(timelimit);
		res = ipasir_solve(solver);
		if (res == 20) {
			bbonesFound++;
			printf("%d 0\n", candidate);
		}
		alarm(0);
	}

	printf("c Found %d backbones\n", bbonesFound);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x18a65(%rip), %rdi     # 0x1a035
movb $0x0, %al
callq 0x1030
leaq 0x18ac0(%rip), %rdi     # 0x1a09e
movb $0x0, %al
callq 0x1030
leaq 0x18b3f(%rip), %rdi     # 0x1a12b
movb $0x0, %al
callq 0x1030
callq 0x24a0
movq %rax, %rsi
leaq 0x18b88(%rip), %rdi     # 0x1a18a
movb $0x0, %al
callq 0x1030
cmpl $0x2, -0x8(%rbp)
jge 0x1627
leaq 0x18b9c(%rip), %rdi     # 0x1a1b2
callq 0x1200
movl $0x0, -0x4(%rbp)
jmp 0x191c
callq 0x24b0
movq %rax, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0x78(%rbp)
callq 0x19b0
movq -0x78(%rbp), %rdx
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x34(%rbp), %rcx
callq 0x1370
movb %al, -0x69(%rbp)
jmp 0x165b
movb -0x69(%rbp), %al
andb $0x1, %al
movb %al, -0x35(%rbp)
testb $0x1, -0x35(%rbp)
jne 0x16ae
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x18b73(%rip), %rdi     # 0x1a1eb
xorl %eax, %eax
callq 0x1030
jmp 0x1681
movl $0x0, -0x4(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x1913
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1a30
jmp 0x1928
leaq -0x30(%rbp), %rdi
callq 0x19d0
cmpq $0x0, %rax
jne 0x1706
leaq -0x30(%rbp), %rdi
callq 0x19f0
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jg 0x16f1
leaq -0x30(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x1940
jmp 0x16e4
jmp 0x16e6
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x16cd
movl -0x34(%rbp), %esi
leaq 0x18b1d(%rip), %rdi     # 0x1a218
xorl %eax, %eax
callq 0x1030
jmp 0x1704
jmp 0x1724
leaq -0x30(%rbp), %rdi
callq 0x19d0
movq %rax, %rsi
leaq 0x18b33(%rip), %rdi     # 0x1a24c
xorl %eax, %eax
callq 0x1030
jmp 0x1722
jmp 0x1724
movl $0x0, -0x50(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x1790
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x74, %eax
jne 0x1790
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x3, %rdi
callq 0x1210
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %esi
leaq 0x18b18(%rip), %rdi     # 0x1a278
xorl %eax, %eax
callq 0x1030
jmp 0x1769
movq -0x18(%rbp), %rdi
leaq -0x1f4(%rip), %rdx      # 0x1580
xorl %eax, %eax
movl %eax, %esi
callq 0x2660
jmp 0x177f
movl $0xe, %edi
leaq -0x1fb(%rip), %rsi      # 0x1590
callq 0x1190
movl $0x0, -0x54(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x19d0
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x18f9
movq -0x60(%rbp), %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x1a10
movq -0x98(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x60(%rbp), %rax
incq %rax
movq %rax, -0x88(%rbp)
callq 0x19d0
movl -0x8c(%rbp), %esi
movq -0x88(%rbp), %rdx
movq %rax, %rcx
leaq 0x18a8b(%rip), %rdi     # 0x1a29e
xorl %eax, %eax
callq 0x1030
jmp 0x181c
movq -0x18(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x2580
jmp 0x182a
movl $0x0, 0x20928(%rip)     # 0x2215c
movl -0x50(%rbp), %edi
callq 0x1260
movq -0x18(%rbp), %rdi
callq 0x25b0
movl %eax, -0x9c(%rbp)
jmp 0x184d
movl -0x9c(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x14, -0x68(%rbp)
jne 0x187b
movl -0x54(%rbp), %eax
incl %eax
movl %eax, -0x54(%rbp)
movl -0x64(%rbp), %esi
negl %esi
leaq 0x196bf(%rip), %rdi     # 0x1af2f
xorl %eax, %eax
callq 0x1030
jmp 0x1879
jmp 0x18e8
xorl %edi, %edi
callq 0x1260
movq -0x18(%rbp), %rdi
movl -0x64(%rbp), %esi
negl %esi
callq 0x2580
jmp 0x1892
movl $0x0, 0x208c0(%rip)     # 0x2215c
movl -0x50(%rbp), %edi
callq 0x1260
movq -0x18(%rbp), %rdi
callq 0x25b0
movl %eax, -0xa0(%rbp)
jmp 0x18b5
movl -0xa0(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x14, -0x68(%rbp)
jne 0x18e1
movl -0x54(%rbp), %eax
incl %eax
movl %eax, -0x54(%rbp)
movl -0x64(%rbp), %esi
leaq 0x19659(%rip), %rdi     # 0x1af2f
xorl %eax, %eax
callq 0x1030
jmp 0x18df
jmp 0x18e1
xorl %edi, %edi
callq 0x1260
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x179f
movl -0x54(%rbp), %esi
leaq 0x189c5(%rip), %rdi     # 0x1a2c8
xorl %eax, %eax
callq 0x1030
jmp 0x190c
movl $0x0, -0x48(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1a30
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x1250
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)