#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(int argc, char **argv) {
  long n = 0;
  signal(SIGUSR1, SIG_IGN);
  signal(SIGUSR2, SIG_IGN);
  printf("Starting mmap test...\n");
  for (n = 0; n < 30000; ++n) {
    if (mmap(0, 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)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
cmpq   $0x7530,-0x18(%rbp)
jge    1286 <main+0x116>
mov    -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1203 <main+0x93>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    120f <main+0x9f>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edx
mov    0x2d8f(%rip),%rax
mov    (%rax),%ecx
mov    0x2dae(%rip),%rax
or     (%rax),%ecx
mov    0x2db5(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0x1,%esi
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <main+0x100>
mov    -0x18(%rbp),%rsi
sub    $0x1,%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1275 <main+0x105>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11d2 <main+0x62>
mov    $0x50,%edi
call   1060 <alarm@plt>
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xd81(%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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%7 = load i32, i32* @SIGUSR1
%8 = load i32, i32* @SIG_IGN
%9 = call i32 @signal(i32 %7, i32 %8)
%10 = load i32, i32* @SIGUSR2
%11 = load i32, i32* @SIG_IGN
%12 = call i32 @signal(i32 %10, i32 %11)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %6
br label %14
14: ; preds = %41, %2
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 30000
br i1 %16, label %17, label %44
17: ; preds = %14
%18 = load i64, i64* %6
%19 = and i64 %18, 1
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @PROT_READ
br label %25
23: ; preds = %17
%24 = load i32, i32* @PROT_WRITE
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
%27 = load i32, i32* @MAP_PRIVATE
%28 = load i32, i32* @MAP_ANONYMOUS
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_NORESERVE
%31 = or i32 %29, %30
%32 = call i64 @mmap(i32 0, i32 1, i32 %26, i32 %31, i32 -1, i32 0)
%33 = load i64, i64* @MAP_FAILED
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %25
%36 = load i64, i64* %6
%37 = sub nsw i64 %36, 1
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %25
br label %41
41: ; preds = %40
%42 = load i64, i64* %6
%43 = add nsw i64 %42, 1
store i64 %43, i64* %6
br label %14
44: ; preds = %14
%45 = call i32 @alarm(i32 80)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 (...) @getpid()
%48 = load i32, i32* @SIGUSR1
%49 = call i32 @kill(i32 %47, i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 (...) @getpid()
%52 = load i32, i32* @SIGUSR2
%53 = call i32 @kill(i32 %51, i32 %52)
%54 = 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(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @SIGUSR1
%4 = load i32, i32* @SIG_IGN
%5 = tail call i32 @signal(i32 %3, i32 %4)
%6 = load i32, i32* @SIGUSR2
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %2, %29
%11 = phi i64 [ 0, %2 ], [ %30, %29 ]
%12 = and i64 %11, 1
%13 = icmp eq i64 %12, 0
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = select i1 %13, i32 %15, i32 %14
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_NORESERVE
%21 = or i32 %19, %20
%22 = tail call i64 @mmap(i32 0, i32 1, i32 %16, i32 %21, i32 -1, i32 0)
%23 = load i64, i64* @MAP_FAILED
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %29
25: ; preds = %10
%26 = add nsw i64 %11, -1
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %26)
%28 = tail call i32 @exit(i32 0)
unreachable
29: ; preds = %10
%30 = add nuw nsw i64 %11, 1
%31 = icmp eq i64 %30, 30000
br i1 %31, label %32, label %10
32: ; preds = %29
%33 = tail call i32 @alarm(i32 80)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%35 = tail call i32 (...) @getpid()
%36 = load i32, i32* @SIGUSR1
%37 = tail call i32 @kill(i32 %35, i32 %36)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
%39 = tail call i32 (...) @getpid()
%40 = load i32, i32* @SIGUSR2
%41 = tail call i32 @kill(i32 %39, i32 %40)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}