#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*/ EINVAL;
int /*<<< orphan*/ MADV_MERGEABLE;
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
void *__mmap2(void *, size_t, int, int, int, size_t);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ madvise(void *, size_t, int /*<<< orphan*/);
void *mmap(void *addr, size_t size, int prot, int flags, int fd, long offset) {
  void *ret;
  if (offset & ((1UL << 12) - 1)) {
    errno = EINVAL;
    return MAP_FAILED;
  }
  ret = __mmap2(addr, size, prot, flags, fd, (size_t)offset >> 12);
  if (ret && (flags & (MAP_PRIVATE | MAP_ANONYMOUS)))
    madvise(ret, size, MADV_MERGEABLE);
  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    %r9,-0x30(%rbp)
mov    -0x30(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
je     1177 <mmap+0x57>
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e76(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11e3 <mmap+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x30(%rbp),%r9
shr    $0xc,%r9
call   1030 <__mmap2@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11db <mmap+0xbb>
mov    -0x20(%rbp),%eax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e30(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11db <mmap+0xbb>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   1040 <madvise@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap>:
push   %rbp
push   %r14
push   %rbx
test   $0xfff,%r9d
je     1151 <mmap+0x31>
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
mov    0x2e9b(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e72(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ecx,%ebp
mov    %rsi,%r14
shr    $0xc,%r9
call   1030 <__mmap2@plt>
test   %rax,%rax
je     1193 <mmap+0x73>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %ebp,%ecx
je     1149 <mmap+0x29>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <madvise@plt>
jmp    1149 <mmap+0x29>
xor    %ebx,%ebx
jmp    1149 <mmap+0x29>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MADV_MERGEABLE = global i32 0
define i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%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 i64 %5, i64* %13
%15 = load i64, i64* %13
%16 = and i64 %15, 4095
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %6
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
%20 = load i8*, i8** @MAP_FAILED
store i8* %20, i8** %7
br label %46
21: ; preds = %6
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = load i32, i32* %12
%27 = load i64, i64* %13
%28 = lshr i64 %27, 12
%29 = call i8* @__mmap2(i8* %22, i64 %23, i32 %24, i32 %25, i32 %26, i64 %28)
store i8* %29, i8** %14
%30 = load i8*, i8** %14
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %44
32: ; preds = %21
%33 = load i32, i32* %11
%34 = load i32, i32* @MAP_PRIVATE
%35 = load i32, i32* @MAP_ANONYMOUS
%36 = or i32 %34, %35
%37 = and i32 %33, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = load i8*, i8** %14
%41 = load i64, i64* %9
%42 = load i32, i32* @MADV_MERGEABLE
%43 = call i32 @madvise(i8* %40, i64 %41, i32 %42)
br label %44
44: ; preds = %39, %32, %21
%45 = load i8*, i8** %14
store i8* %45, i8** %7
br label %46
46: ; preds = %44, %18
%47 = load i8*, i8** %7
ret i8* %47
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MADV_MERGEABLE = global i32 0
define i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = and i64 %5, 4095
%8 = icmp eq i64 %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 %25
12: ; preds = %6
%13 = lshr i64 %5, 12
%14 = tail call i8* @__mmap2(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %25, label %16
16: ; preds = %12
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = or i32 %18, %17
%20 = and i32 %19, %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = load i32, i32* @MADV_MERGEABLE
%24 = tail call i32 @madvise(i8* %14, i64 %1, i32 %23)
br label %25
25: ; preds = %12, %16, %22, %9
%26 = phi i8* [ %11, %9 ], [ %14, %22 ], [ %14, %16 ], [ null, %12 ]
ret i8* %26
}