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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *ReadFile(const char *strFileName) {
  FILE *pFile;
  int iSize = 8192;
  int iRead = 0;
  char *strFile;
  pFile = fopen(strFileName, "r");
  if (pFile == NULL) {
    return NULL;
  }
  strFile = (char *)malloc(iSize);
  do {
    if (iRead == iSize - 1) {
      iSize *= 2;
      strFile = (char *)realloc(strFile, iSize);
    }
    iRead += fread(&strFile[iRead], 1, iSize - iRead - 1, pFile);
  } while (!feof(pFile) && !ferror(pFile));
  strFile[iRead] = '\0';
  if (ferror(pFile)) {
    free(strFile);
    fclose(pFile);
    return NULL;
  }
  fclose(pFile);
  return strFile;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x2000,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <ReadFile+0x46>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <ReadFile+0x133>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11fc <ReadFile+0x7c>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1090 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jne    1259 <ReadFile+0xd9>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11d2 <ReadFile+0x52>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12a2 <ReadFile+0x122>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <ReadFile+0x133>
mov    -0x18(%rbp),%rdi
call   1060 <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
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1239 <ReadFile+0xb9>
mov    %rax,%r15
mov    $0x2000,%r12d
mov    $0x2000,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x1(%r12),%eax
cmp    %eax,%ebx
jne    11da <ReadFile+0x5a>
add    %r12d,%r12d
mov    %r14,%rdi
mov    %r12d,%esi
call   1090 <realloc@plt>
mov    %rax,%r14
movslq %ebx,%rdi
add    %r14,%rdi
mov    %ebx,%edx
not    %edx
add    %r12d,%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1050 <fread@plt>
add    %eax,%ebx
mov    %r15,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    120f <ReadFile+0x8f>
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11c0 <ReadFile+0x40>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     122f <ReadFile+0xaf>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    123c <ReadFile+0xbc>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @ReadFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 8192, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %72
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %7
br label %17
17: ; preds = %53, %13
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 1
%21 = icmp eq i32 %18, %20
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* %5
%24 = mul nsw i32 %23, 2
store i32 %24, i32* %5
%25 = load i8*, i8** %7
%26 = load i32, i32* %5
%27 = call i64 @realloc(i8* %25, i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
br label %29
29: ; preds = %22, %17
%30 = load i8*, i8** %7
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %5
%35 = load i32, i32* %6
%36 = sub nsw i32 %34, %35
%37 = sub nsw i32 %36, 1
%38 = load i32*, i32** %4
%39 = call i64 @fread(i8* %33, i32 1, i32 %37, i32* %38)
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %6
br label %44
44: ; preds = %29
%45 = load i32*, i32** %4
%46 = call i32 @feof(i32* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %44
%49 = load i32*, i32** %4
%50 = call i64 @ferror(i32* %49)
%51 = icmp ne i64 %50, 0
%52 = xor i1 %51, true
br label %53
53: ; preds = %48, %44
%54 = phi i1 [ false, %44 ], [ %52, %48 ]
br i1 %54, label %17, label %55
55: ; preds = %53
%56 = load i8*, i8** %7
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i32*, i32** %4
%61 = call i64 @ferror(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %55
%64 = load i8*, i8** %7
%65 = call i32 @free(i8* %64)
%66 = load i32*, i32** %4
%67 = call i32 @fclose(i32* %66)
store i8* null, i8** %2
br label %72
68: ; preds = %55
%69 = load i32*, i32** %4
%70 = call i32 @fclose(i32* %69)
%71 = load i8*, i8** %7
store i8* %71, i8** %2
br label %72
72: ; preds = %68, %63, %12
%73 = load i8*, i8** %2
ret i8* %73
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @ReadFile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %42, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i32 8192)
%6 = inttoptr i64 %5 to i8*
br label %7
7: ; preds = %29, %4
%8 = phi i32 [ 8192, %4 ], [ %18, %29 ]
%9 = phi i32 [ 0, %4 ], [ %26, %29 ]
%10 = phi i8* [ %6, %4 ], [ %19, %29 ]
%11 = add nsw i32 %8, -1
%12 = icmp eq i32 %9, %11
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = shl nsw i32 %8, 1
%15 = tail call i64 @realloc(i8* %10, i32 %14)
%16 = inttoptr i64 %15 to i8*
br label %17
17: ; preds = %13, %7
%18 = phi i32 [ %14, %13 ], [ %8, %7 ]
%19 = phi i8* [ %16, %13 ], [ %10, %7 ]
%20 = sext i32 %9 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = xor i32 %9, -1
%23 = add i32 %18, %22
%24 = tail call i64 @fread(i8* %21, i32 1, i32 %23, i32* %2)
%25 = trunc i64 %24 to i32
%26 = add i32 %9, %25
%27 = tail call i32 @feof(i32* %2)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = tail call i64 @ferror(i32* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %7, label %32
32: ; preds = %17, %29
%33 = sext i32 %26 to i64
%34 = getelementptr inbounds i8, i8* %19, i64 %33
store i8 0, i8* %34
%35 = tail call i64 @ferror(i32* %2)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = tail call i32 @free(i8* %19)
br label %39
39: ; preds = %32, %37
%40 = phi i8* [ null, %37 ], [ %19, %32 ]
%41 = tail call i32 @fclose(i32* %2)
br label %42
42: ; preds = %39, %1
%43 = phi i8* [ null, %1 ], [ %40, %39 ]
ret i8* %43
}