#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 timeb {
  int time;
  int millitm;
};
typedef int /*<<< orphan*/ old_tsc;
typedef int /*<<< orphan*/ old_mperf;
typedef int /*<<< orphan*/ old_aperf;
typedef int /*<<< orphan*/ new_tsc;
typedef int /*<<< orphan*/ new_mperf;
typedef int /*<<< orphan*/ new_aperf;
typedef int /*<<< orphan*/ cpu_set_t;

/* Variables and functions */
int /*<<< orphan*/ CPU_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ CPU_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ O_RDONLY;
scalar_t__ errno;
int /*<<< orphan*/ ftime(struct timeb *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pread(int, long long *, int, int);
int /*<<< orphan*/ printf(char *, long long);
scalar_t__ sched_setaffinity(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sqrt(int);
int strtol(char *, char **, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int i, cpu, fd;
  char msr_file_name[64];
  long long tsc, old_tsc, new_tsc;
  long long aperf, old_aperf, new_aperf;
  long long mperf, old_mperf, new_mperf;
  struct timeb before, after;
  long long int start, finish, total;
  cpu_set_t cpuset;
  if (argc != 2) {
    usage(argv[0]);
    return 1;
  }
  errno = 0;
  cpu = strtol(argv[1], (char **)NULL, 10);
  if (errno) {
    usage(argv[0]);
    return 1;
  }
  sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
  fd = open(msr_file_name, O_RDONLY);
  if (fd == -1) {
    perror("Failed to open");
    return 1;
  }
  CPU_ZERO(&cpuset);
  CPU_SET(cpu, &cpuset);
  if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset)) {
    perror("Failed to set cpu affinity");
    return 1;
  }
  ftime(&before);
  pread(fd, &old_tsc, sizeof(old_tsc), 0x10);
  pread(fd, &old_aperf, sizeof(old_mperf), 0xe7);
  pread(fd, &old_mperf, sizeof(old_aperf), 0xe8);
  for (i = 0; i < 0x8fffffff; i++) {
    sqrt(i);
  }
  ftime(&after);
  pread(fd, &new_tsc, sizeof(new_tsc), 0x10);
  pread(fd, &new_aperf, sizeof(new_mperf), 0xe7);
  pread(fd, &new_mperf, sizeof(new_aperf), 0xe8);
  tsc = new_tsc - old_tsc;
  aperf = new_aperf - old_aperf;
  mperf = new_mperf - old_mperf;
  start = before.time * 1000 + before.millitm;
  finish = after.time * 1000 + after.millitm;
  total = finish - start;
  printf("runTime: %4.2f\n", 1.0 * total / 1000);
  printf("freq: %7.0f\n", tsc / (1.0 * aperf / (1.0 * mperf)) / total);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fb <main+0x3b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    14bd <main+0x2fd>
mov    0x2dd6(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    0x2daf(%rip),%rax
cmpq   $0x0,(%rax)
je     124b <main+0x8b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    14bd <main+0x2fd>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xdb7(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1295 <main+0xd5>
lea    0xd9c(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14bd <main+0x2fd>
lea    -0xd4(%rbp),%rdi
call   1070 <CPU_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0xd4(%rbp),%rsi
call   10d0 <CPU_SET@plt>
xor    %edi,%edi
mov    $0x4,%esi
lea    -0xd4(%rbp),%rdx
call   1040 <sched_setaffinity@plt>
cmp    $0x0,%rax
je     12e5 <main+0x125>
lea    0xd5b(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    14bd <main+0x2fd>
lea    -0xb0(%rbp),%rdi
call   10e0 <ftime@plt>
mov    -0x1c(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x8,%edx
mov    $0x10,%ecx
call   10b0 <pread@plt>
mov    -0x1c(%rbp),%edi
lea    -0x88(%rbp),%rsi
mov    $0x8,%edx
mov    $0xe7,%ecx
call   10b0 <pread@plt>
mov    -0x1c(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x8,%edx
mov    $0xe8,%ecx
call   10b0 <pread@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x8fffffff,-0x14(%rbp)
jae    1363 <main+0x1a3>
mov    -0x14(%rbp),%edi
call   10a0 <sqrt@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1340 <main+0x180>
lea    -0xb8(%rbp),%rdi
call   10e0 <ftime@plt>
mov    -0x1c(%rbp),%edi
lea    -0x78(%rbp),%rsi
mov    $0x8,%edx
mov    $0x10,%ecx
call   10b0 <pread@plt>
mov    -0x1c(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x8,%edx
mov    $0xe7,%ecx
call   10b0 <pread@plt>
mov    -0x1c(%rbp),%edi
lea    -0xa8(%rbp),%rsi
mov    $0x8,%edx
mov    $0xe8,%ecx
call   10b0 <pread@plt>
mov    -0x78(%rbp),%rax
sub    -0x70(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x90(%rbp),%rax
sub    -0x88(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0xa8(%rbp),%rax
sub    -0xa0(%rbp),%rax
mov    %rax,-0x98(%rbp)
imul   $0x3e8,-0xb0(%rbp),%eax
add    -0xac(%rbp),%eax
cltq
mov    %rax,-0xc0(%rbp)
imul   $0x3e8,-0xb8(%rbp),%eax
add    -0xb4(%rbp),%eax
cltq
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rax
sub    -0xc0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
cvtsi2sdq -0xd0(%rbp),%xmm1
ff ff
movsd  0xbbe(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xbba(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rsi
lea    0xbec(%rip),%rdi
call   1030 <printf@plt>
cvtsi2sdq -0x68(%rbp),%xmm0
cvtsi2sdq -0x80(%rbp),%xmm2
movsd  0xb89(%rip),%xmm1
mulsd  %xmm2,%xmm1
cvtsi2sdq -0x98(%rbp),%xmm3
ff ff
movsd  0xb74(%rip),%xmm2
mulsd  %xmm3,%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvtsi2sdq -0xd0(%rbp),%xmm1
ff ff
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rsi
lea    0xba9(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    11e6 <main+0x36>
mov    0x2e0f(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
cmpq   $0x0,(%rbx)
je     11ff <main+0x4f>
mov    0x0(%rbp),%rdi
call   1050 <usage@plt>
mov    $0x1,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
lea    0xdf7(%rip),%rsi
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12bc <main+0x10c>
mov    %eax,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1060 <CPU_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   10c0 <CPU_SET@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <sched_setaffinity@plt>
test   %rax,%rax
jne    12cd <main+0x11d>
lea    0x10(%rsp),%rdi
call   10d0 <ftime@plt>
lea    0x28(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
mov    $0x10,%ecx
call   10a0 <pread@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
mov    $0xe7,%ecx
call   10a0 <pread@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
mov    $0xe8,%ecx
call   10a0 <pread@plt>
xchg   %ax,%ax
mov    %ebx,%edi
call   1090 <sqrt@plt>
add    $0x1,%ebx
jmp    12b0 <main+0x100>
lea    0xd4d(%rip),%rdi
call   1080 <perror@plt>
jmp    11ef <main+0x3f>
lea    0xd4b(%rip),%rdi
call   1080 <perror@plt>
jmp    11ef <main+0x3f>

LLVM IR (-O0):
%struct.timeb = type { i32, i32 }
@errno = global i64 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to open\00"
@.str.2 = [27 x i8] c"Failed to set cpu affinity\00"
@.str.3 = [16 x i8] c"runTime: %4.2f\0A\00"
@.str.4 = [13 x i8] c"freq: %7.0f\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 [64 x i8]
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64
%19 = alloca %struct.timeb
%20 = alloca %struct.timeb
%21 = alloca i64
%22 = alloca i64
%23 = alloca i64
%24 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 2
br i1 %26, label %27, label %32
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = call i32 @usage(i8* %30)
store i32 1, i32* %3
br label %135
32: ; preds = %2
store i64 0, i64* @errno
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strtol(i8* %35, i8** null, i32 10)
store i32 %36, i32* %7
%37 = load i64, i64* @errno
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 0
%42 = load i8*, i8** %41
%43 = call i32 @usage(i8* %42)
store i32 1, i32* %3
br label %135
44: ; preds = %32
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %7
%47 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* @O_RDONLY
%50 = call i32 @open(i8* %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %135
55: ; preds = %44
%56 = call i32 @CPU_ZERO(i32* %24)
%57 = load i32, i32* %7
%58 = call i32 @CPU_SET(i32 %57, i32* %24)
%59 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %24)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %135
63: ; preds = %55
%64 = call i32 @ftime(%struct.timeb* %19)
%65 = load i32, i32* %8
%66 = call i32 @pread(i32 %65, i64* %11, i32 8, i32 16)
%67 = load i32, i32* %8
%68 = call i32 @pread(i32 %67, i64* %14, i32 8, i32 231)
%69 = load i32, i32* %8
%70 = call i32 @pread(i32 %69, i64* %17, i32 8, i32 232)
store i32 0, i32* %6
br label %71
71: ; preds = %77, %63
%72 = load i32, i32* %6
%73 = icmp ult i32 %72, -1879048193
br i1 %73, label %74, label %80
74: ; preds = %71
%75 = load i32, i32* %6
%76 = call i32 @sqrt(i32 %75)
br label %77
77: ; preds = %74
%78 = load i32, i32* %6
%79 = add nsw i32 %78, 1
store i32 %79, i32* %6
br label %71
80: ; preds = %71
%81 = call i32 @ftime(%struct.timeb* %20)
%82 = load i32, i32* %8
%83 = call i32 @pread(i32 %82, i64* %12, i32 8, i32 16)
%84 = load i32, i32* %8
%85 = call i32 @pread(i32 %84, i64* %15, i32 8, i32 231)
%86 = load i32, i32* %8
%87 = call i32 @pread(i32 %86, i64* %18, i32 8, i32 232)
%88 = load i64, i64* %12
%89 = load i64, i64* %11
%90 = sub nsw i64 %88, %89
store i64 %90, i64* %10
%91 = load i64, i64* %15
%92 = load i64, i64* %14
%93 = sub nsw i64 %91, %92
store i64 %93, i64* %13
%94 = load i64, i64* %18
%95 = load i64, i64* %17
%96 = sub nsw i64 %94, %95
store i64 %96, i64* %16
%97 = getelementptr inbounds %struct.timeb, %struct.timeb* %19, i32 0, i32 0
%98 = load i32, i32* %97
%99 = mul nsw i32 %98, 1000
%100 = getelementptr inbounds %struct.timeb, %struct.timeb* %19, i32 0, i32 1
%101 = load i32, i32* %100
%102 = add nsw i32 %99, %101
%103 = sext i32 %102 to i64
store i64 %103, i64* %21
%104 = getelementptr inbounds %struct.timeb, %struct.timeb* %20, i32 0, i32 0
%105 = load i32, i32* %104
%106 = mul nsw i32 %105, 1000
%107 = getelementptr inbounds %struct.timeb, %struct.timeb* %20, i32 0, i32 1
%108 = load i32, i32* %107
%109 = add nsw i32 %106, %108
%110 = sext i32 %109 to i64
store i64 %110, i64* %22
%111 = load i64, i64* %22
%112 = load i64, i64* %21
%113 = sub nsw i64 %111, %112
store i64 %113, i64* %23
%114 = load i64, i64* %23
%115 = sitofp i64 %114 to double
%116 = fmul double 1.000000e+00, %115
%117 = fdiv double %116, 1.000000e+03
%118 = fptosi double %117 to i64
%119 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %118)
%120 = load i64, i64* %10
%121 = sitofp i64 %120 to double
%122 = load i64, i64* %13
%123 = sitofp i64 %122 to double
%124 = fmul double 1.000000e+00, %123
%125 = load i64, i64* %16
%126 = sitofp i64 %125 to double
%127 = fmul double 1.000000e+00, %126
%128 = fdiv double %124, %127
%129 = fdiv double %121, %128
%130 = load i64, i64* %23
%131 = sitofp i64 %130 to double
%132 = fdiv double %129, %131
%133 = fptosi double %132 to i64
%134 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i64 %133)
store i32 0, i32* %3
br label %135
135: ; preds = %80, %61, %53, %39, %27
%136 = load i32, i32* %3
ret i32 %136
}

LLVM IR (-O2):
%struct.timeb = type { i32, i32 }
@errno = global i64 0
@.str = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Failed to open\00"
@.str.2 = [27 x i8] c"Failed to set cpu affinity\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [64 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.timeb
%8 = alloca i32
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = bitcast i64* %4 to i8*
%11 = bitcast i64* %5 to i8*
%12 = bitcast i64* %6 to i8*
%13 = bitcast %struct.timeb* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = icmp eq i32 %0, 2
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i8*, i8** %1
%18 = tail call i32 @usage(i8* %17)
br label %51
19: ; preds = %2
store i64 0, i64* @errno
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @strtol(i8* nocapture %21, i8** null, i32 10)
%23 = load i64, i64* @errno
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = load i8*, i8** %1
%27 = tail call i32 @usage(i8* %26)
br label %51
28: ; preds = %19
%29 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %22)
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %9, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %51
35: ; preds = %28
%36 = call i32 @CPU_ZERO(i32* %8)
%37 = call i32 @CPU_SET(i32 %22, i32* %8)
%38 = call i64 @sched_setaffinity(i32 0, i32 4, i32* %8)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %51
42: ; preds = %35
%43 = call i32 @ftime(%struct.timeb* %7)
%44 = call i32 @pread(i32 %31, i64* %4, i32 8, i32 16)
%45 = call i32 @pread(i32 %31, i64* %5, i32 8, i32 231)
%46 = call i32 @pread(i32 %31, i64* %6, i32 8, i32 232)
br label %47
47: ; preds = %47, %42
%48 = phi i32 [ 0, %42 ], [ %50, %47 ]
%49 = call i32 @sqrt(i32 %48)
%50 = add nuw nsw i32 %48, 1
br label %47
51: ; preds = %40, %33, %25, %16
ret i32 1
}