#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int writeFile() {
  FILE *f;
  const char *str =
      "This is a simple example showing how to write a string to a file.";
  f = fopen("/tmp/fileTest", "wt");
  if (f == NULL)
    return 1;
  fprintf(f, "%s\n", str);
  fflush(f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xee8(%rip),%rdi
lea    0xeef(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1181 <writeFile+0x41>
movl   $0x1,-0x4(%rbp)
jmp    11ae <writeFile+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xec3(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %rbx
lea    0xefa(%rip),%rdi
lea    0xf01(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1186 <writeFile+0x46>
mov    %rax,%rbx
lea    0xef0(%rip),%rsi
lea    0xe96(%rip),%rdx
mov    %rax,%rdi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fflush@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [66 x i8] c"This is a simple example showing how to write a string to a file.\00"
@.str.1 = [14 x i8] c"/tmp/fileTest\00"
@.str.2 = [3 x i8] c"wt\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @writeFile() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
store i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 1, i32* %1
br label %16
8: ; preds = %0
%9 = load i32*, i32** %2
%10 = load i8*, i8** %3
%11 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%12 = load i32*, i32** %2
%13 = call i32 @fflush(i32* %12)
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
store i32 0, i32* %1
br label %16
16: ; preds = %8, %7
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [66 x i8] c"This is a simple example showing how to write a string to a file.\00"
@.str.1 = [14 x i8] c"/tmp/fileTest\00"
@.str.2 = [3 x i8] c"wt\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @writeFile() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %7, label %3
3: ; preds = %0
%4 = tail call i32 @fprintf(i32* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @fflush(i32* %1)
%6 = tail call i32 @fclose(i32* %1)
br label %7
7: ; preds = %0, %3
%8 = phi i32 [ 0, %3 ], [ 1, %0 ]
ret i32 %8
}