// File: biotomas[P]ipasir/app/genipaessentials/genipaessentials.cpp
// Function: loadFormulaDualRailed(void*, char const*, int*)
bool loadFormulaDualRailed(void* solver, const char* filename, int* outVariables) {
	FILE* f = fopen(filename, "r");
	if (f == NULL) {
		return false;
	}
	int maxVar = 0;
	int c = 0;
	bool neg = false;
	while (c != EOF) {
		c = fgetc(f);

		// comment or problem definition line
		if (c == 'c' || c == 'p') {
			// skip this line
			while(c != '\n') {
				c = fgetc(f);
			}
			continue;
		}
		// whitespace
		if (isspace(c)) {
			continue;
		}
		// negative
		if (c == '-') {
			neg = true;
			continue;
		}

		// number
		if (isdigit(c)) {
			int num = c - '0';
			c = fgetc(f);
			while (isdigit(c)) {
				num = num*10 + (c-'0');
				c = fgetc(f);
			}
			if (neg) {
				num *= -1;
			}
			neg = false;

			if (abs(num) > maxVar) {
				maxVar = abs(num);
			}
			// add to the solver
			ipasir_add(solver, getDualRailName(num));
		}
	}
	fclose(f);
	// add the extra clauses
	for (int var = 1; var <= maxVar; var++) {
		ipasir_add(solver, -getDualRailName(var));
		ipasir_add(solver, -getDualRailName(-var));
		ipasir_add(solver, 0);
	}
	*outVariables = maxVar;
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1ba91(%rip), %rsi     # 0x1ce30
callq 0x1180
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x13b8
movb $0x0, -0x1(%rbp)
jmp 0x1533
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movb $0x0, -0x31(%rbp)
cmpl $-0x1, -0x30(%rbp)
je 0x14bc
movq -0x28(%rbp), %rdi
callq 0x1200
movl %eax, -0x30(%rbp)
cmpl $0x63, -0x30(%rbp)
je 0x13ec
cmpl $0x70, -0x30(%rbp)
jne 0x1404
jmp 0x13ee
cmpl $0xa, -0x30(%rbp)
je 0x1402
movq -0x28(%rbp), %rdi
callq 0x1200
movl %eax, -0x30(%rbp)
jmp 0x13ee
jmp 0x13ca
movl -0x30(%rbp), %edi
callq 0x1040
cmpl $0x0, %eax
je 0x1413
jmp 0x13ca
cmpl $0x2d, -0x30(%rbp)
jne 0x141f
movb $0x1, -0x31(%rbp)
jmp 0x13ca
movl -0x30(%rbp), %edi
callq 0x1100
cmpl $0x0, %eax
je 0x14b7
movl -0x30(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1200
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %edi
callq 0x1100
cmpl $0x0, %eax
je 0x146f
imull $0xa, -0x38(%rbp), %eax
movl -0x30(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1200
movl %eax, -0x30(%rbp)
jmp 0x1445
testb $0x1, -0x31(%rbp)
je 0x147c
imull $-0x1, -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x31(%rbp)
movl -0x38(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl -0x2c(%rbp), %eax
jle 0x149c
movl -0x38(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x38(%rbp), %edi
callq 0x1340
movq -0x48(%rbp), %rdi
movl %eax, %esi
callq 0x2420
jmp 0x13ca
movq -0x28(%rbp), %rdi
callq 0x1120
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x1526
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x3c(%rbp), %edi
callq 0x1340
movq -0x58(%rbp), %rdi
xorl %esi, %esi
subl %eax, %esi
callq 0x2420
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
xorl %edi, %edi
subl -0x3c(%rbp), %edi
callq 0x1340
movq -0x50(%rbp), %rdi
xorl %esi, %esi
subl %eax, %esi
callq 0x2420
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x2420
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x14cc
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nop