#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
void *MAP_FAILED;
int MAP_FIXED;
size_t PTRDIFF_MAX;
int /*<<< orphan*/ __vm_lock(int);
int /*<<< orphan*/ __vm_unlock();
int /*<<< orphan*/ errno;
scalar_t__ syscall(int, void *, size_t, int, int, int, int);
void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off) {
  void *ret;
  if (off & ((-0x2000ULL << (8 * sizeof(long) - 1)) | 0xfff)) {
    errno = EINVAL;
    return MAP_FAILED;
  }
  if (len >= PTRDIFF_MAX) {
    errno = ENOMEM;
    return MAP_FAILED;
  }
  if (flags & MAP_FIXED)
    __vm_lock(-1);
  ret = (void *)syscall(192, start, len, prot, flags, fd, off >> 12);
  if (flags & MAP_FIXED)
    __vm_unlock();
  return ret;
}


x86 assembly (clang, -O0):
<__mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movslq -0x28(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
je     1187 <__mmap+0x57>
mov    0x2e5f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5e(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <__mmap+0xfd>
mov    -0x18(%rbp),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
jb     11c0 <__mmap+0x90>
mov    0x2e36(%rip),%rax
mov    (%rax),%ecx
mov    0x2e25(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <__mmap+0xfd>
mov    -0x20(%rbp),%eax
mov    0x2df6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11df <__mmap+0xaf>
mov    $0xffffffff,%edi
call   1050 <__vm_lock@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x28(%rbp),%eax
sar    $0xc,%eax
mov    $0xc0,%edi
mov    %eax,(%rsp)
call   1030 <syscall@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2dad(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1225 <__mmap+0xf5>
mov    $0x0,%al
call   1040 <__vm_unlock@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   $0xfff,%r9d
je     114d <__mmap+0x1d>
mov    0x2e7d(%rip),%rax
jmp    11be <__mmap+0x8e>
mov    %rsi,%rbp
mov    0x2e91(%rip),%rax
cmp    %rsi,(%rax)
jbe    11b7 <__mmap+0x87>
mov    %r9d,%ebx
mov    %r8d,%r14d
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rdi,%r13
mov    0x2e4e(%rip),%rax
test   %ecx,(%rax)
je     1180 <__mmap+0x50>
mov    $0xffffffff,%edi
call   1050 <__vm_lock@plt>
sar    $0xc,%ebx
mov    %ebx,(%rsp)
mov    $0xc0,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r15d,%ecx
mov    %r12d,%r8d
mov    %r14d,%r9d
call   1030 <syscall@plt>
mov    %rax,%rbx
mov    0x2e17(%rip),%rax
test   %r12d,(%rax)
je     11d3 <__mmap+0xa3>
xor    %eax,%eax
call   1040 <__vm_unlock@plt>
jmp    11d3 <__mmap+0xa3>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    0x2e09(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2de8(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@PTRDIFF_MAX = global i64 0
@ENOMEM = global i32 0
@MAP_FIXED = global i32 0
define i8* @__mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %8
store i64 %1, i64* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%15 = load i32, i32* %13
%16 = sext i32 %15 to i64
%17 = and i64 %16, 4095
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %6
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
%21 = load i8*, i8** @MAP_FAILED
store i8* %21, i8** %7
br label %54
22: ; preds = %6
%23 = load i64, i64* %9
%24 = load i64, i64* @PTRDIFF_MAX
%25 = icmp uge i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @ENOMEM
store i32 %27, i32* @errno
%28 = load i8*, i8** @MAP_FAILED
store i8* %28, i8** %7
br label %54
29: ; preds = %22
%30 = load i32, i32* %11
%31 = load i32, i32* @MAP_FIXED
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @__vm_lock(i32 -1)
br label %36
36: ; preds = %34, %29
%37 = load i8*, i8** %8
%38 = load i64, i64* %9
%39 = load i32, i32* %10
%40 = load i32, i32* %11
%41 = load i32, i32* %12
%42 = load i32, i32* %13
%43 = ashr i32 %42, 12
%44 = call i64 @syscall(i32 192, i8* %37, i64 %38, i32 %39, i32 %40, i32 %41, i32 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %14
%46 = load i32, i32* %11
%47 = load i32, i32* @MAP_FIXED
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %36
%51 = call i32 (...) @__vm_unlock()
br label %52
52: ; preds = %50, %36
%53 = load i8*, i8** %14
store i8* %53, i8** %7
br label %54
54: ; preds = %52, %26, %19
%55 = load i8*, i8** %7
ret i8* %55
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@PTRDIFF_MAX = global i64 0
@ENOMEM = global i32 0
@MAP_FIXED = global i32 0
define i8* @__mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = and i32 %5, 4095
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
%11 = load i8*, i8** @MAP_FAILED
br label %33
12: ; preds = %6
%13 = load i64, i64* @PTRDIFF_MAX
%14 = icmp ugt i64 %13, %1
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
%17 = load i8*, i8** @MAP_FAILED
br label %33
18: ; preds = %12
%19 = load i32, i32* @MAP_FIXED
%20 = and i32 %19, %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @__vm_lock(i32 -1)
br label %24
24: ; preds = %22, %18
%25 = ashr i32 %5, 12
%26 = tail call i64 @syscall(i32 192, i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = load i32, i32* @MAP_FIXED
%29 = and i32 %28, %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %24
%32 = tail call i32 (...) @__vm_unlock()
br label %33
33: ; preds = %24, %31, %15, %9
%34 = phi i8* [ %11, %9 ], [ %17, %15 ], [ %27, %31 ], [ %27, %24 ]
ret i8* %34
}