#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*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *, char *, char *,
                          char *, char *, char *, char *);
scalar_t__ strstr(char *, char *);
int cpu_used_per_get(int per_total) {
  FILE *fp;
  char buff[2048];
  int toogle = 1;
  int out;
  char temp[10][256];
  fp = popen("top -n 2 -d 0.01 | grep Cpu", "r");
  if (fp == NULL) {
    perror("erro : ");
    return 0;
  }
  while (fgets(buff, 2048, fp) != NULL) {
    sscanf(buff, "%s %s %s %s %s %s %s %s", temp[0], temp[1], temp[2], temp[3],
           temp[4], temp[5], temp[6], temp[7]);
    if (toogle == 0) {
      if (strstr(temp[7], "id")) {
        out = 100;
      } else {
        out = (atoi(temp[7]));
      }
    } else
      toogle = 0;
  }
  int out_value = out;
  pclose(fp);
  return (int)((double)out_value * (double)((double)per_total / (double)100));
}


x86 assembly (clang, -O0):
<cpu_used_per_get>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x1258,%rsp
mov    %edi,-0x10(%rbp)
movl   $0x1,-0x824(%rbp)
lea    0xe78(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <cpu_used_per_get+0x53>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0xc(%rbp)
jmp    131b <cpu_used_per_get+0x1ab>
jmp    11c8 <cpu_used_per_get+0x58>
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x800,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12e2 <cpu_used_per_get+0x172>
lea    -0x820(%rbp),%rdi
lea    -0x1230(%rbp),%rdx
lea    -0x1230(%rbp),%rcx
add    $0x100,%rcx
lea    -0x1230(%rbp),%r8
add    $0x200,%r8
lea    -0x1230(%rbp),%r9
add    $0x300,%r9
lea    -0x1230(%rbp),%rbx
add    $0x400,%rbx
lea    -0x1230(%rbp),%r11
add    $0x500,%r11
lea    -0x1230(%rbp),%r10
add    $0x600,%r10
lea    -0x1230(%rbp),%rax
add    $0x700,%rax
lea    0xdd1(%rip),%rsi
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1050 <sscanf@plt>
cmpl   $0x0,-0x824(%rbp)
jne    12d3 <cpu_used_per_get+0x163>
lea    -0x1230(%rbp),%rdi
add    $0x700,%rdi
lea    0xdaf(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     12b5 <cpu_used_per_get+0x145>
movl   $0x64,-0x828(%rbp)
jmp    12ce <cpu_used_per_get+0x15e>
lea    -0x1230(%rbp),%rdi
add    $0x700,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x828(%rbp)
jmp    12dd <cpu_used_per_get+0x16d>
movl   $0x0,-0x824(%rbp)
jmp    11c8 <cpu_used_per_get+0x58>
mov    -0x828(%rbp),%eax
mov    %eax,-0x1234(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
cvtsi2sdl -0x1234(%rbp),%xmm0
ff
cvtsi2sdl -0x10(%rbp),%xmm1
movsd  0xcf4(%rip),%xmm2
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1258,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_used_per_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1218,%rsp
mov    %edi,%ebp
lea    0xe7e(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     131a <cpu_used_per_get+0x1aa>
mov    %rax,%rbx
mov    %ebp,0xc(%rsp)
lea    0xa10(%rsp),%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12e5 <cpu_used_per_get+0x175>
lea    0x110(%rsp),%rcx
lea    0x210(%rsp),%r8
lea    0x310(%rsp),%r9
lea    0x410(%rsp),%r10
lea    0x510(%rsp),%r11
lea    0x610(%rsp),%rax
lea    0x710(%rsp),%rbp
lea    0xe2b(%rip),%rsi
lea    0xa10(%rsp),%r14
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
push   %rbp
push   %rax
push   %r11
push   %r10
call   1050 <sscanf@plt>
add    $0x20,%rsp
mov    %r14,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12e5 <cpu_used_per_get+0x175>
lea    0xa10(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xdf7(%rip),%r13
jmp    1275 <cpu_used_per_get+0x105>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12e5 <cpu_used_per_get+0x175>
mov    %r15,%rdi
lea    0xdaf(%rip),%rsi
mov    %r12,%rdx
lea    0x110(%rsp),%rcx
lea    0x210(%rsp),%r8
lea    0x310(%rsp),%r9
push   %rbp
lea    0x618(%rsp),%rax
push   %rax
lea    0x520(%rsp),%rax
push   %rax
lea    0x428(%rsp),%rax
push   %rax
call   1050 <sscanf@plt>
add    $0x20,%rsp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
mov    $0x64,%r14d
test   %rax,%rax
jne    1260 <cpu_used_per_get+0xf0>
mov    %rbp,%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    1260 <cpu_used_per_get+0xf0>
mov    %rbx,%rdi
call   1030 <pclose@plt>
cvtsi2sd %r14d,%xmm0
cvtsi2sdl 0xc(%rsp),%xmm1
divsd  0xd00(%rip),%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
add    $0x1218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd05(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
jmp    1308 <cpu_used_per_get+0x198>

LLVM IR (-O0):
@.str = [28 x i8] c"top -n 2 -d 0.01 | grep Cpu\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"erro : \00"
@.str.3 = [24 x i8] c"%s %s %s %s %s %s %s %s\00"
@.str.4 = [3 x i8] c"id\00"
define i32 @cpu_used_per_get(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [2048 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x [256 x i8]]
%9 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
%10 = call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %66
15: ; preds = %1
br label %16
16: ; preds = %54, %15
%17 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i32* @fgets(i8* %17, i32 2048, i32* %18)
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %55
21: ; preds = %16
%22 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%23 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 0
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %23, i64 0, i64 0
%25 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 1
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %25, i64 0, i64 0
%27 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 2
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %27, i64 0, i64 0
%29 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 3
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %29, i64 0, i64 0
%31 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 4
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %31, i64 0, i64 0
%33 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 5
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %33, i64 0, i64 0
%35 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 6
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %35, i64 0, i64 0
%37 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 7
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %37, i64 0, i64 0
%39 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %24, i8* %26, i8* %28, i8* %30, i8* %32, i8* %34, i8* %36, i8* %38)
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %21
%43 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 7
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %43, i64 0, i64 0
%45 = call i64 @strstr(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 100, i32* %7
br label %52
48: ; preds = %42
%49 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %8, i64 0, i64 7
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %49, i64 0, i64 0
%51 = call i32 @atoi(i8* %50)
store i32 %51, i32* %7
br label %52
52: ; preds = %48, %47
br label %54
53: ; preds = %21
store i32 0, i32* %6
br label %54
54: ; preds = %53, %52
br label %16
55: ; preds = %16
%56 = load i32, i32* %7
store i32 %56, i32* %9
%57 = load i32*, i32** %4
%58 = call i32 @pclose(i32* %57)
%59 = load i32, i32* %9
%60 = sitofp i32 %59 to double
%61 = load i32, i32* %3
%62 = sitofp i32 %61 to double
%63 = fdiv double %62, 1.000000e+02
%64 = fmul double %60, %63
%65 = fptosi double %64 to i32
store i32 %65, i32* %2
br label %66
66: ; preds = %55, %13
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@.str = [28 x i8] c"top -n 2 -d 0.01 | grep Cpu\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"erro : \00"
@.str.3 = [24 x i8] c"%s %s %s %s %s %s %s %s\00"
@.str.4 = [3 x i8] c"id\00"
define i32 @cpu_used_per_get(i32 %0) {
%2 = alloca [2048 x i8]
%3 = alloca [10 x [256 x i8]]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 0, i64 0
%6 = tail call i32* @popen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %22, label %8
8: ; preds = %1
%9 = call i32* @fgets(i8* %4, i32 2048, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %34, label %11
11: ; preds = %8
%12 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 1, i64 0
%13 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 2, i64 0
%14 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 3, i64 0
%15 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 4, i64 0
%16 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 5, i64 0
%17 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 6, i64 0
%18 = getelementptr inbounds [10 x [256 x i8]], [10 x [256 x i8]]* %3, i64 0, i64 7, i64 0
%19 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %12, i8* %13, i8* %14, i8* %15, i8* %16, i8* %17, i8* %18)
%20 = call i32* @fgets(i8* %4, i32 2048, i32* %6)
%21 = icmp eq i32* %20, null
br i1 %21, label %34, label %24
22: ; preds = %1
%23 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %42
24: ; preds = %11, %30
%25 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %12, i8* %13, i8* %14, i8* %15, i8* %16, i8* %17, i8* %18)
%26 = call i64 @strstr(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @atoi(i8* %18)
br label %30
30: ; preds = %24, %28
%31 = phi i32 [ %29, %28 ], [ 100, %24 ]
%32 = call i32* @fgets(i8* %4, i32 2048, i32* %6)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %24
34: ; preds = %30, %11, %8
%35 = phi i32 [ undef, %8 ], [ undef, %11 ], [ %31, %30 ]
%36 = call i32 @pclose(i32* %6)
%37 = sitofp i32 %35 to double
%38 = sitofp i32 %0 to double
%39 = fdiv double %38, 1.000000e+02
%40 = fmul double %39, %37
%41 = fptosi double %40 to i32
br label %42
42: ; preds = %34, %22
%43 = phi i32 [ 0, %22 ], [ %41, %34 ]
ret i32 %43
}