#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*/ exit(int);
size_t fclose(int /*<<< orphan*/ *);
size_t fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
void WriteToFile(const char *filename) {
  size_t retval = 0;
  char buffer[128];
  size_t buflen = 0;
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    fprintf(stderr, "Could not open file %s\n", filename);
    exit(1);
  }
  strcpy(buffer, "sample data");
  buflen = strlen(buffer);
  retval = fwrite(buffer, sizeof(char), buflen, fp);
  if (retval == buflen)
    fprintf(stdout, "data written %s\n", buffer);
  fprintf(stdout, "fwrite returned %d\n", retval);
  retval = fflush(fp);
  fprintf(stdout, "fflush returned %d\n", retval);
  retval = fclose(fp);
  fprintf(stdout, "fclose returned %d\n", retval);
}


x86 assembly (clang, -O0):
<WriteToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x98(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
jne    11ec <WriteToFile+0x6c>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    -0xa0(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x98(%rbp),%rax
jne    1264 <WriteToFile+0xe4>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    -0x90(%rbp),%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xa0(%rbp),%rdi
call   1070 <fflush@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xa0(%rbp),%rdi
call   1040 <fclose@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteToFile>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%rbx
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1256 <WriteToFile+0xd6>
mov    %rax,%r14
lea    0xe6c(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   10a0 <fwrite@plt>
mov    %rax,%r15
cmp    %rbx,%rax
jne    11f9 <WriteToFile+0x79>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2dd8(%rip),%rbx
mov    (%rbx),%edi
lea    0xe2e(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    (%rbx),%edi
lea    0xe27(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    (%rbx),%edi
lea    0xe20(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Could not open file %s\0A\00"
@.str.2 = [12 x i8] c"sample data\00"
@stdout = global i32 0
@.str.3 = [17 x i8] c"data written %s\0A\00"
@.str.4 = [20 x i8] c"fwrite returned %d\0A\00"
@.str.5 = [20 x i8] c"fflush returned %d\0A\00"
@.str.6 = [20 x i8] c"fclose returned %d\0A\00"
define void @WriteToFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [128 x i8]
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %2
store i64 0, i64* %3
store i64 0, i64* %5
%7 = load i8*, i8** %2
%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 ne i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %5
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%22 = load i64, i64* %5
%23 = load i32*, i32** %6
%24 = call i64 @fwrite(i8* %21, i32 1, i64 %22, i32* %23)
store i64 %24, i64* %3
%25 = load i64, i64* %3
%26 = load i64, i64* %5
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = load i32, i32* @stdout
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %28, %16
%33 = load i32, i32* @stdout
%34 = load i64, i64* %3
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i64 %34)
%36 = load i32*, i32** %6
%37 = call i64 @fflush(i32* %36)
store i64 %37, i64* %3
%38 = load i32, i32* @stdout
%39 = load i64, i64* %3
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %39)
%41 = load i32*, i32** %6
%42 = call i64 @fclose(i32* %41)
store i64 %42, i64* %3
%43 = load i32, i32* @stdout
%44 = load i64, i64* %3
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %44)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Could not open file %s\0A\00"
@.str.2 = [12 x i8] c"sample data\00"
@stdout = global i32 0
@.str.3 = [17 x i8] c"data written %s\0A\00"
@.str.4 = [20 x i8] c"fwrite returned %d\0A\00"
@.str.5 = [20 x i8] c"fflush returned %d\0A\00"
@.str.6 = [20 x i8] c"fclose returned %d\0A\00"
define void @WriteToFile(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%12 = call i64 @strlen(i8* %3)
%13 = call i64 @fwrite(i8* %3, i32 1, i64 %12, i32* %4)
%14 = icmp eq i64 %13, %12
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stdout
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %18
18: ; preds = %15, %10
%19 = load i32, i32* @stdout
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i64 %13)
%21 = call i64 @fflush(i32* %4)
%22 = load i32, i32* @stdout
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %21)
%24 = call i64 @fclose(i32* %4)
%25 = load i32, i32* @stdout
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i64 %24)
ret void
}