#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execv(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, float *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
float av_load() {
  int fd[2];
  pipe(fd);
  pid_t pid = fork();
  if (pid < 0)
    printf("pid error \n");
  if (pid == 0) {
    dup2(fd[1], STDOUT_FILENO);
    execv("/usr/bin/uptime", NULL);
    exit(0);
  }
  char mem[100];
  usleep(1000);
  int n = read(fd[0], mem, 100);
  wait(NULL);
  float load = 100.0, load2 = 100.0;
  char s1[100];
  sscanf(&mem[46], "%4f", &load);
  sscanf(&mem[51], "%4f", &load2);
  return (load + load2) / 2;
}


x86 assembly (clang, -O0):
<av_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
lea    -0x8(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d6 <av_load+0x36>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1209 <av_load+0x69>
mov    -0x4(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <execv@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x84(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
movss  0xdc9(%rip),%xmm0
movss  %xmm0,-0x88(%rbp)
ff
movss  0xdb9(%rip),%xmm0
movss  %xmm0,-0x8c(%rbp)
ff
lea    -0x80(%rbp),%rdi
add    $0x2e,%rdi
lea    0xdc2(%rip),%rsi
lea    -0x88(%rbp),%rdx
call   1070 <sscanf@plt>
lea    -0x80(%rbp),%rdi
add    $0x33,%rdi
lea    0xda7(%rip),%rsi
lea    -0x8c(%rbp),%rdx
call   1070 <sscanf@plt>
movss  -0x88(%rbp),%xmm0
ff
addss  -0x8c(%rbp),%xmm0
ff
movss  0xd5f(%rip),%xmm1
divss  %xmm1,%xmm0
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<av_load>:
push   %rbx
sub    $0x80,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11e7 <av_load+0x47>
jne    11f3 <av_load+0x53>
mov    0xc(%rsp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
lea    0xe2b(%rip),%rdi
xor    %esi,%esi
call   10a0 <execv@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
xor    %edi,%edi
call   1090 <wait@plt>
movl   $0x42c80000,0x4(%rsp)
42
movl   $0x42c80000,(%rsp)
lea    0x3e(%rsp),%rdi
lea    0xde2(%rip),%rbx
lea    0x4(%rsp),%rdx
mov    %rbx,%rsi
call   1070 <sscanf@plt>
lea    0x43(%rsp),%rdi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1070 <sscanf@plt>
movss  0x4(%rsp),%xmm0
addss  (%rsp),%xmm0
mulss  0xd9e(%rip),%xmm0
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"pid error \0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [16 x i8] c"/usr/bin/uptime\00"
@.str.2 = [4 x i8] c"%4f\00"
define float @av_load() {
%1 = alloca [2 x i32]
%2 = alloca i64
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca float
%6 = alloca float
%7 = alloca [100 x i8]
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i64 (...) @fork()
store i64 %10, i64* %2
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i64, i64* %2
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @dup2(i32 %20, i32 %21)
%23 = call i32 @execv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32* null)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %15
%26 = call i32 @usleep(i32 1000)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 100)
store i32 %30, i32* %4
%31 = call i32 @wait(i32* null)
store float 1.000000e+02, float* %5
store float 1.000000e+02, float* %6
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 46
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float* %5)
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 51
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float* %6)
%36 = load float, float* %5
%37 = load float, float* %6
%38 = fadd float %36, %37
%39 = fdiv float %38, 2.000000e+00
ret float %39
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str.1 = [16 x i8] c"/usr/bin/uptime\00"
@.str.2 = [4 x i8] c"%4f\00"
@str = [11 x i8] c"pid error \00"
define float @av_load() {
%1 = alloca [2 x i32]
%2 = alloca [100 x i8]
%3 = alloca float
%4 = alloca float
%5 = bitcast [2 x i32]* %1 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %21
12: ; preds = %0
%13 = icmp eq i64 %8, 0
br i1 %13, label %14, label %21
14: ; preds = %12
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = call i32 @execv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32* null)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %10, %12
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%23 = call i32 @usleep(i32 1000)
%24 = load i32, i32* %6
%25 = call i32 @read(i32 %24, i8* %22, i32 100)
%26 = call i32 @wait(i32* null)
%27 = bitcast float* %3 to i8*
store float 1.000000e+02, float* %3
%28 = bitcast float* %4 to i8*
store float 1.000000e+02, float* %4
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 46
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float* %3)
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 51
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), float* %4)
%33 = load float, float* %3
%34 = load float, float* %4
%35 = fadd float %33, %34
%36 = fmul float %35, 5.000000e-01
ret float %36
}