// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movl %edi, %r15d
leaq 0xcc40(%rip), %rdi      # 0xe1a2
callq 0x11f0
leaq 0xcc9c(%rip), %rdi      # 0xe20a
callq 0x11f0
leaq 0xcd1c(%rip), %rdi      # 0xe296
callq 0x11f0
callq 0x1950
leaq 0xca79(%rip), %rdi      # 0xe004
movq %rax, %rsi
xorl %eax, %eax
callq 0x1030
cmpl $0x1, %r15d
jg 0x15ac
leaq 0xca8a(%rip), %rdi      # 0xe02c
callq 0x11f0
jmp 0x1781
callq 0x1958
movq %rax, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movq 0x8(%r14), %rsi
leaq 0xc(%rsp), %rcx
movq %rax, %rdi
callq 0x135c
testb %al, %al
je 0x15f9
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
je 0x1610
sarq $0x2, %rsi
leaq 0xcad6(%rip), %rdi      # 0xe0c6
xorl %eax, %eax
callq 0x1030
jmp 0x164c
movq 0x8(%r14), %rsi
leaq 0xca61(%rip), %rdi      # 0xe065
xorl %eax, %eax
callq 0x1030
jmp 0x1777
pushq $0x1
popq %rax
movl 0xc(%rsp), %ebp
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
movl %eax, 0x8(%rsp)
cmpl %ebp, %eax
jg 0x163c
movq %r12, %rdi
movq %r13, %rsi
callq 0x17b0
movl 0x8(%rsp), %eax
incl %eax
jmp 0x1621
leaq 0xca4f(%rip), %rdi      # 0xe092
movl %ebp, %esi
xorl %eax, %eax
callq 0x1030
movl $0x0, %ebp
cmpl $0x2, %r15d
je 0x169c
movq 0x10(%r14), %rdi
cmpb $0x74, 0x1(%rdi)
jne 0x169c
addq $0x3, %rdi
callq 0x1200
movl %eax, %ebp
leaq 0xca7f(%rip), %rdi      # 0xe0f2
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
leaq -0x14e(%rip), %rdx      # 0x1535
movq %rbx, %rdi
xorl %esi, %esi
callq 0x19f4
leaq -0x158(%rip), %rsi      # 0x153c
pushq $0xe
popq %rdi
callq 0x1180
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rcx, %r13
jae 0x1766
movl (%rax,%r13,4), %r15d
incq %r13
leaq 0xca4e(%rip), %rdi      # 0xe118
movl %r15d, %esi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1030
movq %rbx, %rdi
movl %r15d, %esi
callq 0x19cc
andl $0x0, 0x13a63(%rip)     # 0x1514c
movl %ebp, %edi
callq 0x1250
movq %rbx, %rdi
callq 0x19d1
movl %r15d, %r12d
negl %r12d
cmpl $0x14, %eax
jne 0x1719
incl %r14d
leaq 0xd822(%rip), %rdi      # 0xef2f
movl %r12d, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x16a2
xorl %edi, %edi
callq 0x1250
movq %rbx, %rdi
movl %r12d, %esi
callq 0x19cc
andl $0x0, 0x13a1a(%rip)     # 0x1514c
movl %ebp, %edi
callq 0x1250
movq %rbx, %rdi
callq 0x19d1
cmpl $0x14, %eax
jne 0x175a
incl %r14d
leaq 0xd7df(%rip), %rdi      # 0xef2f
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
xorl %edi, %edi
callq 0x1250
jmp 0x16a2
leaq 0xc9d5(%rip), %rdi      # 0xe142
movl %r14d, %esi
xorl %eax, %eax
callq 0x1030
leaq 0x10(%rsp), %rdi
callq 0x1930
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x179a
jmp 0x179a
jmp 0x179a
jmp 0x179a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1930
movq %rbx, %rdi
callq 0x1240
nop