#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 *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, size_t);
char *readStringFromFile(const char *filename) {
  FILE *f = fopen(filename, "rb");
  if (f == NULL)
    return NULL;
  char *data = (char *)malloc(1024);
  size_t len = 0;
  size_t r;
  while (0 < (r = fread(&data[len], 1, 1024, f))) {
    len += r;
    data = (char *)realloc(data, len + 1024);
  }
  data = (char *)realloc(data, len + 1);
  data[len] = '\0';
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<readStringFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <readStringFromFile+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1226 <readStringFromFile+0xd6>
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jae    11f4 <readStringFromFile+0xa4>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x400,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    119e <readStringFromFile+0x4e>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readStringFromFile>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f1 <readStringFromFile+0xa1>
mov    %rax,%r14
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11d4 <readStringFromFile+0x84>
cs nopw 0x0(%rax,%rax,1)
lea    (%rax,%rbx,1),%rsi
add    $0x400,%rsi
add    %rax,%rbx
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
lea    (%rax,%rbx,1),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11a0 <readStringFromFile+0x50>
lea    0x1(%rbx),%rsi
mov    %r15,%rdi
call   1060 <realloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax,%rbx,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11f4 <readStringFromFile+0xa4>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i8* @readStringFromFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 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 %44
13: ; preds = %1
%14 = call i64 @malloc(i32 1024)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
store i64 0, i64* %6
br label %16
16: ; preds = %23, %13
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i32*, i32** %4
%21 = call i64 @fread(i8* %19, i32 1, i32 1024, i32* %20)
store i64 %21, i64* %7
%22 = icmp ult i64 0, %21
br i1 %22, label %23, label %32
23: ; preds = %16
%24 = load i64, i64* %7
%25 = load i64, i64* %6
%26 = add i64 %25, %24
store i64 %26, i64* %6
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = add i64 %28, 1024
%30 = call i64 @realloc(i8* %27, i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %5
br label %16
32: ; preds = %16
%33 = load i8*, i8** %5
%34 = load i64, i64* %6
%35 = add i64 %34, 1
%36 = call i64 @realloc(i8* %33, i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %5
%38 = load i8*, i8** %5
%39 = load i64, i64* %6
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %5
store i8* %43, i8** %2
br label %44
44: ; preds = %32, %12
%45 = load i8*, i8** %2
ret i8* %45
}

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