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

/* Variables and functions */
int ENOENT;
int /*<<< orphan*/ errno;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
int cpu_get_freq(char *dst) {
  FILE *fp = NULL;
  char buf[2048];
  char cmdline[2048];
  memset(cmdline, 0, sizeof(cmdline));
  strncpy(cmdline,
          "top -o CPU -s 5 -n1 -l 1|grep \"CPU usage\" | awk -F\"CPU usage:\" "
          "'{ print $2 }' | awk -F\",\" '{ print $1 + $2}'",
          strlen("top -o CPU -s 5 -n1 -l 1|grep \"CPU usage\" | awk -F\"CPU "
                 "usage:\" '{ print $2 }' | awk -F\",\" '{ print $1 + $2}'"));
  fp = popen(cmdline, "r");
  if (!fp) {
    printf("popen %s error:%s",
           "top -o CPU -s 5 -n1 -l 1|grep \"CPU usage\" | awk -F\"CPU usage:\" "
           "'{ print $2 }' | awk -F\",\" '{ print $1 + $2}'",
           strerror(errno));
    return -ENOENT;
  }
  while (fgets(buf, sizeof(buf), fp)) {
    strncpy(dst, buf, strlen(buf) - 1);
    printf("\tSCANCPU:cpu_freq = [%s]", dst);
  }
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<cpu_get_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
lea    0xe41(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x1028(%rbp),%rdi
mov    %rax,%rdx
lea    0xe2b(%rip),%rsi
call   1030 <strncpy@plt>
lea    -0x1020(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1235 <cpu_get_freq+0xb5>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdx
lea    0xe5b(%rip),%rdi
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2da5(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12bf <cpu_get_freq+0x13f>
jmp    123a <cpu_get_freq+0xba>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12af <cpu_get_freq+0x12f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x820(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1038(%rbp),%rdi
mov    -0x1030(%rbp),%rsi
mov    %rax,%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rsi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    123a <cpu_get_freq+0xba>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_get_freq>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
lea    0x800(%rsp),%rbp
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
lea    0xe4f(%rip),%rsi
mov    $0x6c,%edx
mov    %rbp,%rdi
call   1030 <strncpy@plt>
lea    0xea8(%rip),%rsi
mov    %rbp,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1246 <cpu_get_freq+0xc6>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1239 <cpu_get_freq+0xb9>
mov    %rsp,%rbp
lea    0xe8d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1200 <cpu_get_freq+0x80>
mov    %r14,%rdi
call   1060 <pclose@plt>
xor    %r15d,%r15d
jmp    1276 <cpu_get_freq+0xf6>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe14(%rip),%rdi
lea    0xd9e(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d5d(%rip),%rax
sub    (%rax),%r15d
mov    %r15d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [109 x i8] c"top -o CPU -s 5 -n1 -l 1|grep \22CPU usage\22 | awk -F\22CPU usage:\22 '{ print $2 }' | awk -F\22,\22 '{ print $1 + $2}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"popen %s error:%s\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.3 = [25 x i8] c"\09SCANCPU:cpu_freq = [%s]\00"
define i32 @cpu_get_freq(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2048 x i8]
%6 = alloca [2048 x i8]
store i8* %0, i8** %3
store i32* null, i32** %4
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 2048)
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%10 = call i64 @strlen(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @strncpy(i8* %9, i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %1
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @ENOENT
%21 = sub nsw i32 0, %20
store i32 %21, i32* %2
br label %40
22: ; preds = %1
br label %23
23: ; preds = %28, %22
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%25 = load i32*, i32** %4
%26 = call i64 @fgets(i8* %24, i32 2048, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%32 = call i64 @strlen(i8* %31)
%33 = sub nsw i64 %32, 1
%34 = call i32 @strncpy(i8* %29, i8* %30, i64 %33)
%35 = load i8*, i8** %3
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %23
37: ; preds = %23
%38 = load i32*, i32** %4
%39 = call i32 @pclose(i32* %38)
store i32 0, i32* %2
br label %40
40: ; preds = %37, %16
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [109 x i8] c"top -o CPU -s 5 -n1 -l 1|grep \22CPU usage\22 | awk -F\22CPU usage:\22 '{ print $2 }' | awk -F\22,\22 '{ print $1 + $2}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"popen %s error:%s\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.3 = [25 x i8] c"\09SCANCPU:cpu_freq = [%s]\00"
define i32 @cpu_get_freq(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 2048)
%7 = call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0), i64 108)
%8 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i64 @fgets(i8* %4, i32 2048, i32* %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %26, label %19
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @ENOENT
%18 = sub nsw i32 0, %17
br label %28
19: ; preds = %10, %19
%20 = call i64 @strlen(i8* %4)
%21 = add nsw i64 %20, -1
%22 = call i32 @strncpy(i8* %0, i8* %4, i64 %21)
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%24 = call i64 @fgets(i8* %4, i32 2048, i32* %8)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %19
26: ; preds = %19, %10
%27 = call i32 @pclose(i32* %8)
br label %28
28: ; preds = %26, %13
%29 = phi i32 [ 0, %26 ], [ %18, %13 ]
ret i32 %29
}