#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*/ exit(int);
int /*<<< orphan*/ printf(char *);
double sqrt(double);
void avg_std(char *x, int n, double *mean, double *std, int stride) {
  int ii;
  double an = 0.0, an1 = 0.0, dx, var;
  if (n < 1) {
    printf("\vVector length must be > 0 in avg_var().  Exiting\n");
    exit(1);
  } else {
    *mean = (double)x[0];
    var = 0.0;
  }
  for (ii = 1; ii < n; ii++) {
    an = (double)(ii + 1);
    an1 = (double)(ii);
    dx = ((double)x[ii * stride] - *mean) / an;
    var += an * an1 * dx * dx;
    *mean += dx;
  }
  if (n > 1) {
    var /= an1;
    *std = sqrt(var);
  } else {
    *std = 0.0;
  }
  return;
}


x86 assembly (clang, -O0):
<avg_std>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    117b <avg_std+0x4b>
lea    0xe94(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cvtsi2sd %eax,%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1231 <avg_std+0x101>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdl -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%edx
imul   %edx,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cvtsi2sd %eax,%xmm0
mov    -0x18(%rbp),%rax
subsd  (%rax),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x18(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    119d <avg_std+0x6d>
cmpl   $0x1,-0xc(%rbp)
jle    1265 <avg_std+0x135>
movsd  -0x38(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
call   1040 <sqrt@plt>
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    1270 <avg_std+0x140>
mov    -0x20(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<avg_std>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    11f1 <avg_std+0xc1>
mov    %rcx,%r14
movsbl (%rdi),%eax
cvtsi2sd %eax,%xmm2
movsd  %xmm2,(%rdx)
xorpd  %xmm1,%xmm1
cmp    $0x1,%esi
je     11e4 <avg_std+0xb4>
mov    %esi,%r9d
movslq %r8d,%rcx
add    %rcx,%rdi
xorpd  %xmm0,%xmm0
mov    $0x1,%ebx
mov    $0x2,%esi
xchg   %ax,%ax
xorps  %xmm1,%xmm1
cvtsi2sd %esi,%xmm1
xorps  %xmm3,%xmm3
cvtsi2sd %ebx,%xmm3
movsbl (%rdi),%eax
xorps  %xmm4,%xmm4
cvtsi2sd %eax,%xmm4
lea    0x1(%rbx),%rax
subsd  %xmm2,%xmm4
divsd  %xmm1,%xmm4
mulsd  %xmm3,%xmm1
mulsd  %xmm4,%xmm1
mulsd  %xmm4,%xmm1
addsd  %xmm1,%xmm0
addsd  %xmm4,%xmm2
movsd  %xmm2,(%rdx)
add    %rcx,%rdi
add    $0x1,%esi
mov    %rax,%rbx
cmp    %rax,%r9
jne    1170 <avg_std+0x40>
xorpd  %xmm1,%xmm1
cmp    $0x2,%r9d
jl     11e4 <avg_std+0xb4>
divsd  %xmm3,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11db <avg_std+0xab>
xorps  %xmm1,%xmm1
sqrtsd %xmm0,%xmm1
jmp    11e4 <avg_std+0xb4>
call   1040 <sqrt@plt>
movapd %xmm0,%xmm1
movsd  %xmm1,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"\0BVector length must be > 0 in avg_var(). Exiting\0A\00"
define void @avg_std(i8* %0, i32 %1, double* %2, double* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
store i8* %0, i8** %6
store i32 %1, i32* %7
store double* %2, double** %8
store double* %3, double** %9
store i32 %4, i32* %10
store double 0.000000e+00, double* %12
store double 0.000000e+00, double* %13
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %5
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sitofp i8 %24 to double
%26 = load double*, double** %8
store double %25, double* %26
store double 0.000000e+00, double* %15
br label %27
27: ; preds = %21
store i32 1, i32* %11
br label %28
28: ; preds = %63, %27
%29 = load i32, i32* %11
%30 = load i32, i32* %7
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %66
32: ; preds = %28
%33 = load i32, i32* %11
%34 = add nsw i32 %33, 1
%35 = sitofp i32 %34 to double
store double %35, double* %12
%36 = load i32, i32* %11
%37 = sitofp i32 %36 to double
store double %37, double* %13
%38 = load i8*, i8** %6
%39 = load i32, i32* %11
%40 = load i32, i32* %10
%41 = mul nsw i32 %39, %40
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
%44 = load i8, i8* %43
%45 = sitofp i8 %44 to double
%46 = load double*, double** %8
%47 = load double, double* %46
%48 = fsub double %45, %47
%49 = load double, double* %12
%50 = fdiv double %48, %49
store double %50, double* %14
%51 = load double, double* %12
%52 = load double, double* %13
%53 = fmul double %51, %52
%54 = load double, double* %14
%55 = fmul double %53, %54
%56 = load double, double* %14
%57 = load double, double* %15
%58 = call double @llvm.fmuladd.f64(double %55, double %56, double %57)
store double %58, double* %15
%59 = load double, double* %14
%60 = load double*, double** %8
%61 = load double, double* %60
%62 = fadd double %61, %59
store double %62, double* %60
br label %63
63: ; preds = %32
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %28
66: ; preds = %28
%67 = load i32, i32* %7
%68 = icmp sgt i32 %67, 1
br i1 %68, label %69, label %76
69: ; preds = %66
%70 = load double, double* %13
%71 = load double, double* %15
%72 = fdiv double %71, %70
store double %72, double* %15
%73 = load double, double* %15
%74 = call double @sqrt(double %73)
%75 = load double*, double** %9
store double %74, double* %75
br label %78
76: ; preds = %66
%77 = load double*, double** %9
store double 0.000000e+00, double* %77
br label %78
78: ; preds = %76, %69
ret void
}

LLVM IR (-O2):
@str = [50 x i8] c"\0BVector length must be > 0 in avg_var(). Exiting\00"
define void @avg_std(i8* nocapture %0, i32 %1, double* nocapture %2, double* nocapture writeonly %3, i32 %4) {
%6 = icmp slt i32 %1, 1
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %5
%11 = load i8, i8* %0
%12 = sitofp i8 %11 to double
store double %12, double* %2
%13 = icmp eq i32 %1, 1
br i1 %13, label %42, label %14
14: ; preds = %10
%15 = sext i32 %4 to i64
%16 = zext i32 %1 to i64
br label %17
17: ; preds = %14, %17
%18 = phi i64 [ 1, %14 ], [ %21, %17 ]
%19 = phi double [ 0.000000e+00, %14 ], [ %34, %17 ]
%20 = phi double [ %12, %14 ], [ %35, %17 ]
%21 = add nuw nsw i64 %18, 1
%22 = trunc i64 %21 to i32
%23 = sitofp i32 %22 to double
%24 = trunc i64 %18 to i32
%25 = sitofp i32 %24 to double
%26 = mul nsw i64 %18, %15
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = load i8, i8* %27
%29 = sitofp i8 %28 to double
%30 = fsub double %29, %20
%31 = fdiv double %30, %23
%32 = fmul double %25, %23
%33 = fmul double %32, %31
%34 = tail call double @llvm.fmuladd.f64(double %33, double %31, double %19)
%35 = fadd double %20, %31
store double %35, double* %2
%36 = icmp eq i64 %21, %16
br i1 %36, label %37, label %17
37: ; preds = %17
%38 = icmp sgt i32 %1, 1
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = fdiv double %34, %25
%41 = tail call double @sqrt(double %40)
br label %42
42: ; preds = %10, %37, %39
%43 = phi double [ %41, %39 ], [ 0.000000e+00, %37 ], [ 0.000000e+00, %10 ]
store double %43, double* %3
ret void
}