#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 {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int stat(char const *, struct stat *);
char *slurp(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    printf("slurp(): could not read from: %s\n", filename);
    return NULL;
  }
  struct stat s;
  int rc = stat(filename, &s);
  if (rc != 0) {
    printf("slurp(): could not stat: %s\n", filename);
    return NULL;
  }
  size_t length = s.st_size;
  char *result = malloc(length + 1);
  if (result == NULL) {
    printf("slurp(): could not allocate memory for: %s\n", filename);
    return NULL;
  }
  char *p = result;
  size_t actual = fread(p, sizeof(char), length, file);
  if (actual != length) {
    printf("could not read all %zi bytes from file: %s\n", length, filename);
    free(result);
    return NULL;
  }
  result[length] = '\0';
  fclose(file);
  return result;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <slurp+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12af <slurp+0x13f>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f3 <slurp+0x83>
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12af <slurp+0x13f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1236 <slurp+0xc6>
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12af <slurp+0x13f>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1292 <slurp+0x122>
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12af <slurp+0x13f>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe7e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11a7 <slurp+0x37>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %eax,%eax
je     11ca <slurp+0x5a>
lea    0xe7f(%rip),%rdi
jmp    11ae <slurp+0x3e>
lea    0xe54(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%r12
lea    0x1(%r12),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1207 <slurp+0x97>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1210 <slurp+0xa0>
movb   $0x0,(%r14,%r12,1)
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    11bb <slurp+0x4b>
lea    0xe33(%rip),%rdi
jmp    11ae <slurp+0x3e>
lea    0xe56(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11bb <slurp+0x4b>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"slurp(): could not read from: %s\0A\00"
@.str.2 = [29 x i8] c"slurp(): could not stat: %s\0A\00"
@.str.3 = [44 x i8] c"slurp(): could not allocate memory for: %s\0A\00"
@.str.4 = [44 x i8] c"could not read all %zi bytes from file: %s\0A\00"
define i8* @slurp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i8* null, i8** %2
br label %59
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @stat(i8* %19, %struct.stat* %5)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i8* null, i8** %2
br label %59
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = add i64 %29, 1
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i8* null, i8** %2
br label %59
37: ; preds = %26
%38 = load i8*, i8** %8
store i8* %38, i8** %9
%39 = load i8*, i8** %9
%40 = load i64, i64* %7
%41 = load i32*, i32** %4
%42 = call i64 @fread(i8* %39, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %10
%43 = load i64, i64* %10
%44 = load i64, i64* %7
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %37
%47 = load i64, i64* %7
%48 = load i8*, i8** %3
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %47, i8* %48)
%50 = load i8*, i8** %8
%51 = call i32 @free(i8* %50)
store i8* null, i8** %2
br label %59
52: ; preds = %37
%53 = load i8*, i8** %8
%54 = load i64, i64* %7
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 0, i8* %55
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %8
store i8* %58, i8** %2
br label %59
59: ; preds = %52, %46, %34, %23, %15
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"slurp(): could not read from: %s\0A\00"
@.str.2 = [29 x i8] c"slurp(): could not stat: %s\0A\00"
@.str.3 = [44 x i8] c"slurp(): could not allocate memory for: %s\0A\00"
@.str.4 = [44 x i8] c"could not read all %zi bytes from file: %s\0A\00"
define noalias i8* @slurp(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %32
7: ; preds = %1
%8 = bitcast %struct.stat* %2 to i8*
%9 = call i32 @stat(i8* %0, %struct.stat* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %30
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i64, i64* %14
%16 = add i64 %15, 1
%17 = tail call i8* @malloc(i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %30
21: ; preds = %13
%22 = tail call i64 @fread(i8* %17, i32 1, i64 %15, i32* %3)
%23 = icmp eq i64 %22, %15
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %15, i8* %0)
%26 = tail call i32 @free(i8* %17)
br label %30
27: ; preds = %21
%28 = getelementptr inbounds i8, i8* %17, i64 %15
store i8 0, i8* %28
%29 = tail call i32 @fclose(i32* %3)
br label %30
30: ; preds = %19, %27, %24, %11
%31 = phi i8* [ null, %11 ], [ null, %19 ], [ null, %24 ], [ %17, %27 ]
br label %32
32: ; preds = %30, %5
%33 = phi i8* [ null, %5 ], [ %31, %30 ]
ret i8* %33
}