#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free_l(void **);
int fwrite(int *, int, int, int /*<<< orphan*/ *);
int **genbf(int);
int length(int *);
int length_l(void **);
int /*<<< orphan*/ pprintf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strerror(int /*<<< orphan*/);
int mode_a(int nvars) {
  pprintf("mimir operation mode A\n");
  char dumpfile[100];
  snprintf(dumpfile, 10, "mim%d.bfs", nvars);
  pprintf("generating all balanced formulae of %d variables\n"
          "output file: %s\n",
          nvars, dumpfile);
  int **bfs = genbf(nvars);
  int n_bfs = length_l((void **)bfs);
  pprintf("complete - %d balanced formulae generated. writing to file\n",
          n_bfs);
  FILE *fh = fopen(dumpfile, "w+");
  if (fh == NULL) {
    pprintf("error: failed to open output file (%s)\n", strerror(errno));
    free_l((void **)bfs);
    return 1;
  }
  int i;
  for (i = 0; i < n_bfs; i++) {
    int writesize = length(bfs[i]) + 1;
    if (fwrite(bfs[i], sizeof(int), writesize, fh) != writesize) {
      pprintf("error: failed to write to file (%s)\n", strerror(errno));
      if (fclose(fh))
        pprintf("error: failed to close file (%s)\n", strerror(errno));
      free_l((void **)bfs);
      return 1;
    }
  }
  free_l((void **)bfs);
  pprintf("complete - %d balanced formulae successfully written\n", n_bfs);
  if (fclose(fh)) {
    pprintf("error: failed to close file (%s)\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mode_a>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xa,%esi
lea    0xe49(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%esi
lea    -0x70(%rbp),%rdx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
mov    -0x8(%rbp),%edi
call   1080 <genbf@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
call   1090 <length_l@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    126a <mode_a+0xca>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
mov    -0x78(%rbp),%rdi
call   1060 <free_l@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b3 <mode_a+0x213>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x7c(%rbp),%eax
jge    1352 <mode_a+0x1b2>
mov    -0x78(%rbp),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <length@plt>
add    $0x1,%eax
mov    %eax,-0x90(%rbp)
mov    -0x78(%rbp),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x90(%rbp),%edx
mov    -0x88(%rbp),%rcx
mov    $0x4,%esi
call   10b0 <fwrite@plt>
cmp    -0x90(%rbp),%eax
je     1339 <mode_a+0x199>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
mov    -0x88(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1324 <mode_a+0x184>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
mov    -0x78(%rbp),%rdi
call   1060 <free_l@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b3 <mode_a+0x213>
jmp    133e <mode_a+0x19e>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1274 <mode_a+0xd4>
mov    -0x78(%rbp),%rdi
call   1060 <free_l@plt>
mov    -0x7c(%rbp),%esi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
mov    -0x88(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     13ac <mode_a+0x20c>
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%esi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1040 <pprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b3 <mode_a+0x213>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mode_a>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebx
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1040 <pprintf@plt>
lea    0xe53(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xa,%esi
mov    %ebx,%ecx
call   1070 <snprintf@plt>
lea    0xe44(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <pprintf@plt>
mov    %ebx,%edi
call   1080 <genbf@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1090 <length_l@plt>
mov    %eax,%r15d
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <pprintf@plt>
lea    0xe8a(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ba <mode_a+0x11a>
mov    %rax,%r14
test   %r15d,%r15d
jle    126e <mode_a+0xce>
mov    %r15d,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12,%rbx,8),%rdi
call   1030 <length@plt>
mov    %eax,%ebp
add    $0x1,%ebp
mov    (%r12,%rbx,8),%rdi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
cmp    %ebp,%eax
jne    12d1 <mode_a+0x131>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1240 <mode_a+0xa0>
mov    %r12,%rdi
call   1060 <free_l@plt>
lea    0xe95(%rip),%rdi
xor    %ebx,%ebx
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <pprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     1320 <mode_a+0x180>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe41(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <pprintf@plt>
jmp    131b <mode_a+0x17b>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdd4(%rip),%rdi
jmp    130a <mode_a+0x16a>
mov    0x2d08(%rip),%rbx
mov    (%rbx),%edi
call   10c0 <strerror@plt>
lea    0xde5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <pprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     1313 <mode_a+0x173>
mov    (%rbx),%edi
call   10c0 <strerror@plt>
lea    0xde6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <pprintf@plt>
mov    %r12,%rdi
call   1060 <free_l@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"mimir operation mode A\0A\00"
@.str.1 = [10 x i8] c"mim%d.bfs\00"
@.str.2 = [66 x i8] c"generating all balanced formulae of %d variables\0Aoutput file: %s\0A\00"
@.str.3 = [60 x i8] c"complete - %d balanced formulae generated. writing to file\0A\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [40 x i8] c"error: failed to open output file (%s)\0A\00"
@errno = global i32 0
@.str.6 = [37 x i8] c"error: failed to write to file (%s)\0A\00"
@.str.7 = [34 x i8] c"error: failed to close file (%s)\0A\00"
@.str.8 = [54 x i8] c"complete - %d balanced formulae successfully written\0A\00"
define i32 @mode_a(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @snprintf(i8* %11, i32 10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%16 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %15)
%17 = load i32, i32* %3
%18 = call i32** @genbf(i32 %17)
store i32** %18, i32*** %5
%19 = load i32**, i32*** %5
%20 = bitcast i32** %19 to i8**
%21 = call i32 @length_l(i8** %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %35
28: ; preds = %1
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32**, i32*** %5
%33 = bitcast i32** %32 to i8**
%34 = call i32 @free_l(i8** %33)
store i32 1, i32* %2
br label %91
35: ; preds = %1
store i32 0, i32* %8
br label %36
36: ; preds = %74, %35
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %77
40: ; preds = %36
%41 = load i32**, i32*** %5
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32*, i32** %41, i64 %43
%45 = load i32*, i32** %44
%46 = call i32 @length(i32* %45)
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
%48 = load i32**, i32*** %5
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32*, i32** %48, i64 %50
%52 = load i32*, i32** %51
%53 = load i32, i32* %9
%54 = load i32*, i32** %7
%55 = call i32 @fwrite(i32* %52, i32 4, i32 %53, i32* %54)
%56 = load i32, i32* %9
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %73
58: ; preds = %40
%59 = load i32, i32* @errno
%60 = call i32 @strerror(i32 %59)
%61 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %60)
%62 = load i32*, i32** %7
%63 = call i64 @fclose(i32* %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %58
%66 = load i32, i32* @errno
%67 = call i32 @strerror(i32 %66)
%68 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %67)
br label %69
69: ; preds = %65, %58
%70 = load i32**, i32*** %5
%71 = bitcast i32** %70 to i8**
%72 = call i32 @free_l(i8** %71)
store i32 1, i32* %2
br label %91
73: ; preds = %40
br label %74
74: ; preds = %73
%75 = load i32, i32* %8
%76 = add nsw i32 %75, 1
store i32 %76, i32* %8
br label %36
77: ; preds = %36
%78 = load i32**, i32*** %5
%79 = bitcast i32** %78 to i8**
%80 = call i32 @free_l(i8** %79)
%81 = load i32, i32* %6
%82 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i32 %81)
%83 = load i32*, i32** %7
%84 = call i64 @fclose(i32* %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %77
%87 = load i32, i32* @errno
%88 = call i32 @strerror(i32 %87)
%89 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %88)
store i32 1, i32* %2
br label %91
90: ; preds = %77
store i32 0, i32* %2
br label %91
91: ; preds = %90, %86, %69, %28
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
@.str = [24 x i8] c"mimir operation mode A\0A\00"
@.str.1 = [10 x i8] c"mim%d.bfs\00"
@.str.2 = [66 x i8] c"generating all balanced formulae of %d variables\0Aoutput file: %s\0A\00"
@.str.3 = [60 x i8] c"complete - %d balanced formulae generated. writing to file\0A\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [40 x i8] c"error: failed to open output file (%s)\0A\00"
@errno = global i32 0
@.str.6 = [37 x i8] c"error: failed to write to file (%s)\0A\00"
@.str.7 = [34 x i8] c"error: failed to close file (%s)\0A\00"
@.str.8 = [54 x i8] c"complete - %d balanced formulae successfully written\0A\00"
define i32 @mode_a(i32 %0) {
%2 = alloca [100 x i8]
%3 = tail call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%6 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %4)
%7 = call i32** @genbf(i32 %0)
%8 = bitcast i32** %7 to i8**
%9 = call i32 @length_l(i8** %8)
%10 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = icmp sgt i32 %9, 0
br i1 %14, label %15, label %46
15: ; preds = %13
%16 = zext i32 %9 to i64
br label %24
17: ; preds = %1
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = call i32 @free_l(i8** %8)
br label %55
22: ; preds = %24
%23 = icmp eq i64 %33, %16
br i1 %23, label %46, label %24
24: ; preds = %15, %22
%25 = phi i64 [ 0, %15 ], [ %33, %22 ]
%26 = getelementptr inbounds i32*, i32** %7, i64 %25
%27 = load i32*, i32** %26
%28 = call i32 @length(i32* %27)
%29 = add nsw i32 %28, 1
%30 = load i32*, i32** %26
%31 = call i32 @fwrite(i32* %30, i32 4, i32 %29, i32* %11)
%32 = icmp eq i32 %31, %29
%33 = add nuw nsw i64 %25, 1
br i1 %32, label %22, label %34
34: ; preds = %24
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%38 = call i64 @fclose(i32* %11)
%39 = icmp eq i64 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %40, %34
%45 = call i32 @free_l(i8** %8)
br label %55
46: ; preds = %22, %13
%47 = call i32 @free_l(i8** %8)
%48 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.8, i64 0, i64 0), i32 %9)
%49 = call i64 @fclose(i32* %11)
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = call i32 @strerror(i32 %52)
%54 = call i32 (i8*, ...) @pprintf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %44, %51, %46, %17
%56 = phi i32 [ 1, %17 ], [ 1, %44 ], [ 1, %51 ], [ 0, %46 ]
ret i32 %56
}