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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int fread(void *, size_t, int, int /*<<< orphan*/ *);
void *malloc(size_t);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *load_file(const char *filename, size_t *fsize) {
  FILE *f;
  void *buff;
  struct stat st;
  if (stat(filename, &st) == -1) {
    fprintf(stderr, "could not stat file %s: %s\n", filename, strerror(errno));
    return NULL;
  }
  if (!S_ISREG(st.st_mode)) {
    fprintf(stderr, "file %s is not a regular file!", filename);
    return NULL;
  }
  *fsize = st.st_size;
  if ((f = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "file %s could not be openned: %s\n", filename,
            strerror(errno));
    return NULL;
  }
  if ((buff = malloc(*fsize)) == NULL) {
    fprintf(stderr, "could not allocate buffer for file %s: %s\n", filename,
            strerror(errno));
    fclose(f);
    return NULL;
  }
  if (fread(buff, *fsize, 1, f) != 1) {
    fprintf(stderr, "error: short read from file %s\n", filename);
    fclose(f);
    return NULL;
  }
  fclose(f);
  return buff;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ed <load_file+0x6d>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <load_file+0x1e4>
mov    -0x30(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1226 <load_file+0xa6>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <load_file+0x1e4>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1296 <load_file+0x116>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <load_file+0x1e4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1300 <load_file+0x180>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <load_file+0x1e4>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1353 <load_file+0x1d3>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <load_file+0x1e4>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     122d <load_file+0xad>
mov    0x10(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     124d <load_file+0xcd>
mov    0x8(%rsp),%rax
mov    %rax,(%rbx)
lea    0xe77(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     126c <load_file+0xec>
mov    %rax,%r15
mov    (%rbx),%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     129e <load_file+0x11e>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     12ce <load_file+0x14e>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
xor    %r14d,%r14d
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12ce <load_file+0x14e>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb5(%rip),%rsi
jmp    128a <load_file+0x10a>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
xor    %r14d,%r14d
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12d6 <load_file+0x156>
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb3(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12d6 <load_file+0x156>
mov    0x2d3b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda3(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [28 x i8] c"could not stat file %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"file %s is not a regular file!\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [34 x i8] c"file %s could not be openned: %s\0A\00"
@.str.4 = [43 x i8] c"could not allocate buffer for file %s: %s\0A\00"
@.str.5 = [32 x i8] c"error: short read from file %s\0A\00"
define i8* @load_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca %struct.stat
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = call i32 @stat(i8* %9, %struct.stat* %8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
store i8* null, i8** %3
br label %70
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @S_ISREG(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i8* null, i8** %3
br label %70
27: ; preds = %18
%28 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i64*, i64** %5
store i64 %29, i64* %30
%31 = load i8*, i8** %4
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %6
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %40
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %4
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %36, i8* %38)
store i8* null, i8** %3
br label %70
40: ; preds = %27
%41 = load i64*, i64** %5
%42 = load i64, i64* %41
%43 = call i8* @malloc(i64 %42)
store i8* %43, i8** %7
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %53
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %4
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %47, i8* %49)
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
store i8* null, i8** %3
br label %70
53: ; preds = %40
%54 = load i8*, i8** %7
%55 = load i64*, i64** %5
%56 = load i64, i64* %55
%57 = load i32*, i32** %6
%58 = call i32 @fread(i8* %54, i64 %56, i32 1, i32* %57)
%59 = icmp ne i32 %58, 1
br i1 %59, label %60, label %66
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %4
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %62)
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
store i8* null, i8** %3
br label %70
66: ; preds = %53
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i8*, i8** %7
store i8* %69, i8** %3
br label %70
70: ; preds = %66, %60, %45, %34, %23, %12
%71 = load i8*, i8** %3
ret i8* %71
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [28 x i8] c"could not stat file %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"file %s is not a regular file!\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [34 x i8] c"file %s could not be openned: %s\0A\00"
@.str.4 = [43 x i8] c"could not allocate buffer for file %s: %s\0A\00"
@.str.5 = [32 x i8] c"error: short read from file %s\0A\00"
define noalias i8* @load_file(i8* %0, i64* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %49
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = tail call i32 @S_ISREG(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
20: ; preds = %12
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = load i64, i64* %21
store i64 %22, i64* %1
%23 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %28)
br label %49
30: ; preds = %20
%31 = load i64, i64* %1
%32 = tail call i8* @malloc(i64 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = tail call i8* @strerror(i32 %36)
%38 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %37)
%39 = tail call i32 @fclose(i32* %23)
br label %49
40: ; preds = %30
%41 = tail call i32 @fread(i8* %32, i64 %31, i32 1, i32* %23)
%42 = icmp eq i32 %41, 1
br i1 %42, label %47, label %43
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%46 = tail call i32 @fclose(i32* %23)
br label %49
47: ; preds = %40
%48 = tail call i32 @fclose(i32* %23)
br label %49
49: ; preds = %47, %43, %34, %25, %17, %7
%50 = phi i8* [ null, %7 ], [ null, %25 ], [ null, %34 ], [ null, %43 ], [ %32, %47 ], [ null, %17 ]
ret i8* %50
}