#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 int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
double atof(char *);
int atoi(char *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, double);
int /*<<< orphan*/ sysconf(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  int log2_Ncycles;
  uint32_t phase_inc;
  double phase_in, freq_in;
  uint32_t count;
  void *cfg;
  char *name = "/dev/mem";
  const int freq = 125000000;
  int Ncycles;
  if (argc == 3) {
    log2_Ncycles = atoi(argv[1]);
    freq_in = atof(argv[2]);
  } else {
    log2_Ncycles = 1;
    freq_in = 1.;
  }
  phase_inc = (uint32_t)(2.147482 * freq_in);
  Ncycles = 1 << log2_Ncycles;
  if ((fd = open(name, O_RDWR)) < 0) {
    perror("open");
    return 1;
  }
  cfg = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED,
             fd, 0x42000000);
  *((uint32_t *)(cfg + 8)) = (0x1f & log2_Ncycles) + (phase_inc << 5);
  count = *((uint32_t *)(cfg + 0));
  printf("Counts: %5d, cycles: %5d, frequency: %6.5f Hz\n", count, Ncycles,
         (double)Ncycles / count * freq);
  munmap(cfg, sysconf(_SC_PAGESIZE));
  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)
lea    0xe7b(%rip),%rax
mov    %rax,-0x48(%rbp)
movl   $0x7735940,-0x4c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11d9 <main+0x59>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <atof@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    11ed <main+0x6d>
movl   $0x1,-0x18(%rbp)
movsd  0xe18(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe13(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1244 <main+0xc4>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f8 <main+0x178>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%esi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
mov    0x2d5c(%rip),%rax
or     (%rax),%edx
mov    0x2d7b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x42000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%ecx
and    $0x1f,%ecx
mov    -0x1c(%rbp),%eax
shl    $0x5,%eax
add    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x50(%rbp),%edx
cvtsi2sdl -0x50(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xd50(%rip),%xmm1
mulsd  %xmm1,%xmm0
lea    0xd5b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r15d
mov    $0x40,%r12d
mov    $0x1,%ebp
cmp    $0x3,%edi
jne    11c9 <main+0x49>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1030 <atof@plt>
mulsd  0xe40(%rip),%xmm0
cvttsd2si %xmm0,%r12d
shl    $0x5,%r12d
mov    %ebp,%ecx
shl    %cl,%r15d
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1287 <main+0x107>
mov    %eax,%ebx
mov    0x2ddc(%rip),%r13
mov    0x0(%r13),%edi
call   1090 <sysconf@plt>
mov    0x2df4(%rip),%rcx
mov    0x2db5(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
mov    $0x42000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
and    $0x1f,%ebp
or     %r12d,%ebp
mov    %ebp,0x8(%rax)
mov    (%rax),%esi
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
cvtsi2sd %esi,%xmm1
divsd  %xmm1,%xmm0
mulsd  0xdb5(%rip),%xmm0
lea    0xdc4(%rip),%rdi
mov    %r15d,%edx
call   1050 <printf@plt>
mov    0x0(%r13),%edi
call   1090 <sysconf@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <munmap@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    1275 <main+0xf5>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [47 x i8] c"Counts: %5d, cycles: %5d, frequency: %6.5f Hz\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %13
store i32 125000000, i32* %14
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 3
br i1 %17, label %18, label %27
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call double @atof(i8* %25)
store double %26, double* %10
br label %28
27: ; preds = %2
store i32 1, i32* %7
store double 1.000000e+00, double* %10
br label %28
28: ; preds = %27, %18
%29 = load double, double* %10
%30 = fmul double 0x40012E0B0AF5FD48, %29
%31 = fptosi double %30 to i32
store i32 %31, i32* %8
%32 = load i32, i32* %7
%33 = shl i32 1, %32
store i32 %33, i32* %15
%34 = load i8*, i8** %13
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %74
40: ; preds = %28
%41 = load i32, i32* @_SC_PAGESIZE
%42 = call i32 @sysconf(i32 %41)
%43 = load i32, i32* @PROT_READ
%44 = load i32, i32* @PROT_WRITE
%45 = or i32 %43, %44
%46 = load i32, i32* @MAP_SHARED
%47 = load i32, i32* %6
%48 = call i8* @mmap(i32* null, i32 %42, i32 %45, i32 %46, i32 %47, i32 1107296256)
store i8* %48, i8** %12
%49 = load i32, i32* %7
%50 = and i32 31, %49
%51 = load i32, i32* %8
%52 = shl i32 %51, 5
%53 = add nsw i32 %50, %52
%54 = load i8*, i8** %12
%55 = getelementptr i8, i8* %54, i64 8
%56 = bitcast i8* %55 to i32*
store i32 %53, i32* %56
%57 = load i8*, i8** %12
%58 = getelementptr i8, i8* %57, i64 0
%59 = bitcast i8* %58 to i32*
%60 = load i32, i32* %59
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = load i32, i32* %15
%63 = load i32, i32* %15
%64 = sitofp i32 %63 to double
%65 = load i32, i32* %11
%66 = sitofp i32 %65 to double
%67 = fdiv double %64, %66
%68 = fmul double %67, 1.250000e+08
%69 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %61, i32 %62, double %68)
%70 = load i8*, i8** %12
%71 = load i32, i32* @_SC_PAGESIZE
%72 = call i32 @sysconf(i32 %71)
%73 = call i32 @munmap(i8* %70, i32 %72)
store i32 0, i32* %3
br label %74
74: ; preds = %40, %38
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [47 x i8] c"Counts: %5d, cycles: %5d, frequency: %6.5f Hz\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %4, label %14
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call double @atof(i8* %9)
%11 = fmul double %10, 0x40012E0B0AF5FD48
%12 = fptosi double %11 to i32
%13 = shl i32 %12, 5
br label %14
14: ; preds = %2, %4
%15 = phi i32 [ %13, %4 ], [ 64, %2 ]
%16 = phi i32 [ %7, %4 ], [ 1, %2 ]
%17 = shl nuw i32 1, %16
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
23: ; preds = %14
%24 = load i32, i32* @_SC_PAGESIZE
%25 = tail call i32 @sysconf(i32 %24)
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %27, %26
%29 = load i32, i32* @MAP_SHARED
%30 = tail call i8* @mmap(i32* null, i32 %25, i32 %28, i32 %29, i32 %19, i32 1107296256)
%31 = and i32 %16, 31
%32 = or i32 %31, %15
%33 = getelementptr i8, i8* %30, i64 8
%34 = bitcast i8* %33 to i32*
store i32 %32, i32* %34
%35 = bitcast i8* %30 to i32*
%36 = load i32, i32* %35
%37 = sitofp i32 %17 to double
%38 = sitofp i32 %36 to double
%39 = fdiv double %37, %38
%40 = fmul double %39, 1.250000e+08
%41 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %17, double %40)
%42 = load i32, i32* @_SC_PAGESIZE
%43 = tail call i32 @sysconf(i32 %42)
%44 = tail call i32 @munmap(i8* %30, i32 %43)
br label %45
45: ; preds = %23, %21
%46 = phi i32 [ 1, %21 ], [ 0, %23 ]
ret i32 %46
}