#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 */
struct stat {
  int st_size;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
scalar_t__ stat(char const *, struct stat *);
char *read_file(const char *file_name, int *length) {
  struct stat st = {0};
  if (stat(file_name, &st)) {
    return NULL;
  } else {
    char *buffer = malloc(st.st_size + 1);
    FILE *fp = fopen(file_name, "rb");
    size_t fread_ret = 0;
    fread_ret = fread(buffer, 1, st.st_size, fp);
    fclose(fp);
    buffer[st.st_size] = '\0';
    *length = st.st_size;
    return buffer;
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11a4 <read_file+0x44>
movq   $0x0,-0x8(%rbp)
jmp    120e <read_file+0xae>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     117c <read_file+0x2c>
xor    %r15d,%r15d
jmp    11c6 <read_file+0x76>
mov    0x8(%rsp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movslq 0x8(%rsp),%rax
movb   $0x0,(%r15,%rax,1)
mov    %eax,(%r14)
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [3 x i8] c"rb\00"
define i8* @read_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = bitcast %struct.stat* %6 to i8*
%11 = load i8*, i8** %4
%12 = call i64 @stat(i8* %11, %struct.stat* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %38
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %8
store i64 0, i64* %9
%22 = load i8*, i8** %7
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32*, i32** %8
%26 = call i64 @fread(i8* %22, i32 1, i32 %24, i32* %25)
store i64 %26, i64* %9
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %7
%30 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %5
store i32 %35, i32* %36
%37 = load i8*, i8** %7
store i8* %37, i8** %3
br label %38
38: ; preds = %15, %14
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [3 x i8] c"rb\00"
define noalias i8* @read_file(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.stat*
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = bitcast i64* %3 to i32*
%10 = load i32, i32* %9
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
%13 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %9
%15 = call i64 @fread(i8* %12, i32 1, i32 %14, i32* %13)
%16 = call i32 @fclose(i32* %13)
%17 = load i32, i32* %9
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
store i8 0, i8* %19
store i32 %17, i32* %1
br label %20
20: ; preds = %2, %8
%21 = phi i8* [ %12, %8 ], [ null, %2 ]
ret i8* %21
}