#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ *we_wordv;
};
typedef TYPE_1__ wordexp_t;
struct stat {
  int st_size;
};
typedef int off_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wordexp(char const *, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ wordfree(TYPE_1__ *);
const char *slurp_file(const char *filename) {
  wordexp_t expansion;
  struct stat statbuf;
  int fd = -1;
  off_t filesize;
  char *memory;
  wordexp(filename, &expansion, 0);
  fd = open(expansion.we_wordv[0], O_RDONLY);
  wordfree(&expansion);
  if (fd < 0) {
    return NULL;
  }
  if (fstat(fd, &statbuf) < 0) {
    close(fd);
    return NULL;
  }
  filesize = statbuf.st_size;
  if (filesize < 1) {
    close(fd);
    return NULL;
  }
  memory = mmap(NULL, filesize + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  close(fd);
  if (memory == MAP_FAILED) {
    return NULL;
  }
  if (memory[filesize - 1] == '\n') {
    memory[filesize - 1] = '\0';
  } else {
    memory[filesize] = '\0';
  }
  return memory;
}


x86 assembly (clang, -O0):
<slurp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1070 <wordexp@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <wordfree@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11b9 <slurp_file+0x59>
movq   $0x0,-0x8(%rbp)
jmp    12ab <slurp_file+0x14b>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    11e4 <slurp_file+0x84>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ab <slurp_file+0x14b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jge    1209 <slurp_file+0xa9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ab <slurp_file+0x14b>
mov    -0x28(%rbp),%esi
add    $0x1,%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2da0(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jne    1267 <slurp_file+0x107>
movq   $0x0,-0x8(%rbp)
jmp    12ab <slurp_file+0x14b>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1297 <slurp_file+0x137>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12a3 <slurp_file+0x143>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rbx
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <wordexp@plt>
mov    0x10(%rsp),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <wordfree@plt>
test   %ebp,%ebp
js     1225 <slurp_file+0xc5>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     1211 <slurp_file+0xb1>
mov    0x8(%rsp),%ebx
test   %ebx,%ebx
jle    1211 <slurp_file+0xb1>
lea    0x1(%rbx),%esi
mov    0x2e32(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2dc5(%rip),%rax
cmp    (%rax),%r14
je     1225 <slurp_file+0xc5>
lea    -0x1(%rbx),%eax
cmpb   $0xa,(%r14,%rax,1)
jne    121d <slurp_file+0xbd>
movb   $0x0,(%r14,%rax,1)
jmp    1222 <slurp_file+0xc2>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %r15d,%r15d
jmp    1225 <slurp_file+0xc5>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%r15
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32* }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i8* @slurp_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.TYPE_4__
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i32 -1, i32* %6
%9 = load i8*, i8** %3
%10 = call i32 @wordexp(i8* %9, %struct.TYPE_4__* %4, i32 0)
%11 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%12 = load i32*, i32** %11
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i32, i32* %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = call i32 @wordfree(%struct.TYPE_4__* %4)
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i8* null, i8** %2
br label %73
21: ; preds = %1
%22 = load i32, i32* %6
%23 = call i64 @fstat(i32 %22, %struct.stat* %5)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i8* null, i8** %2
br label %73
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i8* null, i8** %2
br label %73
36: ; preds = %28
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @PROT_WRITE
%41 = or i32 %39, %40
%42 = load i32, i32* @MAP_PRIVATE
%43 = load i32, i32* %6
%44 = call i8* @mmap(i32* null, i32 %38, i32 %41, i32 %42, i32 %43, i32 0)
store i8* %44, i8** %8
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
%47 = load i8*, i8** %8
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %51
50: ; preds = %36
store i8* null, i8** %2
br label %73
51: ; preds = %36
%52 = load i8*, i8** %8
%53 = load i32, i32* %7
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 10
br i1 %59, label %60, label %66
60: ; preds = %51
%61 = load i8*, i8** %8
%62 = load i32, i32* %7
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
store i8 0, i8* %65
br label %71
66: ; preds = %51
%67 = load i8*, i8** %8
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 0, i8* %70
br label %71
71: ; preds = %66, %60
%72 = load i8*, i8** %8
store i8* %72, i8** %2
br label %73
73: ; preds = %71, %50, %33, %25, %20
%74 = load i8*, i8** %2
ret i8* %74
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32* }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i8* @slurp_file(i8* %0) {
%2 = alloca %struct.TYPE_4__
%3 = alloca %struct.stat
%4 = bitcast %struct.TYPE_4__* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @wordexp(i8* %0, %struct.TYPE_4__* %2, i32 0)
%7 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%8 = load i32*, i32** %7
%9 = load i32, i32* %8
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i32 %9, i32 %10)
%12 = call i32 @wordfree(%struct.TYPE_4__* %2)
%13 = icmp slt i32 %11, 0
br i1 %13, label %45, label %14
14: ; preds = %1
%15 = call i64 @fstat(i32 %11, %struct.stat* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @close(i32 %11)
br label %45
19: ; preds = %14
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @close(i32 %11)
br label %45
25: ; preds = %19
%26 = add nuw nsw i32 %21, 1
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %28, %27
%30 = load i32, i32* @MAP_PRIVATE
%31 = call i8* @mmap(i32* null, i32 %26, i32 %29, i32 %30, i32 %11, i32 0)
%32 = call i32 @close(i32 %11)
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %31, %33
br i1 %34, label %45, label %35
35: ; preds = %25
%36 = add nsw i32 %21, -1
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %31, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 10
br i1 %40, label %41, label %42
41: ; preds = %35
store i8 0, i8* %38
br label %45
42: ; preds = %35
%43 = zext i32 %21 to i64
%44 = getelementptr inbounds i8, i8* %31, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %41, %42, %25, %1, %23, %17
%46 = phi i8* [ null, %17 ], [ null, %23 ], [ null, %1 ], [ null, %25 ], [ %31, %42 ], [ %31, %41 ]
ret i8* %46
}