#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, double *, double *, double *);
int getloadavg(double loadavg[], int nelem) {
  double load_averages[3];
  FILE *fp;
  int count;
  int i;
  int nc;
  fp = fopen("/proc/loadavg", "r");
  if (fp == NULL)
    return -1;
  count = fscanf(fp, "%lf%lf%lf", &(load_averages[0]), &(load_averages[1]),
                 &(load_averages[2]));
  fclose(fp);
  if ((count == EOF) || (count == 0))
    return -1;
  nc = ((nelem < count) ? nelem : count);
  for (i = 0; i < nc; i++)
    loadavg[i] = load_averages[i];
  return nc;
}


x86 assembly (clang, -O0):
<getloadavg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeba(%rip),%rdi
lea    0xec1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    116d <getloadavg+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <getloadavg+0xf6>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x30(%rbp),%rcx
add    $0x8,%rcx
lea    -0x30(%rbp),%r8
add    $0x10,%r8
lea    0xe84(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%eax
je     11b9 <getloadavg+0x89>
cmpl   $0x0,-0x3c(%rbp)
jne    11c5 <getloadavg+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <getloadavg+0xf6>
mov    -0x14(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    11dc <getloadavg+0xac>
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    11e2 <getloadavg+0xb2>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    1220 <getloadavg+0xf0>
movslq -0x40(%rbp),%rax
movsd  -0x30(%rbp,%rax,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11ef <getloadavg+0xbf>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getloadavg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xea7(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     11c7 <getloadavg+0x87>
mov    %rax,%rbp
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xe8c(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %ebx,%ebx
je     11c7 <getloadavg+0x87>
mov    0x2e44(%rip),%rax
cmp    (%rax),%ebx
je     11c7 <getloadavg+0x87>
cmp    %r15d,%ebx
cmovg  %r15d,%ebx
test   %ebx,%ebx
jle    11c4 <getloadavg+0x84>
mov    %ebx,%edx
shl    $0x3,%rdx
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    %ebx,%r12d
mov    %r12d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [10 x i8] c"%lf%lf%lf\00"
@EOF = global i32 0
define i32 @getloadavg(double* nocapture writeonly %0, i32 %1) {
%3 = bitcast double* %0 to i8*
%4 = alloca [3 x double]
%5 = bitcast [3 x double]* %4 to i8*
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 0
%10 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 1
%11 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 2
%12 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), double* %9, double* %10, double* %11)
%13 = call i32 @fclose(i32* %6)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %12, %14
%16 = icmp eq i32 %12, 0
%17 = or i1 %16, %15
br i1 %17, label %25, label %18
18: ; preds = %8
%19 = icmp sgt i32 %12, %1
%20 = select i1 %19, i32 %1, i32 %12
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = zext i32 %20 to i64
%24 = shl nuw nsw i64 %23, 3
br label %25
25: ; preds = %22, %18, %8, %2
%26 = phi i32 [ -1, %2 ], [ -1, %8 ], [ %20, %18 ], [ %20, %22 ]
ret i32 %26
}