#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 uint64_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoll(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int calibrate_cpufreq(double *nsofclk, double *clkofns, uint64_t *clkofsec) {
  int fd, i;
  char buf[32];
  uint64_t freq;
  if ((fd = open("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
                 O_RDONLY)) < 0) {
    return (-1);
  }
  if ((i = read(fd, buf, sizeof(buf) - 1)) <= 0) {
    close(fd);
    return (-1);
  }
  close(fd);
  buf[i] = 0;
  if ((freq = atoll(buf)) == 0) {
    return (-1);
  }
  *clkofsec = freq * 1000;
  *nsofclk = (double)1000000000 / *clkofsec;
  *clkofns = (double)1.0 / *nsofclk;
  return (0);
}


x86 assembly (clang, -O0):
<calibrate_cpufreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1181 <calibrate_cpufreq+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <calibrate_cpufreq+0xec>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jg     11b2 <calibrate_cpufreq+0x72>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <calibrate_cpufreq+0xec>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
call   1050 <atoll@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jne    11e4 <calibrate_cpufreq+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <calibrate_cpufreq+0xec>
imul   $0x3e8,-0x54(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cvtsi2sdl (%rax),%xmm1
movsd  0xe07(%rip),%xmm0
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  0xde7(%rip),%xmm0
divsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_cpufreq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
js     11d9 <calibrate_cpufreq+0x99>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
jle    11d9 <calibrate_cpufreq+0x99>
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <atoll@plt>
test   %eax,%eax
je     11d9 <calibrate_cpufreq+0x99>
imul   $0x3e8,%eax,%eax
cvtsi2sd %eax,%xmm0
mov    %eax,(%r12)
movsd  0xe44(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%r15)
movsd  0xe3b(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%r14)
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [54 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq\00"
@O_RDONLY = global i32 0
define i32 @calibrate_cpufreq(double* %0, double* %1, i32* %2) {
%4 = alloca i32
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [32 x i8]
%11 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store i32* %2, i32** %7
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %47
16: ; preds = %3
%17 = load i32, i32* %8
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 31)
store i32 %19, i32* %9
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %4
br label %47
24: ; preds = %16
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%31 = call i32 @atoll(i8* %30)
store i32 %31, i32* %11
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %24
store i32 -1, i32* %4
br label %47
34: ; preds = %24
%35 = load i32, i32* %11
%36 = mul nsw i32 %35, 1000
%37 = load i32*, i32** %7
store i32 %36, i32* %37
%38 = load i32*, i32** %7
%39 = load i32, i32* %38
%40 = sitofp i32 %39 to double
%41 = fdiv double 1.000000e+09, %40
%42 = load double*, double** %5
store double %41, double* %42
%43 = load double*, double** %5
%44 = load double, double* %43
%45 = fdiv double 1.000000e+00, %44
%46 = load double*, double** %6
store double %45, double* %46
store i32 0, i32* %4
br label %47
47: ; preds = %34, %33, %21, %15
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [54 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq\00"
@O_RDONLY = global i32 0
define i32 @calibrate_cpufreq(double* nocapture writeonly %0, double* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %3
%10 = call i32 @read(i32 %7, i8* %5, i32 31)
%11 = icmp slt i32 %10, 1
%12 = tail call i32 @close(i32 %7)
br i1 %11, label %23, label %13
13: ; preds = %9
%14 = zext i32 %10 to i64
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @atoll(i8* %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = mul nsw i32 %16, 1000
store i32 %19, i32* %2
%20 = sitofp i32 %19 to double
%21 = fdiv double 1.000000e+09, %20
store double %21, double* %0
%22 = fdiv double 1.000000e+00, %21
store double %22, double* %1
br label %23
23: ; preds = %9, %13, %3, %18
%24 = phi i32 [ 0, %18 ], [ -1, %3 ], [ -1, %13 ], [ -1, %9 ]
ret i32 %24
}