#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*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
long long *malloc(int);
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long long);
unsigned int sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  void *ptr;
  unsigned addr, page_addr, page_offset;
  unsigned page_size = sysconf(_SC_PAGESIZE);
  volatile unsigned *tsc_l, *tsc_h;
  long long *tick;
  unsigned reg;
  unsigned long long tmp;
  int i;
  fd = open("/dev/mem", O_RDONLY);
  if (fd < 1) {
    perror(argv[0]);
    exit(-1);
  }
  addr = 0x80600100;
  page_addr = (addr & ~(page_size - 1));
  page_offset = addr - page_addr;
  ptr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd,
             (addr & ~(page_size - 1)));
  if ((int)ptr == -1) {
    perror(argv[0]);
    exit(-1);
  }
  tsc_l = (unsigned *)(ptr + page_offset);
  tsc_h = (unsigned *)(ptr + page_offset + 4);
  tick = malloc(1000000 * sizeof(*tick));
  for (i = 0; i < 1000000; i++) {
    reg = *tsc_l;
    tmp = *tsc_h;
    tmp = (tmp << 32) | reg;
    tick[i] = tmp;
  }
  for (i = 0; i < 1000000; i++) {
    printf("0x%016llx\n", tick[i]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x30(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11cf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movl   $0x80600100,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2de5(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0xffffffff,%eax
jne    1249 <main+0xd9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    %rcx,%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
mov    $0x7a1200,%edi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0xf4240,-0x5c(%rbp)
jge    12d0 <main+0x160>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,%eax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
shl    $0x20,%rax
mov    -0x4c(%rbp),%ecx
or     %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdx
mov    -0x48(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    127e <main+0x10e>
movl   $0x0,-0x5c(%rbp)
cmpl   $0xf4240,-0x5c(%rbp)
jge    130a <main+0x19a>
mov    -0x48(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd12(%rip),%rdi
call   1040 <printf@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    12d7 <main+0x167>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    1260 <main+0xf0>
mov    %ebx,%ebp
neg    %ebp
and    $0x80600100,%ebp
mov    0x2e43(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %eax,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffff,%ebx
je     1260 <main+0xf0>
mov    %ebp,%eax
mov    $0x80600100,%ebp
sub    %rax,%rbp
mov    $0x7a1200,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0x1,%eax
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,1),%ecx
mov    0x4(%rbx,%rbp,1),%edx
shl    $0x20,%rdx
or     %rcx,%rdx
mov    %rdx,-0x8(%r14,%rax,8)
mov    (%rbx,%rbp,1),%ecx
mov    0x4(%rbx,%rbp,1),%edx
shl    $0x20,%rdx
or     %rcx,%rdx
mov    %rdx,(%r14,%rax,8)
add    $0x2,%rax
cmp    $0xf4241,%rax
jne    1200 <main+0x90>
xor    %ebp,%ebp
lea    0xdcf(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbp,8),%rsi
mov    %rbx,%rdi
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    $0xf4240,%rbp
jne    1240 <main+0xd0>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%r14),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [11 x i8] c"0x%016llx\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i64*
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* @_SC_PAGESIZE
%19 = call i32 @sysconf(i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %2
store i32 -2141191936, i32* %8
%31 = load i32, i32* %8
%32 = load i32, i32* %11
%33 = sub i32 %32, 1
%34 = xor i32 %33, -1
%35 = and i32 %31, %34
store i32 %35, i32* %9
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = sub i32 %36, %37
store i32 %38, i32* %10
%39 = load i32, i32* %11
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %6
%43 = load i32, i32* %8
%44 = load i32, i32* %11
%45 = sub i32 %44, 1
%46 = xor i32 %45, -1
%47 = and i32 %43, %46
%48 = call i8* @mmap(i32* null, i32 %39, i32 %40, i32 %41, i32 %42, i32 %47)
store i8* %48, i8** %7
%49 = load i8*, i8** %7
%50 = ptrtoint i8* %49 to i32
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %58
52: ; preds = %30
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 0
%55 = load i8*, i8** %54
%56 = call i32 @perror(i8* %55)
%57 = call i32 @exit(i32 -1)
unreachable
58: ; preds = %30
%59 = load i8*, i8** %7
%60 = load i32, i32* %10
%61 = zext i32 %60 to i64
%62 = getelementptr i8, i8* %59, i64 %61
%63 = bitcast i8* %62 to i32*
store i32* %63, i32** %12
%64 = load i8*, i8** %7
%65 = load i32, i32* %10
%66 = zext i32 %65 to i64
%67 = getelementptr i8, i8* %64, i64 %66
%68 = getelementptr i8, i8* %67, i64 4
%69 = bitcast i8* %68 to i32*
store i32* %69, i32** %13
%70 = call i64* @malloc(i32 8000000)
store i64* %70, i64** %14
store i32 0, i32* %17
br label %71
71: ; preds = %90, %58
%72 = load i32, i32* %17
%73 = icmp slt i32 %72, 1000000
br i1 %73, label %74, label %93
74: ; preds = %71
%75 = load i32*, i32** %12
%76 = load volatile i32, i32* %75
store i32 %76, i32* %15
%77 = load i32*, i32** %13
%78 = load volatile i32, i32* %77
%79 = zext i32 %78 to i64
store i64 %79, i64* %16
%80 = load i64, i64* %16
%81 = shl i64 %80, 32
%82 = load i32, i32* %15
%83 = zext i32 %82 to i64
%84 = or i64 %81, %83
store i64 %84, i64* %16
%85 = load i64, i64* %16
%86 = load i64*, i64** %14
%87 = load i32, i32* %17
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i64, i64* %86, i64 %88
store i64 %85, i64* %89
br label %90
90: ; preds = %74
%91 = load i32, i32* %17
%92 = add nsw i32 %91, 1
store i32 %92, i32* %17
br label %71
93: ; preds = %71
store i32 0, i32* %17
br label %94
94: ; preds = %104, %93
%95 = load i32, i32* %17
%96 = icmp slt i32 %95, 1000000
br i1 %96, label %97, label %107
97: ; preds = %94
%98 = load i64*, i64** %14
%99 = load i32, i32* %17
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i64, i64* %98, i64 %100
%102 = load i64, i64* %101
%103 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %102)
br label %104
104: ; preds = %97
%105 = load i32, i32* %17
%106 = add nsw i32 %105, 1
store i32 %106, i32* %17
br label %94
107: ; preds = %94
ret i32 0
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [11 x i8] c"0x%016llx\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @perror(i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = sub i32 0, %4
%14 = and i32 %13, -2141191936
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32* null, i32 %4, i32 %15, i32 %16, i32 %6, i32 %14)
%18 = ptrtoint i8* %17 to i64
%19 = trunc i64 %18 to i32
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i8*, i8** %1
%23 = tail call i32 @perror(i8* %22)
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %12
%26 = sub nuw nsw i32 -2141191936, %14
%27 = zext i32 %26 to i64
%28 = getelementptr i8, i8* %17, i64 %27
%29 = bitcast i8* %28 to i32*
%30 = getelementptr i8, i8* %28, i64 4
%31 = bitcast i8* %30 to i32*
%32 = tail call i64* @malloc(i32 8000000)
br label %33
33: ; preds = %33, %25
%34 = phi i64 [ 0, %25 ], [ %50, %33 ]
%35 = load volatile i32, i32* %29
%36 = load volatile i32, i32* %31
%37 = zext i32 %36 to i64
%38 = shl nuw i64 %37, 32
%39 = zext i32 %35 to i64
%40 = or i64 %38, %39
%41 = getelementptr inbounds i64, i64* %32, i64 %34
store i64 %40, i64* %41
%42 = or i64 %34, 1
%43 = load volatile i32, i32* %29
%44 = load volatile i32, i32* %31
%45 = zext i32 %44 to i64
%46 = shl nuw i64 %45, 32
%47 = zext i32 %43 to i64
%48 = or i64 %46, %47
%49 = getelementptr inbounds i64, i64* %32, i64 %42
store i64 %48, i64* %49
%50 = add nuw nsw i64 %34, 2
%51 = icmp eq i64 %50, 1000000
br i1 %51, label %52, label %33
52: ; preds = %33, %52
%53 = phi i64 [ %57, %52 ], [ 0, %33 ]
%54 = getelementptr inbounds i64, i64* %32, i64 %53
%55 = load i64, i64* %54
%56 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 %55)
%57 = add nuw nsw i64 %53, 1
%58 = icmp eq i64 %57, 1000000
br i1 %58, label %59, label %52
59: ; preds = %52
ret i32 0
}