#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 /*<<< orphan*/ debug_print(int /*<<< orphan*/ *, int, char *, double, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int calibrate_cpufreq(double *nsofclk, 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;
  debug_print(NULL, 2,
              "calibrate_cpufreq: nsofclk = %.4f, "
              "clkofsec = %lu\n",
              *nsofclk, *clkofsec);
  return (0);
}


x86 assembly (clang, -O0):
<calibrate_cpufreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    118d <calibrate_cpufreq+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <calibrate_cpufreq+0xf3>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     11be <calibrate_cpufreq+0x6e>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <calibrate_cpufreq+0xf3>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movslq -0x20(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
call   1050 <atoll@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jne    11f0 <calibrate_cpufreq+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <calibrate_cpufreq+0xf3>
imul   $0x3e8,-0x44(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cvtsi2sdl (%rax),%xmm1
movsd  0xdf3(%rip),%xmm0
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2,%esi
lea    0xe07(%rip),%rdx
call   1070 <debug_print@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calibrate_cpufreq>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
js     11e5 <calibrate_cpufreq+0x95>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1f,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %ebx,%ebx
jle    11e5 <calibrate_cpufreq+0x95>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <atoll@plt>
test   %eax,%eax
je     11e5 <calibrate_cpufreq+0x95>
imul   $0x3e8,%eax,%ecx
cvtsi2sd %ecx,%xmm1
mov    %ecx,(%r15)
movsd  0xe3a(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%r14)
lea    0xe68(%rip),%rdx
xor    %r12d,%r12d
xor    %edi,%edi
mov    $0x2,%esi
call   1070 <debug_print@plt>
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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
@.str.1 = [51 x i8] c"calibrate_cpufreq: nsofclk = %.4f, clkofsec = %lu\0A\00"
define i32 @calibrate_cpufreq(double* %0, i32* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i32
store double* %0, double** %4
store i32* %1, i32** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %46
14: ; preds = %2
%15 = load i32, i32* %6
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 31)
store i32 %17, i32* %7
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %3
br label %46
22: ; preds = %14
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%29 = call i32 @atoll(i8* %28)
store i32 %29, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %22
store i32 -1, i32* %3
br label %46
32: ; preds = %22
%33 = load i32, i32* %9
%34 = mul nsw i32 %33, 1000
%35 = load i32*, i32** %5
store i32 %34, i32* %35
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = sitofp i32 %37 to double
%39 = fdiv double 1.000000e+09, %38
%40 = load double*, double** %4
store double %39, double* %40
%41 = load double*, double** %4
%42 = load double, double* %41
%43 = load i32*, i32** %5
%44 = load i32, i32* %43
%45 = call i32 @debug_print(i32* null, i32 2, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), double %42, i32 %44)
store i32 0, i32* %3
br label %46
46: ; preds = %32, %31, %19, %13
%47 = load i32, i32* %3
ret i32 %47
}

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