#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*/ printf(char *);
int /*<<< orphan*/ setup_segfault_handler();
int /*<<< orphan*/ show_fault_delay(uintptr_t, char *);
int main(void) {
  setup_segfault_handler();
  printf("Number of cycles to catch a segmentation fault (R, W):\n");
  if (!show_fault_delay(0, "Null pointer")) {
    return 1;
  }
  show_fault_delay(0x1000, "First page");
  if (!show_fault_delay((uintptr_t)main, "Address of main()")) {
    return 1;
  }
  show_fault_delay(0x8000000000000000, "Canonical hole");
  show_fault_delay(0xff00000000000000, "Canonical hole");
  show_fault_delay(0xffff880000000000, "Linux low kernel mapping");
  show_fault_delay(0xffffffff80000000, "Linux high kernel mapping");
  show_fault_delay((uintptr_t)-0x1000, "Last page");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <setup_segfault_handler@plt>
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xedb(%rip),%rsi
call   1050 <show_fault_delay@plt>
cmp    $0x0,%eax
jne    1177 <main+0x47>
movl   $0x1,-0x4(%rbp)
jmp    121f <main+0xef>
mov    $0x1000,%edi
lea    0xec2(%rip),%rsi
call   1050 <show_fault_delay@plt>
mov    0x2e59(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1050 <show_fault_delay@plt>
cmp    $0x0,%eax
jne    11b0 <main+0x80>
movl   $0x1,-0x4(%rbp)
jmp    121f <main+0xef>
movabs $0x8000000000000000,%rdi
lea    0xea1(%rip),%rsi
call   1050 <show_fault_delay@plt>
movabs $0xff00000000000000,%rdi
lea    0xe8b(%rip),%rsi
call   1050 <show_fault_delay@plt>
movabs $0xffff880000000000,%rdi
88 ff ff
lea    0xe84(%rip),%rsi
call   1050 <show_fault_delay@plt>
mov    $0xffffffff80000000,%rdi
lea    0xe8a(%rip),%rsi
call   1050 <show_fault_delay@plt>
mov    $0xfffffffffffff000,%rdi
lea    0xe91(%rip),%rsi
call   1050 <show_fault_delay@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1040 <setup_segfault_handler@plt>
lea    0xf37(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb5(%rip),%rsi
xor    %edi,%edi
call   1050 <show_fault_delay@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     11f0 <main+0xc0>
lea    0xea7(%rip),%rsi
mov    $0x1000,%edi
call   1050 <show_fault_delay@plt>
mov    0x2e71(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <show_fault_delay@plt>
test   %eax,%eax
je     11f0 <main+0xc0>
lea    0xe9c(%rip),%rbx
movabs $0x8000000000000000,%rdi
mov    %rbx,%rsi
call   1050 <show_fault_delay@plt>
movabs $0xff00000000000000,%rdi
mov    %rbx,%rsi
call   1050 <show_fault_delay@plt>
lea    0xe80(%rip),%rsi
movabs $0xffff880000000000,%rdi
88 ff ff
call   1050 <show_fault_delay@plt>
lea    0xe83(%rip),%rsi
mov    $0xffffffff80000000,%rdi
call   1050 <show_fault_delay@plt>
lea    0xe8a(%rip),%rsi
mov    $0xfffffffffffff000,%rdi
call   1050 <show_fault_delay@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [56 x i8] c"Number of cycles to catch a segmentation fault (R, W):\0A\00"
@.str.1 = [13 x i8] c"Null pointer\00"
@.str.2 = [11 x i8] c"First page\00"
@.str.3 = [18 x i8] c"Address of main()\00"
@.str.4 = [15 x i8] c"Canonical hole\00"
@.str.5 = [25 x i8] c"Linux low kernel mapping\00"
@.str.6 = [26 x i8] c"Linux high kernel mapping\00"
@.str.7 = [10 x i8] c"Last page\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 (...) @setup_segfault_handler()
%3 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @show_fault_delay(i64 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp ne i32 %4, 0
br i1 %5, label %7, label %6
6: ; preds = %0
store i32 1, i32* %1
br label %18
7: ; preds = %0
%8 = call i32 @show_fault_delay(i64 4096, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @show_fault_delay(i64 ptrtoint (i32 ()* @main to i64), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %7
store i32 1, i32* %1
br label %18
12: ; preds = %7
%13 = call i32 @show_fault_delay(i64 -9223372036854775808, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 @show_fault_delay(i64 -72057594037927936, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 @show_fault_delay(i64 -131941395333120, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%16 = call i32 @show_fault_delay(i64 -2147483648, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%17 = call i32 @show_fault_delay(i64 -4096, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %18
18: ; preds = %12, %11, %6
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"Null pointer\00"
@.str.2 = [11 x i8] c"First page\00"
@.str.3 = [18 x i8] c"Address of main()\00"
@.str.4 = [15 x i8] c"Canonical hole\00"
@.str.5 = [25 x i8] c"Linux low kernel mapping\00"
@.str.6 = [26 x i8] c"Linux high kernel mapping\00"
@.str.7 = [10 x i8] c"Last page\00"
@str = [55 x i8] c"Number of cycles to catch a segmentation fault (R, W):\00"
define i32 @main() {
%1 = tail call i32 (...) @setup_segfault_handler()
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @show_fault_delay(i64 0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %0
%6 = tail call i32 @show_fault_delay(i64 4096, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @show_fault_delay(i64 ptrtoint (i32 ()* @main to i64), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = tail call i32 @show_fault_delay(i64 -9223372036854775808, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%11 = tail call i32 @show_fault_delay(i64 -72057594037927936, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%12 = tail call i32 @show_fault_delay(i64 -131941395333120, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%13 = tail call i32 @show_fault_delay(i64 -2147483648, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%14 = tail call i32 @show_fault_delay(i64 -4096, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %15
15: ; preds = %5, %0, %9
%16 = phi i32 [ 0, %9 ], [ 1, %0 ], [ 1, %5 ]
ret i32 %16
}