#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 {
  scalar_t__ 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 *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *malloc(unsigned int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ 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 = (unsigned)sb.st_size;
  buf = malloc(size);
  if (buf == NULL) {
    rtn = ENOMEM;
    goto errOut;
  }
  rtn = (int)lseek(fd, 0, SEEK_SET);
  if (rtn < 0) {
    goto errOut;
  }
  rtn = (int)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    12b2 <readFile+0x142>
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    12a4 <readFile+0x134>
mov    -0x38(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1218 <readFile+0xa8>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12a4 <readFile+0x134>
mov    -0x28(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    123d <readFile+0xcd>
jmp    12a4 <readFile+0x134>
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     1284 <readFile+0x114>
cmpl   $0x0,-0x24(%rbp)
jl     1273 <readFile+0x103>
lea    0xd92(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    129f <readFile+0x12f>
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    12a4 <readFile+0x134>
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),%r12
mov    %r12d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1220 <readFile+0xb0>
mov    %rax,%r13
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,%rbp
test   %ebp,%ebp
js     11b4 <readFile+0x44>
mov    %r12d,%edx
mov    %ebx,%edi
mov    %r13,%rsi
call   1060 <read@plt>
cmp    %r12d,%eax
jne    122b <readFile+0xbb>
mov    %r13,(%r15)
mov    %r12d,(%r14)
xor    %ebp,%ebp
jmp    11b4 <readFile+0x44>
mov    0x2da9(%rip),%rax
mov    (%rax),%ebp
jmp    11b4 <readFile+0x44>
test   %eax,%eax
js     123b <readFile+0xcb>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebp
jmp    11b4 <readFile+0x44>

LLVM IR (-O0):
%struct.stat = type { i64 }
@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 %73
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 %69
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = call i8* @malloc(i32 %32)
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 %69
38: ; preds = %28
%39 = load i32, i32* %9
%40 = load i32, i32* @SEEK_SET
%41 = call i64 @lseek(i32 %39, i32 0, i32 %40)
%42 = trunc i64 %41 to i32
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %38
br label %69
46: ; preds = %38
%47 = load i32, i32* %9
%48 = load i8*, i8** %10
%49 = load i32, i32* %12
%50 = zext i32 %49 to i64
%51 = call i64 @read(i32 %47, i8* %48, i64 %50)
%52 = trunc i64 %51 to i32
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = load i32, i32* %12
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %63
56: ; preds = %46
%57 = load i32, i32* %8
%58 = icmp sge i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
%62 = load i32, i32* @EIO
store i32 %62, i32* %8
br label %68
63: ; preds = %46
store i32 0, i32* %8
%64 = load i8*, i8** %10
%65 = load i8**, i8*** %6
store i8* %64, i8** %65
%66 = load i32, i32* %12
%67 = load i32*, i32** %7
store i32 %66, i32* %67
br label %68
68: ; preds = %63, %61
br label %69
69: ; preds = %68, %45, %36, %27
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %8
store i32 %72, i32* %4
br label %73
73: ; preds = %69, %20
%74 = load i32, i32* %4
ret i32 %74
}

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