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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char **);
int sprintf(char *, char *, char *, char *, char *);
int get_os_freq(uint64_t cpu, uint64_t *freq) {
  char path[60];
  int ret = 0;
  int out = 0;
  FILE *fp;
  out = sprintf(path, "%s%u%s", "/sys/devices/system/cpu/cpu", cpu,
                "/cpufreq/cpuinfo_cur_freq");
  if (out > 0)
    fp = fopen(path, "r");
  if (NULL != fp) {
    fscanf(fp, "%u", freq);
    fclose(fp);
  } else {
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_os_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rcx
lea    0xe93(%rip),%rsi
lea    0xe93(%rip),%rdx
lea    0xea8(%rip),%r8
call   1060 <sprintf@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jle    11a1 <get_os_freq+0x61>
lea    -0x50(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x60(%rbp)
xor    %eax,%eax
cmp    -0x60(%rbp),%rax
je     11cf <get_os_freq+0x8f>
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe83(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x60(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11d6 <get_os_freq+0x96>
movl   $0xffffffff,-0x54(%rbp)
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_os_freq>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xeac(%rip),%rsi
lea    0xeac(%rip),%rdx
lea    0xec1(%rip),%r8
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xec9(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a2 <get_os_freq+0x62>
mov    %rax,%rbx
lea    0xeb4(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11a7 <get_os_freq+0x67>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%u%s\00"
@.str.1 = [28 x i8] c"/sys/devices/system/cpu/cpu\00"
@.str.2 = [26 x i8] c"/cpufreq/cpuinfo_cur_freq\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%u\00"
define i32 @get_os_freq(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca [60 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %6
store i32 0, i32* %7
%9 = getelementptr inbounds [60 x i8], [60 x i8]* %5, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = getelementptr inbounds [60 x i8], [60 x i8]* %5, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %8
br label %17
17: ; preds = %14, %2
%18 = load i32*, i32** %8
%19 = icmp ne i32* null, %18
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32*, i32** %8
%22 = load i8**, i8*** %4
%23 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %22)
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
br label %27
26: ; preds = %17
store i32 -1, i32* %6
br label %27
27: ; preds = %26, %20
%28 = load i32, i32* %6
ret i32 %28
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%u%s\00"
@.str.1 = [28 x i8] c"/sys/devices/system/cpu/cpu\00"
@.str.2 = [26 x i8] c"/cpufreq/cpuinfo_cur_freq\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"%u\00"
define i32 @get_os_freq(i8* %0, i8** %1) {
%3 = alloca [60 x i8]
%4 = getelementptr inbounds [60 x i8], [60 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp sgt i32 %5, 0
tail call void @llvm.assume(i1 %6)
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = tail call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %1)
%11 = tail call i32 @fclose(i32* %7)
br label %12
12: ; preds = %2, %9
%13 = phi i32 [ 0, %9 ], [ -1, %2 ]
ret i32 %13
}