#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ memset(char *, char, long);
int /*<<< orphan*/ stderr;
char *bf_read_file(char *filename) {
  char *buffer = 0;
  long length;
  FILE *f = fopen(filename, "rb");
  if (f) {
    fseek(f, 0, SEEK_END);
    length = ftell(f);
    fseek(f, 0, SEEK_SET);
    buffer = malloc(length + 1);
    memset(buffer, '\0', length + 1);
    if (buffer) {
      int read_bytes = fread(buffer, 1, length, f);
      if (read_bytes != length) {
        fprintf(stderr, "Error while reading file: %s\n", filename);
        return NULL;
      }
    }
    fclose(f);
    return buffer;
  } else {
    fprintf(stderr, "File not found: %s\n", filename);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<bf_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127f <bf_read_file+0xff>
mov    -0x28(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
je     1269 <bf_read_file+0xe9>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1264 <bf_read_file+0xe4>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb1(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a0 <bf_read_file+0x120>
jmp    1269 <bf_read_file+0xe9>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a0 <bf_read_file+0x120>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bf_read_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe6d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1217 <bf_read_file+0x97>
mov    %rax,%r15
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x1(%r12),%r13
mov    %r13,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1050 <memset@plt>
test   %rbx,%rbx
je     120d <bf_read_file+0x8d>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cltq
cmp    %rax,%r12
jne    1229 <bf_read_file+0xa9>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1243 <bf_read_file+0xc3>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
jmp    1239 <bf_read_file+0xb9>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error while reading file: %s\0A\00"
@.str.2 = [20 x i8] c"File not found: %s\0A\00"
define i8* @bf_read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %48
12: ; preds = %1
%13 = load i32*, i32** %6
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %6
%17 = call i64 @ftell(i32* %16)
store i64 %17, i64* %5
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i64, i64* %5
%22 = add nsw i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = add nsw i64 %25, 1
%27 = call i32 @memset(i8* %24, i8 signext 0, i64 %26)
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %44
30: ; preds = %12
%31 = load i8*, i8** %4
%32 = load i64, i64* %5
%33 = load i32*, i32** %6
%34 = call i32 @fread(i8* %31, i32 1, i64 %32, i32* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = load i64, i64* %5
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %30
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %3
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i8* null, i8** %2
br label %52
43: ; preds = %30
br label %44
44: ; preds = %43, %12
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %4
store i8* %47, i8** %2
br label %52
48: ; preds = %1
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %3
%51 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %50)
store i8* null, i8** %2
br label %52
52: ; preds = %48, %44, %39
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error while reading file: %s\0A\00"
@.str.2 = [20 x i8] c"File not found: %s\0A\00"
define i8* @bf_read_file(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 %23, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = tail call i64 @ftell(i32* %2)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = add nsw i64 %7, 1
%11 = tail call i8* @malloc(i64 %10)
%12 = tail call i32 @memset(i8* %11, i8 signext 0, i64 %10)
%13 = icmp eq i8* %11, null
br i1 %13, label %21, label %14
14: ; preds = %4
%15 = tail call i32 @fread(i8* %11, i32 1, i64 %7, i32* %2)
%16 = sext i32 %15 to i64
%17 = icmp eq i64 %7, %16
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
21: ; preds = %14, %4
%22 = tail call i32 @fclose(i32* %2)
br label %26
23: ; preds = %1
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %18, %23, %21
%27 = phi i8* [ %11, %21 ], [ null, %18 ], [ null, %23 ]
ret i8* %27
}