#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*/);
scalar_t__ malloc(unsigned int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, size_t);
int readFile(const char *fileName, unsigned char **bytes, unsigned *numBytes) {
  int rtn;
  int fd;
  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 = (char *)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 = (unsigned char *)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)
jg     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
jle    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 sle 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 %71
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 %67
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 i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %10
%34 = load i8*, i8** %10
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = load i32, i32* @ENOMEM
store i32 %37, i32* %8
br label %67
38: ; preds = %28
%39 = load i32, i32* %9
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %39, i32 0, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
br label %67
45: ; preds = %38
%46 = load i32, i32* %9
%47 = load i8*, i8** %10
%48 = load i32, i32* %12
%49 = zext i32 %48 to i64
%50 = call i32 @read(i32 %46, i8* %47, i64 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = load i32, i32* %12
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %45
%55 = load i32, i32* %8
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = load i32, i32* @EIO
store i32 %60, i32* %8
br label %66
61: ; preds = %45
store i32 0, i32* %8
%62 = load i8*, i8** %10
%63 = load i8**, i8*** %6
store i8* %62, i8** %63
%64 = load i32, i32* %12
%65 = load i32*, i32** %7
store i32 %64, i32* %65
br label %66
66: ; preds = %61, %59
br label %67
67: ; preds = %66, %44, %36, %27
%68 = load i32, i32* %9
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %8
store i32 %70, i32* %4
br label %71
71: ; preds = %67, %20
%72 = load i32, i32* %4
ret i32 %72
}

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, 1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = load i32, i32* @errno
br label %40
11: ; preds = %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* @ENOMEM
br label %37
22: ; preds = %14
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @lseek(i32 %7, i32 0, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %22
%27 = zext i32 %16 to i64
%28 = tail call i32 @read(i32 %7, i8* %18, i64 %27)
%29 = icmp eq i32 %28, %16
br i1 %29, label %36, label %30
30: ; preds = %26
%31 = icmp sgt i32 %28, -1
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = load i32, i32* @EIO
br label %37
36: ; preds = %26
store i8* %18, i8** %1
store i32 %16, i32* %2
br label %37
37: ; preds = %34, %36, %22, %11, %20
%38 = phi i32 [ %12, %11 ], [ %21, %20 ], [ %24, %22 ], [ %35, %34 ], [ 0, %36 ]
%39 = tail call i32 @close(i32 %7)
br label %40
40: ; preds = %37, %9
%41 = phi i32 [ %10, %9 ], [ %38, %37 ]
ret i32 %41
}