#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*/ memcpy(double *, double *, int);
int getloadavg(double *a, int n) {
  int i;
  double b[3];
  FILE *f = fopen("/proc/loadavg", "rbe");
  if (!f)
    return -1;
  i = fscanf(f, "%lf %lf %lf", b, b + 1, b + 2);
  fclose(f);
  if (n > i)
    n = i;
  if (n < 0)
    return -1;
  memcpy(a, b, n * sizeof *a);
  return n;
}


x86 assembly (clang, -O0):
<getloadavg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    117d <getloadavg+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <getloadavg+0xb2>
mov    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x30(%rbp),%rcx
add    $0x8,%rcx
lea    -0x30(%rbp),%r8
add    $0x10,%r8
lea    0xe76(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11bf <getloadavg+0x7f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d5 <getloadavg+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <getloadavg+0xb2>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getloadavg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xea7(%rip),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11bb <getloadavg+0x7b>
mov    %rax,%rbp
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0xe8e(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
cmp    %r12d,%ebx
cmovl  %ebx,%r12d
test   %r12d,%r12d
js     11bb <getloadavg+0x7b>
lea    0x0(,%r12,8),%edx
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <memcpy@plt>
mov    %r12d,%r15d
mov    %r15d,%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 = [4 x i8] c"rbe\00"
@.str.2 = [12 x i8] c"%lf %lf %lf\00"
define i32 @getloadavg(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [3 x double]
%8 = alloca i32*
store double* %0, double** %4
store i32 %1, i32* %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -1, i32* %3
br label %41
13: ; preds = %2
%14 = load i32*, i32** %8
%15 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%16 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%17 = getelementptr inbounds double, double* %16, i64 1
%18 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%19 = getelementptr inbounds double, double* %18, i64 2
%20 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), double* %15, double* %17, double* %19)
store i32 %20, i32* %6
%21 = load i32*, i32** %8
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = load i32, i32* %6
store i32 %27, i32* %5
br label %28
28: ; preds = %26, %13
%29 = load i32, i32* %5
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %3
br label %41
32: ; preds = %28
%33 = load double*, double** %4
%34 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 8
%38 = trunc i64 %37 to i32
%39 = call i32 @memcpy(double* %33, double* %34, i32 %38)
%40 = load i32, i32* %5
store i32 %40, i32* %3
br label %41
41: ; preds = %32, %31, %12
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/loadavg\00"
@.str.1 = [4 x i8] c"rbe\00"
@.str.2 = [12 x i8] c"%lf %lf %lf\00"
define i32 @getloadavg(double* %0, i32 %1) {
%3 = alloca [3 x double]
%4 = bitcast [3 x double]* %3 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %2
%8 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%9 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 1
%10 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 2
%11 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), double* %8, double* %9, double* %10)
%12 = call i32 @fclose(i32* %5)
%13 = icmp slt i32 %11, %1
%14 = select i1 %13, i32 %11, i32 %1
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %7
%17 = shl i32 %14, 3
%18 = call i32 @memcpy(double* %0, double* %8, i32 %17)
br label %19
19: ; preds = %7, %2, %16
%20 = phi i32 [ %14, %16 ], [ -1, %2 ], [ -1, %7 ]
ret i32 %20
}