#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
char *try_readonly_mmap(int file_descriptor, size_t file_size) {
  if (file_descriptor < 0)
    return NULL;
  char *mmap_result =
      mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, file_descriptor, 0);
  if (mmap_result == MAP_FAILED) {
    perror("mmap()");
    exit(EXIT_FAILURE);
  }
  return mmap_result;
}


x86 assembly (clang, -O0):
<try_readonly_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1156 <try_readonly_mmap+0x26>
movq   $0x0,-0x8(%rbp)
jmp    11b6 <try_readonly_mmap+0x86>
mov    -0x18(%rbp),%rsi
mov    0x2e97(%rip),%rax
mov    (%rax),%edx
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11ae <try_readonly_mmap+0x7e>
lea    0xe65(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_readonly_mmap>:
push   %rax
test   %edi,%edi
js     1162 <try_readonly_mmap+0x32>
mov    %edi,%r8d
mov    0x2eb9(%rip),%rax
mov    (%rax),%edx
mov    0x2e88(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
je     1166 <try_readonly_mmap+0x36>
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret
lea    0xe93(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [7 x i8] c"mmap()\00"
@EXIT_FAILURE = global i32 0
define i8* @try_readonly_mmap(i32 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i32 %0, i32* %4
store i64 %1, i64* %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %25
10: ; preds = %2
%11 = load i64, i64* %5
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* %4
%15 = call i8* @mmap(i32* null, i64 %11, i32 %12, i32 %13, i32 %14, i32 0)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %23
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %10
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %25
25: ; preds = %23, %9
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [7 x i8] c"mmap()\00"
@EXIT_FAILURE = global i32 0
define i8* @try_readonly_mmap(i32 %0, i64 %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %14, label %4
4: ; preds = %2
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @MAP_PRIVATE
%7 = tail call i8* @mmap(i32* null, i64 %1, i32 %5, i32 %6, i32 %0, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4, %2
%15 = phi i8* [ null, %2 ], [ %7, %4 ]
ret i8* %15
}