#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 */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char getc(int /*<<< orphan*/ *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
char *readfile(void *space, char *filename, unsigned long long *strlen) {
  char ch, *buffer;
  FILE *fp;
  unsigned long long buffersize = 10000;
  unsigned long long len = 0;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "Opening of file %s failed. Exit forced.\n", filename);
    exit(EXIT_FAILURE);
  }
  buffer = realloc(NULL, sizeof(char) * (buffersize));
  while ((ch = getc(fp)) != EOF) {
    if (len == buffersize - 1) {
      buffersize = buffersize + 10000;
      buffer = realloc(buffer, sizeof(char) * (buffersize));
    }
    len++;
    buffer[len - 1] = (char)ch;
  }
  buffer[len] = '\0';
  fclose(fp);
  *strlen = len;
  return buffer;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x2710,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ca <readfile+0x6a>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
mov    0x2df1(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     125f <readfile+0xff>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    123c <readfile+0xdc>
mov    -0x38(%rbp),%rax
add    $0x2710,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11e1 <readfile+0x81>
mov    -0x28(%rbp),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%rbx
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1227 <readfile+0xc7>
mov    %rax,%r15
mov    %rbp,(%rsp)
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x2710,%esi
call   1050 <realloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1080 <getc@plt>
mov    0x2e38(%rip),%r14
cmp    (%r14),%al
jne    11db <readfile+0x7b>
movb   $0x0,(%r12,%rbp,1)
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rax
mov    %rbp,(%rax)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    $0x2710,%r13d
xor    %ebp,%ebp
jmp    1207 <readfile+0xa7>
nopw   0x0(%rax,%rax,1)
mov    %bl,(%r12,%rbp,1)
add    $0x1,%rbp
mov    %r15,%rdi
call   1080 <getc@plt>
mov    %eax,%ebx
cmp    (%r14),%al
je     11b5 <readfile+0x55>
lea    -0x1(%r13),%rax
cmp    %rax,%rbp
jne    11f0 <readfile+0x90>
add    $0x2710,%r13
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <realloc@plt>
mov    %rax,%r12
jmp    11f0 <readfile+0x90>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Opening of file %s failed. Exit forced.\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
define i8* @readfile(i8* %0, i8* %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64* %2, i64** %6
store i64 10000, i64* %10
store i64 0, i64* %11
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %5
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %3
%23 = load i64, i64* %10
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @realloc(i8* null, i32 %25)
store i8* %26, i8** %8
br label %27
27: ; preds = %47, %22
%28 = load i32*, i32** %9
%29 = call signext i8 @getc(i32* %28)
store i8 %29, i8* %7
%30 = sext i8 %29 to i32
%31 = load i8, i8* @EOF
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %30, %32
br i1 %33, label %34, label %55
34: ; preds = %27
%35 = load i64, i64* %11
%36 = load i64, i64* %10
%37 = sub i64 %36, 1
%38 = icmp eq i64 %35, %37
br i1 %38, label %39, label %47
39: ; preds = %34
%40 = load i64, i64* %10
%41 = add i64 %40, 10000
store i64 %41, i64* %10
%42 = load i8*, i8** %8
%43 = load i64, i64* %10
%44 = mul i64 1, %43
%45 = trunc i64 %44 to i32
%46 = call i8* @realloc(i8* %42, i32 %45)
store i8* %46, i8** %8
br label %47
47: ; preds = %39, %34
%48 = load i64, i64* %11
%49 = add i64 %48, 1
store i64 %49, i64* %11
%50 = load i8, i8* %7
%51 = load i8*, i8** %8
%52 = load i64, i64* %11
%53 = sub i64 %52, 1
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
br label %27
55: ; preds = %27
%56 = load i8*, i8** %8
%57 = load i64, i64* %11
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
%59 = load i32*, i32** %9
%60 = call i32 @fclose(i32* %59)
%61 = load i64, i64* %11
%62 = load i64*, i64** %6
store i64 %61, i64* %62
%63 = load i8*, i8** %8
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Opening of file %s failed. Exit forced.\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
define i8* @readfile(i8* nocapture readnone %0, i8* %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %3
%12 = tail call i8* @realloc(i8* null, i32 10000)
%13 = tail call signext i8 @getc(i32* %4)
%14 = load i8, i8* @EOF
%15 = icmp eq i8 %13, %14
br i1 %15, label %35, label %16
16: ; preds = %11, %27
%17 = phi i8 [ %32, %27 ], [ %13, %11 ]
%18 = phi i64 [ %30, %27 ], [ 0, %11 ]
%19 = phi i64 [ %29, %27 ], [ 10000, %11 ]
%20 = phi i8* [ %28, %27 ], [ %12, %11 ]
%21 = add i64 %19, -1
%22 = icmp eq i64 %18, %21
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = add i64 %19, 10000
%25 = trunc i64 %24 to i32
%26 = tail call i8* @realloc(i8* %20, i32 %25)
br label %27
27: ; preds = %23, %16
%28 = phi i8* [ %26, %23 ], [ %20, %16 ]
%29 = phi i64 [ %24, %23 ], [ %19, %16 ]
%30 = add i64 %18, 1
%31 = getelementptr inbounds i8, i8* %28, i64 %18
store i8 %17, i8* %31
%32 = tail call signext i8 @getc(i32* %4)
%33 = load i8, i8* @EOF
%34 = icmp eq i8 %32, %33
br i1 %34, label %35, label %16
35: ; preds = %27, %11
%36 = phi i8* [ %12, %11 ], [ %28, %27 ]
%37 = phi i64 [ 0, %11 ], [ %30, %27 ]
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @fclose(i32* %4)
store i64 %37, i64* %2
ret i8* %36
}