#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, long *, long *,
                          long *, long *);
int /*<<< orphan*/ printf(char *, char *, long, long, long, long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
double cpuPcent(void) {
  FILE *fp;
  char nom[10];
  long v1, v2, v3, v4;
  long vp1, vp2, vp3, vp4;
  long d1, d2, d3, d4;
  double res = 0.;
  if ((fp = fopen("/proc/stat", "r")) == NULL)
    return (double)-1;
  fscanf(fp, "%s %ld %ld %ld %ld", nom, &v1, &v2, &v3, &v4);
  printf("%s %ld %ld %ld %ld\n", nom, v1, v2, v3, v4);
  sleep(1);
  rewind(fp);
  if ((fp = fopen("/proc/stat", "r")) == NULL)
    return (double)-1;
  fscanf(fp, "%s %ld %ld %ld %ld", nom, &vp1, &vp2, &vp3, &vp4);
  printf("%s %ld %ld %ld %ld\n", nom, vp1, vp2, vp3, vp4);
  d1 = vp1 - v1;
  d2 = vp2 - v2;
  d3 = vp3 - v3;
  d4 = vp4 - v4;
  res = (double)((d1 + d2 + d3) * 100L) / (double)(d1 + d2 + d3 + d4);
  return res;
}


x86 assembly (clang, -O0):
<cpuPcent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
lea    0xe9b(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1199 <cpuPcent+0x49>
movsd  0xe71(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12e2 <cpuPcent+0x192>
mov    -0x10(%rbp),%rdi
lea    -0x1a(%rbp),%rdx
lea    0xe6d(%rip),%rsi
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
lea    -0x38(%rbp),%r9
lea    -0x40(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <fscanf@plt>
lea    -0x1a(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x40(%rbp),%r9
lea    0xe4c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
lea    0xe0d(%rip),%rdi
lea    0xe11(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1227 <cpuPcent+0xd7>
movsd  0xde3(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12e2 <cpuPcent+0x192>
mov    -0x10(%rbp),%rdi
lea    -0x1a(%rbp),%rdx
lea    0xddf(%rip),%rsi
lea    -0x48(%rbp),%rcx
lea    -0x50(%rbp),%r8
lea    -0x58(%rbp),%r9
lea    -0x60(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <fscanf@plt>
lea    -0x1a(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x60(%rbp),%r9
lea    0xdbe(%rip),%rdi
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x50(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x58(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x60(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x68(%rbp),%rax
add    -0x70(%rbp),%rax
add    -0x78(%rbp),%rax
imul   $0x64,%rax,%rax
cvtsi2sd %rax,%xmm0
mov    -0x68(%rbp),%rax
add    -0x70(%rbp),%rax
add    -0x78(%rbp),%rax
add    -0x80(%rbp),%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuPcent>:
push   %r14
push   %rbx
sub    $0x58,%rsp
lea    0xeaa(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
movsd  0xe8e(%rip),%xmm0
test   %rax,%rax
je     129e <cpuPcent+0x14e>
mov    %rax,%rbx
lea    0x28(%rsp),%rax
mov    %rax,(%rsp)
lea    0xe87(%rip),%rsi
lea    0x4e(%rsp),%r14
lea    0x40(%rsp),%rcx
lea    0x38(%rsp),%r8
lea    0x30(%rsp),%r9
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <fscanf@plt>
mov    0x40(%rsp),%rdx
mov    0x38(%rsp),%rcx
mov    0x30(%rsp),%r8
mov    0x28(%rsp),%r9
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
call   1040 <rewind@plt>
lea    0xe1f(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1060 <fopen@plt>
movsd  0xe03(%rip),%xmm0
test   %rax,%rax
je     129e <cpuPcent+0x14e>
lea    0x8(%rsp),%rcx
mov    %rcx,(%rsp)
lea    0xdff(%rip),%rsi
lea    0x4e(%rsp),%rbx
lea    0x20(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %rax,%rdi
mov    %rbx,%rdx
call   1050 <fscanf@plt>
mov    0x20(%rsp),%rdx
mov    0x18(%rsp),%rcx
mov    0x10(%rsp),%r8
mov    0x8(%rsp),%r9
lea    0xdd8(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x20(%rsp),%rax
mov    0x10(%rsp),%rcx
sub    0x30(%rsp),%rcx
mov    0x8(%rsp),%rdx
sub    0x28(%rsp),%rdx
add    0x18(%rsp),%rax
mov    0x40(%rsp),%rsi
add    0x38(%rsp),%rsi
sub    %rsi,%rax
add    %rcx,%rax
imul   $0x64,%rax,%rcx
xorps  %xmm0,%xmm0
cvtsi2sd %rcx,%xmm0
add    %rdx,%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"%s %ld %ld %ld %ld\00"
@.str.3 = [20 x i8] c"%s %ld %ld %ld %ld\0A\00"
define double @cpuPcent() {
%1 = alloca double
%2 = alloca i32*
%3 = alloca [10 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 i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca double
store double 0.000000e+00, double* %16
%17 = 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* %17, i32** %2
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %0
store double -1.000000e+00, double* %1
br label %75
20: ; preds = %0
%21 = load i32*, i32** %2
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%23 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %22, i64* %4, i64* %5, i64* %6, i64* %7)
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%25 = load i64, i64* %4
%26 = load i64, i64* %5
%27 = load i64, i64* %6
%28 = load i64, i64* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %24, i64 %25, i64 %26, i64 %27, i64 %28)
%30 = call i32 @sleep(i32 1)
%31 = load i32*, i32** %2
%32 = call i32 @rewind(i32* %31)
%33 = 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* %33, i32** %2
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %20
store double -1.000000e+00, double* %1
br label %75
36: ; preds = %20
%37 = load i32*, i32** %2
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%39 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %38, i64* %8, i64* %9, i64* %10, i64* %11)
%40 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%41 = load i64, i64* %8
%42 = load i64, i64* %9
%43 = load i64, i64* %10
%44 = load i64, i64* %11
%45 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %40, i64 %41, i64 %42, i64 %43, i64 %44)
%46 = load i64, i64* %8
%47 = load i64, i64* %4
%48 = sub nsw i64 %46, %47
store i64 %48, i64* %12
%49 = load i64, i64* %9
%50 = load i64, i64* %5
%51 = sub nsw i64 %49, %50
store i64 %51, i64* %13
%52 = load i64, i64* %10
%53 = load i64, i64* %6
%54 = sub nsw i64 %52, %53
store i64 %54, i64* %14
%55 = load i64, i64* %11
%56 = load i64, i64* %7
%57 = sub nsw i64 %55, %56
store i64 %57, i64* %15
%58 = load i64, i64* %12
%59 = load i64, i64* %13
%60 = add nsw i64 %58, %59
%61 = load i64, i64* %14
%62 = add nsw i64 %60, %61
%63 = mul nsw i64 %62, 100
%64 = sitofp i64 %63 to double
%65 = load i64, i64* %12
%66 = load i64, i64* %13
%67 = add nsw i64 %65, %66
%68 = load i64, i64* %14
%69 = add nsw i64 %67, %68
%70 = load i64, i64* %15
%71 = add nsw i64 %69, %70
%72 = sitofp i64 %71 to double
%73 = fdiv double %64, %72
store double %73, double* %16
%74 = load double, double* %16
store double %74, double* %1
br label %75
75: ; preds = %36, %35, %19
%76 = load double, double* %1
ret double %76
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"%s %ld %ld %ld %ld\00"
@.str.3 = [20 x i8] c"%s %ld %ld %ld %ld\0A\00"
define double @cpuPcent() {
%1 = alloca [10 x i8]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%11 = bitcast i64* %2 to i8*
%12 = bitcast i64* %3 to i8*
%13 = bitcast i64* %4 to i8*
%14 = bitcast i64* %5 to i8*
%15 = bitcast i64* %6 to i8*
%16 = bitcast i64* %7 to i8*
%17 = bitcast i64* %8 to i8*
%18 = bitcast i64* %9 to i8*
%19 = 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))
%20 = icmp eq i32* %19, null
br i1 %20, label %58, label %21
21: ; preds = %0
%22 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %10, i64* %2, i64* %3, i64* %4, i64* %5)
%23 = load i64, i64* %2
%24 = load i64, i64* %3
%25 = load i64, i64* %4
%26 = load i64, i64* %5
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %10, i64 %23, i64 %24, i64 %25, i64 %26)
%28 = call i32 @sleep(i32 1)
%29 = call i32 @rewind(i32* %19)
%30 = 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))
%31 = icmp eq i32* %30, null
br i1 %31, label %58, label %32
32: ; preds = %21
%33 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %10, i64* %6, i64* %7, i64* %8, i64* %9)
%34 = load i64, i64* %6
%35 = load i64, i64* %7
%36 = load i64, i64* %8
%37 = load i64, i64* %9
%38 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %10, i64 %34, i64 %35, i64 %36, i64 %37)
%39 = load i64, i64* %6
%40 = load i64, i64* %2
%41 = load i64, i64* %7
%42 = load i64, i64* %3
%43 = load i64, i64* %8
%44 = load i64, i64* %4
%45 = sub nsw i64 %43, %44
%46 = load i64, i64* %9
%47 = load i64, i64* %5
%48 = sub i64 %46, %47
%49 = add i64 %39, %41
%50 = add i64 %40, %42
%51 = sub i64 %49, %50
%52 = add i64 %51, %45
%53 = mul nsw i64 %52, 100
%54 = sitofp i64 %53 to double
%55 = add nsw i64 %48, %52
%56 = sitofp i64 %55 to double
%57 = fdiv double %54, %56
br label %58
58: ; preds = %21, %0, %32
%59 = phi double [ %57, %32 ], [ -1.000000e+00, %0 ], [ -1.000000e+00, %21 ]
ret double %59
}