#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror_exit(char *);
int read(int, char *, size_t);
char *consume_file(const char *filename, size_t *retsize) {
  int fd, ret;
  struct stat s;
  char *data;
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    perror_exit("[consume_file] open");
  ret = fstat(fd, &s);
  if (ret < 0)
    perror_exit("[consume_file] fstat");
  *retsize = (size_t)s.st_size;
  if (*retsize > 1024 * 1024 * 10)
    *retsize = 1024 * 1024 * 10;
  data = mmap(NULL, *retsize, PROT_READ | PROT_WRITE | PROT_EXEC,
              MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (data == MAP_FAILED)
    perror_exit("[consume_file] mmap");
  ret = read(fd, data, *retsize);
  if (ret < 0)
    perror_exit("[consume_file] read");
  close(fd);
  return data;
}


x86 assembly (clang, -O0):
<consume_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119b <consume_file+0x3b>
lea    0xe6a(%rip),%rdi
call   1070 <perror_exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c0 <consume_file+0x60>
lea    0xe59(%rip),%rdi
call   1070 <perror_exit@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0xa00000,(%rax)
jbe    11e7 <consume_file+0x87>
mov    -0x10(%rbp),%rax
movq   $0xa00000,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
mov    0x2dc8(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9f(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d74(%rip),%rcx
cmp    (%rcx),%rax
jne    1251 <consume_file+0xf1>
lea    0xddd(%rip),%rdi
call   1070 <perror_exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    127d <consume_file+0x11d>
lea    0xdc5(%rip),%rdi
call   1070 <perror_exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consume_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    118b <consume_file+0x2b>
lea    0xe7a(%rip),%rdi
call   1070 <perror_exit@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
test   %eax,%eax
jns    11a7 <consume_file+0x47>
lea    0xe72(%rip),%rdi
call   1070 <perror_exit@plt>
mov    0x8(%rsp),%rax
cmp    $0xa00000,%rax
mov    $0xa00000,%esi
cmovb  %rax,%rsi
mov    %rsi,(%r14)
mov    0x2e33(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    0x2df8(%rip),%rax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dab(%rip),%rax
cmp    (%rax),%rbx
jne    1216 <consume_file+0xb6>
lea    0xe18(%rip),%rdi
call   1070 <perror_exit@plt>
mov    (%r14),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
test   %eax,%eax
jns    1233 <consume_file+0xd3>
lea    0xe0f(%rip),%rdi
call   1070 <perror_exit@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"[consume_file] open\00"
@.str.1 = [21 x i8] c"[consume_file] fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [20 x i8] c"[consume_file] mmap\00"
@.str.3 = [20 x i8] c"[consume_file] read\00"
define i8* @consume_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %5
%18 = call i32 @fstat(i32 %17, %struct.stat* %7)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64*, i64** %4
store i64 %25, i64* %26
%27 = load i64*, i64** %4
%28 = load i64, i64* %27
%29 = icmp ugt i64 %28, 10485760
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i64*, i64** %4
store i64 10485760, i64* %31
br label %32
32: ; preds = %30, %23
%33 = load i64*, i64** %4
%34 = load i64, i64* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %35, %36
%38 = load i32, i32* @PROT_EXEC
%39 = or i32 %37, %38
%40 = load i32, i32* @MAP_ANONYMOUS
%41 = load i32, i32* @MAP_PRIVATE
%42 = or i32 %40, %41
%43 = call i8* @mmap(i32* null, i64 %34, i32 %39, i32 %42, i32 -1, i32 0)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %49
47: ; preds = %32
%48 = call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %32
%50 = load i32, i32* %5
%51 = load i8*, i8** %8
%52 = load i64*, i64** %4
%53 = load i64, i64* %52
%54 = call i32 @read(i32 %50, i8* %51, i64 %53)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
%62 = load i8*, i8** %8
ret i8* %62
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"[consume_file] open\00"
@.str.1 = [21 x i8] c"[consume_file] fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [20 x i8] c"[consume_file] mmap\00"
@.str.3 = [20 x i8] c"[consume_file] read\00"
define i8* @consume_file(i8* nocapture %0, i64* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i32 @fstat(i32 %6, %struct.stat* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp ult i64 %17, 10485760
%19 = select i1 %18, i64 %17, i64 10485760
store i64 %19, i64* %1
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @PROT_EXEC
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_ANONYMOUS
%26 = load i32, i32* @MAP_PRIVATE
%27 = or i32 %26, %25
%28 = tail call i8* @mmap(i32* null, i64 %19, i32 %24, i32 %27, i32 -1, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = tail call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %15
%34 = load i64, i64* %1
%35 = tail call i32 @read(i32 %6, i8* %28, i64 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = tail call i32 @perror_exit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = tail call i32 @close(i32 %6)
ret i8* %28
}