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

/* Variables and functions */
int ENOTSUP;
int MAP_ANON;
void *MAP_FAILED;
int PROT_READ;
int errno;
int /*<<< orphan*/ free(void *);
int posix_memalign(void **, size_t, int);
int pread(int, void *, size_t, int /*<<< orphan*/);
void *_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) {
  void *v;
  ssize_t nn;
  int error;
  if (fd != -1 && prot != PROT_READ) {
    errno = ENOTSUP;
    return MAP_FAILED;
  }
  if ((error = posix_memalign(&v, len, 0x1000)) != 0) {
    errno = error;
    return MAP_FAILED;
  }
  if (flags & MAP_ANON)
    return v;
  if ((nn = pread(fd, v, len, off)) == -1) {
    error = errno;
    free(v);
    errno = error;
    return MAP_FAILED;
  }
  return v;
}


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)
cmpl   $0xffffffff,-0x24(%rbp)
je     118f <_mmap+0x5f>
mov    -0x1c(%rbp),%eax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%eax
je     118f <_mmap+0x5f>
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e56(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1249 <_mmap+0x119>
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdi
mov    $0x1000,%edx
call   1050 <posix_memalign@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
je     11cc <_mmap+0x9c>
mov    -0x38(%rbp),%ecx
mov    0x2e19(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1249 <_mmap+0x119>
mov    -0x20(%rbp),%eax
mov    0x2df2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ee <_mmap+0xbe>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1249 <_mmap+0x119>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%ecx
call   1040 <pread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1241 <_mmap+0x111>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%ecx
mov    0x2da4(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1249 <_mmap+0x119>
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
mov    %r9d,%r14d
mov    %r8d,%r13d
mov    %ecx,%r12d
mov    %rsi,%r15
cmp    $0xffffffff,%r8d
je     1163 <_mmap+0x33>
mov    0x2ea4(%rip),%rax
cmp    %edx,(%rax)
je     1163 <_mmap+0x33>
mov    0x2e81(%rip),%rax
mov    (%rax),%ebp
jmp    117c <_mmap+0x4c>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <posix_memalign@plt>
mov    %eax,%ebp
test   %eax,%eax
je     119e <_mmap+0x6e>
mov    0x2e4d(%rip),%rax
mov    %ebp,(%rax)
mov    0x2e34(%rip),%rbx
mov    (%rbx),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e23(%rip),%rax
test   %r12d,(%rax)
jne    118c <_mmap+0x5c>
mov    (%rsp),%rsi
mov    %r13d,%edi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1040 <pread@plt>
cmp    $0xffffffff,%eax
jne    118c <_mmap+0x5c>
mov    0x2e08(%rip),%rax
mov    (%rax),%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    117c <_mmap+0x4c>

LLVM IR (-O0):
@PROT_READ = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@MAP_ANON = 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*
%15 = alloca i32
%16 = alloca i32
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
%17 = load i32, i32* %12
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %6
%20 = load i32, i32* %10
%21 = load i32, i32* @PROT_READ
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @ENOTSUP
store i32 %24, i32* @errno
%25 = load i8*, i8** @MAP_FAILED
store i8* %25, i8** %7
br label %55
26: ; preds = %19, %6
%27 = load i64, i64* %9
%28 = call i32 @posix_memalign(i8** %14, i64 %27, i32 4096)
store i32 %28, i32* %16
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %16
store i32 %31, i32* @errno
%32 = load i8*, i8** @MAP_FAILED
store i8* %32, i8** %7
br label %55
33: ; preds = %26
%34 = load i32, i32* %11
%35 = load i32, i32* @MAP_ANON
%36 = and i32 %34, %35
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i8*, i8** %14
store i8* %39, i8** %7
br label %55
40: ; preds = %33
%41 = load i32, i32* %12
%42 = load i8*, i8** %14
%43 = load i64, i64* %9
%44 = load i32, i32* %13
%45 = call i32 @pread(i32 %41, i8* %42, i64 %43, i32 %44)
store i32 %45, i32* %15
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %40
%48 = load i32, i32* @errno
store i32 %48, i32* %16
%49 = load i8*, i8** %14
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %16
store i32 %51, i32* @errno
%52 = load i8*, i8** @MAP_FAILED
store i8* %52, i8** %7
br label %55
53: ; preds = %40
%54 = load i8*, i8** %14
store i8* %54, i8** %7
br label %55
55: ; preds = %53, %47, %38, %30, %23
%56 = load i8*, i8** %7
ret i8* %56
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
@MAP_FAILED = global i8* null
@MAP_ANON = global i32 0
define i8* @_mmap(i8* nocapture readnone %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = bitcast i8** %7 to i8*
%9 = icmp eq i32 %4, -1
%10 = load i32, i32* @PROT_READ
%11 = icmp eq i32 %10, %2
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = load i32, i32* @ENOTSUP
br label %30
15: ; preds = %6
%16 = call i32 @posix_memalign(i8** %7, i64 %1, i32 4096)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i32, i32* @MAP_ANON
%20 = and i32 %19, %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i8*, i8** %7
%24 = call i32 @pread(i32 %4, i8* %23, i64 %1, i32 %5)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* @errno
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %15, %13, %26
%31 = phi i32 [ %27, %26 ], [ %14, %13 ], [ %16, %15 ]
store i32 %31, i32* @errno
br label %32
32: ; preds = %30, %22, %18
%33 = phi i8** [ %7, %18 ], [ %7, %22 ], [ @MAP_FAILED, %30 ]
%34 = load i8*, i8** %33
ret i8* %34
}