#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
scalar_t__ strcmp(char const *, char *);
size_t slurp_file(const char *file, char **outbuf_p) {
  FILE *fp;
  char fbuf[16384];
  char *buf = NULL;
  char *tmp;
  size_t nread = 0;
  size_t buflen = 0;
  if (strcmp(file, "-") == 0) {
    fp = stdin;
  } else {
    fp = fopen(file, "r");
    if (fp == NULL) {
      fprintf(stderr, "Failed to read file: %s\n", file);
      exit(3);
    }
  }
  while ((nread = fread(fbuf, 1, 16384, fp)) > 0) {
    if (buf == NULL) {
      buf = (char *)malloc(nread + 1);
      if (buf == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(3);
      }
      memcpy(buf, fbuf, nread);
    } else {
      tmp = (char *)malloc(buflen + nread + 1);
      if (tmp == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(3);
      }
      memcpy(tmp, buf, buflen);
      memcpy(tmp + buflen, fbuf, nread);
      free(buf);
      buf = tmp;
    }
    buflen += nread;
    buf[buflen] = '\0';
  }
  *outbuf_p = buf;
  return buflen + 1;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x4028(%rbp)
movq   $0x0,-0x4038(%rbp)
movq   $0x0,-0x4040(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11e1 <slurp_file+0x61>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    122a <slurp_file+0xaa>
mov    -0x8(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1225 <slurp_file+0xa5>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
jmp    122a <slurp_file+0xaa>
jmp    122f <slurp_file+0xaf>
lea    -0x4020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   1040 <fread@plt>
mov    %rax,-0x4038(%rbp)
cmp    $0x0,%rax
jbe    139b <slurp_file+0x21b>
cmpq   $0x0,-0x4028(%rbp)
jne    12cd <slurp_file+0x14d>
mov    -0x4038(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x4028(%rbp)
cmpq   $0x0,-0x4028(%rbp)
jne    12ae <slurp_file+0x12e>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd80(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x4028(%rbp),%rdi
lea    -0x4020(%rbp),%rsi
mov    -0x4038(%rbp),%rdx
call   1070 <memcpy@plt>
jmp    136f <slurp_file+0x1ef>
mov    -0x4040(%rbp),%rdi
add    -0x4038(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x4030(%rbp)
cmpq   $0x0,-0x4030(%rbp)
jne    131a <slurp_file+0x19a>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x4030(%rbp),%rdi
mov    -0x4028(%rbp),%rsi
mov    -0x4040(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x4030(%rbp),%rdi
add    -0x4040(%rbp),%rdi
lea    -0x4020(%rbp),%rsi
mov    -0x4038(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x4028(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4030(%rbp),%rax
mov    %rax,-0x4028(%rbp)
mov    -0x4038(%rbp),%rax
add    -0x4040(%rbp),%rax
mov    %rax,-0x4040(%rbp)
mov    -0x4028(%rbp),%rax
mov    -0x4040(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    122f <slurp_file+0xaf>
mov    -0x4028(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4040(%rbp),%rax
add    $0x1,%rax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4018,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe62(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e3 <slurp_file+0x63>
lea    0xe53(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11ed <slurp_file+0x6d>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%r15
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12cf <slurp_file+0x14f>
mov    %rax,%r13
mov    %rbp,0x8(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%r12
xor    %r14d,%r14d
jmp    1255 <slurp_file+0xd5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %r13,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
test   %rax,%rax
je     12c4 <slurp_file+0x144>
test   %r14,%r14
je     12a0 <slurp_file+0x120>
lea    (%rbx,%r13,1),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12f0 <slurp_file+0x170>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
mov    %rbp,%rdi
add    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%r14
jmp    1230 <slurp_file+0xb0>
nop
lea    0x1(%r13),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12f0 <slurp_file+0x170>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <memcpy@plt>
jmp    1230 <slurp_file+0xb0>
add    $0x1,%rbx
mov    0x8(%rsp),%rbp
jmp    12d7 <slurp_file+0x157>
mov    $0x1,%ebx
xor    %r14d,%r14d
mov    %r14,0x0(%rbp)
mov    %rbx,%rax
add    $0x4018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Failed to read file: %s\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
define i64 @slurp_file(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca [16384 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i8* null, i8** %7
store i64 0, i64* %9
store i64 0, i64* %10
%11 = load i8*, i8** %3
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32*, i32** @stdin
store i32* %15, i32** %5
br label %27
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %3
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 3)
unreachable
26: ; preds = %16
br label %27
27: ; preds = %26, %14
br label %28
28: ; preds = %79, %27
%29 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%30 = load i32*, i32** %5
%31 = call i64 @fread(i8* %29, i32 1, i32 16384, i32* %30)
store i64 %31, i64* %9
%32 = icmp ugt i64 %31, 0
br i1 %32, label %33, label %86
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %52
36: ; preds = %33
%37 = load i64, i64* %9
%38 = add i64 %37, 1
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 3)
unreachable
47: ; preds = %36
%48 = load i8*, i8** %7
%49 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%50 = load i64, i64* %9
%51 = call i32 @memcpy(i8* %48, i8* %49, i64 %50)
br label %79
52: ; preds = %33
%53 = load i64, i64* %10
%54 = load i64, i64* %9
%55 = add i64 %53, %54
%56 = add i64 %55, 1
%57 = call i64 @malloc(i64 %56)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %8
%59 = load i8*, i8** %8
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %65
61: ; preds = %52
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 3)
unreachable
65: ; preds = %52
%66 = load i8*, i8** %8
%67 = load i8*, i8** %7
%68 = load i64, i64* %10
%69 = call i32 @memcpy(i8* %66, i8* %67, i64 %68)
%70 = load i8*, i8** %8
%71 = load i64, i64* %10
%72 = getelementptr inbounds i8, i8* %70, i64 %71
%73 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%74 = load i64, i64* %9
%75 = call i32 @memcpy(i8* %72, i8* %73, i64 %74)
%76 = load i8*, i8** %7
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %8
store i8* %78, i8** %7
br label %79
79: ; preds = %65, %47
%80 = load i64, i64* %9
%81 = load i64, i64* %10
%82 = add i64 %81, %80
store i64 %82, i64* %10
%83 = load i8*, i8** %7
%84 = load i64, i64* %10
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8 0, i8* %85
br label %28
86: ; preds = %28
%87 = load i8*, i8** %7
%88 = load i8**, i8*** %4
store i8* %87, i8** %88
%89 = load i64, i64* %10
%90 = add i64 %89, 1
ret i64 %90
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Failed to read file: %s\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
define i64 @slurp_file(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca [16384 x i8]
%4 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i32*, i32** @stdin
br label %16
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%15 = tail call i32 @exit(i32 3)
unreachable
16: ; preds = %9, %7
%17 = phi i32* [ %8, %7 ], [ %10, %9 ]
%18 = call i64 @fread(i8* %4, i32 1, i32 16384, i32* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %59, label %20
20: ; preds = %16, %51
%21 = phi i64 [ %55, %51 ], [ %18, %16 ]
%22 = phi i64 [ %53, %51 ], [ 0, %16 ]
%23 = phi i8* [ %52, %51 ], [ null, %16 ]
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %36
25: ; preds = %20
%26 = add i64 %21, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 3)
unreachable
34: ; preds = %25
%35 = call i32 @memcpy(i8* %28, i8* %4, i64 %21)
br label %51
36: ; preds = %20
%37 = add i64 %22, 1
%38 = add i64 %37, %21
%39 = call i64 @malloc(i64 %38)
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i64 %39, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 3)
unreachable
46: ; preds = %36
%47 = call i32 @memcpy(i8* %40, i8* %23, i64 %22)
%48 = getelementptr inbounds i8, i8* %40, i64 %22
%49 = call i32 @memcpy(i8* %48, i8* %4, i64 %21)
%50 = call i32 @free(i8* %23)
br label %51
51: ; preds = %46, %34
%52 = phi i8* [ %28, %34 ], [ %40, %46 ]
%53 = add i64 %21, %22
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8 0, i8* %54
%55 = call i64 @fread(i8* %4, i32 1, i32 16384, i32* %17)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %20
57: ; preds = %51
%58 = add i64 %53, 1
br label %59
59: ; preds = %57, %16
%60 = phi i8* [ null, %16 ], [ %52, %57 ]
%61 = phi i64 [ 1, %16 ], [ %58, %57 ]
store i8* %60, i8** %1
ret i64 %61
}