#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*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
scalar_t__ malloc(unsigned int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ read(int, char *, unsigned int);
int /*<<< orphan*/ stderr;
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 = (unsigned)sb.st_size;
  buf = (char *)malloc(size);
  if (buf == NULL) {
    rtn = ENOMEM;
    goto errOut;
  }
  rtn = (int)read(fd, buf, size);
  if (rtn != size) {
    if (rtn >= 0) {
      fprintf(stderr, "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    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     11bb <readFile+0x5b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <readFile+0x124>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11d9 <readFile+0x79>
jmp    1276 <readFile+0x116>
mov    -0x38(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1208 <readFile+0xa8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1276 <readFile+0x116>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x3c(%rbp),%eax
je     1256 <readFile+0xf6>
cmpl   $0x0,-0x24(%rbp)
jl     1245 <readFile+0xe5>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1271 <readFile+0x111>
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    1276 <readFile+0x116>
mov    -0x28(%rbp),%edi
call   1030 <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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
movl   $0x0,(%rdx)
movq   $0x0,(%rsi)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
test   %eax,%eax
jle    11b0 <readFile+0x50>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11c8 <readFile+0x68>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11b9 <readFile+0x59>
mov    0x2e09(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f5 <readFile+0x95>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    %ebp,%eax
jne    1200 <readFile+0xa0>
mov    %r12,(%r15)
mov    %ebp,(%r14)
xor    %ebp,%ebp
jmp    11a7 <readFile+0x47>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ebp
jmp    11a7 <readFile+0x47>
test   %eax,%eax
js     1219 <readFile+0xb9>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%ebp
jmp    11a7 <readFile+0x47>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@stderr = 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 %66
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 %62
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 i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = load i32, i32* @ENOMEM
store i32 %38, i32* %8
br label %62
39: ; preds = %28
%40 = load i32, i32* %9
%41 = load i8*, i8** %10
%42 = load i32, i32* %12
%43 = call i64 @read(i32 %40, i8* %41, i32 %42)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = load i32, i32* %12
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %56
48: ; preds = %39
%49 = load i32, i32* %8
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* @EIO
store i32 %55, i32* %8
br label %61
56: ; preds = %39
store i32 0, i32* %8
%57 = load i8*, i8** %10
%58 = load i8**, i8*** %6
store i8* %57, i8** %58
%59 = load i32, i32* %12
%60 = load i32*, i32** %7
store i32 %59, i32* %60
br label %61
61: ; preds = %56, %54
br label %62
62: ; preds = %61, %37, %27
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %8
store i32 %65, i32* %4
br label %66
66: ; preds = %62, %20
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"readFile: short read\0A\00"
@EIO = global i32 0
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 %38
11: ; preds = %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %35
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 i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* @ENOMEM
br label %35
23: ; preds = %14
%24 = tail call i64 @read(i32 %7, i8* %19, i32 %17)
%25 = trunc i64 %24 to i32
%26 = icmp eq i32 %25, %17
br i1 %26, label %34, label %27
27: ; preds = %23
%28 = icmp sgt i32 %25, -1
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %29, %27
%33 = load i32, i32* @EIO
br label %35
34: ; preds = %23
store i8* %19, i8** %1
store i32 %17, i32* %2
br label %35
35: ; preds = %32, %34, %11, %21
%36 = phi i32 [ %12, %11 ], [ %22, %21 ], [ %33, %32 ], [ 0, %34 ]
%37 = tail call i32 @close(i32 %7)
br label %38
38: ; preds = %35, %9
%39 = phi i32 [ %10, %9 ], [ %36, %35 ]
ret i32 %39
}