#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*/ str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  FILE *f;
  char str[] = "Hello World";
  f = fopen("./test.txt", "w");
  if (!f) {
    perror("Error opening file");
    exit(1);
  }
  fwrite(str, sizeof(str) - 1, 1, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe93(%rip),%rax
mov    %rax,-0x24(%rbp)
mov    0xe91(%rip),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe8b(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <main+0x62>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    -0x24(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0xb,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x6f57206f6c6c6548,%rax
20 57 6f
mov    %rax,(%rsp)
movl   $0x646c72,0x8(%rsp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11ab <main+0x5b>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@__const.main.str = [12 x i8] c"Hello World\00"
@.str = [11 x i8] c"./test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [12 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [12 x i8]* %7 to i8*
%9 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32 @fwrite(i8* %16, i32 11, i32 1, i32* %17)
%19 = load i32*, i32** %6
%20 = call i32 @fclose(i32* %19)
ret i32 0
}

LLVM IR (-O2):
@__const.main.str = [12 x i8] c"Hello World\00"
@.str = [11 x i8] c"./test.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"Error opening file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [12 x i8]
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = call i32 @fwrite(i8* %4, i32 11, i32 1, i32* %5)
%12 = tail call i32 @fclose(i32* %5)
ret i32 0
}