#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 scalar_t__ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ die(char *, char const *, scalar_t__, ...);
scalar_t__ errno;
scalar_t__ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, scalar_t__);
int /*<<< orphan*/ strerror(scalar_t__);
char *xmalloc(scalar_t__, char const *);
char *slurp_file(const char *filename, off_t *r_size) {
  int fd;
  char *buf;
  off_t size, progress;
  ssize_t result;
  struct stat stats;
  if (!filename) {
    *r_size = 0;
    return 0;
  }
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    die("Cannot open `%s': %s\n", filename, strerror(errno));
  }
  result = fstat(fd, &stats);
  if (result < 0) {
    die("Cannot stat: %s: %s\n", filename, strerror(errno));
  }
  size = stats.st_size;
  *r_size = size;
  buf = xmalloc(size, filename);
  progress = 0;
  while (progress < size) {
    result = read(fd, buf + progress, size - progress);
    if (result < 0) {
      if ((errno == EINTR) || (errno == EAGAIN))
        continue;
      die("read on %s of %ld bytes failed: %s\n", filename,
          (size - progress) + 0UL, strerror(errno));
    }
    progress += result;
  }
  result = close(fd);
  if (result < 0) {
    die("Close of %s failed: %s\n", filename, strerror(errno));
  }
  return buf;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <slurp_file+0x33>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    136a <slurp_file+0x1fa>
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ee <slurp_file+0x7e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x50(%rbp),%rsi
movslq %eax,%rdx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1090 <fstat@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1235 <slurp_file+0xc5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x58(%rbp),%rsi
movslq %eax,%rdx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    131f <slurp_file+0x1af>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    130e <slurp_file+0x19e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
je     12ca <slurp_file+0x15a>
mov    0x2d21(%rip),%rax
mov    (%rax),%rax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
jne    12cf <slurp_file+0x15f>
jmp    1261 <slurp_file+0xf1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rax
sub    -0x38(%rbp),%rax
add    $0x0,%rax
mov    %rax,-0x60(%rbp)
mov    0x2cea(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1261 <slurp_file+0xf1>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    1362 <slurp_file+0x1f2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c93(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x70(%rbp),%rsi
movslq %eax,%rdx
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
test   %rdi,%rdi
je     12d6 <slurp_file+0x166>
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c4 <slurp_file+0x54>
mov    0x2e30(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
movslq %eax,%rdx
lea    0xe46(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <die@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jns    11f8 <slurp_file+0x88>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
movslq %eax,%rdx
lea    0xe28(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <die@plt>
mov    0x20(%rsp),%rbx
mov    %rbx,(%r15)
mov    %rbx,%rdi
mov    %r14,0x8(%rsp)
mov    %r14,%rsi
call   1030 <xmalloc@plt>
mov    %rax,%r14
mov    %rbx,0x10(%rsp)
test   %rbx,%rbx
jle    12a3 <slurp_file+0x133>
mov    0x2da8(%rip),%r13
mov    0x10(%rsp),%rbx
mov    %r14,%r15
xor    %r12d,%r12d
mov    %r14,0x18(%rsp)
jmp    1270 <slurp_file+0x100>
call   1080 <strerror@plt>
lea    0xde5(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <die@plt>
add    %r14,%r12
mov    0x18(%rsp),%r14
lea    (%r14,%r12,1),%r15
mov    0x10(%rsp),%rbx
sub    %r12,%rbx
jle    12a3 <slurp_file+0x133>
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %rax,%r14
test   %rax,%rax
jns    1257 <slurp_file+0xe7>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rdi
cmp    0x0(%r13),%rdi
je     1270 <slurp_file+0x100>
mov    0x2d24(%rip),%rax
cmp    (%rax),%rdi
je     1270 <slurp_file+0x100>
jmp    123a <slurp_file+0xca>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    12e0 <slurp_file+0x170>
mov    0x2d22(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
movslq %eax,%rdx
lea    0xd87(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1070 <die@plt>
jmp    12e0 <slurp_file+0x170>
movq   $0x0,(%r15)
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Cannot open `%s': %s\0A\00"
@errno = global i64 0
@.str.1 = [21 x i8] c"Cannot stat: %s: %s\0A\00"
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.2 = [36 x i8] c"read on %s of %ld bytes failed: %s\0A\00"
@.str.3 = [24 x i8] c"Close of %s failed: %s\0A\00"
define i8* @slurp_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.stat
store i8* %0, i8** %4
store i64* %1, i64** %5
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i64*, i64** %5
store i64 0, i64* %15
store i8* null, i8** %3
br label %97
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load i64, i64* @errno
%25 = call i32 @strerror(i64 %24)
%26 = sext i32 %25 to i64
%27 = call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %23, i64 %26)
br label %28
28: ; preds = %22, %16
%29 = load i32, i32* %6
%30 = call i64 @fstat(i32 %29, %struct.stat* %11)
store i64 %30, i64* %10
%31 = load i64, i64* %10
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i8*, i8** %4
%35 = load i64, i64* @errno
%36 = call i32 @strerror(i64 %35)
%37 = sext i32 %36 to i64
%38 = call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %34, i64 %37)
br label %39
39: ; preds = %33, %28
%40 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%41 = load i64, i64* %40
store i64 %41, i64* %8
%42 = load i64, i64* %8
%43 = load i64*, i64** %5
store i64 %42, i64* %43
%44 = load i64, i64* %8
%45 = load i8*, i8** %4
%46 = call i8* @xmalloc(i64 %44, i8* %45)
store i8* %46, i8** %7
store i64 0, i64* %9
br label %47
47: ; preds = %80, %70, %39
%48 = load i64, i64* %9
%49 = load i64, i64* %8
%50 = icmp slt i64 %48, %49
br i1 %50, label %51, label %84
51: ; preds = %47
%52 = load i32, i32* %6
%53 = load i8*, i8** %7
%54 = load i64, i64* %9
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i64, i64* %8
%57 = load i64, i64* %9
%58 = sub nsw i64 %56, %57
%59 = call i64 @read(i32 %52, i8* %55, i64 %58)
store i64 %59, i64* %10
%60 = load i64, i64* %10
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %80
62: ; preds = %51
%63 = load i64, i64* @errno
%64 = load i64, i64* @EINTR
%65 = icmp eq i64 %63, %64
br i1 %65, label %70, label %66
66: ; preds = %62
%67 = load i64, i64* @errno
%68 = load i64, i64* @EAGAIN
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66, %62
br label %47
71: ; preds = %66
%72 = load i8*, i8** %4
%73 = load i64, i64* %8
%74 = load i64, i64* %9
%75 = sub nsw i64 %73, %74
%76 = add i64 %75, 0
%77 = load i64, i64* @errno
%78 = call i32 @strerror(i64 %77)
%79 = call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %72, i64 %76, i32 %78)
br label %80
80: ; preds = %71, %51
%81 = load i64, i64* %10
%82 = load i64, i64* %9
%83 = add nsw i64 %82, %81
store i64 %83, i64* %9
br label %47
84: ; preds = %47
%85 = load i32, i32* %6
%86 = call i64 @close(i32 %85)
store i64 %86, i64* %10
%87 = load i64, i64* %10
%88 = icmp slt i64 %87, 0
br i1 %88, label %89, label %95
89: ; preds = %84
%90 = load i8*, i8** %4
%91 = load i64, i64* @errno
%92 = call i32 @strerror(i64 %91)
%93 = sext i32 %92 to i64
%94 = call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %90, i64 %93)
br label %95
95: ; preds = %89, %84
%96 = load i8*, i8** %7
store i8* %96, i8** %3
br label %97
97: ; preds = %95, %14
%98 = load i8*, i8** %3
ret i8* %98
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Cannot open `%s': %s\0A\00"
@errno = global i64 0
@.str.1 = [21 x i8] c"Cannot stat: %s: %s\0A\00"
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.2 = [36 x i8] c"read on %s of %ld bytes failed: %s\0A\00"
@.str.3 = [24 x i8] c"Close of %s failed: %s\0A\00"
define i8* @slurp_file(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %7
6: ; preds = %2
store i64 0, i64* %1
br label %59
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = tail call i32 @strerror(i64 %12)
%14 = sext i32 %13 to i64
%15 = tail call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %14)
br label %16
16: ; preds = %11, %7
%17 = call i64 @fstat(i32 %9, %struct.stat* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = tail call i32 @strerror(i64 %20)
%22 = sext i32 %21 to i64
%23 = tail call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %22)
br label %24
24: ; preds = %19, %16
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i64, i64* %25
store i64 %26, i64* %1
%27 = tail call i8* @xmalloc(i64 %26, i8* %0)
%28 = icmp sgt i64 %26, 0
br i1 %28, label %29, label %51
29: ; preds = %24, %46
%30 = phi i64 [ %50, %46 ], [ %26, %24 ]
%31 = phi i8* [ %49, %46 ], [ %27, %24 ]
%32 = phi i64 [ %47, %46 ], [ 0, %24 ]
br label %33
33: ; preds = %29, %36
%34 = tail call i64 @read(i32 %9, i8* %31, i64 %30)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
%40 = load i64, i64* @EAGAIN
%41 = icmp eq i64 %37, %40
%42 = select i1 %39, i1 true, i1 %41
br i1 %42, label %33, label %43
43: ; preds = %36
%44 = tail call i32 @strerror(i64 %37)
%45 = tail call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %30, i32 %44)
br label %46
46: ; preds = %33, %43
%47 = add nsw i64 %34, %32
%48 = icmp sgt i64 %26, %47
%49 = getelementptr inbounds i8, i8* %27, i64 %47
%50 = sub nsw i64 %26, %47
br i1 %48, label %29, label %51
51: ; preds = %46, %24
%52 = tail call i64 @close(i32 %9)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i64, i64* @errno
%56 = tail call i32 @strerror(i64 %55)
%57 = sext i32 %56 to i64
%58 = tail call i32 (i8*, i8*, i64, ...) @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0, i64 %57)
br label %59
59: ; preds = %51, %54, %6
%60 = phi i8* [ null, %6 ], [ %27, %54 ], [ %27, %51 ]
ret i8* %60
}