#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, double *, double *, double *);
int load_check(double *current_load) {
  FILE *fp;
  char loadavg[50];
  int bytes = 0;
  double avg_1 = 0, avg_5 = 0, avg_15 = 0;
  if ((fp = fopen("/proc/loadavg", "r")) == NULL)
    return 0;
  if (!(bytes = fread(loadavg, sizeof(char), 49, fp))) {
    printf("Error reading load average\n");
    fclose(fp);
    return 0;
  }
  loadavg[bytes] = 0;
  fclose(fp);
  if (sscanf(loadavg, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
    printf("Error getting load average\n");
    return 0;
  }
  *current_load = avg_1;
  return 1;
}


x86 assembly (clang, -O0):
<load_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x54(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x68(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x70(%rbp)
lea    0xe7e(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a8 <load_check+0x58>
movl   $0x0,-0x4(%rbp)
jmp    124f <load_check+0xff>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x31,%edx
call   1030 <fread@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jne    11ec <load_check+0x9c>
lea    0xe3e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <load_check+0xff>
movslq -0x54(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x50(%rbp),%rdi
lea    0xe23(%rip),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
lea    -0x70(%rbp),%r8
call   1060 <sscanf@plt>
cmp    $0x3,%eax
jge    123b <load_check+0xeb>
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124f <load_check+0xff>
movsd  -0x60(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_check>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe83(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     121a <load_check+0xca>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
mov    $0x31,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     11f4 <load_check+0xa4>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe48(%rip),%rsi
lea    0x20(%rsp),%rdi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1060 <sscanf@plt>
cmp    $0x2,%eax
jg     120a <load_check+0xba>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
jmp    121a <load_check+0xca>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    121a <load_check+0xca>
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"Error reading load average\0A\00"
@.str.3 = [12 x i8] c"%lf %lf %lf\00"
@.str.4 = [28 x i8] c"Error getting load average\0A\00"
define i32 @load_check(double* %0) {
%2 = alloca i32
%3 = alloca double*
%4 = alloca i32*
%5 = alloca [50 x i8]
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
store double* %0, double** %3
store i32 0, i32* %6
store double 0.000000e+00, double* %7
store double 0.000000e+00, double* %8
store double 0.000000e+00, double* %9
%10 = 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* %10, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %2
br label %36
13: ; preds = %1
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i32 @fread(i8* %14, i32 1, i32 49, i32* %15)
store i32 %16, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %2
br label %36
22: ; preds = %13
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i32*, i32** %4
%27 = call i32 @fclose(i32* %26)
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double* %7, double* %8, double* %9)
%30 = icmp slt i32 %29, 3
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %36
33: ; preds = %22
%34 = load double, double* %7
%35 = load double*, double** %3
store double %34, double* %35
store i32 1, i32* %2
br label %36
36: ; preds = %33, %31, %18, %12
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [12 x i8] c"%lf %lf %lf\00"
@str = [27 x i8] c"Error reading load average\00"
@str.5 = [27 x i8] c"Error getting load average\00"
define i32 @load_check(double* nocapture writeonly %0) {
%2 = alloca [50 x i8]
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%7 = bitcast double* %3 to i8*
store double 0.000000e+00, double* %3
%8 = bitcast double* %4 to i8*
store double 0.000000e+00, double* %4
%9 = bitcast double* %5 to i8*
store double 0.000000e+00, double* %5
%10 = 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))
%11 = icmp eq i32* %10, null
br i1 %11, label %28, label %12
12: ; preds = %1
%13 = call i32 @fread(i8* %6, i32 1, i32 49, i32* %10)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %10)
br label %28
18: ; preds = %12
%19 = sext i32 %13 to i64
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %19
store i8 0, i8* %20
%21 = tail call i32 @fclose(i32* %10)
%22 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double* %3, double* %4, double* %5)
%23 = icmp slt i32 %22, 3
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %28
26: ; preds = %18
%27 = load double, double* %3
store double %27, double* %0
br label %28
28: ; preds = %1, %26, %24, %15
%29 = phi i32 [ 0, %24 ], [ 1, %26 ], [ 0, %15 ], [ 0, %1 ]
ret i32 %29
}