#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, ...);
scalar_t__ safe_read(int, char *, size_t);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
int file_read(const char *filename, void **datap, size_t *szp) {
  struct stat sb;
  size_t sz;
  char *data;
  int fd;
  if (stat(filename, &sb)) {
    printf("file_read: can't stat %s: %s\n", filename, strerror(errno));
    return -1;
  }
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("file_read: can't open file %s: %s\n", filename, strerror(errno));
    return -1;
  }
  sz = sb.st_size;
  data = malloc(sz);
  if (!datap) {
    printf("memory allocation failure: %s\n", strerror(errno));
    return -1;
  }
  if (safe_read(fd, data, sz)) {
    close(fd);
    free(data);
    return -1;
  }
  *datap = data;
  *szp = sz;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11e2 <file_read+0x62>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <file_read+0x15f>
mov    -0x10(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1238 <file_read+0xb8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <file_read+0x15f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1283 <file_read+0x103>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <file_read+0x15f>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <safe_read@plt>
cmp    $0x0,%rax
je     12ba <file_read+0x13a>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12df <file_read+0x15f>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11d8 <file_read+0x58>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1227 <file_read+0xa7>
mov    %eax,%ebp
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %r15,%r15
je     1241 <file_read+0xc1>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1080 <safe_read@plt>
test   %rax,%rax
je     1265 <file_read+0xe5>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11c6 <file_read+0x46>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde2(%rip),%rdi
jmp    11b9 <file_read+0x39>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdeb(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c6 <file_read+0x46>
mov    %r12,(%r15)
mov    %rbx,(%r14)
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11cb <file_read+0x4b>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [30 x i8] c"file_read: can't stat %s: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"file_read: can't open file %s: %s\0A\00"
@.str.2 = [31 x i8] c"memory allocation failure: %s\0A\00"
define i32 @file_read(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca %struct.stat
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%12 = load i8*, i8** %5
%13 = call i64 @stat(i8* %12, %struct.stat* %8)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %4
br label %60
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %29)
store i32 -1, i32* %4
br label %60
31: ; preds = %20
%32 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%33 = load i64, i64* %32
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %10
%36 = load i8**, i8*** %6
%37 = icmp ne i8** %36, null
br i1 %37, label %42, label %38
38: ; preds = %31
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i32 -1, i32* %4
br label %60
42: ; preds = %31
%43 = load i32, i32* %11
%44 = load i8*, i8** %10
%45 = load i64, i64* %9
%46 = call i64 @safe_read(i32 %43, i8* %44, i64 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i32, i32* %11
%50 = call i32 @close(i32 %49)
%51 = load i8*, i8** %10
%52 = call i32 @free(i8* %51)
store i32 -1, i32* %4
br label %60
53: ; preds = %42
%54 = load i8*, i8** %10
%55 = load i8**, i8*** %6
store i8* %54, i8** %55
%56 = load i64, i64* %9
%57 = load i64*, i64** %7
store i64 %56, i64* %57
%58 = load i32, i32* %11
%59 = call i32 @close(i32 %58)
store i32 0, i32* %4
br label %60
60: ; preds = %53, %48, %38, %26, %15
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [30 x i8] c"file_read: can't stat %s: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"file_read: can't open file %s: %s\0A\00"
@.str.2 = [31 x i8] c"memory allocation failure: %s\0A\00"
define i32 @file_read(i8* %0, i8** writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %37
12: ; preds = %3
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
br label %37
20: ; preds = %12
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i64, i64* %21
%23 = call i8* @malloc(i64 %22)
%24 = icmp eq i8** %1, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %27)
br label %37
29: ; preds = %20
%30 = call i64 @safe_read(i32 %14, i8* %23, i64 %22)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = call i32 @close(i32 %14)
%34 = call i32 @free(i8* %23)
br label %37
35: ; preds = %29
store i8* %23, i8** %1
store i64 %22, i64* %2
%36 = call i32 @close(i32 %14)
br label %37
37: ; preds = %35, %32, %25, %16, %8
%38 = phi i32 [ -1, %8 ], [ -1, %16 ], [ -1, %32 ], [ 0, %35 ], [ -1, %25 ]
ret i32 %38
}