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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_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;
  }
  off_t length = s.st_size;
  char *result = malloc((size_t)length + 1);
  if (result == NULL) {
    printf("slurp(): could not allocate memory for: %s\n", filename);
    return NULL;
  }
  char *p = result;
  int actual = (int)fread(p, sizeof(char), (size_t)length, file);
  if (actual != length) {
    printf("could not read all %li bytes from file: %s\n", (long)length,
           filename);
    free(result);
    return NULL;
  }
  result[length] = '\0';
  fclose(file);
  return result;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    12aa <slurp+0x13a>
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    12aa <slurp+0x13a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1234 <slurp+0xc4>
mov    -0x10(%rbp),%rsi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12aa <slurp+0x13a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     128d <slurp+0x11d>
movslq -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12aa <slurp+0x13a>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%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
movslq (%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    %eax,%r12d
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 { i32 }
@.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 %li 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 i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
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 %64
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 %64
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = add i64 %30, 1
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %36)
store i8* null, i8** %2
br label %64
38: ; preds = %26
%39 = load i8*, i8** %8
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = load i32*, i32** %4
%44 = call i64 @fread(i8* %40, i32 1, i64 %42, i32* %43)
%45 = trunc i64 %44 to i32
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = load i32, i32* %7
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %56
49: ; preds = %38
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = load i8*, i8** %3
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %51, i8* %52)
%54 = load i8*, i8** %8
%55 = call i32 @free(i8* %54)
store i8* null, i8** %2
br label %64
56: ; preds = %38
%57 = load i8*, i8** %8
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
store i8 0, i8* %60
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %8
store i8* %63, i8** %2
br label %64
64: ; preds = %56, %49, %35, %23, %15
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.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 %li 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 %34
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 %32
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sext i32 %15 to i64
%17 = add nsw i64 %16, 1
%18 = tail call i8* @malloc(i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = 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 %32
22: ; preds = %13
%23 = tail call i64 @fread(i8* %18, i32 1, i64 %16, i32* %3)
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %15, %24
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %16, i8* %0)
%28 = tail call i32 @free(i8* %18)
br label %32
29: ; preds = %22
%30 = getelementptr inbounds i8, i8* %18, i64 %16
store i8 0, i8* %30
%31 = tail call i32 @fclose(i32* %3)
br label %32
32: ; preds = %20, %29, %26, %11
%33 = phi i8* [ null, %11 ], [ null, %20 ], [ null, %26 ], [ %18, %29 ]
br label %34
34: ; preds = %32, %5
%35 = phi i8* [ null, %5 ], [ %33, %32 ]
ret i8* %35
}