#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;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int /*<<< orphan*/ exit(int);
void *mmap(void *, int, int /*<<< orphan*/, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ write(int, char const *, int);
void _start(void) {
  const char msg[] = "mmap ok\n";
  void *p, *addr;
  p = mmap(NULL, 0x2000, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (p == MAP_FAILED) {
    const char fail[] = "map failed (1)\n";
    write(2, fail, sizeof fail - 1);
    exit(1);
  }
  addr = mmap(p, 0x1000, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1,
              0);
  if (addr == MAP_FAILED) {
    const char fail[] = "map failed (2)\n";
    write(2, fail, sizeof fail - 1);
    exit(1);
  }
  munmap(p, 0x1000);
  write(1, msg, sizeof msg - 1);
  exit(0);
}


x86 assembly (clang, -O0):
<_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xeb1(%rip),%rax
mov    %rax,-0x9(%rbp)
mov    0xeaf(%rip),%al
mov    %al,-0x1(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <_start+0x99>
mov    0xe63(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe60(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rsi
mov    $0x2,%edi
mov    $0xf,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dda(%rip),%rax
or     (%rax),%ecx
mov    0x2de1(%rip),%rax
or     (%rax),%ecx
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    125f <_start+0x11f>
mov    0xded(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xdea(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0x40(%rbp),%rsi
mov    $0x2,%edi
mov    $0xf,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
lea    -0x9(%rbp),%rsi
mov    $0x1,%edi
mov    $0x8,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<_start>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
movabs $0xa6b6f2070616d6d,%rax
6f 6b 0a
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2e89(%rip),%r15
mov    (%r15),%edx
mov    0x2e5f(%rip),%r12
mov    0x2e68(%rip),%r14
mov    (%r14),%ecx
or     (%r12),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e25(%rip),%r13
cmp    0x0(%r13),%rax
jne    11aa <_start+0x6a>
movaps 0xe68(%rip),%xmm0
jmp    11e3 <_start+0xa3>
mov    %rax,%rbx
mov    (%r15),%edx
mov    0x2e11(%rip),%rax
mov    (%r12),%ecx
or     (%rax),%ecx
or     (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    0x0(%r13),%rax
jne    1206 <_start+0xc6>
movaps 0xe3d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x2,%edi
mov    $0xf,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    $0x8,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@__const._start.msg = [9 x i8] c"mmap ok\0A\00"
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@__const._start.fail = [16 x i8] c"map failed (1)\0A\00"
@MAP_FIXED = global i32 0
@__const._start.fail.1 = [16 x i8] c"map failed (2)\0A\00"
define void @_start() {
%1 = alloca [9 x i8]
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = bitcast [9 x i8]* %1 to i8*
%7 = load i32, i32* @PROT_NONE
%8 = load i32, i32* @MAP_PRIVATE
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %8, %9
%11 = call i8* @mmap(i8* null, i32 8192, i32 %7, i32 %10, i32 -1, i32 0)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = bitcast [16 x i8]* %4 to i8*
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%18 = call i32 @write(i32 2, i8* %17, i32 15)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i8*, i8** %2
%22 = load i32, i32* @PROT_NONE
%23 = load i32, i32* @MAP_FIXED
%24 = load i32, i32* @MAP_PRIVATE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_ANONYMOUS
%27 = or i32 %25, %26
%28 = call i8* @mmap(i8* %21, i32 4096, i32 %22, i32 %27, i32 -1, i32 0)
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %37
32: ; preds = %20
%33 = bitcast [16 x i8]* %5 to i8*
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%35 = call i32 @write(i32 2, i8* %34, i32 15)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %20
%38 = load i8*, i8** %2
%39 = call i32 @munmap(i8* %38, i32 4096)
%40 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%41 = call i32 @write(i32 1, i8* %40, i32 8)
%42 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const._start.msg = [9 x i8] c"mmap ok\0A\00"
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@__const._start.fail = [16 x i8] c"map failed (1)\0A\00"
@MAP_FIXED = global i32 0
@__const._start.fail.1 = [16 x i8] c"map failed (2)\0A\00"
define void @_start() {
%1 = alloca [9 x i8]
%2 = alloca [16 x i8]
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%5 = load i32, i32* @PROT_NONE
%6 = load i32, i32* @MAP_PRIVATE
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %7, %6
%9 = tail call i8* @mmap(i8* null, i32 8192, i32 %5, i32 %8, i32 -1, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%14 = call i32 @write(i32 2, i8* %13, i32 15)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @PROT_NONE
%18 = load i32, i32* @MAP_FIXED
%19 = load i32, i32* @MAP_PRIVATE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_ANONYMOUS
%22 = or i32 %20, %21
%23 = tail call i8* @mmap(i8* %9, i32 4096, i32 %17, i32 %22, i32 -1, i32 0)
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %30
26: ; preds = %16
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%28 = call i32 @write(i32 2, i8* %27, i32 15)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %16
%31 = tail call i32 @munmap(i8* %9, i32 4096)
%32 = call i32 @write(i32 1, i8* %4, i32 8)
%33 = tail call i32 @exit(i32 0)
unreachable
}