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


x86 assembly (clang, -O0):
<mmapcall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0xeb0(%rip),%rax
mov    %rax,-0x15(%rbp)
mov    0xeae(%rip),%al
mov    %al,-0xd(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11ea <mmapcall+0xba>
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
mov    0x2e57(%rip),%rax
or     (%rax),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5d(%rip),%rax
or     (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    11ce <mmapcall+0x9e>
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
lea    0xe40(%rip),%rsi
call   1040 <errx@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115d <mmapcall+0x2d>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmapcall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     11c1 <mmapcall+0x91>
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2e79(%rip),%r14
mov    0x2e92(%rip),%r12
mov    0x2e5b(%rip),%r13
jmp    1175 <mmapcall+0x45>
nop
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
add    $0x1,%ebx
cmp    %ebx,%r15d
je     11c1 <mmapcall+0x91>
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    0x2e73(%rip),%rax
or     (%rax),%edx
mov    (%r12),%ecx
or     (%r14),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
cmp    %rax,0x0(%r13)
jne    1160 <mmapcall+0x30>
mov    $0x1,%edi
lea    0xe48(%rip),%rsi
mov    %ebx,%edx
call   1040 <errx@plt>
jmp    1160 <mmapcall+0x30>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
add    $0x8,%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(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [9 x i8]
%6 = alloca i8*
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 -1, i32* %4
%7 = bitcast [9 x i8]* %5 to i8*
br label %8
8: ; preds = %28, %1
%9 = load i32, i32* %3
%10 = load i32, i32* %2
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %33
12: ; preds = %8
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_ANON
%17 = load i32, i32* @MAP_SHARED
%18 = or i32 %16, %17
%19 = load i32, i32* %4
%20 = call i64 @mmap(i32* null, i32 4096, i32 %15, i32 %18, i32 %19, i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = load i32, i32* %3
%27 = call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %12
%29 = load i8*, i8** %6
%30 = call i32 @munmap(i8* %29, i32 4096)
%31 = load i32, i32* %3
%32 = add i32 %31, 1
store i32 %32, i32* %3
br label %8
33: ; preds = %8
%34 = load i32, i32* @EXIT_SUCCESS
ret i32 %34
}

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(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %21, label %3
3: ; preds = %1, %17
%4 = phi i32 [ %19, %17 ], [ 0, %1 ]
%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 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %13, %12
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %17
17: ; preds = %15, %3
%18 = tail call i32 @munmap(i8* %12, i32 4096)
%19 = add nuw i32 %4, 1
%20 = icmp eq i32 %19, %0
br i1 %20, label %21, label %3
21: ; preds = %17, %1
%22 = load i32, i32* @EXIT_SUCCESS
ret i32 %22
}