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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ die(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int get_file_size(int /*<<< orphan*/ *);
scalar_t__ getcwd(char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ xchdir(char const *);
char *xmalloc(int, char const *);
char *slurp_file(const char *dirname, const char *filename, off_t *r_size) {
  char cwd[4096];
  char *buf;
  off_t size, progress;
  ssize_t result;
  FILE *file;
  if (!filename) {
    *r_size = 0;
    return 0;
  }
  if (getcwd(cwd, sizeof(cwd)) == 0) {
    die("cwd buffer to small");
  }
  xchdir(dirname);
  file = fopen(filename, "rb");
  xchdir(cwd);
  if (file == NULL) {
    die("Cannot open '%s' : %s\n", filename, strerror(errno));
  }
  size = get_file_size(file);
  if (size == -1) {
    die("Could not fetch size of '%s': %s\n", filename, strerror(errno));
  }
  *r_size = size + 1;
  buf = xmalloc(size + 2, filename);
  buf[size] = '\n';
  buf[size + 1] = '\0';
  progress = 0;
  while (progress < size) {
    result = fread(buf + progress, 1, size - progress, file);
    if (result < 0) {
      if ((errno == EINTR) || (errno == EAGAIN))
        continue;
      die("read on %s of %ld bytes failed: %s\n", filename,
          (size - progress) + 0UL, strerror(errno));
    }
    progress += result;
  }
  fclose(file);
  return buf;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <slurp_file+0x39>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    141a <slurp_file+0x28a>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    11f2 <slurp_file+0x62>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <xchdir@plt>
mov    -0x18(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1040(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <xchdir@plt>
cmpq   $0x0,-0x1040(%rbp)
jne    125d <slurp_file+0xcd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x1048(%rbp),%rsi
mov    %eax,%edx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x1040(%rbp),%rdi
call   1080 <get_file_size@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0xffffffff,-0x102c(%rbp)
jne    12ad <slurp_file+0x11d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x102c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x102c(%rbp),%edi
add    $0x2,%edi
mov    -0x18(%rbp),%rsi
call   1070 <xmalloc@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rax
movslq -0x102c(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x1028(%rbp),%rax
mov    -0x102c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1030(%rbp)
mov    -0x1030(%rbp),%eax
cmp    -0x102c(%rbp),%eax
jge    1403 <slurp_file+0x273>
mov    -0x1028(%rbp),%rdi
movslq -0x1030(%rbp),%rax
add    %rax,%rdi
mov    -0x102c(%rbp),%edx
sub    -0x1030(%rbp),%edx
mov    -0x1040(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jge    13e7 <slurp_file+0x257>
mov    0x2c7c(%rip),%rax
mov    (%rax),%rax
mov    0x2c6a(%rip),%rcx
cmp    (%rcx),%rax
je     1391 <slurp_file+0x201>
mov    0x2c62(%rip),%rax
mov    (%rax),%rax
mov    0x2c40(%rip),%rcx
cmp    (%rcx),%rax
jne    1396 <slurp_file+0x206>
jmp    1308 <slurp_file+0x178>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1060(%rbp)
mov    -0x102c(%rbp),%eax
sub    -0x1030(%rbp),%eax
cltq
add    $0x0,%rax
mov    %rax,-0x1058(%rbp)
mov    0x2c1f(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x1060(%rbp),%rsi
mov    -0x1058(%rbp),%rdx
mov    %eax,%ecx
lea    0xc70(%rip),%rdi
mov    $0x0,%al
call   10a0 <die@plt>
mov    -0x1038(%rbp),%rcx
movslq -0x1030(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1030(%rbp)
jmp    1308 <slurp_file+0x178>
mov    -0x1040(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1028(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdx,%r12
mov    %rsi,(%rsp)
test   %rsi,%rsi
je     132f <slurp_file+0x19f>
mov    %rdi,%rbx
lea    0x20(%rsp),%rdi
mov    $0x1000,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
jne    11d6 <slurp_file+0x46>
lea    0xe31(%rip),%rdi
xor    %eax,%eax
call   10a0 <die@plt>
mov    %rbx,%rdi
call   1030 <xchdir@plt>
lea    0xe2f(%rip),%rsi
mov    (%rsp),%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
call   1030 <xchdir@plt>
test   %rbx,%rbx
jne    1223 <slurp_file+0x93>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xe01(%rip),%rdi
mov    (%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <die@plt>
mov    %rbx,%rdi
call   1080 <get_file_size@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1256 <slurp_file+0xc6>
mov    0x2da6(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xde5(%rip),%rdi
mov    (%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <die@plt>
movslq %r14d,%rbp
lea    0x1(%r14),%eax
mov    %eax,(%r12)
lea    0x2(%r14),%edi
mov    (%rsp),%rsi
call   1070 <xmalloc@plt>
movw   $0xa,(%rax,%rbp,1)
test   %r14d,%r14d
mov    %rax,0x10(%rsp)
jle    1320 <slurp_file+0x190>
mov    0x2d57(%rip),%r15
mov    0x2d48(%rip),%r13
mov    %r14d,%ebp
mov    %rax,%r12
xor    %edx,%edx
mov    %r14,0x18(%rsp)
jmp    12bf <slurp_file+0x12f>
nop
mov    0xc(%rsp),%edx
add    %r14d,%edx
movslq %edx,%rax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%r12
mov    0x18(%rsp),%r14
mov    %r14d,%ebp
sub    %eax,%ebp
jle    1320 <slurp_file+0x190>
mov    %edx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
test   %rax,%rax
jns    12a0 <slurp_file+0x110>
mov    (%r15),%rdi
cmp    0x0(%r13),%rdi
je     12d0 <slurp_file+0x140>
mov    0x2cce(%rip),%rax
cmp    (%rax),%rdi
je     12d0 <slurp_file+0x140>
movslq %ebp,%rbp
call   10b0 <strerror@plt>
lea    0xd42(%rip),%rdi
mov    (%rsp),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10a0 <die@plt>
jmp    12a0 <slurp_file+0x110>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rax
jmp    1339 <slurp_file+0x1a9>
movl   $0x0,(%r12)
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"cwd buffer to small\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [23 x i8] c"Cannot open '%s' : %s\0A\00"
@errno = global i64 0
@.str.3 = [34 x i8] c"Could not fetch size of '%s': %s\0A\00"
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.4 = [36 x i8] c"read on %s of %ld bytes failed: %s\0A\00"
define i8* @slurp_file(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = load i32*, i32** %7
store i32 0, i32* %17
store i8* null, i8** %4
br label %110
18: ; preds = %3
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%20 = call i64 @getcwd(i8* %19, i32 4096)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i8*, i8** %5
%26 = call i32 @xchdir(i8* %25)
%27 = load i8*, i8** %6
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %13
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%30 = call i32 @xchdir(i8* %29)
%31 = load i32*, i32** %13
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %38
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = load i64, i64* @errno
%36 = call i32 @strerror(i64 %35)
%37 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %36)
br label %38
38: ; preds = %33, %24
%39 = load i32*, i32** %13
%40 = call i32 @get_file_size(i32* %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i8*, i8** %6
%45 = load i64, i64* @errno
%46 = call i32 @strerror(i64 %45)
%47 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %46)
br label %48
48: ; preds = %43, %38
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
%51 = load i32*, i32** %7
store i32 %50, i32* %51
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 2
%54 = load i8*, i8** %6
%55 = call i8* @xmalloc(i32 %53, i8* %54)
store i8* %55, i8** %9
%56 = load i8*, i8** %9
%57 = load i32, i32* %10
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 10, i8* %59
%60 = load i8*, i8** %9
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
store i32 0, i32* %11
br label %65
65: ; preds = %100, %89, %48
%66 = load i32, i32* %11
%67 = load i32, i32* %10
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %106
69: ; preds = %65
%70 = load i8*, i8** %9
%71 = load i32, i32* %11
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i32, i32* %10
%75 = load i32, i32* %11
%76 = sub nsw i32 %74, %75
%77 = load i32*, i32** %13
%78 = call i64 @fread(i8* %73, i32 1, i32 %76, i32* %77)
store i64 %78, i64* %12
%79 = load i64, i64* %12
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %100
81: ; preds = %69
%82 = load i64, i64* @errno
%83 = load i64, i64* @EINTR
%84 = icmp eq i64 %82, %83
br i1 %84, label %89, label %85
85: ; preds = %81
%86 = load i64, i64* @errno
%87 = load i64, i64* @EAGAIN
%88 = icmp eq i64 %86, %87
br i1 %88, label %89, label %90
89: ; preds = %85, %81
br label %65
90: ; preds = %85
%91 = load i8*, i8** %6
%92 = load i32, i32* %10
%93 = load i32, i32* %11
%94 = sub nsw i32 %92, %93
%95 = sext i32 %94 to i64
%96 = add i64 %95, 0
%97 = load i64, i64* @errno
%98 = call i32 @strerror(i64 %97)
%99 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %91, i64 %96, i32 %98)
br label %100
100: ; preds = %90, %69
%101 = load i64, i64* %12
%102 = load i32, i32* %11
%103 = sext i32 %102 to i64
%104 = add nsw i64 %103, %101
%105 = trunc i64 %104 to i32
store i32 %105, i32* %11
br label %65
106: ; preds = %65
%107 = load i32*, i32** %13
%108 = call i32 @fclose(i32* %107)
%109 = load i8*, i8** %9
store i8* %109, i8** %4
br label %110
110: ; preds = %106, %16
%111 = load i8*, i8** %4
ret i8* %111
}

LLVM IR (-O2):
@.str = [20 x i8] c"cwd buffer to small\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [23 x i8] c"Cannot open '%s' : %s\0A\00"
@errno = global i64 0
@.str.3 = [34 x i8] c"Could not fetch size of '%s': %s\0A\00"
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.4 = [36 x i8] c"read on %s of %ld bytes failed: %s\0A\00"
define i8* @slurp_file(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = icmp eq i8* %1, null
br i1 %6, label %7, label %8
7: ; preds = %3
store i32 0, i32* %2
br label %65
8: ; preds = %3
%9 = call i64 @getcwd(i8* %5, i32 4096)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = call i32 @xchdir(i8* %0)
%15 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @xchdir(i8* %5)
%17 = icmp eq i32* %15, null
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = call i32 @strerror(i64 %19)
%21 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %20)
br label %22
22: ; preds = %18, %13
%23 = call i32 @get_file_size(i32* %15)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = call i32 @strerror(i64 %26)
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %27)
br label %29
29: ; preds = %25, %22
%30 = add nsw i32 %23, 1
store i32 %30, i32* %2
%31 = add nsw i32 %23, 2
%32 = call i8* @xmalloc(i32 %31, i8* %1)
%33 = sext i32 %23 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 10, i8* %34
%35 = sext i32 %30 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
store i8 0, i8* %36
%37 = icmp sgt i32 %23, 0
br i1 %37, label %38, label %63
38: ; preds = %29, %56
%39 = phi i32 [ %62, %56 ], [ %23, %29 ]
%40 = phi i8* [ %61, %56 ], [ %32, %29 ]
%41 = phi i32 [ %58, %56 ], [ 0, %29 ]
br label %42
42: ; preds = %38, %45
%43 = call i64 @fread(i8* %40, i32 1, i32 %39, i32* %15)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
%49 = load i64, i64* @EAGAIN
%50 = icmp eq i64 %46, %49
%51 = select i1 %48, i1 true, i1 %50
br i1 %51, label %42, label %52
52: ; preds = %45
%53 = sext i32 %39 to i64
%54 = call i32 @strerror(i64 %46)
%55 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %1, i64 %53, i32 %54)
br label %56
56: ; preds = %42, %52
%57 = trunc i64 %43 to i32
%58 = add i32 %41, %57
%59 = icmp sgt i32 %23, %58
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds i8, i8* %32, i64 %60
%62 = sub nsw i32 %23, %58
br i1 %59, label %38, label %63
63: ; preds = %56, %29
%64 = call i32 @fclose(i32* %15)
br label %65
65: ; preds = %63, %7
%66 = phi i8* [ %32, %63 ], [ null, %7 ]
ret i8* %66
}