#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_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errx(int, char *, unsigned int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int mmapcall(void) {
  unsigned int i = 0;
  int fd = -1;
  const char str1[] = "string 1";
  char *anon[60000];
  int len = 4096;
  while (i < 60000) {
    anon[i] = (char *)mmap(NULL, len, PROT_READ | PROT_WRITE,
                           MAP_ANON | MAP_SHARED, fd, 0);
    if (anon[i] == MAP_FAILED) {
      errx(1, "either anon when i=%d", i);
    }
    i++;
  }
  i = 0;
  while (i < 60000) {
    munmap(anon[i], len);
    i++;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<mmapcall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x75330,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0xeb0(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xeae(%rip),%al
mov    %al,-0x9(%rbp)
movl   $0x1000,-0x75324(%rbp)
cmpl   $0xea60,-0x4(%rbp)
jae    11f9 <mmapcall+0xc9>
mov    -0x75324(%rbp),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
mov    0x2e35(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4c(%rip),%rax
or     (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x4(%rbp),%eax
mov    %rcx,-0x75320(%rbp,%rax,8)
ff
mov    -0x4(%rbp),%eax
mov    -0x75320(%rbp,%rax,8),%rax
ff
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <mmapcall+0xbb>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xe23(%rip),%rsi
call   1040 <errx@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1167 <mmapcall+0x37>
movl   $0x0,-0x4(%rbp)
cmpl   $0xea60,-0x4(%rbp)
jae    1231 <mmapcall+0x101>
mov    -0x4(%rbp),%eax
mov    -0x75320(%rbp,%rax,8),%rdi
ff
mov    -0x75324(%rbp),%esi
call   1050 <munmap@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <mmapcall+0xd0>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
add    $0x75330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmapcall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x75308,%rsp
xor    %ebx,%ebx
mov    0x2eae(%rip),%r15
mov    0x2e7f(%rip),%r12
mov    0x2e70(%rip),%r13
mov    0x2e89(%rip),%rbp
mov    0x2e52(%rip),%r14
jmp    117d <mmapcall+0x4d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0xea60,%rbx
je     11be <mmapcall+0x8e>
mov    (%r12),%edx
or     (%r15),%edx
mov    0x0(%rbp),%ecx
or     0x0(%r13),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%rsp,%rbx,8)
cmp    %rax,(%r14)
jne    1170 <mmapcall+0x40>
mov    $0x1,%edi
lea    0xe4b(%rip),%rsi
mov    %ebx,%edx
call   1040 <errx@plt>
jmp    1170 <mmapcall+0x40>
xor    %ebx,%ebx
mov    (%rsp,%rbx,8),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
add    $0x1,%rbx
cmp    $0xea60,%rbx
jne    11c0 <mmapcall+0x90>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
add    $0x75308,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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