#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_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 | 0, -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    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%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    127a <main+0x10a>
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    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    0x2d9e(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbd(%rip),%rax
or     (%rax),%ecx
or     $0x0,%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    1264 <main+0xf4>
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1269 <main+0xf9>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11cb <main+0x5b>
mov    $0x50,%edi
call   1060 <alarm@plt>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
call   1080 <kill@plt>
lea    0xd8d(%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    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e35(%rip),%rbx
mov    (%rbx),%esi
call   1080 <signal@plt>
mov    0x2e17(%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    0x2de5(%rip),%r13
mov    0x2dd6(%rip),%rbp
mov    0x2df7(%rip),%r14
mov    0x2dc0(%rip),%r15
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%ecx
mov    %r12,%rax
test   $0x1,%cl
jne    11fe <main+0x7e>
mov    %r13,%rax
mov    (%rax),%edx
mov    (%r14),%ecx
or     0x0(%rbp),%ecx
xor    %edi,%edi
mov    $0x1,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    (%r15),%rax
je     129a <main+0x11a>
add    $0x1,%rbx
cmp    $0x752f,%rbx
jne    11f0 <main+0x70>
mov    $0x50,%edi
call   1070 <alarm@plt>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x2d4c(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1090 <kill@plt>
lea    0xddb(%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    0xd5f(%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_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 = %36, %0
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 30000
br i1 %12, label %13, label %39
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 = or i32 %25, 0
%27 = call i64 @mmap(i32* null, i32 1, i32 %22, i32 %26, i32 -1, i32 0)
%28 = load i64, i64* @MAP_FAILED
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %21
%31 = load i64, i64* %2
%32 = sub nsw i64 %31, 1
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %21
br label %36
36: ; preds = %35
%37 = load i64, i64* %2
%38 = add nsw i64 %37, 1
store i64 %38, i64* %2
br label %10
39: ; preds = %10
%40 = call i32 @alarm(i32 80)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 (...) @getpid()
%43 = load i32, i32* @SIGUSR1
%44 = call i32 @kill(i32 %42, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 (...) @getpid()
%47 = load i32, i32* @SIGUSR2
%48 = call i32 @kill(i32 %46, i32 %47)
%49 = 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_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, %25
%9 = phi i64 [ 0, %0 ], [ %26, %25 ]
%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 = tail call i64 @mmap(i32* null, i32 1, i32 %14, i32 %17, i32 -1, i32 0)
%19 = load i64, i64* @MAP_FAILED
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %8
%22 = add nsw i64 %9, -1
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %8
%26 = add nuw nsw i64 %9, 1
%27 = icmp eq i64 %26, 30000
br i1 %27, label %28, label %8
28: ; preds = %25
%29 = tail call i32 @alarm(i32 80)
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%31 = tail call i32 (...) @getpid()
%32 = load i32, i32* @SIGUSR1
%33 = tail call i32 @kill(i32 %31, i32 %32)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
%35 = tail call i32 (...) @getpid()
%36 = load i32, i32* @SIGUSR2
%37 = tail call i32 @kill(i32 %35, i32 %36)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}