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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errx(int, char *, char const *);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
void read_file(const char *filename, void **ptr, size_t *size) {
  int fd, ret;
  struct stat st;
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    errx(1, "couldn't open `%s'", filename);
  ret = fstat(fd, &st);
  if (ret)
    errx(1, "couldn't stat `%s'", filename);
  *size = st.st_size;
  *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (*ptr == MAP_FAILED)
    errx(1, "couldn't map `%s'", filename);
  close(fd);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1198 <read_file+0x48>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe6d(%rip),%rsi
call   1040 <errx@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11c6 <read_file+0x76>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe52(%rip),%rsi
call   1040 <errx@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rsi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
jne    122d <read_file+0xdd>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdfe(%rip),%rsi
call   1040 <errx@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1189 <read_file+0x39>
lea    0xe84(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1040 <errx@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <fstat@plt>
test   %eax,%eax
je     11ab <read_file+0x5b>
lea    0xe75(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1040 <errx@plt>
mov    (%rsp),%rsi
mov    %rsi,(%r15)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jne    11f4 <read_file+0xa4>
lea    0xe3f(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1040 <errx@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"couldn't open `%s'\00"
@.str.1 = [19 x i8] c"couldn't stat `%s'\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [18 x i8] c"couldn't map `%s'\00"
define void @read_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %3
%19 = load i32, i32* %7
%20 = call i32 @fstat(i32 %19, %struct.stat* %9)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i64*, i64** %6
store i64 %28, i64* %29
%30 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %7
%35 = call i8* @mmap(i32* null, i64 %31, i32 %32, i32 %33, i32 %34, i32 0)
%36 = load i8**, i8*** %5
store i8* %35, i8** %36
%37 = load i8**, i8*** %5
%38 = load i8*, i8** %37
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %44
41: ; preds = %26
%42 = load i8*, i8** %4
%43 = call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %41, %26
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"couldn't open `%s'\00"
@.str.1 = [19 x i8] c"couldn't stat `%s'\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [18 x i8] c"couldn't map `%s'\00"
define void @read_file(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %14, %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i64, i64* %17
store i64 %18, i64* %2
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i64 %18, i32 %19, i32 %20, i32 %7, i32 0)
store i8* %21, i8** %1
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %24, %16
%27 = tail call i32 @close(i32 %7)
ret void
}