#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 {
  unsigned int st_size;
};

/* Variables and functions */
int EIO;
int ENOMEM;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *malloc(unsigned int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned char *, size_t);
int readFile(const char *fileName, unsigned char **bytes, unsigned *numBytes) {
  int rtn;
  int fd;
  unsigned char *buf;
  struct stat sb;
  unsigned size;
  *numBytes = 0;
  *bytes = NULL;
  fd = open(fileName, O_RDONLY, 0);
  if (fd < 0) {
    return errno;
  }
  rtn = fstat(fd, &sb);
  if (rtn) {
    goto errOut;
  }
  size = sb.st_size;
  buf = malloc(size);
  if (buf == NULL) {
    rtn = ENOMEM;
    goto errOut;
  }
  rtn = lseek(fd, 0, SEEK_SET);
  if (rtn < 0) {
    goto errOut;
  }
  rtn = read(fd, buf, (size_t)size);
  if (rtn != (int)size) {
    if (rtn >= 0) {
      printf("readFile: short read\n");
    }
    rtn = EIO;
  } else {
    rtn = 0;
    *bytes = buf;
    *numBytes = size;
  }
errOut:
  close(fd);
  return rtn;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11cb <readFile+0x5b>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b1 <readFile+0x141>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11e9 <readFile+0x79>
jmp    12a3 <readFile+0x133>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1217 <readFile+0xa7>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12a3 <readFile+0x133>
mov    -0x28(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    123c <readFile+0xcc>
jmp    12a3 <readFile+0x133>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%eax
mov    %eax,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     1283 <readFile+0x113>
cmpl   $0x0,-0x24(%rbp)
jl     1272 <readFile+0x102>
lea    0xd93(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    129e <readFile+0x12e>
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    12a3 <readFile+0x133>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
movl   $0x0,(%rdx)
movq   $0x0,(%rsi)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11bd <readFile+0x4d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11d7 <readFile+0x67>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11c6 <readFile+0x56>
mov    0x2e04(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121d <readFile+0xad>
mov    %rax,%r13
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %eax,%eax
js     1228 <readFile+0xb8>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <read@plt>
cmp    %r12d,%eax
jne    122c <readFile+0xbc>
mov    %r13,(%r15)
mov    %r12d,(%r14)
xor    %ebp,%ebp
jmp    11b4 <readFile+0x44>
mov    0x2dac(%rip),%rax
mov    (%rax),%ebp
jmp    11b4 <readFile+0x44>
mov    %eax,%ebp
jmp    11b4 <readFile+0x44>
test   %eax,%eax
js     123c <readFile+0xcc>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%ebp
jmp    11b4 <readFile+0x44>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@SEEK_SET = global i32 0
@.str = [22 x i8] c"readFile: short read\0A\00"
@EIO = global i32 0
define i32 @readFile(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 %struct.stat
%12 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%13 = load i32*, i32** %7
store i32 0, i32* %13
%14 = load i8**, i8*** %6
store i8* null, i8** %14
%15 = load i8*, i8** %5
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16, i32 0)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = load i32, i32* @errno
store i32 %21, i32* %4
br label %70
22: ; preds = %3
%23 = load i32, i32* %9
%24 = call i32 @fstat(i32 %23, %struct.stat* %11)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
br label %66
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = load i32, i32* @ENOMEM
store i32 %36, i32* %8
br label %66
37: ; preds = %28
%38 = load i32, i32* %9
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %38, i32 0, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %37
br label %66
44: ; preds = %37
%45 = load i32, i32* %9
%46 = load i8*, i8** %10
%47 = load i32, i32* %12
%48 = zext i32 %47 to i64
%49 = call i32 @read(i32 %45, i8* %46, i64 %48)
store i32 %49, i32* %8
%50 = load i32, i32* %8
%51 = load i32, i32* %12
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %60
53: ; preds = %44
%54 = load i32, i32* %8
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* @EIO
store i32 %59, i32* %8
br label %65
60: ; preds = %44
store i32 0, i32* %8
%61 = load i8*, i8** %10
%62 = load i8**, i8*** %6
store i8* %61, i8** %62
%63 = load i32, i32* %12
%64 = load i32*, i32** %7
store i32 %63, i32* %64
br label %65
65: ; preds = %60, %58
br label %66
66: ; preds = %65, %43, %35, %27
%67 = load i32, i32* %9
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %8
store i32 %69, i32* %4
br label %70
70: ; preds = %66, %20
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@SEEK_SET = global i32 0
@EIO = global i32 0
@str = [21 x i8] c"readFile: short read\00"
define i32 @readFile(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
store i32 0, i32* %2
store i8* null, i8** %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = load i32, i32* @errno
br label %39
11: ; preds = %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* @ENOMEM
br label %36
21: ; preds = %14
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @lseek(i32 %7, i32 0, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %36, label %25
25: ; preds = %21
%26 = zext i32 %16 to i64
%27 = tail call i32 @read(i32 %7, i8* %17, i64 %26)
%28 = icmp eq i32 %27, %16
br i1 %28, label %35, label %29
29: ; preds = %25
%30 = icmp sgt i32 %27, -1
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %31, %29
%34 = load i32, i32* @EIO
br label %36
35: ; preds = %25
store i8* %17, i8** %1
store i32 %16, i32* %2
br label %36
36: ; preds = %33, %35, %21, %11, %19
%37 = phi i32 [ %12, %11 ], [ %20, %19 ], [ %23, %21 ], [ %34, %33 ], [ 0, %35 ]
%38 = tail call i32 @close(i32 %7)
br label %39
39: ; preds = %36, %9
%40 = phi i32 [ %10, %9 ], [ %37, %36 ]
ret i32 %40
}