#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;
scalar_t__ MAP_FAILED;
int MAP_NORESERVE;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIGUSR2;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  long n = 0;
  signal(SIGUSR1, SIG_IGN);
  signal(SIGUSR2, SIG_IGN);
  printf("Starting mmap test...\n");
  for (n = 0; n < 30000; ++n) {
    if (mmap(NULL, 1, (n & 1) ? PROT_READ : PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1,
             0) == MAP_FAILED) {
      printf("Failed after %ld successful maps\n", n - 1);
      exit(0);
    }
  }
  alarm(80);
  printf("Turning on single-stepping...\n");
  kill(getpid(), SIGUSR1);
  printf("Va bene?\n");
  kill(getpid(), SIGUSR2);
  printf("Turned single-stepping off...\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x7530,-0x10(%rbp)
jge    1280 <main+0x110>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11fc <main+0x8c>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1208 <main+0x98>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
mov    0x2db5(%rip),%rax
or     (%rax),%ecx
mov    0x2dbc(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <main+0xfa>
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    126f <main+0xff>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11cb <main+0x5b>
mov    $0x50,%edi
call   1060 <alarm@plt>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rbx
mov    (%rbx),%esi
call   1080 <signal@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%esi
call   1080 <signal@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%rbx
mov    0x2e2c(%rip),%r12
mov    0x2dd5(%rip),%rbp
mov    0x2df6(%rip),%r14
mov    0x2dff(%rip),%r15
mov    0x2db8(%rip),%r13
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%ecx
mov    %r12,%rax
test   $0x1,%cl
jne    1202 <main+0x82>
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    (%r14),%ecx
or     0x0(%rbp),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    0x0(%r13),%rax
je     12a2 <main+0x122>
add    $0x1,%rbx
cmp    $0x752f,%rbx
jne    11f0 <main+0x70>
mov    $0x50,%edi
call   1070 <alarm@plt>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2d7f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xdd3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd57(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@SIGUSR2 = global i32 0
@.str = [23 x i8] c"Starting mmap test...\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_FAILED = global i64 0
@.str.1 = [34 x i8] c"Failed after %ld successful maps\0A\00"
@.str.2 = [31 x i8] c"Turning on single-stepping...\0A\00"
@.str.3 = [10 x i8] c"Va bene?\0A\00"
@.str.4 = [31 x i8] c"Turned single-stepping off...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
store i64 0, i64* %2
%3 = load i32, i32* @SIGUSR1
%4 = load i32, i32* @SIG_IGN
%5 = call i32 @signal(i32 %3, i32 %4)
%6 = load i32, i32* @SIGUSR2
%7 = load i32, i32* @SIG_IGN
%8 = call i32 @signal(i32 %6, i32 %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %2
br label %10
10: ; preds = %37, %0
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 30000
br i1 %12, label %13, label %40
13: ; preds = %10
%14 = load i64, i64* %2
%15 = and i64 %14, 1
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @PROT_READ
br label %21
19: ; preds = %13
%20 = load i32, i32* @PROT_WRITE
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
%23 = load i32, i32* @MAP_PRIVATE
%24 = load i32, i32* @MAP_ANONYMOUS
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_NORESERVE
%27 = or i32 %25, %26
%28 = call i64 @mmap(i32* null, i32 1, i32 %22, i32 %27, i32 -1, i32 0)
%29 = load i64, i64* @MAP_FAILED
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %21
%32 = load i64, i64* %2
%33 = sub nsw i64 %32, 1
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %21
br label %37
37: ; preds = %36
%38 = load i64, i64* %2
%39 = add nsw i64 %38, 1
store i64 %39, i64* %2
br label %10
40: ; preds = %10
%41 = call i32 @alarm(i32 80)
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 (...) @getpid()
%44 = load i32, i32* @SIGUSR1
%45 = call i32 @kill(i32 %43, i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 (...) @getpid()
%48 = load i32, i32* @SIGUSR2
%49 = call i32 @kill(i32 %47, i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
@SIG_IGN = global i32 0
@SIGUSR2 = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_FAILED = global i64 0
@.str.1 = [34 x i8] c"Failed after %ld successful maps\0A\00"
@str = [22 x i8] c"Starting mmap test...\00"
@str.5 = [30 x i8] c"Turning on single-stepping...\00"
@str.6 = [9 x i8] c"Va bene?\00"
@str.7 = [30 x i8] c"Turned single-stepping off...\00"
define i32 @main() {
%1 = load i32, i32* @SIGUSR1
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = load i32, i32* @SIGUSR2
%5 = load i32, i32* @SIG_IGN
%6 = tail call i32 @signal(i32 %4, i32 %5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %8
8: ; preds = %0, %27
%9 = phi i64 [ 0, %0 ], [ %28, %27 ]
%10 = and i64 %9, 1
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = select i1 %11, i32 %13, i32 %12
%15 = load i32, i32* @MAP_PRIVATE
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_NORESERVE
%19 = or i32 %17, %18
%20 = tail call i64 @mmap(i32* null, i32 1, i32 %14, i32 %19, i32 -1, i32 0)
%21 = load i64, i64* @MAP_FAILED
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %27
23: ; preds = %8
%24 = add nsw i64 %9, -1
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %24)
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %8
%28 = add nuw nsw i64 %9, 1
%29 = icmp eq i64 %28, 30000
br i1 %29, label %30, label %8
30: ; preds = %27
%31 = tail call i32 @alarm(i32 80)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%33 = tail call i32 (...) @getpid()
%34 = load i32, i32* @SIGUSR1
%35 = tail call i32 @kill(i32 %33, i32 %34)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
%37 = tail call i32 (...) @getpid()
%38 = load i32, i32* @SIGUSR2
%39 = tail call i32 @kill(i32 %37, i32 %38)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}