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

/* Variables and functions */
scalar_t__ get_cpu_load(int *, int *);
int /*<<< orphan*/ warnx(char *);
float get_cpu_usage() {
  static int total_0 = 0, work_0 = 0;
  int total_1 = 0, work_1 = 0;
  float total_d, work_d, cpu_d;
  if (get_cpu_load(&total_1, &work_1) < 0) {
    warnx("ERROR: failed to get cpu load.");
    return -1;
  }
  if (!work_0) {
    total_0 = total_1;
    work_0 = work_1;
    return 1000;
  }
  total_d = total_1 - total_0;
  work_d = work_1 - work_0;
  cpu_d = work_d / total_d * 100.;
  total_0 = total_1;
  work_0 = work_1;
  return cpu_d;
}


x86 assembly (clang, -O0):
<get_cpu_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1030 <get_cpu_load@plt>
cmp    $0x0,%rax
jge    116b <get_cpu_usage+0x4b>
lea    0xebc(%rip),%rdi
call   1040 <warnx@plt>
movss  0xea3(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    11ff <get_cpu_usage+0xdf>
cmpl   $0x0,0x2ec6(%rip)
jne    119c <get_cpu_usage+0x7c>
mov    -0x8(%rbp),%eax
mov    %eax,0x2eb3(%rip)
mov    -0xc(%rbp),%eax
mov    %eax,0x2eae(%rip)
movss  0xe6e(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    11ff <get_cpu_usage+0xdf>
mov    -0x8(%rbp),%eax
sub    0x2e8f(%rip),%eax
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    0x2e81(%rip),%eax
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
divss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe32(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,0x2e48(%rip)
mov    -0xc(%rbp),%eax
mov    %eax,0x2e43(%rip)
movss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_usage>:
push   %rax
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <get_cpu_load@plt>
test   %rax,%rax
js     117f <get_cpu_usage+0x5f>
mov    0x2ef0(%rip),%ecx
mov    0x4(%rsp),%eax
test   %ecx,%ecx
je     1195 <get_cpu_usage+0x75>
mov    %eax,%edx
sub    0x2edc(%rip),%edx
cvtsi2ss %edx,%xmm1
mov    (%rsp),%edx
mov    %edx,0x2ed3(%rip)
sub    %ecx,%edx
cvtsi2ss %edx,%xmm0
divss  %xmm1,%xmm0
mulss  0xe89(%rip),%xmm0
mov    %eax,0x2eb7(%rip)
pop    %rax
ret
lea    0xe86(%rip),%rdi
call   1040 <warnx@plt>
movss  0xe75(%rip),%xmm0
pop    %rax
ret
mov    %eax,0x2e99(%rip)
mov    (%rsp),%eax
mov    %eax,0x2e94(%rip)
movss  0xe58(%rip),%xmm0
pop    %rax
ret

LLVM IR (-O0):
@get_cpu_usage.total_0 = internal global i32 0
@get_cpu_usage.work_0 = internal global i32 0
@.str = [31 x i8] c"ERROR: failed to get cpu load.\00"
define float @get_cpu_usage() {
%1 = alloca float
%2 = alloca i32
%3 = alloca i32
%4 = alloca float
%5 = alloca float
%6 = alloca float
store i32 0, i32* %2
store i32 0, i32* %3
%7 = call i64 @get_cpu_load(i32* %2, i32* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @warnx(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store float -1.000000e+00, float* %1
br label %35
11: ; preds = %0
%12 = load i32, i32* @get_cpu_usage.work_0
%13 = icmp ne i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i32, i32* %2
store i32 %15, i32* @get_cpu_usage.total_0
%16 = load i32, i32* %3
store i32 %16, i32* @get_cpu_usage.work_0
store float 1.000000e+03, float* %1
br label %35
17: ; preds = %11
%18 = load i32, i32* %2
%19 = load i32, i32* @get_cpu_usage.total_0
%20 = sub nsw i32 %18, %19
%21 = sitofp i32 %20 to float
store float %21, float* %4
%22 = load i32, i32* %3
%23 = load i32, i32* @get_cpu_usage.work_0
%24 = sub nsw i32 %22, %23
%25 = sitofp i32 %24 to float
store float %25, float* %5
%26 = load float, float* %5
%27 = load float, float* %4
%28 = fdiv float %26, %27
%29 = fpext float %28 to double
%30 = fmul double %29, 1.000000e+02
%31 = fptrunc double %30 to float
store float %31, float* %6
%32 = load i32, i32* %2
store i32 %32, i32* @get_cpu_usage.total_0
%33 = load i32, i32* %3
store i32 %33, i32* @get_cpu_usage.work_0
%34 = load float, float* %6
store float %34, float* %1
br label %35
35: ; preds = %17, %14, %9
%36 = load float, float* %1
ret float %36
}

LLVM IR (-O2):
@get_cpu_usage.total_0 = internal unnamed_addr global i32 0
@get_cpu_usage.work_0 = internal unnamed_addr global i32 0
@.str = [31 x i8] c"ERROR: failed to get cpu load.\00"
define float @get_cpu_usage() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = call i64 @get_cpu_load(i32* %1, i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @warnx(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %0
%10 = load i32, i32* @get_cpu_usage.work_0
%11 = icmp eq i32 %10, 0
%12 = load i32, i32* %1
br i1 %11, label %13, label %15
13: ; preds = %9
store i32 %12, i32* @get_cpu_usage.total_0
%14 = load i32, i32* %2
store i32 %14, i32* @get_cpu_usage.work_0
br label %24
15: ; preds = %9
%16 = load i32, i32* @get_cpu_usage.total_0
%17 = sub nsw i32 %12, %16
%18 = sitofp i32 %17 to float
%19 = load i32, i32* %2
%20 = sub nsw i32 %19, %10
%21 = sitofp i32 %20 to float
%22 = fdiv float %21, %18
%23 = fmul float %22, 1.000000e+02
store i32 %12, i32* @get_cpu_usage.total_0
store i32 %19, i32* @get_cpu_usage.work_0
br label %24
24: ; preds = %15, %13, %7
%25 = phi float [ -1.000000e+00, %7 ], [ %23, %15 ], [ 1.000000e+03, %13 ]
ret float %25
}