#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, long long *,
                          long long *, long long *, long long *);
int /*<<< orphan*/ sleep(int);
float cpu_load_percent(void) {
  FILE *p;
  p = fopen("/proc/stat", "r");
  char buffer[6];
  long long user, nice, sys, idle;
  fscanf(p, "%s  %lld %lld %lld %lld", buffer, &user, &nice, &sys, &idle);
  long long start_1, start_2;
  start_1 = user + nice + sys;
  start_2 = idle;
  fclose(p);
  sleep(1);
  p = fopen("/proc/stat", "r");
  fscanf(p, "%s  %lld %lld %lld %lld", buffer, &user, &nice, &sys, &idle);
  fclose(p);
  long long end_1, end_2;
  end_1 = user + nice + sys;
  end_2 = idle;
  float ret = ((float)end_1 - start_1) / ((float)end_2 - start_2) * 100;
  if (ret > 100)
    return 100.0;
  else
    return ret;
}


x86 assembly (clang, -O0):
<cpu_load_percent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xeb5(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x16(%rbp),%rdx
lea    0xea3(%rip),%rsi
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
lea    -0x38(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <fscanf@plt>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe4b(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x16(%rbp),%rdx
lea    0xe39(%rip),%rsi
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
lea    -0x38(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
cvtsi2ssq -0x50(%rbp),%xmm0
cvtsi2ssq -0x40(%rbp),%xmm1
subss  %xmm1,%xmm0
cvtsi2ssq -0x58(%rbp),%xmm1
cvtsi2ssq -0x48(%rbp),%xmm2
subss  %xmm2,%xmm1
divss  %xmm1,%xmm0
movss  0xdc2(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x5c(%rbp)
movss  -0x5c(%rbp),%xmm0
movss  0xdac(%rip),%xmm1
ucomiss %xmm1,%xmm0
jbe    126f <cpu_load_percent+0x12f>
movss  0xd9b(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1279 <cpu_load_percent+0x139>
movss  -0x5c(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_load_percent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xeaf(%rip),%rbx
lea    0xeb3(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%r15
lea    0x8(%rsp),%rax
mov    %rax,(%rsp)
lea    0xe9a(%rip),%rsi
lea    0x32(%rsp),%r12
lea    0x28(%rsp),%r13
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r9
mov    %r15,%rdi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1040 <fscanf@plt>
mov    0x20(%rsp),%r14
add    0x28(%rsp),%r14
add    0x10(%rsp),%r14
cvtsi2ssq 0x8(%rsp),%xmm0
movss  %xmm0,0x1c(%rsp)
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
lea    0xe3b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
lea    0xe22(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
lea    0x10(%rsp),%r9
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x20(%rsp),%rax
add    0x28(%rsp),%rax
add    0x10(%rsp),%rax
cvtsi2ss %rax,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %r14,%xmm0
subss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ssq 0x8(%rsp),%xmm0
subss  0x1c(%rsp),%xmm0
divss  %xmm0,%xmm1
movss  0xdba(%rip),%xmm0
mulss  %xmm0,%xmm1
minss  %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"%s %lld %lld %lld %lld\00"
define float @cpu_load_percent() {
%1 = alloca float
%2 = alloca i32*
%3 = alloca [6 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca float
%13 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%16 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %15, i64* %4, i64* %5, i64* %6, i64* %7)
%17 = load i64, i64* %4
%18 = load i64, i64* %5
%19 = add nsw i64 %17, %18
%20 = load i64, i64* %6
%21 = add nsw i64 %19, %20
store i64 %21, i64* %8
%22 = load i64, i64* %7
store i64 %22, i64* %9
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = call i32 @sleep(i32 1)
%26 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %2
%27 = load i32*, i32** %2
%28 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%29 = call i32 @fscanf(i32* %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %28, i64* %4, i64* %5, i64* %6, i64* %7)
%30 = load i32*, i32** %2
%31 = call i32 @fclose(i32* %30)
%32 = load i64, i64* %4
%33 = load i64, i64* %5
%34 = add nsw i64 %32, %33
%35 = load i64, i64* %6
%36 = add nsw i64 %34, %35
store i64 %36, i64* %10
%37 = load i64, i64* %7
store i64 %37, i64* %11
%38 = load i64, i64* %10
%39 = sitofp i64 %38 to float
%40 = load i64, i64* %8
%41 = sitofp i64 %40 to float
%42 = fsub float %39, %41
%43 = load i64, i64* %11
%44 = sitofp i64 %43 to float
%45 = load i64, i64* %9
%46 = sitofp i64 %45 to float
%47 = fsub float %44, %46
%48 = fdiv float %42, %47
%49 = fmul float %48, 1.000000e+02
store float %49, float* %12
%50 = load float, float* %12
%51 = fcmp ogt float %50, 1.000000e+02
br i1 %51, label %52, label %53
52: ; preds = %0
store float 1.000000e+02, float* %1
br label %55
53: ; preds = %0
%54 = load float, float* %12
store float %54, float* %1
br label %55
55: ; preds = %53, %52
%56 = load float, float* %1
ret float %56
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"%s %lld %lld %lld %lld\00"
define float @cpu_load_percent() {
%1 = alloca [6 x i8]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%8 = bitcast i64* %2 to i8*
%9 = bitcast i64* %3 to i8*
%10 = bitcast i64* %4 to i8*
%11 = bitcast i64* %5 to i8*
%12 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %7, i64* %2, i64* %3, i64* %4, i64* %5)
%13 = load i64, i64* %2
%14 = load i64, i64* %3
%15 = add nsw i64 %14, %13
%16 = load i64, i64* %4
%17 = add nsw i64 %15, %16
%18 = load i64, i64* %5
%19 = call i32 @fclose(i32* %6)
%20 = call i32 @sleep(i32 1)
%21 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %7, i64* %2, i64* %3, i64* %4, i64* %5)
%23 = call i32 @fclose(i32* %21)
%24 = load i64, i64* %2
%25 = load i64, i64* %3
%26 = add nsw i64 %25, %24
%27 = load i64, i64* %4
%28 = add nsw i64 %26, %27
%29 = load i64, i64* %5
%30 = sitofp i64 %28 to float
%31 = sitofp i64 %17 to float
%32 = fsub float %30, %31
%33 = sitofp i64 %29 to float
%34 = sitofp i64 %18 to float
%35 = fsub float %33, %34
%36 = fdiv float %32, %35
%37 = fmul float %36, 1.000000e+02
%38 = fcmp ogt float %37, 1.000000e+02
%39 = select i1 %38, float 1.000000e+02, float %37
ret float %39
}