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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ bailout(char *, ...);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
char *slurpfile(const char *filename, size_t *len) {
  int fd;
  struct stat stbuf;
  int readlen;
  off_t filelen;
  char *p;
  fd = open(filename, O_RDONLY, 0);
  if (fd == -1) {
    if (errno == ENOENT)
      return NULL;
    bailout("could not open file %s: %s\n", filename, strerror(errno));
  }
  if (fstat(fd, &stbuf) < 0)
    bailout("fstat failed on file %s: %s\n", filename, strerror(errno));
  filelen = stbuf.st_size;
  p = malloc(filelen + 1);
  if (!p)
    bailout("out of memory reading file %s\n", filename);
  readlen = read(fd, p, filelen);
  if (readlen != filelen)
    bailout("read only %d bytes out of %d from %s\n", (int)readlen,
            (int)filelen, filename);
  p[readlen] = '\0';
  close(fd);
  *len = readlen;
  return p;
}


x86 assembly (clang, -O0):
<slurpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f3 <slurpfile+0x83>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <slurpfile+0x58>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <slurpfile+0x153>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <bailout@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1234 <slurpfile+0xc4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <bailout@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1266 <slurpfile+0xf6>
mov    -0x10(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <bailout@plt>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     129c <slurpfile+0x12c>
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1030 <bailout@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurpfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11ce <slurpfile+0x5e>
mov    0x2e38(%rip),%rax
mov    (%rax),%rdi
mov    0x2e1e(%rip),%rax
cmp    (%rax),%rdi
jne    11b6 <slurpfile+0x46>
xor    %ebx,%ebx
jmp    1263 <slurpfile+0xf3>
call   1080 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <bailout@plt>
mov    %rsp,%rsi
mov    %r12d,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jns    1200 <slurpfile+0x90>
mov    0x2df3(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <bailout@plt>
mov    (%rsp),%r13d
lea    0x1(%r13),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1226 <slurpfile+0xb6>
lea    0xe1d(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <bailout@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <read@plt>
mov    %eax,%ebp
cmp    %r13d,%eax
je     1251 <slurpfile+0xe1>
lea    0xe16(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1030 <bailout@plt>
movslq %ebp,%rbp
movb   $0x0,(%rbx,%rbp,1)
mov    %r12d,%edi
call   1040 <close@plt>
mov    %rbp,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [28 x i8] c"could not open file %s: %s\0A\00"
@.str.1 = [29 x i8] c"fstat failed on file %s: %s\0A\00"
@.str.2 = [31 x i8] c"out of memory reading file %s\0A\00"
@.str.3 = [38 x i8] c"read only %d bytes out of %d from %s\0A\00"
define i8* @slurpfile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %3
br label %70
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i64, i64* @errno
%24 = call i32 @strerror(i64 %23)
%25 = call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %24)
br label %26
26: ; preds = %21, %2
%27 = load i32, i32* %6
%28 = call i64 @fstat(i32 %27, %struct.stat* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i8*, i8** %4
%32 = load i64, i64* @errno
%33 = call i32 @strerror(i64 %32)
%34 = call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %33)
br label %35
35: ; preds = %30, %26
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i32, i32* %36
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %10
%41 = load i8*, i8** %10
%42 = icmp ne i8* %41, null
br i1 %42, label %46, label %43
43: ; preds = %35
%44 = load i8*, i8** %4
%45 = call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %35
%47 = load i32, i32* %6
%48 = load i8*, i8** %10
%49 = load i32, i32* %9
%50 = call i32 @read(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = load i32, i32* %9
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %46
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = load i8*, i8** %4
%58 = call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %55, i32 %56, i8* %57)
br label %59
59: ; preds = %54, %46
%60 = load i8*, i8** %10
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = load i64*, i64** %5
store i64 %67, i64* %68
%69 = load i8*, i8** %10
store i8* %69, i8** %3
br label %70
70: ; preds = %59, %20
%71 = load i8*, i8** %3
ret i8* %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [28 x i8] c"could not open file %s: %s\0A\00"
@.str.1 = [29 x i8] c"fstat failed on file %s: %s\0A\00"
@.str.2 = [31 x i8] c"out of memory reading file %s\0A\00"
@.str.3 = [38 x i8] c"read only %d bytes out of %d from %s\0A\00"
define noalias i8* @slurpfile(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %39, label %12
12: ; preds = %8
%13 = tail call i32 @strerror(i64 %9)
%14 = tail call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %13)
br label %15
15: ; preds = %12, %2
%16 = call i64 @fstat(i32 %6, %struct.stat* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = tail call i32 @strerror(i64 %19)
%21 = tail call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %20)
br label %22
22: ; preds = %18, %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1
%26 = tail call i8* @malloc(i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %28, %22
%31 = tail call i32 @read(i32 %6, i8* %26, i32 %24)
%32 = icmp eq i32 %31, %24
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 (i8*, ...) @bailout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %24, i8* %0)
br label %35
35: ; preds = %33, %30
%36 = sext i32 %31 to i64
%37 = getelementptr inbounds i8, i8* %26, i64 %36
store i8 0, i8* %37
%38 = tail call i32 @close(i32 %6)
store i64 %36, i64* %1
br label %39
39: ; preds = %8, %35
%40 = phi i8* [ %26, %35 ], [ null, %8 ]
ret i8* %40
}