#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;
};
typedef size_t ssize_t;

/* Variables and functions */
int EFBIG;
int EIO;
int ENOMEM;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ SIZE_MAX;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
scalar_t__ malloc(size_t);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
int readFile(const char *fileName, unsigned char **bytes, size_t *numBytes) {
  int rtn;
  int fd;
  char *buf;
  struct stat sb;
  size_t size;
  ssize_t rrc;
  *numBytes = 0;
  *bytes = NULL;
  fd = open(fileName, O_RDONLY);
  if (fd <= 0) {
    return errno;
  }
  rtn = fstat(fd, &sb);
  if (rtn) {
    goto errOut;
  }
  if (sb.st_size > SIZE_MAX) {
    rtn = EFBIG;
    goto errOut;
  }
  size = (size_t)sb.st_size;
  buf = (char *)malloc(size);
  if (buf == NULL) {
    rtn = ENOMEM;
    goto errOut;
  }
  rrc = read(fd, buf, size);
  if (rrc != size) {
    if (rtn >= 0) {
      free(buf);
      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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     11ca <readFile+0x5a>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c9 <readFile+0x159>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1090 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11e8 <readFile+0x78>
jmp    12bb <readFile+0x14b>
mov    -0x38(%rbp),%rax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jle    120d <readFile+0x9d>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12bb <readFile+0x14b>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123e <readFile+0xce>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12bb <readFile+0x14b>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     1299 <readFile+0x129>
cmpl   $0x0,-0x24(%rbp)
jl     1288 <readFile+0x118>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12b6 <readFile+0x146>
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    12bb <readFile+0x14b>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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
movq   $0x0,(%rdx)
movq   $0x0,(%rsi)
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jle    11d4 <readFile+0x64>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    123a <readFile+0xca>
mov    0x8(%rsp),%rbp
mov    0x2de2(%rip),%rax
cmp    (%rax),%rbp
jle    11df <readFile+0x6f>
mov    0x2e0e(%rip),%rax
jmp    1238 <readFile+0xc8>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebp
jmp    1241 <readFile+0xd1>
mov    %rbp,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     120b <readFile+0x9b>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
cmp    %rbp,%rax
jne    1214 <readFile+0xa4>
mov    %r12,(%r15)
mov    %rbp,(%r14)
xor    %ebp,%ebp
jmp    123a <readFile+0xca>
mov    0x2db6(%rip),%rax
jmp    1238 <readFile+0xc8>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@errno = global i32 0
@SIZE_MAX = global i64 0
@EFBIG = 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, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
store i64 0, i64* %2
store i8* null, i8** %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 1
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 i64, i64* %15
%17 = load i64, i64* @SIZE_MAX
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* @EFBIG
br label %36
21: ; preds = %14
%22 = tail call i64 @malloc(i64 %16)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @ENOMEM
br label %36
27: ; preds = %21
%28 = tail call i64 @read(i32 %7, i8* %23, i64 %16)
%29 = icmp eq i64 %28, %16
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = tail call i32 @free(i8* %23)
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @EIO
br label %36
35: ; preds = %27
store i8* %23, i8** %1
store i64 %16, i64* %2
br label %36
36: ; preds = %30, %35, %11, %25, %19
%37 = phi i32 [ %12, %11 ], [ %20, %19 ], [ %26, %25 ], [ %34, %30 ], [ 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
}