#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char const *);
int /*<<< orphan*/ stderr;
int main() {
  const char filename[] = "test.txt";
  const char data[] = "123456789\n";
  FILE *fp;
  int r;
  fp = fopen(filename, "w");
  if (fp == NULL) {
    fprintf(stderr, "Unable to create %s\n", filename);
    return (1);
  }
  r = fwrite(data, 1, 10, fp);
  if (r != 10) {
    fprintf(stderr, "Unable to write %d bytes to %s\n", r, filename);
    fclose(fp);
    return (1);
  }
  printf("Wrote %d bytes to %s\n", r, filename);
  r = fclose(fp);
  if (r != 0) {
    fprintf(stderr, "Unable to close file\n");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0xd(%rbp)
mov    0xe98(%rip),%al
mov    %al,-0x5(%rbp)
mov    0xe8f(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe8c(%rip),%ax
mov    %ax,-0x10(%rbp)
mov    0xe84(%rip),%al
mov    %al,-0xe(%rbp)
lea    -0xd(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d8 <main+0x88>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rdx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1282 <main+0x132>
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
je     122f <main+0xdf>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    -0xd(%rbp),%rcx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1282 <main+0x132>
mov    -0x24(%rbp),%esi
lea    -0xd(%rbp),%rdx
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     127b <main+0x12b>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1282 <main+0x132>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
movabs $0x3837363534333231,%rax
36 37 38
mov    %rax,0x18(%rsp)
movl   $0xa3938,0x1f(%rsp)
lea    0xe8c(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f6 <main+0xa6>
mov    %rax,%rbx
lea    0x18(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    $0xa,%eax
jne    1219 <main+0xc9>
lea    0xe89(%rip),%rdi
lea    0x8(%rsp),%rdx
mov    $0xa,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
je     1241 <main+0xf1>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1243 <main+0xf3>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    1243 <main+0xf3>
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%edi
lea    0xe02(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1243 <main+0xf3>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@__const.main.data = [11 x i8] c"123456789\0A\00"
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Unable to create %s\0A\00"
@.str.2 = [32 x i8] c"Unable to write %d bytes to %s\0A\00"
@.str.3 = [22 x i8] c"Wrote %d bytes to %s\0A\00"
@.str.4 = [22 x i8] c"Unable to close file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [9 x i8]
%3 = alloca [11 x i8]
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [9 x i8]* %2 to i8*
%7 = bitcast [11 x i8]* %3 to i8*
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 1, i32* %1
br label %41
16: ; preds = %0
%17 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i32 @fwrite(i8* %17, i32 1, i32 10, i32* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 10
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i32, i32* %5
%25 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %25)
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
store i32 1, i32* %1
br label %41
29: ; preds = %16
%30 = load i32, i32* %5
%31 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %30, i8* %31)
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %41
40: ; preds = %29
store i32 0, i32* %1
br label %41
41: ; preds = %40, %37, %22, %12
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@__const.main.data = [11 x i8] c"123456789\0A\00"
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Unable to create %s\0A\00"
@.str.2 = [32 x i8] c"Unable to write %d bytes to %s\0A\00"
@.str.3 = [22 x i8] c"Wrote %d bytes to %s\0A\00"
@.str.4 = [22 x i8] c"Unable to close file\0A\00"
define i32 @main() {
%1 = alloca [9 x i8]
%2 = alloca [11 x i8]
%3 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %24
10: ; preds = %0
%11 = call i32 @fwrite(i8* %4, i32 1, i32 10, i32* %5)
%12 = icmp eq i32 %11, 10
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %3)
%16 = call i32 @fclose(i32* %5)
br label %24
17: ; preds = %10
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 10, i8* %3)
%19 = call i32 @fclose(i32* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %17, %21, %13, %7
%25 = phi i32 [ 1, %7 ], [ 1, %13 ], [ 1, %21 ], [ 0, %17 ]
ret i32 %25
}