#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 time_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *,
                          unsigned long long *, unsigned long long *,
                          unsigned long long *, unsigned long long *);
int /*<<< orphan*/ time(int *);
int check_idle() {
  FILE *fd;
  char statline[128], cputext[6];
  unsigned long long user, nice, sys, idle, total;
  static unsigned long long ouser, onice, osys, oidle;
  static time_t lastchecked = 0;
  static int lastidle = 50;
  time_t currenttime;
  int idleproc;
  time(&currenttime);
  if ((currenttime - lastchecked) < 1) {
    return (lastidle);
  }
  fd = fopen("/proc/stat", "r");
  fscanf(fd, "%s %llu %llu %llu %llu", cputext, &user, &nice, &sys, &idle);
  total = ((user - ouser) + (nice - onice) + (sys - osys) + (idle - oidle));
  if (total > 0) {
    idleproc = ((idle - oidle) * 100) / total;
  } else {
    idleproc = 100;
  }
  lastidle = idleproc;
  oidle = idle;
  ouser = user;
  osys = sys;
  onice = nice;
  fclose(fd);
  time(&lastchecked);
  return (idleproc);
}


x86 assembly (clang, -O0):
<check_idle>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
lea    -0xc4(%rbp),%rdi
call   1050 <time@plt>
mov    -0xc4(%rbp),%eax
sub    0x2f0d(%rip),%eax
cmp    $0x1,%eax
jge    117a <check_idle+0x3a>
mov    0x2ece(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    12b2 <check_idle+0x172>
lea    0xe7f(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x96(%rbp),%rdx
lea    0xe6a(%rip),%rsi
lea    -0xa0(%rbp),%rcx
lea    -0xa8(%rbp),%r8
lea    -0xb0(%rbp),%r9
lea    -0xb8(%rbp),%rax
mov    %rax,(%rsp)
call   1040 <fscanf@plt>
mov    -0xa0(%rbp),%rax
sub    0x2e7a(%rip),%rax
mov    -0xa8(%rbp),%rcx
sub    0x2e74(%rip),%rcx
add    %rcx,%rax
mov    -0xb0(%rbp),%rcx
sub    0x2e6b(%rip),%rcx
add    %rcx,%rax
mov    -0xb8(%rbp),%rcx
sub    0x2e62(%rip),%rcx
add    %rcx,%rax
mov    %rax,-0xc0(%rbp)
cmpq   $0x0,-0xc0(%rbp)
jbe    1246 <check_idle+0x106>
mov    -0xb8(%rbp),%rax
sub    0x2e3c(%rip),%rax
imul   $0x64,%rax,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0xc0(%rbp)
mov    %eax,-0xc8(%rbp)
jmp    1250 <check_idle+0x110>
movl   $0x64,-0xc8(%rbp)
mov    -0xc8(%rbp),%eax
mov    %eax,0x2de4(%rip)
mov    -0xb8(%rbp),%rax
mov    %rax,0x2dfe(%rip)
mov    -0xa0(%rbp),%rax
mov    %rax,0x2dd8(%rip)
mov    -0xb0(%rbp),%rax
mov    %rax,0x2dda(%rip)
mov    -0xa8(%rbp),%rax
mov    %rax,0x2dc4(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0x2dcc(%rip),%rdi
call   1050 <time@plt>
mov    -0xc8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_idle>:
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xc(%rsp),%rdi
call   1050 <time@plt>
mov    0xc(%rsp),%eax
cmp    0x2f15(%rip),%eax
jle    11ff <check_idle+0xbf>
lea    0xe98(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rax
mov    %rax,(%rsp)
lea    0xe86(%rip),%rsi
lea    0x32(%rsp),%rdx
lea    0x28(%rsp),%rcx
lea    0x20(%rsp),%r8
lea    0x18(%rsp),%r9
mov    %r14,%rdi
call   1040 <fscanf@plt>
mov    0x28(%rsp),%r8
mov    0x2ea1(%rip),%rdx
mov    0x20(%rsp),%rsi
mov    0x18(%rsp),%rdi
mov    0x10(%rsp),%rcx
mov    %rcx,%rax
sub    0x2ea0(%rip),%rax
lea    (%r8,%rsi,1),%rbx
add    0x2e85(%rip),%rdx
add    %rdi,%rbx
add    0x2e83(%rip),%rdx
sub    %rdx,%rbx
add    %rax,%rbx
je     1207 <check_idle+0xc7>
imul   $0x64,%rax,%rax
mov    %rax,%rdx
or     %rbx,%rdx
shr    $0x20,%rdx
je     120e <check_idle+0xce>
xor    %edx,%edx
div    %rbx
mov    %rax,%rbx
jmp    1214 <check_idle+0xd4>
mov    0x2e3b(%rip),%ebx
jmp    124a <check_idle+0x10a>
mov    $0x64,%ebx
jmp    1214 <check_idle+0xd4>
xor    %edx,%edx
div    %ebx
mov    %eax,%ebx
mov    %ebx,0x2e26(%rip)
mov    %rcx,0x2e47(%rip)
mov    %r8,0x2e28(%rip)
mov    %rdi,0x2e31(%rip)
mov    %rsi,0x2e22(%rip)
mov    %r14,%rdi
call   1030 <fclose@plt>
lea    0x2e2b(%rip),%rdi
call   1050 <time@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@check_idle.ouser = internal global i64 0
@check_idle.onice = internal global i64 0
@check_idle.osys = internal global i64 0
@check_idle.oidle = internal global i64 0
@check_idle.lastchecked = internal global i32 0
@check_idle.lastidle = internal global i32 50
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%s %llu %llu %llu %llu\00"
define i32 @check_idle() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca [6 x i8]
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = call i32 @time(i32* %10)
%13 = load i32, i32* %10
%14 = load i32, i32* @check_idle.lastchecked
%15 = sub nsw i32 %13, %14
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = load i32, i32* @check_idle.lastidle
store i32 %18, i32* %1
br label %60
19: ; preds = %0
%20 = 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* %20, i32** %2
%21 = load i32*, i32** %2
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%23 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %22, i64* %5, i64* %6, i64* %7, i64* %8)
%24 = load i64, i64* %5
%25 = load i64, i64* @check_idle.ouser
%26 = sub i64 %24, %25
%27 = load i64, i64* %6
%28 = load i64, i64* @check_idle.onice
%29 = sub i64 %27, %28
%30 = add i64 %26, %29
%31 = load i64, i64* %7
%32 = load i64, i64* @check_idle.osys
%33 = sub i64 %31, %32
%34 = add i64 %30, %33
%35 = load i64, i64* %8
%36 = load i64, i64* @check_idle.oidle
%37 = sub i64 %35, %36
%38 = add i64 %34, %37
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %19
%42 = load i64, i64* %8
%43 = load i64, i64* @check_idle.oidle
%44 = sub i64 %42, %43
%45 = mul i64 %44, 100
%46 = load i64, i64* %9
%47 = udiv i64 %45, %46
%48 = trunc i64 %47 to i32
store i32 %48, i32* %11
br label %50
49: ; preds = %19
store i32 100, i32* %11
br label %50
50: ; preds = %49, %41
%51 = load i32, i32* %11
store i32 %51, i32* @check_idle.lastidle
%52 = load i64, i64* %8
store i64 %52, i64* @check_idle.oidle
%53 = load i64, i64* %5
store i64 %53, i64* @check_idle.ouser
%54 = load i64, i64* %7
store i64 %54, i64* @check_idle.osys
%55 = load i64, i64* %6
store i64 %55, i64* @check_idle.onice
%56 = load i32*, i32** %2
%57 = call i32 @fclose(i32* %56)
%58 = call i32 @time(i32* @check_idle.lastchecked)
%59 = load i32, i32* %11
store i32 %59, i32* %1
br label %60
60: ; preds = %50, %17
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@check_idle.ouser = internal unnamed_addr global i64 0
@check_idle.onice = internal unnamed_addr global i64 0
@check_idle.osys = internal unnamed_addr global i64 0
@check_idle.oidle = internal unnamed_addr global i64 0
@check_idle.lastchecked = internal global i32 0
@check_idle.lastidle = internal unnamed_addr global i32 50
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"%s %llu %llu %llu %llu\00"
define i32 @check_idle() {
%1 = alloca [6 x i8]
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%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 = bitcast i32* %6 to i8*
%13 = call i32 @time(i32* %6)
%14 = load i32, i32* %6
%15 = load i32, i32* @check_idle.lastchecked
%16 = icmp sgt i32 %14, %15
br i1 %16, label %19, label %17
17: ; preds = %0
%18 = load i32, i32* @check_idle.lastidle
br label %46
19: ; preds = %0
%20 = 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))
%21 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %7, i64* %2, i64* %3, i64* %4, i64* %5)
%22 = load i64, i64* %2
%23 = load i64, i64* @check_idle.ouser
%24 = load i64, i64* %3
%25 = load i64, i64* @check_idle.onice
%26 = load i64, i64* %4
%27 = load i64, i64* @check_idle.osys
%28 = load i64, i64* %5
%29 = load i64, i64* @check_idle.oidle
%30 = sub i64 %28, %29
%31 = add i64 %22, %24
%32 = add i64 %23, %25
%33 = add i64 %31, %26
%34 = add i64 %32, %27
%35 = sub i64 %33, %34
%36 = add i64 %35, %30
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %19
%39 = mul i64 %30, 100
%40 = udiv i64 %39, %36
%41 = trunc i64 %40 to i32
br label %42
42: ; preds = %19, %38
%43 = phi i32 [ %41, %38 ], [ 100, %19 ]
store i32 %43, i32* @check_idle.lastidle
store i64 %28, i64* @check_idle.oidle
store i64 %22, i64* @check_idle.ouser
store i64 %26, i64* @check_idle.osys
store i64 %24, i64* @check_idle.onice
%44 = call i32 @fclose(i32* %20)
%45 = call i32 @time(i32* @check_idle.lastchecked)
br label %46
46: ; preds = %42, %17
%47 = phi i32 [ %18, %17 ], [ %43, %42 ]
ret i32 %47
}