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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char const *);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
char *read_all_file(const char *input_file) {
  int buffer_size = 256;
  char *file_contents = malloc(buffer_size);
  file_contents[0] = '\0';
  FILE *file_stream = NULL;
  char buffer[buffer_size];
  ssize_t length_read = 0;
  int open_failed = 0;
  file_stream = fopen(input_file, "r");
  if (file_stream == NULL) {
    printf("Could not open file %s\n", input_file);
    open_failed = 1;
  }
  if (open_failed == 0) {
    while (fgets(buffer, buffer_size, file_stream) != NULL) {
      length_read += buffer_size;
      file_contents = realloc(file_contents, length_read + 1);
      strcat(file_contents, buffer);
    }
  }
  fclose(file_stream);
  return file_contents;
}


x86 assembly (clang, -O0):
<read_all_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x100,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <read_all_file+0x99>
mov    -0x8(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    126a <read_all_file+0xfa>
jmp    1218 <read_all_file+0xa8>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1265 <read_all_file+0xf5>
movslq -0xc(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <strcat@plt>
jmp    1218 <read_all_file+0xa8>
jmp    126a <read_all_file+0xfa>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
lea    0xe68(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1207 <read_all_file+0x97>
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1216 <read_all_file+0xa6>
mov    $0x101,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %r12,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
add    $0x100,%rbx
test   %rax,%rax
jne    11d0 <read_all_file+0x60>
jmp    1216 <read_all_file+0xa6>
lea    0xdf4(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Could not open file %s\0A\00"
define i8* @read_all_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %2
store i32 256, i32* %3
%10 = load i32, i32* %3
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
store i32* null, i32** %5
%14 = load i32, i32* %3
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
store i64 0, i64* %8
store i32 0, i32* %9
%18 = load i8*, i8** %2
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = load i8*, i8** %2
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 1, i32* %9
br label %25
25: ; preds = %22, %1
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %25
br label %29
29: ; preds = %34, %28
%30 = load i32, i32* %3
%31 = load i32*, i32** %5
%32 = call i32* @fgets(i8* %17, i32 %30, i32* %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = load i64, i64* %8
%38 = add nsw i64 %37, %36
store i64 %38, i64* %8
%39 = load i8*, i8** %4
%40 = load i64, i64* %8
%41 = add nsw i64 %40, 1
%42 = call i8* @realloc(i8* %39, i64 %41)
store i8* %42, i8** %4
%43 = load i8*, i8** %4
%44 = call i32 @strcat(i8* %43, i8* %17)
br label %29
45: ; preds = %29
br label %46
46: ; preds = %45, %25
%47 = load i32*, i32** %5
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %4
%50 = load i8*, i8** %6
ret i8* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Could not open file %s\0A\00"
define i8* @read_all_file(i8* %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
store i8 0, i8* %2
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = call i32* @fgets(i8* %4, i32 256, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %12
10: ; preds = %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
12: ; preds = %7, %12
%13 = phi i64 [ %15, %12 ], [ 0, %7 ]
%14 = phi i8* [ %17, %12 ], [ %2, %7 ]
%15 = add nuw nsw i64 %13, 256
%16 = add nuw nsw i64 %13, 257
%17 = call i8* @realloc(i8* %14, i64 %16)
%18 = call i32 @strcat(i8* %17, i8* %4)
%19 = call i32* @fgets(i8* %4, i32 256, i32* %5)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %12
21: ; preds = %12, %7, %10
%22 = phi i8* [ %2, %10 ], [ %2, %7 ], [ %17, %12 ]
%23 = call i32 @fclose(i32* %5)
ret i8* %22
}