#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;
int MAP_NORESERVE;
int MAP_POPULATE;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int atoll(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int munmap(char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  long long n = (argc > 1) ? atoll(argv[1]) : 1024 * 1024 * 1024;
  for (size_t i = 1; i <= n; i *= 2) {
    size_t bytes = i;
    printf("bytes=%zu\n", bytes);
    {
      int prot = PROT_READ | PROT_WRITE;
      int flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_POPULATE | MAP_PRIVATE;
      char *ptr = mmap(NULL, bytes, prot, flags, -1, 0);
      printf("ptr=%p\n", ptr);
      memset(ptr, 0, bytes);
      int rc = munmap(ptr, bytes);
      printf("rc=%d\n", rc);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1185 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoll@plt>
mov    %eax,-0x40(%rbp)
jmp    1192 <main+0x42>
mov    $0x40000000,%eax
mov    %eax,-0x40(%rbp)
jmp    1192 <main+0x42>
mov    -0x40(%rbp),%eax
cltq
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     127a <main+0x12a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    0x2de5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    0x2df0(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <munmap@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11a3 <main+0x53>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x40000000,%ebp
cmp    $0x2,%edi
jl     1179 <main+0x29>
mov    0x8(%rsi),%rdi
call   1060 <atoll@plt>
test   %eax,%eax
je     1234 <main+0xe4>
movslq %eax,%rbp
mov    $0x1,%ebx
mov    0x2e33(%rip),%r14
lea    0xe7f(%rip),%r15
lea    0xe80(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe59(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    0x2e05(%rip),%rax
or     (%rax),%ecx
mov    0x2df4(%rip),%rax
or     (%rax),%ecx
or     (%r14),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <munmap@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    %rbx,%rbx
cmp    %rbp,%rbx
jbe    11a0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"bytes=%zu\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_POPULATE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [8 x i8] c"ptr=%p\0A\00"
@.str.2 = [7 x i8] c"rc=%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoll(i8* %18)
br label %21
20: ; preds = %2
br label %21
21: ; preds = %20, %15
%22 = phi i32 [ %19, %15 ], [ 1073741824, %20 ]
%23 = sext i32 %22 to i64
store i64 %23, i64* %6
store i64 1, i64* %7
br label %24
24: ; preds = %56, %21
%25 = load i64, i64* %7
%26 = load i64, i64* %6
%27 = icmp ule i64 %25, %26
br i1 %27, label %28, label %59
28: ; preds = %24
%29 = load i64, i64* %7
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %30)
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %32, %33
store i32 %34, i32* %9
%35 = load i32, i32* @MAP_ANONYMOUS
%36 = load i32, i32* @MAP_NORESERVE
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_POPULATE
%39 = or i32 %37, %38
%40 = load i32, i32* @MAP_PRIVATE
%41 = or i32 %39, %40
store i32 %41, i32* %10
%42 = load i64, i64* %8
%43 = load i32, i32* %9
%44 = load i32, i32* %10
%45 = call i8* @mmap(i32* null, i64 %42, i32 %43, i32 %44, i32 -1, i32 0)
store i8* %45, i8** %11
%46 = load i8*, i8** %11
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %11
%49 = load i64, i64* %8
%50 = call i32 @memset(i8* %48, i32 0, i64 %49)
%51 = load i8*, i8** %11
%52 = load i64, i64* %8
%53 = call i32 @munmap(i8* %51, i64 %52)
store i32 %53, i32* %12
%54 = load i32, i32* %12
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %28
%57 = load i64, i64* %7
%58 = mul i64 %57, 2
store i64 %58, i64* %7
br label %24
59: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"bytes=%zu\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_POPULATE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [8 x i8] c"ptr=%p\0A\00"
@.str.2 = [7 x i8] c"rc=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoll(i8* %6)
%8 = sext i32 %7 to i64
%9 = icmp eq i32 %7, 0
br i1 %9, label %12, label %10
10: ; preds = %2, %4
%11 = phi i64 [ %8, %4 ], [ 1073741824, %2 ]
br label %13
12: ; preds = %13, %4
ret i32 0
13: ; preds = %10, %13
%14 = phi i64 [ %31, %13 ], [ 1, %10 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %14)
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = load i32, i32* @MAP_NORESERVE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_POPULATE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_PRIVATE
%25 = or i32 %23, %24
%26 = tail call i8* @mmap(i32* null, i64 %14, i32 %18, i32 %25, i32 -1, i32 0)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = tail call i32 @memset(i8* %26, i32 0, i64 %14)
%29 = tail call i32 @munmap(i8* %26, i64 %14)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = shl i64 %14, 1
%32 = icmp ugt i64 %31, %11
br i1 %32, label %12, label %13
}