// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x1aa1e(%rip), %rdi     # 0x1d02e
callq 0x2220
movl $0x7df, %edi            # imm = 0x7DF
callq 0x2140
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x4, -0x1c(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x2648
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2230
movl %eax, -0x1c(%rbp)
movslq -0x1c(%rbp), %rdi
shlq $0x3, %rdi
callq 0x21f0
movq %rax, -0x28(%rbp)
movslq -0x1c(%rbp), %rdi
shlq $0x3, %rdi
callq 0x21f0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x94(%rbp)
callq 0x4b40
movq -0xa0(%rbp), %rsi
movl -0x94(%rbp), %edx
movq %rax, %rcx
leaq 0x1a9cc(%rip), %rdi     # 0x1d066
xorl %eax, %eax
callq 0x2030
leaq -0x48(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x2b90
movq -0x90(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2390
jmp 0x26c3
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x28ac
callq 0x4b50
movq %rax, -0xa8(%rbp)
jmp 0x26e4
movq -0xa8(%rbp), %rdx
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x18a(%rip), %rdx      # 0x2580
xorl %eax, %eax
movl %eax, %esi
callq 0x4d00
jmp 0x2715
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2bb0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jae 0x27f9
movq -0x60(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2be0
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x68(%rbp), %rdi
callq 0x2c00
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jae 0x27d1
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xc0(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x2c20
movq -0xc0(%rbp), %rdi
movl (%rax), %esi
callq 0x4bf0
jmp 0x27b0
jmp 0x27b2
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x275d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x2946
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
callq 0x4bf0
jmp 0x27e6
jmp 0x27e8
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x271d
movl $0x8, %edi
callq 0x2160
movq %rax, -0xc8(%rbp)
jmp 0x280c
movq -0xc8(%rbp), %rdi
movq -0x28(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq -0x296(%rip), %rsi      # 0x2590
callq 0x2c40
jmp 0x282d
movq -0xc8(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x48(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x2d40
movq -0xd0(%rbp), %rdi
movq %rax, -0x78(%rbp)
callq 0x2d70
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
leaq -0x2a3(%rip), %rdx      # 0x25d0
callq 0x2c70
jmp 0x287a
jmp 0x287c
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x26ca
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x8, %esi
callq 0x2170
jmp 0x2946
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2900
movq -0x30(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2da0
jmp 0x28d7
movq -0x28(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4bc0
jmp 0x28ed
jmp 0x28ef
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x28b6
movq -0x28(%rbp), %rdi
callq 0x21c0
movq -0x30(%rbp), %rdi
callq 0x21c0
movl 0x22854(%rip), %esi     # 0x2516c
leaq 0x1a789(%rip), %rdi     # 0x1d0a8
xorl %eax, %eax
callq 0x2030
jmp 0x2928
movl 0x2283e(%rip), %eax     # 0x2516c
movl %eax, -0x4(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2dd0
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x2dd0
movq -0x50(%rbp), %rdi
callq 0x2270
nopl (%rax,%rax)