#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 */
typedef unsigned long long uint64_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
char *malloc(int);
int /*<<< orphan*/ printf(char *, double);
unsigned long long rdtscp(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strtoull(char *, int /*<<< orphan*/ *, int);
void main(int argc, char *argv[]) {
  int pgsz;
  char *rgn;
  int i, n;
  unsigned long long elapsed;
  uint64_t start, finished;
  uint32_t aux;
  if (argc != 2) {
    fprintf(stderr, "usage: mfault npages\n");
    exit(1);
  }
  n = strtoull(argv[1], NULL, 10);
  pgsz = getpagesize();
  rgn = malloc(pgsz * n);
  if (rgn == NULL) {
    fprintf(stderr, "couldn't allocate region\n");
    exit(1);
  }
  if (fork() == 0)
    exit(0);
  start = rdtscp(&aux);
  rgn[0] = 1;
  for (i = n; i; i--) {
    rgn[i * pgsz] = rgn[(i - 1) * pgsz];
  }
  finished = rdtscp(&aux);
  elapsed = finished - start;
  printf("cycles/fault %F\n", (double)elapsed / (double)n);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11b8 <main+0x38>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoull@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
imul   -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1215 <main+0x95>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    122d <main+0xad>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x44(%rbp),%rdi
call   1040 <rdtscp@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1284 <main+0x104>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
imul   -0x14(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x14(%rbp),%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1247 <main+0xc7>
lea    -0x44(%rbp),%rdi
call   1040 <rdtscp@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
movaps 0xd59(%rip),%xmm1
movq   -0x30(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xd58(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsi2sdl -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xd79(%rip),%rdi
call   1030 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12fe <main+0x17e>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoull@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %eax,%ebx
mov    %eax,%edi
imul   %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1310 <main+0x190>
mov    %rax,%rbp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     132f <main+0x1af>
lea    0x4(%rsp),%rdi
call   1040 <rdtscp@plt>
mov    %rax,%r15
movb   $0x1,0x0(%rbp)
test   %r14d,%r14d
je     12ad <main+0x12d>
movslq %r14d,%rax
movslq %ebx,%r8
lea    -0x1(%r14),%r9d
test   $0x3,%r14b
je     1236 <main+0xb6>
mov    %rax,%r10
imul   %r8,%r10
mov    %r8,%r11
neg    %r11
lea    -0x1(%rax),%rsi
imul   %r8,%rsi
mov    %r14d,%edi
and    $0x3,%edi
xor    %edx,%edx
mov    %rbp,%rbx
nopl   (%rax)
movzbl (%rbx,%rsi,1),%ecx
mov    %cl,(%rbx,%r10,1)
add    %r11,%rbx
add    $0x1,%rdx
cmp    %edx,%edi
jne    1220 <main+0xa0>
sub    %rdx,%rax
cmp    $0x3,%r9d
jb     12ad <main+0x12d>
lea    -0x4(%rax),%r9
imul   %r8,%r9
lea    0x0(,%r8,4),%r10
neg    %r10
lea    -0x3(%rax),%rdi
imul   %r8,%rdi
lea    -0x2(%rax),%rbx
imul   %r8,%rbx
mov    %rax,%rsi
imul   %r8,%rsi
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
imul   %r8,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%rbp,%rdx,1),%ecx
mov    %cl,0x0(%rbp,%rsi,1)
movzbl 0x0(%rbp,%rbx,1),%ecx
mov    %cl,0x0(%rbp,%rdx,1)
movzbl 0x0(%rbp,%rdi,1),%ecx
mov    %cl,0x0(%rbp,%rbx,1)
movzbl 0x0(%rbp,%r9,1),%ecx
mov    %cl,0x0(%rbp,%rdi,1)
add    %r10,%rbp
add    $0xfffffffc,%eax
jne    1280 <main+0x100>
lea    0x4(%rsp),%rdi
call   1040 <rdtscp@plt>
sub    %r15,%rax
movq   %rax,%xmm1
punpckldq 0xd39(%rip),%xmm1
subpd  0xd41(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
divsd  %xmm1,%xmm0
lea    0xd62(%rip),%rdi
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
lea    0xd12(%rip),%rsi
jmp    1320 <main+0x1a0>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"usage: mfault npages\0A\00"
@.str.1 = [26 x i8] c"couldn't allocate region\0A\00"
@.str.2 = [17 x i8] c"cycles/fault %F\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strtoull(i8* %22, i32* null, i32 10)
store i32 %23, i32* %8
%24 = call i32 (...) @getpagesize()
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = load i32, i32* %8
%27 = mul nsw i32 %25, %26
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %19
%36 = call i64 (...) @fork()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %35
%41 = call i64 @rdtscp(i32* %12)
store i64 %41, i64* %10
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 0
store i8 1, i8* %43
%44 = load i32, i32* %8
store i32 %44, i32* %7
br label %45
45: ; preds = %63, %40
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %66
48: ; preds = %45
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, 1
%52 = load i32, i32* %5
%53 = mul nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %49, i64 %54
%56 = load i8, i8* %55
%57 = load i8*, i8** %6
%58 = load i32, i32* %7
%59 = load i32, i32* %5
%60 = mul nsw i32 %58, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %57, i64 %61
store i8 %56, i8* %62
br label %63
63: ; preds = %48
%64 = load i32, i32* %7
%65 = add nsw i32 %64, -1
store i32 %65, i32* %7
br label %45
66: ; preds = %45
%67 = call i64 @rdtscp(i32* %12)
store i64 %67, i64* %11
%68 = load i64, i64* %11
%69 = load i64, i64* %10
%70 = sub i64 %68, %69
store i64 %70, i64* %9
%71 = load i64, i64* %9
%72 = uitofp i64 %71 to double
%73 = load i32, i32* %8
%74 = sitofp i32 %73 to double
%75 = fdiv double %72, %74
%76 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %75)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"usage: mfault npages\0A\00"
@.str.1 = [26 x i8] c"couldn't allocate region\0A\00"
@.str.2 = [17 x i8] c"cycles/fault %F\0A\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @strtoull(i8* nocapture %12, i32* null, i32 10)
%14 = tail call i32 (...) @getpagesize()
%15 = mul nsw i32 %14, %13
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %10
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %22
%28 = call i64 @rdtscp(i32* %3)
store i8 1, i8* %16
%29 = icmp eq i32 %13, 0
br i1 %29, label %78, label %30
30: ; preds = %27
%31 = sext i32 %13 to i64
%32 = sext i32 %14 to i64
%33 = add i32 %13, -1
%34 = and i32 %13, 3
%35 = icmp eq i32 %34, 0
br i1 %35, label %47, label %36
36: ; preds = %30, %36
%37 = phi i64 [ %39, %36 ], [ %31, %30 ]
%38 = phi i32 [ %45, %36 ], [ 0, %30 ]
%39 = add nsw i64 %37, -1
%40 = mul nsw i64 %39, %32
%41 = getelementptr inbounds i8, i8* %16, i64 %40
%42 = load i8, i8* %41
%43 = mul nsw i64 %37, %32
%44 = getelementptr inbounds i8, i8* %16, i64 %43
store i8 %42, i8* %44
%45 = add i32 %38, 1
%46 = icmp eq i32 %45, %34
br i1 %46, label %47, label %36
47: ; preds = %36, %30
%48 = phi i64 [ %31, %30 ], [ %39, %36 ]
%49 = icmp ult i32 %33, 3
br i1 %49, label %78, label %50
50: ; preds = %47, %50
%51 = phi i64 [ %70, %50 ], [ %48, %47 ]
%52 = add nsw i64 %51, -1
%53 = mul nsw i64 %52, %32
%54 = getelementptr inbounds i8, i8* %16, i64 %53
%55 = load i8, i8* %54
%56 = mul nsw i64 %51, %32
%57 = getelementptr inbounds i8, i8* %16, i64 %56
store i8 %55, i8* %57
%58 = add nsw i64 %51, -2
%59 = mul nsw i64 %58, %32
%60 = getelementptr inbounds i8, i8* %16, i64 %59
%61 = load i8, i8* %60
%62 = mul nsw i64 %52, %32
%63 = getelementptr inbounds i8, i8* %16, i64 %62
store i8 %61, i8* %63
%64 = add nsw i64 %51, -3
%65 = mul nsw i64 %64, %32
%66 = getelementptr inbounds i8, i8* %16, i64 %65
%67 = load i8, i8* %66
%68 = mul nsw i64 %58, %32
%69 = getelementptr inbounds i8, i8* %16, i64 %68
store i8 %67, i8* %69
%70 = add nsw i64 %51, -4
%71 = mul nsw i64 %70, %32
%72 = getelementptr inbounds i8, i8* %16, i64 %71
%73 = load i8, i8* %72
%74 = mul nsw i64 %64, %32
%75 = getelementptr inbounds i8, i8* %16, i64 %74
store i8 %73, i8* %75
%76 = trunc i64 %70 to i32
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %50
78: ; preds = %47, %50, %27
%79 = call i64 @rdtscp(i32* %3)
%80 = sub i64 %79, %28
%81 = uitofp i64 %80 to double
%82 = sitofp i32 %13 to double
%83 = fdiv double %81, %82
%84 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), double %83)
ret void
}