#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 MAP_ANONYMOUS;
int MAP_PRIVATE;
int MAP_SHARED;
int /*<<< orphan*/ POSIX_FADV_RANDOM;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ posix_fadvise(int, int /*<<< orphan*/, size_t,
                                 int /*<<< orphan*/);
void *do_mmap(int fd, size_t len) {
  int map_flags = (fd >= 0) ? MAP_SHARED : MAP_ANONYMOUS | MAP_PRIVATE;
  if (fd >= 0)
    posix_fadvise(fd, 0, len, POSIX_FADV_RANDOM);
  return mmap(NULL, len, PROT_READ | PROT_WRITE, map_flags, fd, 0);
}


x86 assembly (clang, -O0):
<do_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     114a <do_mmap+0x2a>
mov    0x2ea8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    115f <do_mmap+0x3f>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1186 <do_mmap+0x66>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
xor    %esi,%esi
call   1040 <posix_fadvise@plt>
mov    -0x10(%rbp),%rsi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mmap>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
test   %edi,%edi
js     114d <do_mmap+0x2d>
mov    0x2eb4(%rip),%rax
mov    (%rax),%ebp
mov    0x2e8b(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <posix_fadvise@plt>
jmp    115f <do_mmap+0x3f>
mov    0x2e84(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e92(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mmap@plt>

LLVM IR (-O0):
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@POSIX_FADV_RANDOM = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i8* @do_mmap(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @MAP_SHARED
br label %14
10: ; preds = %2
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = load i32, i32* @MAP_PRIVATE
%13 = or i32 %11, %12
br label %14
14: ; preds = %10, %8
%15 = phi i32 [ %9, %8 ], [ %13, %10 ]
store i32 %15, i32* %5
%16 = load i32, i32* %3
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* %3
%20 = load i64, i64* %4
%21 = load i32, i32* @POSIX_FADV_RANDOM
%22 = call i32 @posix_fadvise(i32 %19, i32 0, i64 %20, i32 %21)
br label %23
23: ; preds = %18, %14
%24 = load i64, i64* %4
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* %5
%29 = load i32, i32* %3
%30 = call i8* @mmap(i32* null, i64 %24, i32 %27, i32 %28, i32 %29, i32 0)
ret i8* %30
}

LLVM IR (-O2):
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@POSIX_FADV_RANDOM = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
define i8* @do_mmap(i32 %0, i64 %1) {
%3 = icmp sgt i32 %0, -1
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %5, %4
br i1 %3, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @MAP_SHARED
%9 = load i32, i32* @POSIX_FADV_RANDOM
%10 = tail call i32 @posix_fadvise(i32 %0, i32 0, i64 %1, i32 %9)
br label %11
11: ; preds = %7, %2
%12 = phi i32 [ %8, %7 ], [ %6, %2 ]
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = tail call i8* @mmap(i32* null, i64 %1, i32 %15, i32 %12, i32 %0, i32 0)
ret i8* %16
}