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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
scalar_t__ stat(char const *, struct stat *);
char *ReadFile(const char *fileName) {
  struct stat statBuf;
  if (stat(fileName, &statBuf) != 0) {
    return 0;
  }
  int actLength = (int)statBuf.st_size;
  char *data = malloc(sizeof(char) * (actLength + 1));
  if (data && actLength > 0) {
    FILE *fp = fopen(fileName, "r");
    if (!fp || fread(data, actLength, 1, fp) != 1) {
      free(data);
      data = 0;
    }
    if (fp) {
      fclose(fp);
    }
  }
  if (data) {
    data[actLength] = 0;
  }
  return data;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1190 <ReadFile+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1249 <ReadFile+0xe9>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     122a <ReadFile+0xca>
cmpl   $0x0,-0x1c(%rbp)
jle    122a <ReadFile+0xca>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1200 <ReadFile+0xa0>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1211 <ReadFile+0xb1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1225 <ReadFile+0xc5>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
jmp    122a <ReadFile+0xca>
cmpq   $0x0,-0x28(%rbp)
je     1241 <ReadFile+0xe1>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
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    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     118a <ReadFile+0x2a>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%r14
lea    0x1(%r14),%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11e6 <ReadFile+0x86>
test   %r14d,%r14d
jle    11e6 <ReadFile+0x86>
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f5 <ReadFile+0x95>
mov    %rax,%r15
mov    %r12,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     11de <ReadFile+0x7e>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %r12,%r12
je     1178 <ReadFile+0x18>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
jmp    117b <ReadFile+0x1b>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1178 <ReadFile+0x18>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"r\00"
define i8* @ReadFile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %35
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = trunc i64 %8 to i32
%10 = add nsw i32 %9, 1
%11 = call i8* @malloc(i32 %10)
%12 = icmp ne i8* %11, null
%13 = icmp sgt i32 %9, 0
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %15, label %28
15: ; preds = %6
%16 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @free(i8* %11)
br label %35
20: ; preds = %15
%21 = call i32 @fread(i8* %11, i32 %9, i32 1, i32* %16)
%22 = icmp eq i32 %21, 1
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = call i32 @free(i8* %11)
br label %25
25: ; preds = %20, %23
%26 = phi i8* [ null, %23 ], [ %11, %20 ]
%27 = call i32 @fclose(i32* %16)
br label %28
28: ; preds = %25, %6
%29 = phi i8* [ %11, %6 ], [ %26, %25 ]
%30 = icmp eq i8* %29, null
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = shl i64 %8, 32
%33 = ashr exact i64 %32, 32
%34 = getelementptr inbounds i8, i8* %29, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %18, %28, %31, %1
%36 = phi i8* [ null, %1 ], [ %29, %31 ], [ null, %28 ], [ null, %18 ]
ret i8* %36
}