#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 *, long *, long *, long *,
                          long *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, double);
void cpu_util(char *buf) {
  static long o0, o3;
  FILE *f;
  long n0, n1, n2, n3;
  f = fopen("/proc/stat", "r");
  rewind(f);
  fscanf(f, "%*s %ld %ld %ld %ld", &n0, &n1, &n2, &n3);
  n0 += n1 + n2;
  sprintf(buf, "%.1f", (100.0 * (n0 - o0)) / (n0 + n3 - o0 - o3));
  o0 = n0;
  o3 = n3;
  fclose(f);
}


x86 assembly (clang, -O0):
<cpu_util>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xea5(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1050 <fscanf@plt>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
sub    0x2e95(%rip),%rax
cvtsi2sd %rax,%xmm1
movsd  0xe38(%rip),%xmm0
mulsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
add    -0x30(%rbp),%rax
sub    0x2e75(%rip),%rax
sub    0x2e76(%rip),%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
lea    0xe37(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,0x2e4e(%rip)
mov    -0x30(%rbp),%rax
mov    %rax,0x2e4b(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_util>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0xea7(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <rewind@plt>
lea    0xe96(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
mov    %rbx,%rdi
call   1050 <fscanf@plt>
mov    0x18(%rsp),%rax
add    0x20(%rsp),%rax
add    0x8(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x2e9a(%rip),%rcx
mov    %rax,%rdx
sub    %rcx,%rdx
cvtsi2sd %rdx,%xmm0
mulsd  0xe37(%rip),%xmm0
add    0x10(%rsp),%rax
add    0x2e83(%rip),%rcx
sub    %rcx,%rax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
call   1070 <sprintf@plt>
mov    0x8(%rsp),%rax
mov    %rax,0x2e54(%rip)
mov    0x10(%rsp),%rax
mov    %rax,0x2e50(%rip)
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@cpu_util.o0 = internal global i64 0
@cpu_util.o3 = internal global i64 0
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"%*s %ld %ld %ld %ld\00"
@.str.3 = [5 x i8] c"%.1f\00"
define void @cpu_util(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %2
%8 = 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* %8, i32** %3
%9 = load i32*, i32** %3
%10 = call i32 @rewind(i32* %9)
%11 = load i32*, i32** %3
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64* %4, i64* %5, i64* %6, i64* %7)
%13 = load i64, i64* %5
%14 = load i64, i64* %6
%15 = add nsw i64 %13, %14
%16 = load i64, i64* %4
%17 = add nsw i64 %16, %15
store i64 %17, i64* %4
%18 = load i8*, i8** %2
%19 = load i64, i64* %4
%20 = load i64, i64* @cpu_util.o0
%21 = sub nsw i64 %19, %20
%22 = sitofp i64 %21 to double
%23 = fmul double 1.000000e+02, %22
%24 = load i64, i64* %4
%25 = load i64, i64* %7
%26 = add nsw i64 %24, %25
%27 = load i64, i64* @cpu_util.o0
%28 = sub nsw i64 %26, %27
%29 = load i64, i64* @cpu_util.o3
%30 = sub nsw i64 %28, %29
%31 = sitofp i64 %30 to double
%32 = fdiv double %23, %31
%33 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), double %32)
%34 = load i64, i64* %4
store i64 %34, i64* @cpu_util.o0
%35 = load i64, i64* %7
store i64 %35, i64* @cpu_util.o3
%36 = load i32*, i32** %3
%37 = call i32 @fclose(i32* %36)
ret void
}

LLVM IR (-O2):
@cpu_util.o0 = internal unnamed_addr global i64 0
@cpu_util.o3 = internal unnamed_addr global i64 0
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"%*s %ld %ld %ld %ld\00"
@.str.3 = [5 x i8] c"%.1f\00"
define void @cpu_util(i8* nocapture writeonly %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = 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))
%11 = tail call i32 @rewind(i32* %10)
%12 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64* %2, i64* %3, i64* %4, i64* %5)
%13 = load i64, i64* %3
%14 = load i64, i64* %4
%15 = add nsw i64 %14, %13
%16 = load i64, i64* %2
%17 = add nsw i64 %15, %16
store i64 %17, i64* %2
%18 = load i64, i64* @cpu_util.o0
%19 = sub nsw i64 %17, %18
%20 = sitofp i64 %19 to double
%21 = fmul double %20, 1.000000e+02
%22 = load i64, i64* %5
%23 = load i64, i64* @cpu_util.o3
%24 = add i64 %17, %22
%25 = add i64 %18, %23
%26 = sub i64 %24, %25
%27 = sitofp i64 %26 to double
%28 = fdiv double %21, %27
%29 = call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), double %28)
%30 = load i64, i64* %2
store i64 %30, i64* @cpu_util.o0
%31 = load i64, i64* %5
store i64 %31, i64* @cpu_util.o3
%32 = call i32 @fclose(i32* %10)
ret void
}