#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 /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *pFile;
  int buffer[] = {1, 2, 3, 4};
  if ((pFile = fopen("myfile.txt", "wb")) == NULL) {
    printf("cant open the file");
    exit(0);
  }
  fwrite(buffer, sizeof(int), 4, pFile);
  fclose(pFile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe94(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a9 <main+0x59>
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movaps 0xea4(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xea9(%rip),%rdi
lea    0xead(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a0 <main+0x50>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x4,%esi
mov    $0x4,%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    0xe77(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@__const.main.buffer = [4 x i32] [i32 1, i32 2, i32 3, i32 4]
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [19 x i8] c"cant open the file\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [4 x i32]
store i32 0, i32* %1
%4 = bitcast [4 x i32]* %3 to i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%12 = load i32*, i32** %2
%13 = call i32 @fwrite(i32* %11, i32 4, i32 4, i32* %12)
%14 = load i32*, i32** %2
%15 = call i32 @fclose(i32* %14)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buffer = [4 x i32] [i32 1, i32 2, i32 3, i32 4]
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [19 x i8] c"cant open the file\00"
define i32 @main() {
%1 = alloca [4 x i32]
%2 = bitcast [4 x i32]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%10 = call i32 @fwrite(i32* %9, i32 4, i32 4, i32* %3)
%11 = tail call i32 @fclose(i32* %3)
ret i32 0
}