#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 *, double);
int /*<<< orphan*/ sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
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);
  usleep(Ncycles / 10000000 * 1000000 * 2);
  count = *((uint32_t *)(cfg + 0));
  printf("%6.5f\n", (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    0xe6b(%rip),%rax
mov    %rax,-0x48(%rbp)
movl   $0x7735940,-0x4c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11e9 <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    11fd <main+0x6d>
movl   $0x1,-0x18(%rbp)
movsd  0xe08(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe03(%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    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1254 <main+0xc4>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131b <main+0x18b>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    0x2d4c(%rip),%rax
or     (%rax),%edx
mov    0x2d6b(%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    -0x50(%rbp),%eax
mov    $0x989680,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%eax,%edi
shl    $0x1,%edi
call   10b0 <usleep@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
cvtsi2sdl -0x50(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xd2d(%rip),%xmm1
mulsd  %xmm1,%xmm0
lea    0xd38(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2cce(%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,%r12d
mov    $0x40,%r13d
mov    $0x1,%ebx
cmp    $0x3,%edi
jne    11d9 <main+0x49>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1030 <atof@plt>
mulsd  0xe30(%rip),%xmm0
cvttsd2si %xmm0,%r13d
shl    $0x5,%r13d
mov    %ebx,%ecx
shl    %cl,%r12d
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe22(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     12b7 <main+0x127>
mov    %eax,%ebp
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    0x2de6(%rip),%rcx
mov    0x2da7(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %eax,%esi
mov    %ebp,%r8d
mov    $0x42000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%r15
and    $0x1f,%ebx
or     %r13d,%ebx
mov    %ebx,0x8(%rax)
movslq %r12d,%rbx
imul   $0x6b5fca6b,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x36,%rax
add    %ecx,%eax
imul   $0x1e8480,%eax,%edi
call   10b0 <usleep@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
cvtsi2sdl (%r15),%xmm1
divsd  %xmm1,%xmm0
mulsd  0xd87(%rip),%xmm0
lea    0xd96(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %r15,%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    0xd5b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    12a5 <main+0x115>

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 = [7 x i8] c"%6.5f\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 %77
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 i32, i32* %15
%58 = sdiv i32 %57, 10000000
%59 = mul nsw i32 %58, 1000000
%60 = mul nsw i32 %59, 2
%61 = call i32 @usleep(i32 %60)
%62 = load i8*, i8** %12
%63 = getelementptr i8, i8* %62, i64 0
%64 = bitcast i8* %63 to i32*
%65 = load i32, i32* %64
store i32 %65, i32* %11
%66 = load i32, i32* %15
%67 = sitofp i32 %66 to double
%68 = load i32, i32* %11
%69 = sitofp i32 %68 to double
%70 = fdiv double %67, %69
%71 = fmul double %70, 1.250000e+08
%72 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %71)
%73 = load i8*, i8** %12
%74 = load i32, i32* @_SC_PAGESIZE
%75 = call i32 @sysconf(i32 %74)
%76 = call i32 @munmap(i8* %73, i32 %75)
store i32 0, i32* %3
br label %77
77: ; preds = %40, %38
%78 = load i32, i32* %3
ret i32 %78
}

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 = [7 x i8] c"%6.5f\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 %48
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 = sdiv i32 %17, 10000000
%36 = mul nsw i32 %35, 2000000
%37 = tail call i32 @usleep(i32 %36)
%38 = bitcast i8* %30 to i32*
%39 = load i32, i32* %38
%40 = sitofp i32 %17 to double
%41 = sitofp i32 %39 to double
%42 = fdiv double %40, %41
%43 = fmul double %42, 1.250000e+08
%44 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), double %43)
%45 = load i32, i32* @_SC_PAGESIZE
%46 = tail call i32 @sysconf(i32 %45)
%47 = tail call i32 @munmap(i8* %30, i32 %46)
br label %48
48: ; preds = %23, %21
%49 = phi i32 [ 1, %21 ], [ 0, %23 ]
ret i32 %49
}