#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 fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *, int *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
void put_cpu_usage(char *out, void *arg) {
  static int prev_total = 0;
  static int prev_idle = 0;
  int curr_user = 0, curr_nice = 0, curr_system = 0, curr_idle = 0, curr_total;
  int diff_idle, diff_total, diff_usage;
  FILE *stat = fopen("/proc/stat", "r");
  if (fscanf(stat, "cpu %d %d %d %d", &curr_user, &curr_nice, &curr_system,
             &curr_idle) != 4) {
    sprintf(out, "??%%");
    fclose(stat);
    return;
  }
  fclose(stat);
  curr_total = curr_user + curr_nice + curr_system + curr_idle;
  diff_idle = curr_idle - prev_idle;
  diff_total = curr_total - prev_total;
  diff_usage =
      (diff_total ? (1000 * (diff_total - diff_idle) / diff_total + 5) / 10
                  : 0);
  prev_total = curr_total;
  prev_idle = curr_idle;
  sprintf(out, "%2d%%", diff_usage);
}


x86 assembly (clang, -O0):
<put_cpu_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe7f(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
lea    -0x20(%rbp),%r9
call   1040 <fscanf@plt>
cmp    $0x4,%eax
je     11cc <put_cpu_usage+0x8c>
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1260 <put_cpu_usage+0x120>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
sub    0x2e5b(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
sub    0x2e4b(%rip),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1229 <put_cpu_usage+0xe9>
mov    -0x2c(%rbp),%eax
sub    -0x28(%rbp),%eax
imul   $0x3e8,%eax,%eax
cltd
idivl  -0x2c(%rbp)
add    $0x5,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
jmp    1233 <put_cpu_usage+0xf3>
xor    %eax,%eax
mov    %eax,-0x3c(%rbp)
jmp    1233 <put_cpu_usage+0xf3>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,0x2e02(%rip)
mov    -0x20(%rbp),%eax
mov    %eax,0x2dfd(%rip)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_cpu_usage>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xe8f(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xe86(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x4,%eax
jne    11fa <put_cpu_usage+0xba>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%ecx
add    0x14(%rsp),%ecx
add    0xc(%rsp),%ecx
mov    0x8(%rsp),%esi
add    %esi,%ecx
mov    %ecx,%edi
sub    0x2e7a(%rip),%edi
je     1215 <put_cpu_usage+0xd5>
mov    %edi,%eax
sub    %esi,%eax
add    0x2e72(%rip),%eax
imul   $0x3e8,%eax,%eax
cltd
idiv   %edi
add    $0x5,%eax
cltq
imul   $0x66666667,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x22,%rdx
add    %eax,%edx
jmp    1217 <put_cpu_usage+0xd7>
lea    0xe1c(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1234 <put_cpu_usage+0xf4>
xor    %edx,%edx
mov    %ecx,0x2e27(%rip)
mov    %esi,0x2e25(%rip)
lea    0xdf8(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@put_cpu_usage.prev_total = internal global i32 0
@put_cpu_usage.prev_idle = internal global i32 0
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"cpu %d %d %d %d\00"
@.str.3 = [5 x i8] c"??%%\00"
@.str.4 = [6 x i8] c"%2d%%\00"
define void @put_cpu_usage(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%14 = 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* %14, i32** %13
%15 = load i32*, i32** %13
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32* %5, i32* %6, i32* %7, i32* %8)
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8*, i8** %3
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32*, i32** %13
%22 = call i32 @fclose(i32* %21)
br label %58
23: ; preds = %2
%24 = load i32*, i32** %13
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = add nsw i32 %26, %27
%29 = load i32, i32* %7
%30 = add nsw i32 %28, %29
%31 = load i32, i32* %8
%32 = add nsw i32 %30, %31
store i32 %32, i32* %9
%33 = load i32, i32* %8
%34 = load i32, i32* @put_cpu_usage.prev_idle
%35 = sub nsw i32 %33, %34
store i32 %35, i32* %10
%36 = load i32, i32* %9
%37 = load i32, i32* @put_cpu_usage.prev_total
%38 = sub nsw i32 %36, %37
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %23
%42 = load i32, i32* %11
%43 = load i32, i32* %10
%44 = sub nsw i32 %42, %43
%45 = mul nsw i32 1000, %44
%46 = load i32, i32* %11
%47 = sdiv i32 %45, %46
%48 = add nsw i32 %47, 5
%49 = sdiv i32 %48, 10
br label %51
50: ; preds = %23
br label %51
51: ; preds = %50, %41
%52 = phi i32 [ %49, %41 ], [ 0, %50 ]
store i32 %52, i32* %12
%53 = load i32, i32* %9
store i32 %53, i32* @put_cpu_usage.prev_total
%54 = load i32, i32* %8
store i32 %54, i32* @put_cpu_usage.prev_idle
%55 = load i8*, i8** %3
%56 = load i32, i32* %12
%57 = call i32 (i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %51, %18
ret void
}

LLVM IR (-O2):
@put_cpu_usage.prev_total = internal unnamed_addr global i32 0
@put_cpu_usage.prev_idle = internal unnamed_addr global i32 0
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"cpu %d %d %d %d\00"
@.str.3 = [5 x i8] c"??%%\00"
@.str.4 = [6 x i8] c"%2d%%\00"
define void @put_cpu_usage(i8* nocapture writeonly %0, i8* nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%11 = 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))
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4, i32* %5, i32* %6)
%13 = icmp eq i32 %12, 4
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @fclose(i32* %11)
br label %40
17: ; preds = %2
%18 = call i32 @fclose(i32* %11)
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = add nsw i32 %20, %19
%22 = load i32, i32* %5
%23 = add nsw i32 %21, %22
%24 = load i32, i32* %6
%25 = add nsw i32 %23, %24
%26 = load i32, i32* @put_cpu_usage.prev_total
%27 = sub nsw i32 %25, %26
%28 = icmp eq i32 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %17
%30 = load i32, i32* @put_cpu_usage.prev_idle
%31 = sub i32 %27, %24
%32 = add i32 %31, %30
%33 = mul nsw i32 %32, 1000
%34 = sdiv i32 %33, %27
%35 = add nsw i32 %34, 5
%36 = sdiv i32 %35, 10
br label %37
37: ; preds = %17, %29
%38 = phi i32 [ %36, %29 ], [ 0, %17 ]
store i32 %25, i32* @put_cpu_usage.prev_total
store i32 %24, i32* @put_cpu_usage.prev_idle
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %37, %14
ret void
}