#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*/ SEEK_END;
char *calloc(int, long);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *make_str_clean(char *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *read_whole_str(const char *filename) {
  FILE *fp;
  long lsize;
  char *buffer;
  fp = fopen(filename, "r");
  if (!fp) {
    perror(filename);
    exit(1);
  }
  fseek(fp, 0L, SEEK_END);
  lsize = ftell(fp);
  rewind(fp);
  buffer = calloc(1, lsize + 1);
  if (!buffer) {
    fclose(fp);
    fputs("memory alloc fails", stderr);
    exit(1);
  }
  if (1 != fread(buffer, lsize, 1, fp)) {
    fclose(fp);
    free(buffer);
    fputs("entire read fails", stderr);
    exit(1);
  }
  fclose(fp);
  buffer = make_str_clean(buffer);
  return buffer;
}


x86 assembly (clang, -O0):
<read_whole_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <read_whole_str+0x3e>
mov    -0x8(%rbp),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1090 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1273 <read_whole_str+0xb3>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xd9e(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     12c9 <read_whole_str+0x109>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
lea    0xd5b(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <make_str_clean@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_whole_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe2f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     124e <read_whole_str+0x8e>
mov    %rax,%rbx
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x1(%r15),%rsi
mov    $0x1,%edi
call   1090 <calloc@plt>
test   %rax,%rax
je     1260 <read_whole_str+0xa0>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
cmp    $0x1,%ebp
jne    127a <read_whole_str+0xba>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <make_str_clean@plt>
mov    %r14,%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    0xd8a(%rip),%rdi
jmp    128f <read_whole_str+0xcf>
call   1030 <free@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
lea    0xd86(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [19 x i8] c"memory alloc fails\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"entire read fails\00"
define i8* @read_whole_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @perror(i8* %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i64 0, i32 %16)
%18 = load i32*, i32** %3
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %4
%20 = load i32*, i32** %3
%21 = call i32 @rewind(i32* %20)
%22 = load i64, i64* %4
%23 = add nsw i64 %22, 1
%24 = call i8* @calloc(i32 1, i64 %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %33, label %27
27: ; preds = %14
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @stderr
%31 = call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = load i8*, i8** %5
%35 = load i64, i64* %4
%36 = load i32*, i32** %3
%37 = call i32 @fread(i8* %34, i64 %35, i32 1, i32* %36)
%38 = icmp ne i32 1, %37
br i1 %38, label %39, label %47
39: ; preds = %33
%40 = load i32*, i32** %3
%41 = call i32 @fclose(i32* %40)
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* @stderr
%45 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %33
%48 = load i32*, i32** %3
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %5
%51 = call i8* @make_str_clean(i8* %50)
store i8* %51, i8** %5
%52 = load i8*, i8** %5
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [19 x i8] c"memory alloc fails\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"entire read fails\00"
define i8* @read_whole_str(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 %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* %0)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i64 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = tail call i32 @rewind(i32* %2)
%12 = add nsw i64 %10, 1
%13 = tail call i8* @calloc(i32 1, i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %7
%16 = tail call i32 @fclose(i32* %2)
%17 = load i32, i32* @stderr
%18 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
%21 = tail call i32 @fread(i8* %13, i64 %10, i32 1, i32* %2)
%22 = icmp eq i32 %21, 1
%23 = tail call i32 @fclose(i32* %2)
br i1 %22, label %29, label %24
24: ; preds = %20
%25 = tail call i32 @free(i8* %13)
%26 = load i32, i32* @stderr
%27 = tail call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = tail call i8* @make_str_clean(i8* %13)
ret i8* %30
}