#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 EXIT_SUCCESS;
int MAP_ANON;
char *MAP_FAILED;
int MAP_FIXED;
int MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ errx(int, char *, unsigned int);
scalar_t__ mmap(char *, size_t, int /*<<< orphan*/, int, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int /*<<< orphan*/ printf(char *, char *);
int mmapcall(int N) {
  size_t len = 4096;
  int fd = -1;
  char *anon;
  char *addr = NULL;
  addr = (char *)mmap(addr, len, PROT_READ, MAP_ANON | MAP_SHARED, fd, 0);
  printf("addr=%p.\n", addr);
  unsigned int i = 0;
  while (i < N) {
    anon = (char *)mmap(addr, len, PROT_READ, MAP_ANON | MAP_SHARED | MAP_FIXED,
                        fd, 0);
    if (anon == MAP_FAILED) {
      errx(1, "either anon when i=%d", i);
    }
    i++;
  }
  munmap(anon, len);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<mmapcall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x1000,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
mov    0x2e65(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    122a <mmapcall+0xea>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0f(%rip),%rax
or     (%rax),%ecx
mov    0x2dde(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <mmapcall+0xdc>
mov    -0x2c(%rbp),%edx
mov    $0x1,%edi
lea    0xdf3(%rip),%rsi
call   1040 <errx@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ac <mmapcall+0x6c>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <munmap@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmapcall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2ea3(%rip),%rax
mov    (%rax),%edx
mov    0x2e72(%rip),%rax
mov    0x2e83(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %ebx,%ebx
mov    $0x1000,%esi
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
lea    0xe76(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    0x2e27(%rip),%r15
mov    0x2e18(%rip),%r13
jmp    11b8 <mmapcall+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%ebx
cmp    %ebx,%r14d
je     120a <mmapcall+0xca>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2dff(%rip),%rax
or     (%rax),%ecx
or     (%r15),%ecx
mov    $0x1000,%esi
mov    %r12,%rdi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
cmp    %rax,0x0(%r13)
jne    11b0 <mmapcall+0x70>
mov    $0x1,%edi
lea    0xe09(%rip),%rsi
mov    %ebx,%edx
call   1040 <errx@plt>
jmp    11b0 <mmapcall+0x70>
mov    $0x1000,%esi
mov    %rbp,%rdi
call   1060 <munmap@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@.str = [10 x i8] c"addr=%p.\0A\00"
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [22 x i8] c"either anon when i=%d\00"
@EXIT_SUCCESS = global i32 0
define i32 @mmapcall(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %2
store i64 4096, i64* %3
store i32 -1, i32* %4
store i8* null, i8** %6
%8 = load i8*, i8** %6
%9 = load i64, i64* %3
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @MAP_ANON
%12 = load i32, i32* @MAP_SHARED
%13 = or i32 %11, %12
%14 = load i32, i32* %4
%15 = call i64 @mmap(i8* %8, i64 %9, i32 %10, i32 %13, i32 %14, i32 0)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %7
br label %19
19: ; preds = %41, %1
%20 = load i32, i32* %7
%21 = load i32, i32* %2
%22 = icmp ult i32 %20, %21
br i1 %22, label %23, label %44
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = load i64, i64* %3
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_ANON
%28 = load i32, i32* @MAP_SHARED
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_FIXED
%31 = or i32 %29, %30
%32 = load i32, i32* %4
%33 = call i64 @mmap(i8* %24, i64 %25, i32 %26, i32 %31, i32 %32, i32 0)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %23
%39 = load i32, i32* %7
%40 = call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %23
%42 = load i32, i32* %7
%43 = add i32 %42, 1
store i32 %43, i32* %7
br label %19
44: ; preds = %19
%45 = load i8*, i8** %5
%46 = load i64, i64* %3
%47 = call i32 @munmap(i8* %45, i64 %46)
%48 = load i32, i32* @EXIT_SUCCESS
ret i32 %48
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@.str = [10 x i8] c"addr=%p.\0A\00"
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [22 x i8] c"either anon when i=%d\00"
@EXIT_SUCCESS = global i32 0
define i32 @mmapcall(i32 %0) {
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @MAP_ANON
%4 = load i32, i32* @MAP_SHARED
%5 = or i32 %4, %3
%6 = tail call i64 @mmap(i8* null, i64 4096, i32 %2, i32 %5, i32 -1, i32 0)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = icmp ne i32 %0, 0
br label %10
10: ; preds = %1, %24
%11 = phi i32 [ %25, %24 ], [ 0, %1 ]
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_ANON
%14 = load i32, i32* @MAP_SHARED
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_FIXED
%17 = or i32 %15, %16
%18 = tail call i64 @mmap(i8* %7, i64 4096, i32 %12, i32 %17, i32 -1, i32 0)
%19 = inttoptr i64 %18 to i8*
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %20, %19
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %24
24: ; preds = %22, %10
%25 = add nuw i32 %11, 1
%26 = icmp eq i32 %25, %0
br i1 %26, label %27, label %10
27: ; preds = %24
%28 = inttoptr i64 %18 to i8*
%29 = tail call i32 @munmap(i8* %28, i64 4096)
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}