#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 */
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *file_map(int fd, int prot, off_t offset, size_t size) {
  assert(fd >= 0);
  assert(prot == PROT_READ || prot == PROT_WRITE);
  void *mem;
  if ((mem = mmap(NULL, size, prot, MAP_SHARED, fd, offset)) == MAP_FAILED) {
    perror("mmap");
    abort();
  }
  return mem;
}


x86 assembly (clang, -O0):
<file_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%ecx
mov    $0x1,%al
mov    0x2e86(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
je     118f <file_map+0x4f>
mov    -0x8(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0xc(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11e4 <file_map+0xa4>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_map>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    0x2e96(%rip),%rax
cmp    %ebx,(%rax)
sete   %al
mov    0x2e62(%rip),%rcx
cmp    %ebx,(%rcx)
sete   %cl
or     %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %ebp,%r8d
mov    %r14d,%r9d
call   1040 <mmap@plt>
mov    0x2e29(%rip),%rcx
cmp    (%rcx),%rax
je     11af <file_map+0x6f>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @file_map(i32 %0, i32 %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64 %3, i64* %8
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %6
%15 = load i32, i32* @PROT_READ
%16 = icmp eq i32 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %4
%18 = load i32, i32* %6
%19 = load i32, i32* @PROT_WRITE
%20 = icmp eq i32 %18, %19
br label %21
21: ; preds = %17, %4
%22 = phi i1 [ true, %4 ], [ %20, %17 ]
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i64, i64* %8
%26 = load i32, i32* %6
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %5
%29 = load i32, i32* %7
%30 = call i8* @mmap(i32* null, i64 %25, i32 %26, i32 %27, i32 %28, i32 %29)
store i8* %30, i8** %9
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %21
%37 = load i8*, i8** %9
ret i8* %37
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @file_map(i32 %0, i32 %1, i32 %2, i64 %3) {
%5 = xor i32 %0, -1
%6 = lshr i32 %5, 31
%7 = tail call i32 @assert(i32 %6)
%8 = load i32, i32* @PROT_READ
%9 = icmp eq i32 %8, %1
%10 = load i32, i32* @PROT_WRITE
%11 = icmp eq i32 %10, %1
%12 = select i1 %9, i1 true, i1 %11
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32* null, i64 %3, i32 %1, i32 %15, i32 %0, i32 %2)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %22
19: ; preds = %4
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %4
ret i8* %16
}