#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 INT_MAX;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int main() {
  FILE *fh;
  int i;
  if ((fh = fopen("output", "a")) == NULL) {
    perror("could not open 'output' file");
    return (1);
  }
  for (i = 0; i < INT_MAX; i++) {
    if (fprintf(fh, "%d\n", i) < 0) {
      perror("fprintf() error");
      goto cleanup;
    }
    if (i % 4096 == 0) {
      if (fflush(fh) != 0) {
        perror("fflush() error");
        goto cleanup;
      }
    }
  }
cleanup:
  if (fclose(fh) != 0) {
    perror("error closing output");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeea(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1198 <main+0x48>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <main+0x116>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%eax
jge    122f <main+0xdf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    11df <main+0x8f>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
jmp    1234 <main+0xe4>
mov    -0x14(%rbp),%eax
mov    $0x1000,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    121c <main+0xcc>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
cmp    $0x0,%rax
je     1217 <main+0xc7>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
jmp    1234 <main+0xe4>
jmp    121c <main+0xcc>
jmp    1221 <main+0xd1>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119f <main+0x4f>
jmp    1234 <main+0xe4>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     125f <main+0x10f>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <main+0x116>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeee(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120c <main+0xbc>
mov    %rax,%rbx
mov    0x2e67(%rip),%r12
cmpl   $0x0,(%r12)
jle    11eb <main+0x9b>
xor    %ebp,%ebp
lea    0xe8e(%rip),%r15
lea    0xe8b(%rip),%r14
lea    0xe94(%rip),%r13
jmp    11b9 <main+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    (%r12),%ebp
jge    11eb <main+0x9b>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
test   %rax,%rax
js     11e3 <main+0x93>
test   $0xfff,%ebp
jne    11b0 <main+0x60>
mov    %rbx,%rdi
call   1050 <fflush@plt>
test   %rax,%rax
je     11b0 <main+0x60>
mov    %r13,%r14
mov    %r14,%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1215 <main+0xc5>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdef(%rip),%rdi
jmp    121c <main+0xcc>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11fd <main+0xad>

LLVM IR (-O0):
@.str = [7 x i8] c"output\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [29 x i8] c"could not open 'output' file\00"
@INT_MAX = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [16 x i8] c"fprintf() error\00"
@.str.5 = [15 x i8] c"fflush() error\00"
@.str.6 = [21 x i8] c"error closing output\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %43
8: ; preds = %0
store i32 0, i32* %3
br label %9
9: ; preds = %32, %8
%10 = load i32, i32* %3
%11 = load i32, i32* @INT_MAX
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = load i32*, i32** %2
%15 = load i32, i32* %3
%16 = call i64 @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %36
20: ; preds = %13
%21 = load i32, i32* %3
%22 = srem i32 %21, 4096
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i32*, i32** %2
%26 = call i64 @fflush(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %36
30: ; preds = %24
br label %31
31: ; preds = %30, %20
br label %32
32: ; preds = %31
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %9
35: ; preds = %9
br label %36
36: ; preds = %35, %28, %18
%37 = load i32*, i32** %2
%38 = call i64 @fclose(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %43
42: ; preds = %36
store i32 0, i32* %1
br label %43
43: ; preds = %42, %40, %6
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [7 x i8] c"output\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [29 x i8] c"could not open 'output' file\00"
@INT_MAX = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [16 x i8] c"fprintf() error\00"
@.str.5 = [15 x i8] c"fflush() error\00"
@.str.6 = [21 x i8] c"error closing output\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %26, label %3
3: ; preds = %0
%4 = load i32, i32* @INT_MAX
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %23
6: ; preds = %3, %16
%7 = phi i32 [ %17, %16 ], [ 0, %3 ]
%8 = tail call i64 @fprintf(i32* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = and i32 %7, 4095
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i64 @fflush(i32* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10, %13
%17 = add nuw nsw i32 %7, 1
%18 = load i32, i32* @INT_MAX
%19 = icmp slt i32 %17, %18
br i1 %19, label %6, label %23
20: ; preds = %13, %6
%21 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), %6 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %13 ]
%22 = tail call i32 @perror(i8* %21)
br label %23
23: ; preds = %16, %20, %3
%24 = tail call i64 @fclose(i32* %1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23, %0
%27 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), %0 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), %23 ]
%28 = tail call i32 @perror(i8* %27)
br label %29
29: ; preds = %26, %23
%30 = phi i32 [ 0, %23 ], [ 1, %26 ]
ret i32 %30
}