#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 *, double *, double *, double *);
int /*<<< orphan*/ sprintf(char *, char *, double, double, double);
int /*<<< orphan*/ strcpy(char *, char *);
int cpuload(char *str) {
  double l[3] = {-1, -1, -1};
  FILE *fp;
  if ((fp = fopen("/proc/loadavg", "r"))) {
    if (fscanf(fp, "%lf %lf %lf", &l[0], &l[1], &l[2]) != 3)
      l[0] = -1;
    fclose(fp);
  }
  if (str) {
    if (l[0] != -1)
      sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
    else
      strcpy(str, " (unknown) ");
  }
  return (int)l[0];
}


x86 assembly (clang, -O0):
<cpuload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0xead(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeaa(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xea7(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xea4(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11e1 <cpuload+0x91>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x20(%rbp),%rcx
add    $0x8,%rcx
lea    -0x20(%rbp),%r8
add    $0x10,%r8
lea    0xe7b(%rip),%rsi
call   1050 <fscanf@plt>
cmp    $0x3,%eax
je     11d8 <cpuload+0x88>
movsd  0xe2d(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x8(%rbp)
je     1247 <cpuload+0xf7>
movsd  -0x20(%rbp),%xmm0
movsd  0xe07(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    120e <cpuload+0xbe>
jp     120e <cpuload+0xbe>
jmp    1232 <cpuload+0xe2>
mov    -0x8(%rbp),%rdi
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
lea    0xe1c(%rip),%rsi
call   1070 <sprintf@plt>
jmp    1242 <cpuload+0xf2>
mov    -0x8(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1247 <cpuload+0xf7>
cvttsd2si -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuload>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0xebf(%rip),%rax
mov    %rax,0x10(%rsp)
movapd 0xea2(%rip),%xmm0
movapd %xmm0,(%rsp)
lea    0xeae(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c5 <cpuload+0x75>
mov    %rax,%rbx
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xe99(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x3,%eax
je     11bd <cpuload+0x6d>
movabs $0xbff0000000000000,%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     1207 <cpuload+0xb7>
movsd  (%rsp),%xmm0
ucomisd 0xe29(%rip),%xmm0
jne    11db <cpuload+0x8b>
jnp    11f8 <cpuload+0xa8>
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
lea    0xe56(%rip),%rsi
mov    %r14,%rdi
call   1070 <sprintf@plt>
jmp    1207 <cpuload+0xb7>
lea    0xe55(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
cvttsd2si (%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.cpuload.l = [3 x double] [double -1.000000e+00, double -1.000000e+00, double -1.000000e+00]
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%lf %lf %lf\00"
@.str.3 = [16 x i8] c" %.2f %.2f %.2f\00"
@.str.4 = [12 x i8] c" (unknown) \00"
define i32 @cpuload(i8* %0) {
%2 = alloca i8*
%3 = alloca [3 x double]
%4 = alloca i32*
store i8* %0, i8** %2
%5 = bitcast [3 x double]* %3 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %20
8: ; preds = %1
%9 = load i32*, i32** %4
%10 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%11 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 1
%12 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 2
%13 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), double* %10, double* %11, double* %12)
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
store double -1.000000e+00, double* %16
br label %17
17: ; preds = %15, %8
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %17, %1
%21 = load i8*, i8** %2
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%25 = load double, double* %24
%26 = fcmp une double %25, -1.000000e+00
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i8*, i8** %2
%29 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%30 = load double, double* %29
%31 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 1
%32 = load double, double* %31
%33 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 2
%34 = load double, double* %33
%35 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), double %30, double %32, double %34)
br label %39
36: ; preds = %23
%37 = load i8*, i8** %2
%38 = call i32 @strcpy(i8* %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %36, %27
br label %40
40: ; preds = %39, %20
%41 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%42 = load double, double* %41
%43 = fptosi double %42 to i32
ret i32 %43
}

LLVM IR (-O2):
@__const.cpuload.l = [3 x double] [double -1.000000e+00, double -1.000000e+00, double -1.000000e+00]
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"%lf %lf %lf\00"
@.str.3 = [16 x i8] c" %.2f %.2f %.2f\00"
@.str.4 = [12 x i8] c" (unknown) \00"
define i32 @cpuload(i8* %0) {
%2 = alloca [3 x double]
%3 = bitcast [3 x double]* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 0
%8 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 1
%9 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 2
%10 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), double* %7, double* %8, double* %9)
%11 = icmp eq i32 %10, 3
br i1 %11, label %13, label %12
12: ; preds = %6
store double -1.000000e+00, double* %7
br label %13
13: ; preds = %12, %6
%14 = call i32 @fclose(i32* %4)
br label %15
15: ; preds = %13, %1
%16 = icmp eq i8* %0, null
br i1 %16, label %29, label %17
17: ; preds = %15
%18 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 0
%19 = load double, double* %18
%20 = fcmp une double %19, -1.000000e+00
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 1
%23 = load double, double* %22
%24 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 2
%25 = load double, double* %24
%26 = call i32 @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), double %19, double %23, double %25)
br label %29
27: ; preds = %17
%28 = call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %21, %27, %15
%30 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 0
%31 = load double, double* %30
%32 = fptosi double %31 to i32
ret i32 %32
}