#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*/ fmax(int /*<<< orphan*/, double);
double log(double const);
double sqrt(int /*<<< orphan*/);
double weighted_log_std(const double *x, const double *weight, const int n) {
  double m1 = 0, m2 = 0, total_weight = 0, total_weight2 = 0, t;
  for (int i = 0; i < n; i++) {
    t = log(x[i]);
    m1 += weight[i] * t;
    m2 += weight[i] * t * t;
    total_weight += weight[i];
    total_weight2 += weight[i] * weight[i];
  }
  return sqrt(fmax(0, (m2 * total_weight - m1 * m1) /
                          (total_weight * total_weight - total_weight2)));
}


x86 assembly (clang, -O0):
<weighted_log_std>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122b <weighted_log_std+0xfb>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1040 <log@plt>
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    116a <weighted_log_std+0x3a>
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm3
movsd  -0x38(%rbp),%xmm2
movq   %xmm2,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm2
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
xor    %edi,%edi
call   1030 <fmax@plt>
mov    %eax,%edi
call   1050 <sqrt@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<weighted_log_std>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %edx,%edx
jle    11c6 <weighted_log_std+0x96>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xorpd  %xmm1,%xmm1
xorpd  %xmm4,%xmm4
xor    %ebx,%ebx
xorpd  %xmm3,%xmm3
nopw   0x0(%rax,%rax,1)
movsd  %xmm4,(%rsp)
movsd  %xmm3,0x8(%rsp)
movapd %xmm1,0x10(%rsp)
movsd  (%r15,%rbx,8),%xmm0
call   1040 <log@plt>
movsd  (%rsp),%xmm4
movsd  0x8(%rsp),%xmm3
movsd  (%r14,%rbx,8),%xmm1
movapd %xmm1,%xmm2
mulsd  %xmm0,%xmm2
addsd  %xmm2,%xmm4
addsd  %xmm1,%xmm3
unpcklpd %xmm1,%xmm2
unpcklpd %xmm1,%xmm0
mulpd  %xmm2,%xmm0
movapd 0x10(%rsp),%xmm2
movapd %xmm0,%xmm1
addpd  %xmm2,%xmm1
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <weighted_log_std+0x30>
addpd  %xmm2,%xmm0
jmp    11d2 <weighted_log_std+0xa2>
xorpd  %xmm0,%xmm0
xorpd  %xmm3,%xmm3
xorpd  %xmm4,%xmm4
mulsd  %xmm4,%xmm4
movapd %xmm0,%xmm1
mulsd  %xmm3,%xmm1
subsd  %xmm4,%xmm1
unpckhpd %xmm0,%xmm0
mulsd  %xmm3,%xmm3
subsd  %xmm0,%xmm3
divsd  %xmm3,%xmm1
xor    %edi,%edi
movapd %xmm1,%xmm0
call   1030 <fmax@plt>
mov    %eax,%edi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <sqrt@plt>

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

LLVM IR (-O2):
define double @weighted_log_std(double* nocapture %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %23
7: ; preds = %23
%8 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %37, <2 x double> %39, <2 x double> %27)
br label %9
9: ; preds = %7, %3
%10 = phi double [ 0.000000e+00, %3 ], [ %35, %7 ]
%11 = phi double [ 0.000000e+00, %3 ], [ %33, %7 ]
%12 = phi <2 x double> [ zeroinitializer, %3 ], [ %8, %7 ]
%13 = fneg double %11
%14 = fmul double %11, %13
%15 = extractelement <2 x double> %12, i64 0
%16 = tail call double @llvm.fmuladd.f64(double %15, double %10, double %14)
%17 = extractelement <2 x double> %12, i64 1
%18 = fneg double %17
%19 = tail call double @llvm.fmuladd.f64(double %10, double %10, double %18)
%20 = fdiv double %16, %19
%21 = tail call i32 @fmax(i32 0, double %20)
%22 = tail call double @sqrt(i32 %21)
ret double %22
23: ; preds = %5, %23
%24 = phi i64 [ 0, %5 ], [ %41, %23 ]
%25 = phi double [ 0.000000e+00, %5 ], [ %33, %23 ]
%26 = phi double [ 0.000000e+00, %5 ], [ %35, %23 ]
%27 = phi <2 x double> [ zeroinitializer, %5 ], [ %40, %23 ]
%28 = getelementptr inbounds double, double* %0, i64 %24
%29 = load double, double* %28
%30 = tail call double @log(double %29)
%31 = getelementptr inbounds double, double* %1, i64 %24
%32 = load double, double* %31
%33 = tail call double @llvm.fmuladd.f64(double %32, double %30, double %25)
%34 = fmul double %30, %32
%35 = fadd double %26, %32
%36 = insertelement <2 x double> poison, double %34, i64 0
%37 = insertelement <2 x double> %36, double %32, i64 1
%38 = insertelement <2 x double> poison, double %30, i64 0
%39 = insertelement <2 x double> %38, double %32, i64 1
%40 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %37, <2 x double> %39, <2 x double> %27)
%41 = add nuw nsw i64 %24, 1
%42 = icmp eq i64 %41, %6
br i1 %42, label %7, label %23
}