#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, char *);
void file_write(char *filename) {
  FILE *outputfile;
  int i;
  outputfile = fopen(filename, "w");
  if (outputfile) {
    for (i = 1; i <= 20; i++) {
      fprintf(outputfile, "%d", i);
      if (i % 5 == 0) {
        fprintf(outputfile, "\n");
      } else {
        fprintf(outputfile, " ");
      }
    }
    fclose(outputfile);
  } else {
    printf("Error: could not open %s for writing\n", filename);
  }
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ef <file_write+0xaf>
movl   $0x1,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jg     11e1 <file_write+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11bc <file_write+0x7c>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11ce <file_write+0x8e>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11d3 <file_write+0x93>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1172 <file_write+0x32>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11ff <file_write+0xbf>
mov    -0x8(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e4 <file_write+0x94>
mov    %rax,%r12
mov    $0x1,%ebp
lea    0xe84(%rip),%r14
mov    $0xa,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl %bpl,%r13d
imul   $0xcd,%r13d,%eax
shr    $0xa,%eax
lea    (%rax,%rax,4),%ebx
neg    %ebx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    %bl,%r13b
mov    $0x20,%edi
cmove  %r15d,%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
add    $0x1,%ebp
cmp    $0x15,%ebp
jne    1190 <file_write+0x40>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe1a(%rip),%rdi
mov    %rbp,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [38 x i8] c"Error: could not open %s for writing\0A\00"
define void @file_write(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %33
9: ; preds = %1
store i32 1, i32* %4
br label %10
10: ; preds = %27, %9
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 20
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = load i32, i32* %4
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %4
%18 = srem i32 %17, 5
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32*, i32** %3
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %13
%24 = load i32*, i32** %3
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %23, %20
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %10
30: ; preds = %10
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
br label %36
33: ; preds = %1
%34 = load i8*, i8** %2
%35 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %33, %30
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [38 x i8] c"Error: could not open %s for writing\0A\00"
define void @file_write(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1, %4
%5 = phi i32 [ %12, %4 ], [ 1, %1 ]
%6 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = trunc i32 %5 to i8
%8 = urem i8 %7, 5
%9 = icmp eq i8 %8, 0
%10 = select i1 %9, i32 10, i32 32
%11 = tail call i32 @fputc(i32 %10, i32* %2)
%12 = add nuw nsw i32 %5, 1
%13 = icmp eq i32 %12, 21
br i1 %13, label %14, label %4
14: ; preds = %4
%15 = tail call i32 @fclose(i32* %2)
br label %18
16: ; preds = %1
%17 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %14
ret void
}