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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int fstat(int /*<<< orphan*/, struct stat *);
char *malloc(int /*<<< orphan*/);
char *readfile(const char *fname, size_t *size) {
  struct stat st;
  FILE *f = fopen(fname, "rb");
  if (!f)
    return NULL;
  if (fstat(fileno(f), &st) == -1)
    return NULL;
  char *buf = malloc(st.st_size);
  *size = fread(buf, st.st_size, 1, f);
  fclose(f);
  return buf;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <readfile+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1202 <readfile+0xa2>
mov    -0x28(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11c6 <readfile+0x66>
movq   $0x0,-0x8(%rbp)
jmp    1202 <readfile+0xa2>
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c1 <readfile+0x61>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11c1 <readfile+0x61>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11c4 <readfile+0x64>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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