#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
char *read_file_into_string(char filename[]) {
  struct stat sb;
  if (-1 == stat(filename, &sb)) {
    perror("stat file");
    printf("function: read_file_into_string\n");
    printf("filename: [%s]\n", filename);
    return NULL;
  }
  FILE *fp = fopen(filename, "r");
  if (NULL == fp) {
    perror("fopen file");
    printf("function: read_file_into_string\n");
    printf("filename: [%s]\n", filename);
    return NULL;
  }
  char *str = malloc(sb.st_size + 1);
  if (NULL == str) {
    perror("malloc");
    printf("function: read_file_into_string\n");
    printf("filename: [%s]\n", filename);
    return NULL;
  }
  int cnt = fread(str, 1, sb.st_size, fp);
  if (sb.st_size != cnt) {
    perror("fread");
    printf("function: read_file_into_string\n");
    printf("filename: [%s]\n", filename);
    return NULL;
  }
  fclose(fp);
  str[sb.st_size] = 0;
  return str;
}


x86 assembly (clang, -O0):
<read_file_into_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d1 <read_file_into_string+0x61>
lea    0xe61(%rip),%rdi
call   1090 <perror@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <read_file_into_string+0x188>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    122a <read_file_into_string+0xba>
lea    0xe45(%rip),%rdi
call   1090 <perror@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <read_file_into_string+0x188>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    127e <read_file_into_string+0x10e>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <read_file_into_string+0x188>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     12db <read_file_into_string+0x16b>
lea    0xda6(%rip),%rdi
call   1090 <perror@plt>
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f8 <read_file_into_string+0x188>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_into_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11e8 <read_file_into_string+0x68>
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f1 <read_file_into_string+0x71>
mov    %rax,%r12
movslq (%rsp),%rbx
lea    0x1(%rbx),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11fa <read_file_into_string+0x7a>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    %eax,%ebx
jne    1203 <read_file_into_string+0x83>
mov    %r12,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    122f <read_file_into_string+0xaf>
lea    0xe11(%rip),%rdi
jmp    120a <read_file_into_string+0x8a>
lea    0xe24(%rip),%rdi
jmp    120a <read_file_into_string+0x8a>
lea    0xe26(%rip),%rdi
jmp    120a <read_file_into_string+0x8a>
lea    0xe24(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
lea    0xde8(%rip),%rdi
xor    %r15d,%r15d
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"stat file\00"
@.str.1 = [33 x i8] c"function: read_file_into_string\0A\00"
@.str.2 = [16 x i8] c"filename: [%s]\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"fopen file\00"
@.str.5 = [7 x i8] c"malloc\00"
@.str.6 = [6 x i8] c"fread\00"
define i8* @read_file_into_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %4)
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %14)
store i8* null, i8** %2
br label %62
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp eq i32* null, %19
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i8* null, i8** %2
br label %62
26: ; preds = %16
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp eq i8* null, %31
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i8* null, i8** %2
br label %62
38: ; preds = %26
%39 = load i8*, i8** %6
%40 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i32*, i32** %5
%43 = call i32 @fread(i8* %39, i32 1, i32 %41, i32* %42)
store i32 %43, i32* %7
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%45 = load i32, i32* %44
%46 = load i32, i32* %7
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %53
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%51 = load i8*, i8** %3
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %51)
store i8* null, i8** %2
br label %62
53: ; preds = %38
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
%56 = load i8*, i8** %6
%57 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%58 = load i32, i32* %57
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %6
store i8* %61, i8** %2
br label %62
62: ; preds = %53, %48, %33, %21, %11
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"stat file\00"
@.str.2 = [16 x i8] c"filename: [%s]\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [11 x i8] c"fopen file\00"
@.str.5 = [7 x i8] c"malloc\00"
@.str.6 = [6 x i8] c"fread\00"
@str.9 = [32 x i8] c"function: read_file_into_string\00"
define noalias i8* @read_file_into_string(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
10: ; preds = %1
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = add nsw i32 %19, 1
%21 = tail call i8* @malloc(i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
27: ; preds = %17
%28 = tail call i32 @fread(i8* %21, i32 1, i32 %19, i32* %11)
%29 = icmp eq i32 %19, %28
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.9, i64 0, i64 0))
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
34: ; preds = %27
%35 = tail call i32 @fclose(i32* %11)
%36 = sext i32 %19 to i64
%37 = getelementptr inbounds i8, i8* %21, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %13, %30, %34, %23, %6
%39 = phi i8* [ null, %6 ], [ null, %13 ], [ null, %23 ], [ null, %30 ], [ %21, %34 ]
ret i8* %39
}