// File: biotomas[P]ipasir/app/genipabones/genipabones.cpp
// Function: loadFormula(void*, char const*, std::vector<int, std::allocator<int>>&, int&)
bool loadFormula(void* solver, const char* filename, vector<int>& boundary, int& maxVar) {
	FILE* f = fopen(filename, "r");
	if (f == NULL) {
		return false;
	}
	maxVar = 0;
	int c = 0;
	bool neg = false;
	while (c != EOF) {
		c = fgetc(f);

		// comment or problem definition line
		if (c == 'c' || c == 'p') {
			c = fgetc(f);
			if (c == 'v' && fgetc(f) == 'i' && fgetc(f) == 'p' && fgetc(f) == ' ') {
				// boundary variables
				printf("c back-bone candidates specified in input file.\n");
				int num = 0;
				while(c != '\n') {
					c = fgetc(f);
					while (isdigit(c)) {
						num = num*10 + (c-'0');
						c = fgetc(f);
					}
					boundary.push_back(num);
					num = 0;
				}
			}
			// 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, num);
		}
	}
	fclose(f);
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1ba51(%rip), %rsi     # 0x1cde4
callq 0x1180
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x13ac
movb $0x0, -0x1(%rbp)
jmp 0x156d
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x34(%rbp)
movb $0x0, -0x35(%rbp)
cmpl $-0x1, -0x34(%rbp)
je 0x1560
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
cmpl $0x63, -0x34(%rbp)
je 0x13e7
cmpl $0x70, -0x34(%rbp)
jne 0x14af
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
cmpl $0x76, -0x34(%rbp)
jne 0x1494
movq -0x30(%rbp), %rdi
callq 0x1220
cmpl $0x69, %eax
jne 0x1494
movq -0x30(%rbp), %rdi
callq 0x1220
cmpl $0x70, %eax
jne 0x1494
movq -0x30(%rbp), %rdi
callq 0x1220
cmpl $0x20, %eax
jne 0x1494
leaq 0x18bd2(%rip), %rdi     # 0x1a004
movb $0x0, %al
callq 0x1030
movl $0x0, -0x3c(%rbp)
cmpl $0xa, -0x34(%rbp)
je 0x1492
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edi
callq 0x1100
cmpl $0x0, %eax
je 0x147c
imull $0xa, -0x3c(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
jmp 0x1452
movq -0x20(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x1940
movl $0x0, -0x3c(%rbp)
jmp 0x1440
jmp 0x1494
jmp 0x1496
cmpl $0xa, -0x34(%rbp)
je 0x14aa
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
jmp 0x1496
jmp 0x13c1
movl -0x34(%rbp), %edi
callq 0x1040
cmpl $0x0, %eax
je 0x14c1
jmp 0x13c1
cmpl $0x2d, -0x34(%rbp)
jne 0x14d0
movb $0x1, -0x35(%rbp)
jmp 0x13c1
movl -0x34(%rbp), %edi
callq 0x1100
cmpl $0x0, %eax
je 0x155b
movl -0x34(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edi
callq 0x1100
cmpl $0x0, %eax
je 0x151c
imull $0xa, -0x40(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x30, %ecx
addl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1220
movl %eax, -0x34(%rbp)
jmp 0x14f2
testb $0x1, -0x35(%rbp)
je 0x1529
imull $-0x1, -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movb $0x0, -0x35(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x154f
movl -0x40(%rbp), %ecx
movl %ecx, %eax
negl %eax
cmovnsl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
callq 0x2550
jmp 0x13c1
movq -0x30(%rbp), %rdi
callq 0x1120
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)