#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*/ fprintf(int /*<<< orphan*/, char *);
int fscanf(int /*<<< orphan*/ *, char *, double *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
double loadAverage() {
  FILE *fp = fopen("/proc/loadavg", "rt");
  if (fp == NULL) {
    perror("can't open /proc/loadavg");
    return 0;
  }
  double loadavg = 0;
  if (fscanf(fp, "%lf", &loadavg) != 1) {
    fprintf(stderr, "warning: problem with loadavg\n");
  }
  fclose(fp);
  return loadavg;
}


x86 assembly (clang, -O0):
<loadAverage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeaf(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <loadAverage+0x43>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11e0 <loadAverage+0x90>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11cd <loadAverage+0x7d>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadAverage>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb2(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bb <loadAverage+0x6b>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
lea    0xe9c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11a7 <loadAverage+0x57>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
movsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
xorps  %xmm0,%xmm0
jmp    11b5 <loadAverage+0x65>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [25 x i8] c"can't open /proc/loadavg\00"
@.str.3 = [4 x i8] c"%lf\00"
@stderr = global i32 0
@.str.4 = [31 x i8] c"warning: problem with loadavg\0A\00"
define double @loadAverage() {
%1 = alloca double
%2 = alloca i32*
%3 = alloca double
%4 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store double 0.000000e+00, double* %1
br label %20
9: ; preds = %0
store double 0.000000e+00, double* %3
%10 = load i32*, i32** %2
%11 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double* %3)
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %13, %9
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = load double, double* %3
store double %19, double* %1
br label %20
20: ; preds = %16, %7
%21 = load double, double* %1
ret double %21
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [25 x i8] c"can't open /proc/loadavg\00"
@.str.3 = [4 x i8] c"%lf\00"
@stderr = global i32 0
@.str.4 = [31 x i8] c"warning: problem with loadavg\0A\00"
define double @loadAverage() {
%1 = alloca double
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %16
6: ; preds = %0
%7 = bitcast double* %1 to i8*
store double 0.000000e+00, double* %1
%8 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double* %1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %10, %6
%14 = call i32 @fclose(i32* %2)
%15 = load double, double* %1
br label %16
16: ; preds = %13, %4
%17 = phi double [ 0.000000e+00, %4 ], [ %15, %13 ]
ret double %17
}