#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 */
scalar_t__ calloc(int, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int main() {
  char *buffer = NULL;
  size_t size1;
  size_t size2;
  FILE *filedes;
  filedes = fopen("out.txt", "w+");
  if (filedes == NULL) {
    printf("Could not open file\n");
  }
  size1 = 6;
  buffer = (char *)calloc(1, size1);
  if (buffer == NULL) {
    printf("Could not allocate memory\n");
  }
  buffer[0] = 'a';
  buffer[1] = 'b';
  buffer[2] = 'c';
  buffer[3] = 'd';
  buffer[4] = '\0';
  buffer[5] = 'f';
  buffer[6] = 'g';
  buffer[7] = 'p';
  size2 = 8;
  fwrite(buffer, 1, size2, filedes);
  free(buffer);
  buffer = NULL;
  fclose(filedes);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe82(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a5 <main+0x45>
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
movq   $0x6,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d6 <main+0x76>
lea    0xe4f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
movb   $0x61,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x62,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x63,0x2(%rax)
mov    -0x10(%rbp),%rax
movb   $0x64,0x3(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x4(%rax)
mov    -0x10(%rbp),%rax
movb   $0x66,0x5(%rax)
mov    -0x10(%rbp),%rax
movb   $0x67,0x6(%rax)
mov    -0x10(%rbp),%rax
movb   $0x70,0x7(%rax)
movq   $0x8,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    118b <main+0x2b>
lea    0xe9f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x6,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ae <main+0x4e>
lea    0xe62(%rip),%rdi
call   1040 <puts@plt>
movabs $0x7067660064636261,%rax
66 67 70
mov    %rax,(%rbx)
mov    $0x8,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [8 x i8] c"out.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [21 x i8] c"Could not open file\0A\00"
@.str.3 = [27 x i8] c"Could not allocate memory\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
store i32 0, i32* %1
store i8* null, i8** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
%7 = load i32*, i32** %5
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
store i64 6, i64* %3
%12 = load i64, i64* %3
%13 = call i64 @calloc(i32 1, i64 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i8*, i8** %2
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 97, i8* %21
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 98, i8* %23
%24 = load i8*, i8** %2
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8 99, i8* %25
%26 = load i8*, i8** %2
%27 = getelementptr inbounds i8, i8* %26, i64 3
store i8 100, i8* %27
%28 = load i8*, i8** %2
%29 = getelementptr inbounds i8, i8* %28, i64 4
store i8 0, i8* %29
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i64 5
store i8 102, i8* %31
%32 = load i8*, i8** %2
%33 = getelementptr inbounds i8, i8* %32, i64 6
store i8 103, i8* %33
%34 = load i8*, i8** %2
%35 = getelementptr inbounds i8, i8* %34, i64 7
store i8 112, i8* %35
store i64 8, i64* %4
%36 = load i8*, i8** %2
%37 = load i64, i64* %4
%38 = load i32*, i32** %5
%39 = call i32 @fwrite(i8* %36, i32 1, i64 %37, i32* %38)
%40 = load i8*, i8** %2
%41 = call i32 @free(i8* %40)
store i8* null, i8** %2
%42 = load i32*, i32** %5
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [8 x i8] c"out.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@str = [26 x i8] c"Could not allocate memory\00"
@str.4 = [20 x i8] c"Could not open file\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
%6 = tail call i64 @calloc(i32 1, i64 6)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
store i8 97, i8* %7
%12 = getelementptr inbounds i8, i8* %7, i64 1
store i8 98, i8* %12
%13 = getelementptr inbounds i8, i8* %7, i64 2
store i8 99, i8* %13
%14 = getelementptr inbounds i8, i8* %7, i64 3
store i8 100, i8* %14
%15 = getelementptr inbounds i8, i8* %7, i64 4
store i8 0, i8* %15
%16 = getelementptr inbounds i8, i8* %7, i64 5
store i8 102, i8* %16
%17 = getelementptr inbounds i8, i8* %7, i64 6
store i8 103, i8* %17
%18 = getelementptr inbounds i8, i8* %7, i64 7
store i8 112, i8* %18
%19 = tail call i32 @fwrite(i8* %7, i32 1, i64 8, i32* %1)
%20 = tail call i32 @free(i8* %7)
%21 = tail call i32 @fclose(i32* %1)
ret i32 0
}