#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*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ malloc(int);
char *readFILE(char *path, long *length) {
  register FILE *fp;
  register char *p;
  struct stat s;
  if (!(fp = fopen(path, "r")))
    return NULL;
  if (fstat(fileno(fp), &s) == -1) {
    fclose(fp);
    return NULL;
  }
  if (!(p = (char *)malloc(sizeof(char) * (s.st_size + 1)))) {
    fclose(fp);
    return NULL;
  }
  if (!(*length = fread(p, sizeof(char), s.st_size, fp))) {
    free(p);
    fclose(fp);
    return NULL;
  }
  p[*length] = '\0';
  fclose(fp);
  return p;
}


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    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ab <readFILE+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    127a <readFILE+0x10a>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11de <readFILE+0x6e>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    127a <readFILE+0x10a>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1215 <readFILE+0xa5>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    127a <readFILE+0x10a>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    125a <readFILE+0xea>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    127a <readFILE+0x10a>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
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   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11dd <readFILE+0x6d>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     11e9 <readFILE+0x79>
mov    (%rsp),%r12d
lea    0x1(%r12),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e9 <readFILE+0x79>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11e1 <readFILE+0x71>
movb   $0x0,(%rbx,%rax,1)
jmp    11eb <readFILE+0x7b>
xor    %ebx,%ebx
jmp    11f3 <readFILE+0x83>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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