#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/ *);
int write_file(const char *filename) {
  int i = 10;
  int retval = 0;
  FILE *file = fopen(filename, "w");
  if (NULL == file) {
    return 1;
  }
  while (i-- > 0) {
    if (EOF != fputs("A text row\n", file)) {
      continue;
    }
    retval = 2;
    break;
  }
  if (0 != fclose(file)) {
    retval = 3;
  }
  return retval;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xa,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1176 <write_file+0x46>
movl   $0x1,-0x4(%rbp)
jmp    11ed <write_file+0xbd>
jmp    117b <write_file+0x4b>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11c9 <write_file+0x99>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1040 <fputs@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     11c2 <write_file+0x92>
jmp    117b <write_file+0x4b>
movl   $0x2,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11e7 <write_file+0xb7>
movl   $0x3,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1270 <write_file+0x140>
mov    %rax,%rbx
mov    0x2e92(%rip),%r14
mov    (%r14),%rbp
lea    0xea2(%rip),%rdi
mov    %rax,%rsi
call   1040 <fputs@plt>
mov    $0x2,%r15d
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe81(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe66(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe4b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xe02(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xdeb(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xdd4(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
cmp    %rax,%rbp
je     125a <write_file+0x12a>
mov    (%r14),%rbp
lea    0xdbd(%rip),%rdi
mov    %rbx,%rsi
call   1040 <fputs@plt>
xor    %r15d,%r15d
cmp    %rax,%rbp
sete   %r15b
add    %r15d,%r15d
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
mov    $0x3,%eax
cmove  %r15d,%eax
jmp    1275 <write_file+0x145>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@EOF = global i64 0
@.str.1 = [12 x i8] c"A text row\0A\00"
define i32 @write_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
store i32 10, i32* %4
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* null, %9
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1, i32* %2
br label %31
12: ; preds = %1
br label %13
13: ; preds = %22, %12
%14 = load i32, i32* %4
%15 = add nsw i32 %14, -1
store i32 %15, i32* %4
%16 = icmp sgt i32 %14, 0
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i64, i64* @EOF
%19 = load i32*, i32** %6
%20 = call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %19)
%21 = icmp ne i64 %18, %20
br i1 %21, label %22, label %23
22: ; preds = %17
br label %13
23: ; preds = %17
store i32 2, i32* %5
br label %24
24: ; preds = %23, %13
%25 = load i32*, i32** %6
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 0, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 3, i32* %5
br label %29
29: ; preds = %28, %24
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %11
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@EOF = global i64 0
@.str.1 = [12 x i8] c"A text row\0A\00"
define i32 @write_file(i8* nocapture %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 %50, label %4
4: ; preds = %1
%5 = load i64, i64* @EOF
%6 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%7 = icmp eq i64 %5, %6
br i1 %7, label %45, label %8
8: ; preds = %4
%9 = load i64, i64* @EOF
%10 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%11 = icmp eq i64 %9, %10
br i1 %11, label %45, label %12
12: ; preds = %8
%13 = load i64, i64* @EOF
%14 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%15 = icmp eq i64 %13, %14
br i1 %15, label %45, label %16
16: ; preds = %12
%17 = load i64, i64* @EOF
%18 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%19 = icmp eq i64 %17, %18
br i1 %19, label %45, label %20
20: ; preds = %16
%21 = load i64, i64* @EOF
%22 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%23 = icmp eq i64 %21, %22
br i1 %23, label %45, label %24
24: ; preds = %20
%25 = load i64, i64* @EOF
%26 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%27 = icmp eq i64 %25, %26
br i1 %27, label %45, label %28
28: ; preds = %24
%29 = load i64, i64* @EOF
%30 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%31 = icmp eq i64 %29, %30
br i1 %31, label %45, label %32
32: ; preds = %28
%33 = load i64, i64* @EOF
%34 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%35 = icmp eq i64 %33, %34
br i1 %35, label %45, label %36
36: ; preds = %32
%37 = load i64, i64* @EOF
%38 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%39 = icmp eq i64 %37, %38
br i1 %39, label %45, label %40
40: ; preds = %36
%41 = load i64, i64* @EOF
%42 = tail call i64 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%43 = icmp eq i64 %41, %42
%44 = select i1 %43, i32 2, i32 0
br label %45
45: ; preds = %40, %36, %32, %28, %24, %20, %16, %12, %8, %4
%46 = phi i32 [ 2, %4 ], [ 2, %8 ], [ 2, %12 ], [ 2, %16 ], [ 2, %20 ], [ 2, %24 ], [ 2, %28 ], [ 2, %32 ], [ 2, %36 ], [ %44, %40 ]
%47 = tail call i64 @fclose(i32* %2)
%48 = icmp eq i64 %47, 0
%49 = select i1 %48, i32 %46, i32 3
br label %50
50: ; preds = %1, %45
%51 = phi i32 [ %49, %45 ], [ 1, %1 ]
ret i32 %51
}