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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ stat(char *, struct stat *);
char *readfile(char *filename, int *size) {
  FILE *fp;
  char *buffer;
  struct stat info;
  if (stat(filename, &info) != 0)
    return NULL;
  if ((fp = fopen(filename, "r")) == NULL)
    return NULL;
  buffer = NULL;
  for (;;) {
    if ((buffer = (char *)malloc(info.st_size + 1)) == NULL)
      break;
    if (fread(buffer, 1, info.st_size, fp) != info.st_size) {
      free(buffer);
      buffer = NULL;
      break;
    }
    buffer[info.st_size] = '\0';
    if (size)
      *size = info.st_size;
    break;
  }
  (void)fclose(fp);
  return buffer;
}


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    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1194 <readfile+0x34>
movq   $0x0,-0x8(%rbp)
jmp    124f <readfile+0xef>
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bf <readfile+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    124f <readfile+0xef>
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e5 <readfile+0x85>
jmp    123e <readfile+0xde>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x30(%rbp),%eax
je     1219 <readfile+0xb9>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    123e <readfile+0xde>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     1239 <readfile+0xd9>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    123e <readfile+0xde>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     1182 <readfile+0x22>
xor    %ebx,%ebx
jmp    11ed <readfile+0x8d>
lea    0xe77(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     117e <readfile+0x1e>
mov    %rax,%r14
mov    0x8(%rsp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e3 <readfile+0x83>
mov    %rax,%rbx
mov    0x8(%rsp),%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    0x8(%rsp),%eax
jne    11db <readfile+0x7b>
cltq
movb   $0x0,(%rbx,%rax,1)
test   %r15,%r15
je     11e5 <readfile+0x85>
mov    0x8(%rsp),%eax
mov    %eax,(%r15)
jmp    11e5 <readfile+0x85>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i8* @readfile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i64 @stat(i8* %9, %struct.stat* %8)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* null, i8** %3
br label %56
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i8* null, i8** %3
br label %56
18: ; preds = %13
store i8* null, i8** %7
br label %19
19: ; preds = %18
%20 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %19
br label %52
27: ; preds = %19
%28 = load i8*, i8** %7
%29 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32*, i32** %6
%32 = call i32 @fread(i8* %28, i32 1, i32 %30, i32* %31)
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
store i8* null, i8** %7
br label %52
39: ; preds = %27
%40 = load i8*, i8** %7
%41 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8 0, i8* %44
%45 = load i32*, i32** %5
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%49 = load i32, i32* %48
%50 = load i32*, i32** %5
store i32 %49, i32* %50
br label %51
51: ; preds = %47, %39
br label %52
52: ; preds = %51, %36, %26
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %7
store i8* %55, i8** %3
br label %56
56: ; preds = %52, %17, %12
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i8* @readfile(i8* %0, i32* writeonly %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 %33
7: ; preds = %2
%8 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %33, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %30, label %17
17: ; preds = %10
%18 = load i32, i32* %11
%19 = call i32 @fread(i8* %15, i32 1, i32 %18, i32* %8)
%20 = load i32, i32* %11
%21 = icmp eq i32 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 @free(i8* %15)
br label %30
24: ; preds = %17
%25 = sext i32 %19 to i64
%26 = getelementptr inbounds i8, i8* %15, i64 %25
store i8 0, i8* %26
%27 = icmp eq i32* %1, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* %11
store i32 %29, i32* %1
br label %30
30: ; preds = %24, %28, %10, %22
%31 = phi i8* [ null, %10 ], [ null, %22 ], [ %15, %28 ], [ %15, %24 ]
%32 = call i32 @fclose(i32* %8)
br label %33
33: ; preds = %7, %2, %30
%34 = phi i8* [ %31, %30 ], [ null, %2 ], [ null, %7 ]
ret i8* %34
}