#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 *);
int /*<<< orphan*/ 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");
    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    $0x30,%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    1202 <read_file+0xa2>
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)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
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    $0x30,%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*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = bitcast %struct.stat* %6 to i8*
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %6)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %37
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %4
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %8
%21 = load i8*, i8** %7
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %8
%25 = call i32 @fread(i8* %21, i32 1, i32 %23, i32* %24)
%26 = load i32*, i32** %8
%27 = call i32 @fclose(i32* %26)
%28 = load i8*, i8** %7
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32*, i32** %5
store i32 %34, i32* %35
%36 = load i8*, i8** %7
store i8* %36, i8** %3
br label %37
37: ; preds = %14, %13
%38 = load i8*, i8** %3
ret i8* %38
}

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 i32 @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
}