// File: /workspace/github_cpp_makefile_star2_O2/biotomas[P]ipasir/app/genipafolio/genipafolio.cpp
// Function: main
int main(int argc, char** argv) {

	puts("c [genipafolio] USAGE: ./pfolio dimacs.cnf [#threads=4]");

	srand(2015);
	char* filename = argv[1];
	int cores = 4;
	if (argc > 2) {
		cores = atoi(argv[2]);
	}

	void** solvers = (void**)malloc(cores*sizeof(void*));
	Thread** threads = (Thread**)malloc(cores*sizeof(Thread*));

	printf("c [genipafolio] Solving %s with %d cores using %s and shuffling.\n", filename, cores, ipasir_signature());

	vector<vector<int> > fla;
	loadFormula(fla, filename);

	for (int i = 0; i < cores; i++) {
		// initialize the solver
		solvers[i] = ipasir_init();
		// set temination callback
		ipasir_set_terminate(solvers[i], NULL, terminator);
		// add the clauses (might be shuffled)
		for (size_t j = 0; j < fla.size(); j++) {
			vector<int>& cls = fla[j];
			for (size_t k = 0; k < cls.size(); k++) {
				ipasir_add(solvers[i], cls[k]);
			}
			ipasir_add(solvers[i], 0);
		}
		// start the solver
		threads[i] = new Thread(solverThread, solvers[i]);
		// shuffle the clauses for the next solver
		random_shuffle(fla.begin(), fla.end(), randGen);
	}
	// wait for each solver to stop and release them
	for (int i = 0; i < cores; i++) {
		threads[i]->join();
		ipasir_release(solvers[i]);
	}
	free(solvers);
	free(threads);
	printf("c [genipafolio] All done, result = %d\n", result);
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xdaf0(%rip), %rdi      # 0xf02e
callq 0x1210
movl $0x7df, %edi            # imm = 0x7DF
callq 0x1130
movq 0x8(%rbx), %r14
cmpl $0x3, %ebp
jl 0x1561
movq 0x10(%rbx), %rdi
callq 0x1220
jmp 0x1564
pushq $0x4
popq %rax
movslq %eax, %r13
movq %rax, %rbx
leaq (,%r13,8), %r15
movq %r15, %rdi
callq 0x11e0
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x11e0
movq %rax, %r15
callq 0x1c64
leaq 0xdad0(%rip), %rdi      # 0xf066
xorl %r12d, %r12d
movq %r14, %rsi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1030
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq %r14, %rsi
callq 0x137c
testl %ebx, %ebx
cmovlel %r12d, %ebx
pushq $0x18
popq %r14
movq %rbx, 0x30(%rsp)
movq %r15, 0x28(%rsp)
cmpq %rbx, %r12
je 0x169d
callq 0x1c6c
movq %rax, %r13
movq 0x8(%rsp), %rax
movq %r13, (%rax,%r12,8)
movq %r13, %rdi
xorl %esi, %esi
leaq -0x113(%rip), %rdx      # 0x14e8
callq 0x1d08
xorl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rax
subq %r15, %rax
cqto
idivq %r14
cmpq %rax, %rbp
jae 0x1654
imulq $0x18, %rbp, %rax
addq %rax, %r15
xorl %ebx, %ebx
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rcx, %rbx
jae 0x1645
movl (%rax,%rbx,4), %esi
movq %r13, %rdi
callq 0x1cdb
incq %rbx
jmp 0x1622
movq %r13, %rdi
xorl %esi, %esi
callq 0x1cdb
incq %rbp
jmp 0x1602
pushq $0x8
popq %rdi
callq 0x1150
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r15
movq %rax, %rbp
movq %rax, %rdi
leaq -0x184(%rip), %rsi      # 0x14ef
movq %r13, %rdx
callq 0x1792
movq %rbp, (%r15,%r12,8)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq -0x17b(%rip), %rdx      # 0x1515
callq 0x179f
incq %r12
jmp 0x15d5
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x16c1
movq (%r15,%r14,8), %rdi
callq 0x1800
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x1cc9
incq %r14
jmp 0x16a0
movq 0x8(%rsp), %rdi
callq 0x11b0
movq %r15, %rdi
callq 0x11b0
movl 0x14a83(%rip), %esi     # 0x1615c
leaq 0xd9c8(%rip), %rdi      # 0xf0a8
xorl %eax, %eax
callq 0x1030
movl 0x14a6f(%rip), %ebx     # 0x1615c
leaq 0x10(%rsp), %rdi
callq 0x180a
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1710
jmp 0x1710
jmp 0x1710
jmp 0x1710
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x180a
movq %rbx, %rdi
callq 0x1260
nop