#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;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, unsigned char *, size_t);
scalar_t__ stat(char const *, struct stat *);
unsigned char *read_file(const char *filename, size_t *len) {
  unsigned char *data = NULL;
  ssize_t total = 0;
  size_t filesize = 0;
  {
    struct stat info;
    if (stat(filename, &info)) {
      printf("failed to get file info [%s]: %m\n", filename);
      _exit(-1);
    }
    filesize = info.st_size;
  }
  {
    data = (char *)malloc(filesize);
    if (!data) {
      printf("failed to allocate buffer\n");
      _exit(-2);
    }
  }
  {
    int fd = open(filename, O_RDONLY);
    if (0 > fd) {
      printf("failed to open file [%s]: %m\n", filename);
      _exit(-3);
    }
    while (total < (ssize_t)filesize) {
      ssize_t now = read(fd, &data[total], filesize - total);
      if (0 > now) {
        printf("error while reading from file [%s]: %m\n", filename);
        _exit(-3);
      }
      if (0 == now) {
        printf("end-of-file\n");
        break;
      }
      total += now;
    }
    *len = (size_t)total;
    close(fd);
  }
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11cb <read_file+0x5b>
mov    -0x8(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <read_file+0x93>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xfffffffe,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
jle    123f <read_file+0xcf>
mov    -0x8(%rbp),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xfffffffd,%edi
call   1030 <_exit@plt>
jmp    1244 <read_file+0xd4>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12c6 <read_file+0x156>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmp    -0x40(%rbp),%rax
jbe    1296 <read_file+0x126>
mov    -0x8(%rbp),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xfffffffd,%edi
call   1030 <_exit@plt>
xor    %eax,%eax
cmp    -0x40(%rbp),%rax
jne    12b5 <read_file+0x145>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <read_file+0x156>
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1244 <read_file+0xd4>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    122a <read_file+0xaa>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1245 <read_file+0xc5>
mov    %rax,%r15
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     125b <read_file+0xdb>
mov    %eax,%r13d
xor    %ebp,%ebp
test   %rbx,%rbx
je     120d <read_file+0x8d>
nopl   (%rax)
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r13d,%edi
call   1070 <read@plt>
test   %rax,%rax
je     1201 <read_file+0x81>
add    %rax,%rbp
cmp    %rbp,%rbx
ja     11e0 <read_file+0x60>
jmp    120d <read_file+0x8d>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,(%r14)
mov    %r13d,%edi
call   1060 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffe,%edi
call   1030 <_exit@plt>
lea    0xdc0(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffd,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [34 x i8] c"failed to get file info [%s]: %m\0A\00"
@.str.1 = [27 x i8] c"failed to allocate buffer\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [30 x i8] c"failed to open file [%s]: %m\0A\00"
@.str.3 = [40 x i8] c"error while reading from file [%s]: %m\0A\00"
@.str.4 = [13 x i8] c"end-of-file\0A\00"
define i8* @read_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
store i8* null, i8** %5
store i64 0, i64* %6
store i64 0, i64* %7
%11 = load i8*, i8** %3
%12 = call i64 @stat(i8* %11, %struct.stat* %8)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @_exit(i32 -1)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* %7
%21 = load i64, i64* %7
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @_exit(i32 -2)
unreachable
29: ; preds = %18
%30 = load i8*, i8** %3
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp sgt i32 0, %33
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @_exit(i32 -3)
unreachable
39: ; preds = %29
br label %40
40: ; preds = %64, %39
%41 = load i64, i64* %6
%42 = load i64, i64* %7
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %68
44: ; preds = %40
%45 = load i32, i32* %9
%46 = load i8*, i8** %5
%47 = load i64, i64* %6
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = load i64, i64* %7
%50 = load i64, i64* %6
%51 = sub i64 %49, %50
%52 = call i64 @read(i32 %45, i8* %48, i64 %51)
store i64 %52, i64* %10
%53 = load i64, i64* %10
%54 = icmp ugt i64 0, %53
br i1 %54, label %55, label %59
55: ; preds = %44
%56 = load i8*, i8** %3
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = call i32 @_exit(i32 -3)
unreachable
59: ; preds = %44
%60 = load i64, i64* %10
%61 = icmp eq i64 0, %60
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %68
64: ; preds = %59
%65 = load i64, i64* %10
%66 = load i64, i64* %6
%67 = add i64 %66, %65
store i64 %67, i64* %6
br label %40
68: ; preds = %62, %40
%69 = load i64, i64* %6
%70 = load i64*, i64** %4
store i64 %69, i64* %70
%71 = load i32, i32* %9
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %5
ret i8* %73
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [34 x i8] c"failed to get file info [%s]: %m\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [30 x i8] c"failed to open file [%s]: %m\0A\00"
@str = [26 x i8] c"failed to allocate buffer\00"
@str.5 = [12 x i8] c"end-of-file\00"
define i8* @read_file(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = call i32 @_exit(i32 -1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i64, i64* %11
%13 = call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%18 = call i32 @_exit(i32 -2)
unreachable
19: ; preds = %10
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %0, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = icmp eq i64 %12, 0
br i1 %24, label %39, label %30
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%27 = call i32 @_exit(i32 -3)
unreachable
28: ; preds = %30
%29 = icmp ugt i64 %12, %36
br i1 %29, label %30, label %39
30: ; preds = %23, %28
%31 = phi i64 [ %36, %28 ], [ 0, %23 ]
%32 = getelementptr inbounds i8, i8* %14, i64 %31
%33 = sub i64 %12, %31
%34 = call i64 @read(i32 %21, i8* %32, i64 %33)
%35 = icmp eq i64 %34, 0
%36 = add i64 %34, %31
br i1 %35, label %37, label %28
37: ; preds = %30
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %39
39: ; preds = %28, %23, %37
%40 = phi i64 [ %31, %37 ], [ 0, %23 ], [ %36, %28 ]
store i64 %40, i64* %1
%41 = call i32 @close(i32 %21)
ret i8* %14
}