#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 fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
void *_mmap_ro(const char *filename, size_t *size) {
  int fd = open(filename, O_RDONLY);
  if (fd == -1)
    return NULL;
  struct stat st;
  if (fstat(fd, &st) == -1)
    goto err;
  void *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (ptr == MAP_FAILED)
    goto err;
  close(fd);
  *size = st.st_size;
  return ptr;
err:
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<_mmap_ro>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117c <_mmap_ro+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1209 <_mmap_ro+0xc9>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1196 <_mmap_ro+0x56>
jmp    11f9 <_mmap_ro+0xb9>
mov    -0x28(%rbp),%rsi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <_mmap_ro+0x99>
jmp    11f9 <_mmap_ro+0xb9>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <_mmap_ro+0xc9>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mmap_ro>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b6 <_mmap_ro+0x76>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11af <_mmap_ro+0x6f>
mov    (%rsp),%r15
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e2a(%rip),%rax
cmp    (%rax),%rbx
je     11af <_mmap_ro+0x6f>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15,(%r14)
jmp    11b8 <_mmap_ro+0x78>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i8* @_mmap_ro(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %41
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @fstat(i32 %16, %struct.stat* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
br label %38
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* %6
%26 = call i8* @mmap(i32* null, i64 %22, i32 %23, i32 %24, i32 %25, i32 0)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %31
30: ; preds = %20
br label %38
31: ; preds = %20
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64*, i64** %5
store i64 %35, i64* %36
%37 = load i8*, i8** %8
store i8* %37, i8** %3
br label %41
38: ; preds = %30, %19
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
store i8* null, i8** %3
br label %41
41: ; preds = %38, %31, %14
%42 = load i8*, i8** %3
ret i8* %42
}

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