#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int get_load_avg(char aveLoad[]) {
  int fd;
  int n, k = 0;
  int res = -1;
  char line_buf[50];
  if ((fd = open("/proc/loadavg", O_RDONLY)) == -1) {
    perror("fail to loadavg");
    exit(-1);
  }
  if ((n = read(fd, line_buf, 1024)) == -1) {
    perror("fail to read");
    goto err;
  }
  line_buf[n] = '/0';
  n = 0;
  while (line_buf[n] != '\n' && line_buf[n] != '\0') {
    aveLoad[k++] = line_buf[n++];
  }
  aveLoad[k] = '\0';
  res = 0;
err:
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<get_load_avg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a1 <get_load_avg+0x51>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11cf <get_load_avg+0x7f>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
jmp    1256 <get_load_avg+0x106>
movslq -0x10(%rbp),%rax
movb   $0x30,-0x50(%rbp,%rax,1)
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x51(%rbp)
je     1208 <get_load_avg+0xb8>
movslq -0x10(%rbp),%rax
movsbl -0x50(%rbp,%rax,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1218 <get_load_avg+0xc8>
jmp    1243 <get_load_avg+0xf3>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
cltq
mov    -0x50(%rbp,%rax,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11df <get_load_avg+0x8f>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_load_avg>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e1 <get_load_avg+0x91>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11ce <get_load_avg+0x7e>
cltq
movb   $0x30,(%rsp,%rax,1)
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rax,1),%ecx
test   %cl,%cl
je     11b6 <get_load_avg+0x66>
cmp    $0xa,%cl
je     11b6 <get_load_avg+0x66>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
jmp    11a0 <get_load_avg+0x50>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
xor    %ebx,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11be <get_load_avg+0x6e>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/loadavg\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"fail to loadavg\00"
@.str.2 = [13 x i8] c"fail to read\00"
define i32 @get_load_avg(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [50 x i8]
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 -1, i32* %6
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %1
%15 = load i32, i32* %3
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1024)
store i32 %17, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %57
21: ; preds = %14
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 %23
store i8 48, i8* %24
store i32 0, i32* %4
br label %25
25: ; preds = %41, %21
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %39
32: ; preds = %25
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 0
br label %39
39: ; preds = %32, %25
%40 = phi i1 [ false, %25 ], [ %38, %32 ]
br i1 %40, label %41, label %52
41: ; preds = %39
%42 = load i32, i32* %4
%43 = add nsw i32 %42, 1
store i32 %43, i32* %4
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = load i8*, i8** %2
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 %46, i8* %51
br label %25
52: ; preds = %39
%53 = load i8*, i8** %2
%54 = load i32, i32* %5
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
store i32 0, i32* %6
br label %57
57: ; preds = %52, %19
%58 = load i32, i32* %3
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %6
ret i32 %60
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/loadavg\00"
@O_RDONLY = global i32 0
@.str.1 = [16 x i8] c"fail to loadavg\00"
@.str.2 = [13 x i8] c"fail to read\00"
define i32 @get_load_avg(i8* nocapture writeonly %0) {
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %1
%11 = call i32 @read(i32 %5, i8* %3, i32 1024)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %28
15: ; preds = %10
%16 = sext i32 %11 to i64
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %16
store i8 48, i8* %17
br label %18
18: ; preds = %22, %15
%19 = phi i64 [ %23, %22 ], [ 0, %15 ]
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %19
%21 = load i8, i8* %20
switch i8 %21, label %22 [
i8 10, label %25
i8 0, label %25
]
22: ; preds = %18
%23 = add nuw i64 %19, 1
%24 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 %21, i8* %24
br label %18
25: ; preds = %18, %18
%26 = and i64 %19, 4294967295
%27 = getelementptr inbounds i8, i8* %0, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %25, %13
%29 = phi i32 [ -1, %13 ], [ 0, %25 ]
%30 = tail call i32 @close(i32 %5)
ret i32 %29
}