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

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *);
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *m_strdup(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *get_scpufreq(char *args) {
  char *path;
  FILE *freq_file;
  char *fgets_check;
  char freq[16];
  asprintf(&path,
           "/sys/devices/system/cpu/cpu"
           "%s"
           "/cpufreq/scaling_cur_freq",
           (args != NULL) ? args : "0");
  freq_file = fopen(path, "r");
  free(path);
  if (freq_file == NULL)
    return "n/a";
  fgets_check = fgets(freq, sizeof(freq), freq_file);
  fclose(freq_file);
  if (fgets_check == NULL)
    return "n/a";
  snprintf(freq, sizeof(freq), "%d", atoi(freq) / 1000);
  return m_strdup(freq);
}


x86 assembly (clang, -O0):
<get_scpufreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <get_scpufreq+0x24>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11b4 <get_scpufreq+0x34>
lea    0xe8c(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11b4 <get_scpufreq+0x34>
mov    -0x48(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1200 <get_scpufreq+0x80>
lea    0xe44(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1277 <get_scpufreq+0xf7>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x10,%esi
call   1060 <fgets@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
jne    123a <get_scpufreq+0xba>
lea    0xe0a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1277 <get_scpufreq+0xf7>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1090 <atoi@plt>
mov    -0x50(%rbp),%rdi
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    $0x10,%esi
lea    0xdda(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x40(%rbp),%rdi
call   10a0 <m_strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_scpufreq>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
test   %rdi,%rdi
lea    0xea4(%rip),%rdx
cmovne %rdi,%rdx
lea    0xe62(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0xe6e(%rip),%r14
test   %rbx,%rbx
je     1236 <get_scpufreq+0xb6>
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r15,%r15
je     1236 <get_scpufreq+0xb6>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <atoi@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x26,%rcx
add    %eax,%ecx
lea    0xe21(%rip),%rdx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   10a0 <m_strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%s/cpufreq/scaling_cur_freq\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"n/a\00"
@.str.4 = [3 x i8] c"%d\00"
define i8* @get_scpufreq(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca [16 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** %3
br label %13
12: ; preds = %1
br label %13
13: ; preds = %12, %10
%14 = phi i8* [ %11, %10 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %12 ]
%15 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %13
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %40
23: ; preds = %13
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %5
%26 = call i8* @fgets(i8* %24, i32 16, i32* %25)
store i8* %26, i8** %6
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %23
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %40
32: ; preds = %23
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%35 = call i32 @atoi(i8* %34)
%36 = sdiv i32 %35, 1000
%37 = call i32 @snprintf(i8* %33, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%39 = call i8* @m_strdup(i8* %38)
store i8* %39, i8** %2
br label %40
40: ; preds = %32, %31, %22
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%s/cpufreq/scaling_cur_freq\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"n/a\00"
@.str.4 = [3 x i8] c"%d\00"
define i8* @get_scpufreq(i8* %0) {
%2 = alloca i8*
%3 = alloca [16 x i8]
%4 = bitcast i8** %2 to i8*
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = icmp eq i8* %0, null
%7 = select i1 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %0
%8 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = load i8*, i8** %2
%12 = call i32 @free(i8* %11)
%13 = icmp eq i32* %10, null
br i1 %13, label %23, label %14
14: ; preds = %1
%15 = call i8* @fgets(i8* %5, i32 16, i32* %10)
%16 = call i32 @fclose(i32* %10)
%17 = icmp eq i8* %15, null
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = call i32 @atoi(i8* %5)
%20 = sdiv i32 %19, 1000
%21 = call i32 @snprintf(i8* %5, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = call i8* @m_strdup(i8* %5)
br label %23
23: ; preds = %14, %1, %18
%24 = phi i8* [ %22, %18 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %1 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %14 ]
ret i8* %24
}