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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, long long, int /*<<< orphan*/ *);
char *malloc(long long);
scalar_t__ stat(char const *, struct stat *);
char *read_file(const char *file, long long *len) {
  struct stat st;
  char *ret = NULL;
  FILE *fp;
  if (stat(file, &st))
    return ret;
  *len = st.st_size;
  fp = fopen(file, "r");
  if (!fp)
    return ret;
  ret = malloc(*len);
  if (!ret)
    return NULL;
  fread(ret, 1, *len, fp);
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     118c <read_file+0x3c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <read_file+0xc5>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <read_file+0x73>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <read_file+0xc5>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11eb <read_file+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    1215 <read_file+0xc5>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
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,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     117d <read_file+0x2d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rax
mov    %rax,(%r15)
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     116e <read_file+0x1e>
mov    %rax,%r14
mov    (%r15),%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     116e <read_file+0x1e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1170 <read_file+0x20>

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

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