#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dommap(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
int getpagesize();
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
void benchmark_page_fault() {
  int fd, pagesize;
  struct stat stat;
  if ((fd = open("file.txt", O_RDWR, 0)) < 0)
    fprintf(stderr, "open file error\n");
  fstat(fd, &stat);
  printf("file size = %lld\n", stat.st_size);
  pagesize = getpagesize();
  printf("page size = %d\n", pagesize);
  dommap(fd, stat.st_size);
  close(fd);
}


x86 assembly (clang, -O0):
<benchmark_page_fault>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11b0 <benchmark_page_fault+0x40>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x10(%rbp),%esi
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <dommap@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark_page_fault>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11a5 <benchmark_page_fault+0x35>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
mov    (%rsp),%ebp
lea    0xe61(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <getpagesize@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <dommap@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"file.txt\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"open file error\0A\00"
@.str.2 = [18 x i8] c"file size = %lld\0A\00"
@.str.3 = [16 x i8] c"page size = %d\0A\00"
define void @benchmark_page_fault() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4, i32 0)
store i32 %5, i32* %1
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %7, %0
%11 = load i32, i32* %1
%12 = call i32 @fstat(i32 %11, %struct.stat* %3)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 (...) @getpagesize()
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %1
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @dommap(i32 %19, i32 %21)
%23 = load i32, i32* %1
%24 = call i32 @close(i32 %23)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"file.txt\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"open file error\0A\00"
@.str.2 = [18 x i8] c"file size = %lld\0A\00"
@.str.3 = [16 x i8] c"page size = %d\0A\00"
define void @benchmark_page_fault() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %6, %0
%10 = call i32 @fstat(i32 %4, %struct.stat* %1)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @getpagesize()
%15 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @dommap(i32 %4, i32 %12)
%17 = tail call i32 @close(i32 %4)
ret void
}