#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;
float atof(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, double);
int /*<<< orphan*/ sleep(float);
int /*<<< orphan*/ sysconf(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  float wait_time;
  uint32_t count;
  void *cfg;
  char *name = "/dev/mem";
  const int freq = 124998750;
  if (argc == 2)
    wait_time = atof(argv[1]);
  else
    wait_time = 1.0;
  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 + 0)) = 1 << 1;
  *((uint32_t *)(cfg + 0)) = 1 << 0;
  sleep(wait_time);
  *((uint32_t *)(cfg + 0)) = 0 << 0;
  count = *((uint32_t *)(cfg + 8));
  printf("Clock count: %5d, calculated time: %5f s\n", count,
         (double)count / freq);
  munmap(cfg, sysconf(_SC_PAGESIZE));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x773545e,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11c9 <main+0x49>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0x18(%rbp)
jmp    11d6 <main+0x56>
movss  0xe2f(%rip),%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120c <main+0x8c>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x148>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2d94(%rip),%rax
or     (%rax),%edx
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x42000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
movss  -0x18(%rbp),%xmm0
call   10a0 <sleep@plt>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  0xd78(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xd83(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1198 <main+0x18>
mov    0x8(%rsi),%rdi
call   1030 <atof@plt>
jmp    11a0 <main+0x20>
movss  0xe60(%rip),%xmm0
movss  %xmm0,0xc(%rsp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1259 <main+0xd9>
mov    %eax,%ebp
mov    0x2e04(%rip),%r14
mov    (%r14),%edi
call   1090 <sysconf@plt>
mov    0x2e1d(%rip),%rcx
mov    0x2dde(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebp,%r8d
mov    $0x42000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
movl   $0x1,(%rax)
movss  0xc(%rsp),%xmm0
call   10a0 <sleep@plt>
movl   $0x0,0x0(%rbp)
mov    0x8(%rbp),%esi
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
divsd  0xdd8(%rip),%xmm0
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
mov    (%r14),%edi
call   1090 <sysconf@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1060 <munmap@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    124e <main+0xce>

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 = [42 x i8] c"Clock count: %5d, calculated time: %5f s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca float
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = 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** %10
store i32 124998750, i32* %11
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call float @atof(i8* %17)
store float %18, float* %7
br label %20
19: ; preds = %2
store float 1.000000e+00, float* %7
br label %20
20: ; preds = %19, %14
%21 = load i8*, i8** %10
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = 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 %60
27: ; preds = %20
%28 = load i32, i32* @_SC_PAGESIZE
%29 = call i32 @sysconf(i32 %28)
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %6
%35 = call i8* @mmap(i32* null, i32 %29, i32 %32, i32 %33, i32 %34, i32 1107296256)
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = getelementptr i8, i8* %36, i64 0
%38 = bitcast i8* %37 to i32*
store i32 2, i32* %38
%39 = load i8*, i8** %9
%40 = getelementptr i8, i8* %39, i64 0
%41 = bitcast i8* %40 to i32*
store i32 1, i32* %41
%42 = load float, float* %7
%43 = call i32 @sleep(float %42)
%44 = load i8*, i8** %9
%45 = getelementptr i8, i8* %44, i64 0
%46 = bitcast i8* %45 to i32*
store i32 0, i32* %46
%47 = load i8*, i8** %9
%48 = getelementptr i8, i8* %47, i64 8
%49 = bitcast i8* %48 to i32*
%50 = load i32, i32* %49
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = load i32, i32* %8
%53 = sitofp i32 %52 to double
%54 = fdiv double %53, 0x419DCD5178000000
%55 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %51, double %54)
%56 = load i8*, i8** %9
%57 = load i32, i32* @_SC_PAGESIZE
%58 = call i32 @sysconf(i32 %57)
%59 = call i32 @munmap(i8* %56, i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %27, %25
%61 = load i32, i32* %3
ret i32 %61
}

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 = [42 x i8] c"Clock count: %5d, calculated time: %5f s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call float @atof(i8* %6)
br label %8
8: ; preds = %2, %4
%9 = phi float [ %7, %4 ], [ 1.000000e+00, %2 ]
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %34
15: ; preds = %8
%16 = load i32, i32* @_SC_PAGESIZE
%17 = tail call i32 @sysconf(i32 %16)
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i8* @mmap(i32* null, i32 %17, i32 %20, i32 %21, i32 %11, i32 1107296256)
%23 = bitcast i8* %22 to i32*
store i32 1, i32* %23
%24 = tail call i32 @sleep(float %9)
store i32 0, i32* %23
%25 = getelementptr i8, i8* %22, i64 8
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %26
%28 = sitofp i32 %27 to double
%29 = fdiv double %28, 0x419DCD5178000000
%30 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %27, double %29)
%31 = load i32, i32* @_SC_PAGESIZE
%32 = tail call i32 @sysconf(i32 %31)
%33 = tail call i32 @munmap(i8* %22, i32 %32)
br label %34
34: ; preds = %15, %13
%35 = phi i32 [ 1, %13 ], [ 0, %15 ]
ret i32 %35
}