#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
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 /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(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));
  }
  fseek(file, 0, SEEK_END);
  size = ftell(file);
  fseek(file, 0, SEEK_SET);
  *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    11d9 <slurp_file+0x39>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    141a <slurp_file+0x27a>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
jne    1202 <slurp_file+0x62>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   10b0 <die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <xchdir@plt>
mov    -0x18(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x1040(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <xchdir@plt>
cmpq   $0x0,-0x1040(%rbp)
jne    126d <slurp_file+0xcd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x1048(%rbp),%rsi
mov    %eax,%edx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   10b0 <die@plt>
mov    -0x1040(%rbp),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x1040(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x102c(%rbp)
mov    -0x1040(%rbp),%rdi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@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+0x263>
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+0x247>
mov    0x2c74(%rip),%rax
mov    (%rax),%rax
mov    0x2c5a(%rip),%rcx
cmp    (%rcx),%rax
je     1391 <slurp_file+0x1f1>
mov    0x2c5a(%rip),%rax
mov    (%rax),%rax
mov    0x2c30(%rip),%rcx
cmp    (%rcx),%rax
jne    1396 <slurp_file+0x1f6>
jmp    1308 <slurp_file+0x168>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    -0x102c(%rbp),%eax
sub    -0x1030(%rbp),%eax
cltq
add    $0x0,%rax
mov    %rax,-0x1050(%rbp)
mov    0x2c17(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x1058(%rbp),%rsi
mov    -0x1050(%rbp),%rdx
mov    %eax,%ecx
lea    0xc4e(%rip),%rdi
mov    $0x0,%al
call   10b0 <die@plt>
mov    -0x1038(%rbp),%rcx
movslq -0x1030(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1030(%rbp)
jmp    1308 <slurp_file+0x168>
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,%r15
test   %rsi,%rsi
je     134f <slurp_file+0x1af>
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x20(%rsp),%rdi
mov    $0x1000,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
jne    11e5 <slurp_file+0x45>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   10b0 <die@plt>
mov    %rbx,%rdi
call   1030 <xchdir@plt>
lea    0xe20(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
call   1030 <xchdir@plt>
test   %rbx,%rbx
jne    1230 <slurp_file+0x90>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xdf3(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <die@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
movslq %ebp,%r14
lea    0x1(%rbp),%eax
mov    %eax,(%r15)
lea    0x2(%rbp),%edi
mov    %r12,0x10(%rsp)
mov    %r12,%rsi
call   1070 <xmalloc@plt>
movw   $0xa,(%rax,%r14,1)
mov    %rax,%r14
mov    %rbp,0x8(%rsp)
test   %ebp,%ebp
jle    1345 <slurp_file+0x1a5>
mov    0x2d41(%rip),%r12
mov    0x2d2a(%rip),%r13
mov    0x8(%rsp),%rax
mov    %eax,%ebp
mov    %r14,%r15
xor    %edx,%edx
mov    %r14,0x18(%rsp)
jmp    12de <slurp_file+0x13e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%edx
add    %r14d,%edx
movslq %edx,%rax
mov    0x18(%rsp),%r14
lea    (%r14,%rax,1),%r15
mov    0x8(%rsp),%rcx
mov    %ecx,%ebp
sub    %eax,%ebp
jle    1345 <slurp_file+0x1a5>
mov    %edx,0x4(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
test   %rax,%rax
jns    12c0 <slurp_file+0x120>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     12f0 <slurp_file+0x150>
mov    0x2c9d(%rip),%rax
cmp    (%rax),%rdi
je     12f0 <slurp_file+0x150>
movslq %ebp,%rbp
call   10c0 <strerror@plt>
lea    0xcff(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10b0 <die@plt>
jmp    12c0 <slurp_file+0x120>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1359 <slurp_file+0x1b9>
movl   $0x0,(%r15)
xor    %r14d,%r14d
mov    %r14,%rax
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
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.3 = [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 %108
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 = load i32, i32* @SEEK_END
%41 = call i32 @fseek(i32* %39, i32 0, i32 %40)
%42 = load i32*, i32** %13
%43 = call i32 @ftell(i32* %42)
store i32 %43, i32* %10
%44 = load i32*, i32** %13
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %44, i32 0, i32 %45)
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
%49 = load i32*, i32** %7
store i32 %48, i32* %49
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 2
%52 = load i8*, i8** %6
%53 = call i8* @xmalloc(i32 %51, i8* %52)
store i8* %53, i8** %9
%54 = load i8*, i8** %9
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 10, i8* %57
%58 = load i8*, i8** %9
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 0, i8* %62
store i32 0, i32* %11
br label %63
63: ; preds = %98, %87, %38
%64 = load i32, i32* %11
%65 = load i32, i32* %10
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %104
67: ; preds = %63
%68 = load i8*, i8** %9
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32, i32* %10
%73 = load i32, i32* %11
%74 = sub nsw i32 %72, %73
%75 = load i32*, i32** %13
%76 = call i64 @fread(i8* %71, i32 1, i32 %74, i32* %75)
store i64 %76, i64* %12
%77 = load i64, i64* %12
%78 = icmp slt i64 %77, 0
br i1 %78, label %79, label %98
79: ; preds = %67
%80 = load i64, i64* @errno
%81 = load i64, i64* @EINTR
%82 = icmp eq i64 %80, %81
br i1 %82, label %87, label %83
83: ; preds = %79
%84 = load i64, i64* @errno
%85 = load i64, i64* @EAGAIN
%86 = icmp eq i64 %84, %85
br i1 %86, label %87, label %88
87: ; preds = %83, %79
br label %63
88: ; preds = %83
%89 = load i8*, i8** %6
%90 = load i32, i32* %10
%91 = load i32, i32* %11
%92 = sub nsw i32 %90, %91
%93 = sext i32 %92 to i64
%94 = add i64 %93, 0
%95 = load i64, i64* @errno
%96 = call i32 @strerror(i64 %95)
%97 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %89, i64 %94, i32 %96)
br label %98
98: ; preds = %88, %67
%99 = load i64, i64* %12
%100 = load i32, i32* %11
%101 = sext i32 %100 to i64
%102 = add nsw i64 %101, %99
%103 = trunc i64 %102 to i32
store i32 %103, i32* %11
br label %63
104: ; preds = %63
%105 = load i32*, i32** %13
%106 = call i32 @fclose(i32* %105)
%107 = load i8*, i8** %9
store i8* %107, i8** %4
br label %108
108: ; preds = %104, %16
%109 = load i8*, i8** %4
ret i8* %109
}

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
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.3 = [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 %63
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 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %15, i32 0, i32 %23)
%25 = call i32 @ftell(i32* %15)
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %15, i32 0, i32 %26)
%28 = add nsw i32 %25, 1
store i32 %28, i32* %2
%29 = add nsw i32 %25, 2
%30 = call i8* @xmalloc(i32 %29, i8* %1)
%31 = sext i32 %25 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 10, i8* %32
%33 = sext i32 %28 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = icmp sgt i32 %25, 0
br i1 %35, label %36, label %61
36: ; preds = %22, %54
%37 = phi i32 [ %60, %54 ], [ %25, %22 ]
%38 = phi i8* [ %59, %54 ], [ %30, %22 ]
%39 = phi i32 [ %56, %54 ], [ 0, %22 ]
br label %40
40: ; preds = %36, %43
%41 = call i64 @fread(i8* %38, i32 1, i32 %37, i32* %15)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %40
%44 = load i64, i64* @errno
%45 = load i64, i64* @EINTR
%46 = icmp eq i64 %44, %45
%47 = load i64, i64* @EAGAIN
%48 = icmp eq i64 %44, %47
%49 = select i1 %46, i1 true, i1 %48
br i1 %49, label %40, label %50
50: ; preds = %43
%51 = sext i32 %37 to i64
%52 = call i32 @strerror(i64 %44)
%53 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %1, i64 %51, i32 %52)
br label %54
54: ; preds = %40, %50
%55 = trunc i64 %41 to i32
%56 = add i32 %39, %55
%57 = icmp sgt i32 %25, %56
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %30, i64 %58
%60 = sub nsw i32 %25, %56
br i1 %57, label %36, label %61
61: ; preds = %54, %22
%62 = call i32 @fclose(i32* %15)
br label %63
63: ; preds = %61, %7
%64 = phi i8* [ %30, %61 ], [ null, %7 ]
ret i8* %64
}