#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 /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SIGSEGV;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ close(int);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  signal(SIGSEGV, SIG_IGN);
  int fd = open("/dev/zero", O_RDONLY);
  printf("fd = %d\n", fd);
  char *cp = mmap(NULL, 1024 * 1024, PROT_READ, MAP_PRIVATE, fd, 0);
  printf("addr = %p\n", cp);
  while (1) {
    printf("cp=%p %x\n", cp, *cp);
    cp += 8192;
    int fd = open(cp, O_RDONLY);
    if (fd > 0)
      close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x2000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    122a <main+0xda>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    11dc <main+0x8c>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    0x2e65(%rip),%r15
mov    (%r15),%esi
lea    0xe83(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    0x2e1c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x100000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    0xe51(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%rbx),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x2000,%rbx
mov    (%r15),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
jle    11e0 <main+0x90>
mov    %eax,%edi
call   1050 <close@plt>
jmp    11e0 <main+0x90>

LLVM IR (-O0):
@SIGSEGV = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"fd = %d\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [11 x i8] c"addr = %p\0A\00"
@.str.3 = [10 x i8] c"cp=%p %x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @SIGSEGV
%6 = load i32, i32* @SIG_IGN
%7 = call i32 @signal(i32 %5, i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* %2
%15 = call i8* @mmap(i32* null, i32 1048576, i32 %12, i32 %13, i32 %14, i32 0)
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %0, %34
%19 = load i8*, i8** %3
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %19, i32 %22)
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 8192
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = load i32, i32* %4
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %18
br label %18
}

LLVM IR (-O2):
@SIGSEGV = global i32 0
@SIG_IGN = global i32 0
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"fd = %d\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [11 x i8] c"addr = %p\0A\00"
@.str.3 = [10 x i8] c"cp=%p %x\0A\00"
define i32 @main() {
%1 = load i32, i32* @SIGSEGV
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @MAP_PRIVATE
%9 = tail call i8* @mmap(i32* null, i32 1048576, i32 %7, i32 %8, i32 %5, i32 0)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %22, %0
%12 = phi i8* [ %9, %0 ], [ %16, %22 ]
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %12, i32 %14)
%16 = getelementptr inbounds i8, i8* %12, i64 8192
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = tail call i32 @close(i32 %18)
br label %22
22: ; preds = %20, %11
br label %11
}