#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 */
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char *alloc(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ epanic(char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
char *slurp(char const *path, size_t *n) {
  int fd = open(path, O_RDONLY);
  if (fd == -1)
    epanic("open(%s)", path);
  off_t size = lseek(fd, 0, SEEK_END);
  if (size == -1)
    epanic("lseek()");
  lseek(fd, 0, SEEK_SET);
  char *data = alloc(size + 1);
  if (read(fd, data, size) != size)
    epanic("read()");
  data[size] = '\0';
  close(fd);
  if (n != NULL)
    *n = size;
  return data;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a1 <slurp+0x41>
mov    -0x8(%rbp),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1060 <epanic@plt>
mov    -0x14(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11cf <slurp+0x6f>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1060 <epanic@plt>
mov    -0x14(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <alloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <read@plt>
cmp    -0x18(%rbp),%eax
je     1217 <slurp+0xb7>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1060 <epanic@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x10(%rbp)
je     1241 <slurp+0xe1>
movslq -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1194 <slurp+0x34>
lea    0xe76(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <epanic@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bc <slurp+0x5c>
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1060 <epanic@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
lea    0x1(%rbx),%edi
call   1070 <alloc@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
je     11f9 <slurp+0x99>
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1060 <epanic@plt>
movslq %ebx,%rbx
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %r15d,%edi
call   1040 <close@plt>
test   %r14,%r14
je     1211 <slurp+0xb1>
mov    %rbx,(%r14)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@SEEK_END = global i32 0
@.str.1 = [8 x i8] c"lseek()\00"
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"read()\00"
define i8* @slurp(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* %5
%18 = load i32, i32* @SEEK_END
%19 = call i32 @lseek(i32 %17, i32 0, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* %5
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
%30 = call i8* @alloc(i32 %29)
store i8* %30, i8** %7
%31 = load i32, i32* %5
%32 = load i8*, i8** %7
%33 = load i32, i32* %6
%34 = call i32 @read(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %6
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %24
%40 = load i8*, i8** %7
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %5
%45 = call i32 @close(i32 %44)
%46 = load i64*, i64** %4
%47 = icmp ne i64* %46, null
br i1 %47, label %48, label %52
48: ; preds = %39
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = load i64*, i64** %4
store i64 %50, i64* %51
br label %52
52: ; preds = %48, %39
%53 = load i8*, i8** %7
ret i8* %53
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@SEEK_END = global i32 0
@.str.1 = [8 x i8] c"lseek()\00"
@SEEK_SET = global i32 0
@.str.2 = [7 x i8] c"read()\00"
define i8* @slurp(i8* %0, i64* writeonly %1) {
%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 %8
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @lseek(i32 %4, i32 0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %4, i32 0, i32 %15)
%17 = add nsw i32 %10, 1
%18 = tail call i8* @alloc(i32 %17)
%19 = tail call i32 @read(i32 %4, i8* %18, i32 %10)
%20 = icmp eq i32 %19, %10
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = sext i32 %10 to i64
%25 = getelementptr inbounds i8, i8* %18, i64 %24
store i8 0, i8* %25
%26 = tail call i32 @close(i32 %4)
%27 = icmp eq i64* %1, null
br i1 %27, label %29, label %28
28: ; preds = %23
store i64 %24, i64* %1
br label %29
29: ; preds = %28, %23
ret i8* %18
}