#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 ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ DEFFILEMODE;
int /*<<< orphan*/ EX_IOERR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int filesize(int);
scalar_t__ malloc(int);
int open(char const *const, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int read_file(const char *const filename, char **outbuf, off_t *size) {
  int fd = open(filename, O_RDWR, DEFFILEMODE);
  if (fd < 0) {
    err(EX_IOERR, "open %s", filename);
    return -1;
  }
  off_t file_size = filesize(fd);
  if (file_size == -1) {
    return -1;
  }
  char *buf = (char *)malloc(file_size);
  ssize_t ret = read(fd, buf, file_size);
  if (ret < 0) {
    err(EX_IOERR, "read %s", filename);
    return -1;
  }
  if (ret != file_size) {
    err(EX_IOERR, "read got %d, expected %d", ret, file_size);
    return -1;
  }
  close(fd);
  *outbuf = buf;
  *size = file_size;
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c3 <read_file+0x63>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_file+0x12b>
mov    -0x24(%rbp),%edi
call   1080 <filesize@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11e4 <read_file+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_file+0x12b>
mov    -0x28(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1233 <read_file+0xd3>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_file+0x12b>
mov    -0x34(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1268 <read_file+0x108>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <read_file+0x12b>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
test   %eax,%eax
js     11e0 <read_file+0x80>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <filesize@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1239 <read_file+0xd9>
mov    %eax,%ebp
mov    %r14,(%rsp)
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1202 <read_file+0xa2>
cmp    %ebp,%eax
jne    121e <read_file+0xbe>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %r14,(%r15)
mov    (%rsp),%rax
mov    %ebp,(%rax)
xor    %r12d,%r12d
jmp    1239 <read_file+0xd9>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    $0xffffffff,%r12d
jmp    1239 <read_file+0xd9>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <err@plt>
jmp    1239 <read_file+0xd9>
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%edi
lea    0xde2(%rip),%rsi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <err@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@DEFFILEMODE = global i32 0
@EX_IOERR = global i32 0
@.str = [8 x i8] c"open %s\00"
@.str.1 = [8 x i8] c"read %s\00"
@.str.2 = [25 x i8] c"read got %d, expected %d\00"
define i32 @read_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @DEFFILEMODE
%15 = call i32 @open(i8* %12, i32 %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @EX_IOERR
%20 = load i8*, i8** %5
%21 = call i32 (i32, i8*, ...) @err(i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %4
br label %58
22: ; preds = %3
%23 = load i32, i32* %8
%24 = call i32 @filesize(i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -1, i32* %4
br label %58
28: ; preds = %22
%29 = load i32, i32* %9
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %10
%32 = load i32, i32* %8
%33 = load i8*, i8** %10
%34 = load i32, i32* %9
%35 = call i32 @read(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %28
%39 = load i32, i32* @EX_IOERR
%40 = load i8*, i8** %5
%41 = call i32 (i32, i8*, ...) @err(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i32 -1, i32* %4
br label %58
42: ; preds = %28
%43 = load i32, i32* %11
%44 = load i32, i32* %9
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i32, i32* @EX_IOERR
%48 = load i32, i32* %11
%49 = load i32, i32* %9
%50 = call i32 (i32, i8*, ...) @err(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %49)
store i32 -1, i32* %4
br label %58
51: ; preds = %42
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = load i8*, i8** %10
%55 = load i8**, i8*** %6
store i8* %54, i8** %55
%56 = load i32, i32* %9
%57 = load i32*, i32** %7
store i32 %56, i32* %57
store i32 0, i32* %4
br label %58
58: ; preds = %51, %46, %38, %27, %18
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@DEFFILEMODE = global i32 0
@EX_IOERR = global i32 0
@.str = [8 x i8] c"open %s\00"
@.str.1 = [8 x i8] c"read %s\00"
@.str.2 = [25 x i8] c"read got %d, expected %d\00"
define i32 @read_file(i8* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @DEFFILEMODE
%6 = tail call i32 @open(i8* %0, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @EX_IOERR
%10 = tail call i32 (i32, i8*, ...) @err(i32 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
11: ; preds = %3
%12 = tail call i32 @filesize(i32 %6)
%13 = icmp eq i32 %12, -1
br i1 %13, label %29, label %14
14: ; preds = %11
%15 = tail call i64 @malloc(i32 %12)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @read(i32 %6, i8* %16, i32 %12)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @EX_IOERR
%21 = tail call i32 (i32, i8*, ...) @err(i32 %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
22: ; preds = %14
%23 = icmp eq i32 %17, %12
br i1 %23, label %27, label %24
24: ; preds = %22
%25 = load i32, i32* @EX_IOERR
%26 = tail call i32 (i32, i8*, ...) @err(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %12)
br label %29
27: ; preds = %22
%28 = tail call i32 @close(i32 %6)
store i8* %16, i8** %1
store i32 %12, i32* %2
br label %29
29: ; preds = %11, %27, %24, %19, %8
%30 = phi i32 [ -1, %8 ], [ -1, %11 ], [ -1, %19 ], [ -1, %24 ], [ 0, %27 ]
ret i32 %30
}