#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *slurp_file(char *path) {
  int fd = open(path, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "file \"%s\" couldn't be opened: %s\n", path,
            strerror(errno));
    exit(1);
  }
  struct stat st;
  if (fstat(fd, &st)) {
    fprintf(stderr, "could not stat \"%s\": %s\n", path, strerror(errno));
    exit(1);
  }
  char *buf = malloc(st.st_size + 1);
  buf[st.st_size] = '\0';
  if (buf == NULL) {
    perror("unable to allocate memory for file slurping");
    exit(1);
  }
  if (read(fd, buf, st.st_size) != st.st_size) {
    perror("unable to slurp some file at once");
    exit(1);
  }
  close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fd <slurp_file+0x6d>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe12(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
je     1255 <slurp_file+0xc5>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xddc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
jne    1291 <slurp_file+0x101>
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x10(%rbp),%rdx
call   1040 <read@plt>
movslq -0x10(%rbp),%rcx
cmp    %rcx,%rax
je     12c4 <slurp_file+0x134>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fd <slurp_file+0x6d>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
jne    121d <slurp_file+0x8d>
movslq 0x8(%rsp),%rbp
lea    0x1(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax,%rbp,1)
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
cmp    %rbp,%rax
jne    1252 <slurp_file+0xc2>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dd4(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde5(%rip),%rsi
jmp    123b <slurp_file+0xab>
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde7(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"file \22%s\22 couldn't be opened: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"could not stat \22%s\22: %s\0A\00"
@.str.2 = [44 x i8] c"unable to allocate memory for file slurping\00"
@.str.3 = [34 x i8] c"unable to slurp some file at once\00"
define i8* @slurp_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i64 @fstat(i32 %19, %struct.stat* %4)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %2
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %18
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %5
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %29
%45 = load i32, i32* %3
%46 = load i8*, i8** %5
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = sext i32 %48 to i64
%50 = call i64 @read(i32 %45, i8* %46, i64 %49)
%51 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%52 = load i32, i32* %51
%53 = sext i32 %52 to i64
%54 = icmp ne i64 %50, %53
br i1 %54, label %55, label %58
55: ; preds = %44
%56 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %44
%59 = load i32, i32* %3
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %5
ret i8* %61
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"file \22%s\22 couldn't be opened: %s\0A\00"
@errno = global i32 0
@.str.1 = [25 x i8] c"could not stat \22%s\22: %s\0A\00"
@.str.3 = [34 x i8] c"unable to slurp some file at once\00"
define noalias i8* @slurp_file(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = bitcast %struct.stat* %2 to i8*
%14 = call i64 @fstat(i32 %4, %struct.stat* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1
%26 = tail call i8* @malloc(i32 %25)
%27 = sext i32 %24 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = tail call i64 @read(i32 %4, i8* %26, i64 %27)
%30 = icmp eq i64 %29, %27
br i1 %30, label %34, label %31
31: ; preds = %22
%32 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = tail call i32 @close(i32 %4)
ret i8* %26
}