#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*/ *fopen(char const *, char *);
long fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, long);
char *realloc(char *, long);
char *file_read(char const *in_filepath) {
  char *result = NULL;
  long result_size = 0;
  char buffer[4096];
  long bytes_read;
  FILE *fp = fopen(in_filepath, "r");
  if (!fp)
    return NULL;
  while ((bytes_read = fread(buffer, 1, 4096, fp)) > 0) {
    char *new_result = realloc(result, result_size + bytes_read + 1);
    if (!new_result) {
      if (result)
        free(result);
      return NULL;
    }
    result = new_result;
    memcpy(result + result_size, buffer, bytes_read);
    result_size += bytes_read;
  }
  fclose(fp);
  result[result_size] = 0;
  return result;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    11b1 <file_read+0x51>
movq   $0x0,-0x8(%rbp)
jmp    128c <file_read+0x12c>
jmp    11b6 <file_read+0x56>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jle    126c <file_read+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x1028(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    1232 <file_read+0xd2>
cmpq   $0x0,-0x18(%rbp)
je     1225 <file_read+0xc5>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    128c <file_read+0x12c>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x1028(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11b6 <file_read+0x56>
mov    -0x1030(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1238 <file_read+0xd8>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,0x8(%rsp)
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jle    1214 <file_read+0xb4>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0x10(%rsp),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    (%rbx,%rbp,1),%rsi
add    $0x1,%rsi
mov    %r13,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     122b <file_read+0xcb>
mov    %rax,%r12
lea    (%rbx,%rbp,1),%r14
mov    %rax,%rdi
add    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    0x8(%rsp),%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%r13
mov    %r14,%rbp
test   %rax,%rax
jg     11c0 <file_read+0x60>
jmp    121a <file_read+0xba>
xor    %r14d,%r14d
xor    %r12d,%r12d
mov    0x8(%rsp),%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r12,%r14,1)
jmp    123b <file_read+0xdb>
test   %r13,%r13
je     1238 <file_read+0xd8>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @file_read(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %31, label %6
6: ; preds = %1
%7 = call i64 @fread(i8* %3, i32 1, i32 4096, i32* %4)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %6, %21
%10 = phi i64 [ %24, %21 ], [ %7, %6 ]
%11 = phi i8* [ %15, %21 ], [ null, %6 ]
%12 = phi i64 [ %13, %21 ], [ 0, %6 ]
%13 = add nsw i64 %10, %12
%14 = add nsw i64 %13, 1
%15 = call i8* @realloc(i8* %11, i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = icmp eq i8* %11, null
br i1 %18, label %31, label %19
19: ; preds = %17
%20 = call i32 @free(i8* %11)
br label %31
21: ; preds = %9
%22 = getelementptr inbounds i8, i8* %15, i64 %12
%23 = call i32 @memcpy(i8* %22, i8* %3, i64 %10)
%24 = call i64 @fread(i8* %3, i32 1, i32 4096, i32* %4)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %9, label %26
26: ; preds = %21, %6
%27 = phi i64 [ 0, %6 ], [ %13, %21 ]
%28 = phi i8* [ null, %6 ], [ %15, %21 ]
%29 = call i32 @fclose(i32* %4)
%30 = getelementptr inbounds i8, i8* %28, i64 %27
store i8 0, i8* %30
br label %31
31: ; preds = %19, %17, %1, %26
%32 = phi i8* [ %28, %26 ], [ null, %1 ], [ null, %17 ], [ null, %19 ]
ret i8* %32
}