#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;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
char *slurp_file(const char *path, int *bytes) {
  int rc, fd;
  struct stat statbuf;
  char *buffer;
  int bytesleft, bytesread, count;
  rc = stat(path, &statbuf);
  if (rc) {
    fprintf(stderr, "stat(%s): %s\n", path, strerror(errno));
    return NULL;
  }
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open(%s): %s\n", path, strerror(errno));
    return NULL;
  }
  buffer = malloc(statbuf.st_size + 1);
  count = 0;
  bytesleft = (int)statbuf.st_size;
  do {
    bytesread = read(fd, buffer + count, bytesleft);
    if (bytesread < 0) {
      if (errno == EINTR)
        continue;
      else {
        fprintf(stderr, "read: %d bytes from %s failed: %s\n", bytesleft, path,
                strerror(errno));
        close(fd);
        free(buffer);
        return NULL;
      }
    } else {
      bytesleft -= bytesread;
      count += bytesread;
    }
  } while (bytesleft > 0);
  close(fd);
  buffer[statbuf.st_size] = '\0';
  if (bytes)
    *bytes = (int)statbuf.st_size;
  return buffer;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11f2 <slurp_file+0x72>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    136c <slurp_file+0x1ec>
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1259 <slurp_file+0xd9>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    136c <slurp_file+0x1ec>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x3c(%rbp),%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1319 <slurp_file+0x199>
mov    0x2d31(%rip),%rax
mov    (%rax),%rax
mov    0x2d1f(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <slurp_file+0x137>
jmp    1332 <slurp_file+0x1b2>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2cf8(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    -0x6c(%rbp),%edx
mov    -0x68(%rbp),%rcx
mov    %rax,%r8
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    136c <slurp_file+0x1ec>
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1332 <slurp_file+0x1b2>
cmpl   $0x0,-0x34(%rbp)
jg     1275 <slurp_file+0xf5>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     1364 <slurp_file+0x1e4>
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%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    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %eax,%eax
je     11e3 <slurp_file+0x63>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e20(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xe41(%rip),%rsi
xor    %r12d,%r12d
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12c4 <slurp_file+0x144>
mov    %eax,%ebp
mov    %rbx,0x10(%rsp)
mov    (%rsp),%ebx
movslq %ebx,%rcx
mov    %rcx,0x8(%rsp)
lea    0x1(%rcx),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
xor    %r13d,%r13d
mov    0x2da5(%rip),%r15
mov    %ebx,0x4(%rsp)
jmp    1239 <slurp_file+0xb9>
nopl   0x0(%rax)
sub    %eax,%ebx
add    %eax,%r13d
test   %ebx,%ebx
jle    129a <slurp_file+0x11a>
movslq %r13d,%rsi
add    %r12,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    1230 <slurp_file+0xb0>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rdi
cmp    (%r15),%rdi
je     1235 <slurp_file+0xb5>
mov    0x2d76(%rip),%rax
mov    (%rax),%r15d
call   10a0 <strerror@plt>
lea    0xdab(%rip),%rsi
mov    %r15d,%edi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    11d1 <slurp_file+0x51>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rax
movb   $0x0,(%r12,%rax,1)
mov    0x10(%rsp),%rax
test   %rax,%rax
je     11d1 <slurp_file+0x51>
mov    0x4(%rsp),%ecx
mov    %ecx,(%rax)
jmp    11d1 <slurp_file+0x51>
mov    0x2d0d(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfc(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd2b(%rip),%rsi
jmp    11bf <slurp_file+0x3f>

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

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