#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 */
typedef int /*<<< orphan*/ str;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
scalar_t__ mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  const char *str[] = {"Hello", "This is an example", "of memory",
                       "of memory that is captured", "by mmap extension"};
  int size = 128;
  char *map = (char *)mmap(0, size, PROT_READ | PROT_WRITE,
                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (map <= 0) {
    perror("mmap");
    return 1;
  }
  for (int i = 0; i < sizeof(str) / sizeof(*str); i++) {
    strncpy(map, str[i], size);
    write(1, ".", 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe85(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe8c(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe9c(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x80,-0x34(%rbp)
mov    -0x34(%rbp),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e10(%rip),%rax
or     (%rax),%edx
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    0x2df6(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
ja     120c <main+0xbc>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0x107>
movl   $0x0,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    $0x5,%rax
jae    1257 <main+0x107>
mov    -0x40(%rbp),%rdi
movslq -0x44(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rsi
mov    -0x34(%rbp),%edx
call   1030 <strncpy@plt>
mov    $0x1,%edx
lea    0xe13(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1213 <main+0xc3>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ead(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e83(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x80,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     1259 <main+0x119>
mov    %rax,%rbx
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
lea    0xeae(%rip),%rbp
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe33(%rip),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1030 <strncpy@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    1251 <main+0x111>

LLVM IR (-O0):
@.str = [6 x i8] c"Hello\00"
@.str.1 = [19 x i8] c"This is an example\00"
@.str.2 = [10 x i8] c"of memory\00"
@.str.3 = [27 x i8] c"of memory that is captured\00"
@.str.4 = [18 x i8] c"by mmap extension\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [5 x i8] c"mmap\00"
@.str.6 = [2 x i8] c".\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8*]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [5 x i8*]* %2 to i8*
%7 = bitcast i8* %6 to [5 x i8*]*
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 1
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i32 0, i32 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 3
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %7, i32 0, i32 4
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i32 0, i32 0), i8** %12
store i32 128, i32* %3
%13 = load i32, i32* %3
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = load i32, i32* @MAP_PRIVATE
%19 = or i32 %17, %18
%20 = call i64 @mmap(i32 0, i32 %13, i32 %16, i32 %19, i32 -1, i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp ule i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %43
26: ; preds = %0
store i32 0, i32* %5
br label %27
27: ; preds = %40, %26
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = icmp ult i64 %29, 5
br i1 %30, label %31, label %43
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = load i32, i32* %3
%38 = call i32 @strncpy(i8* %32, i8* %36, i32 %37)
%39 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
br label %40
40: ; preds = %31
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %27
43: ; preds = %24, %27
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [6 x i8] c"Hello\00"
@.str.1 = [19 x i8] c"This is an example\00"
@.str.2 = [10 x i8] c"of memory\00"
@.str.3 = [27 x i8] c"of memory that is captured\00"
@.str.4 = [18 x i8] c"by mmap extension\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [5 x i8] c"mmap\00"
@.str.6 = [2 x i8] c".\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %5, %4
%7 = tail call i64 @mmap(i32 0, i32 128, i32 %3, i32 %6, i32 -1, i32 0)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %21, label %10
10: ; preds = %0
%11 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 128)
%12 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%13 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 128)
%14 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%15 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 128)
%16 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%17 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 128)
%18 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%19 = tail call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 128)
%20 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
br label %23
21: ; preds = %0
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %10, %21
%24 = phi i32 [ 1, %21 ], [ 0, %10 ]
ret i32 %24
}