#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_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
char *file_open(const char *fname) {
  FILE *file = fopen(fname, "rb");
  char *buffer;
  size_t sz_file;
  if (file == NULL) {
    fprintf(stdout, "Couldn't open file: %s\n", fname);
    exit(EXIT_FAILURE);
  }
  fseek(file, 0, SEEK_END);
  sz_file = ftell(file);
  rewind(file);
  buffer = ((char *)malloc((sz_file + 3) * sizeof(char)));
  fread(buffer, sz_file, 1, file);
  buffer[sz_file + 0] = '\n';
  buffer[sz_file + 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<file_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <file_open+0x52>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x3,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_open>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f1 <file_open+0x71>
mov    %rax,%r15
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <rewind@plt>
lea    0x3(%r14),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
movw   $0xa,(%rbx,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stdout = global i32 0
@.str.1 = [24 x i8] c"Couldn't open file: %s\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
define i8* @file_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @stdout
%12 = load i8*, i8** %2
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %3
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i32*, i32** %3
%23 = call i32 @rewind(i32* %22)
%24 = load i64, i64* %5
%25 = add i64 %24, 3
%26 = mul i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = load i64, i64* %5
%31 = load i32*, i32** %3
%32 = call i32 @fread(i8* %29, i64 %30, i32 1, i32* %31)
%33 = load i8*, i8** %4
%34 = load i64, i64* %5
%35 = add i64 %34, 0
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 10, i8* %36
%37 = load i8*, i8** %4
%38 = load i64, i64* %5
%39 = add i64 %38, 1
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stdout = global i32 0
@.str.1 = [24 x i8] c"Couldn't open file: %s\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
define i8* @file_open(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stdout
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %2, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %2)
%13 = tail call i32 @rewind(i32* %2)
%14 = add i64 %12, 3
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @fread(i8* %16, i64 %12, i32 1, i32* %2)
%18 = getelementptr inbounds i8, i8* %16, i64 %12
store i8 10, i8* %18
%19 = add i64 %12, 1
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8 0, i8* %20
ret i8* %16
}