#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*/ free_matrix2(double **);
int /*<<< orphan*/ matrix_add(double **, double **, double **, int, int);
int /*<<< orphan*/ memset(double *, int /*<<< orphan*/, int);
int /*<<< orphan*/ mult(double *, double *, double, int);
double **new_matrix2(int, int);
int /*<<< orphan*/ outer_prod(double **, double *, double *, int, int);
int /*<<< orphan*/ sub(double *, double *, double *, int);
double sum(double *, int);
void wcov(double **S, double **X, double *w, double *mu, int n, int m) {
  int i;
  memset(S[0], 0, m * m * sizeof(double));
  double **Si = new_matrix2(m, m);
  for (i = 0; i < n; i++) {
    outer_prod(Si, X[i], X[i], m, m);
    mult(Si[0], Si[0], w[i], m * m);
    matrix_add(S, S, Si, m, m);
  }
  mult(S[0], S[0], 1.0 / sum(w, n), m * m);
  if (mu != NULL) {
    double **S_mu = new_matrix2(m, m);
    outer_prod(S_mu, mu, mu, m, m);
    sub(S[0], S[0], S_mu[0], m * m);
    free_matrix2(S_mu);
  }
}


x86 assembly (clang, -O0):
<wcov>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%eax
imul   -0x28(%rbp),%eax
cltq
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1070 <new_matrix2@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1254 <wcov+0xd4>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1080 <outer_prod@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%edx
imul   -0x28(%rbp),%edx
call   1060 <mult@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1090 <matrix_add@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d3 <wcov+0x53>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <sum@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
movaps %xmm0,%xmm1
movsd  0xd77(%rip),%xmm0
divsd  %xmm1,%xmm0
mov    -0x28(%rbp),%edx
imul   -0x28(%rbp),%edx
call   1060 <mult@plt>
cmpq   $0x0,-0x20(%rbp)
je     12f5 <wcov+0x175>
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1070 <new_matrix2@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x28(%rbp),%r8d
call   1080 <outer_prod@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x40(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%ecx
imul   -0x28(%rbp),%ecx
call   10a0 <sub@plt>
mov    -0x40(%rbp),%rdi
call   1050 <free_matrix2@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wcov>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%ebp
mov    %r8d,%r14d
mov    %rcx,0x18(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    %r9d,%r13d
imul   %r13d,%r13d
lea    0x0(,%r13,8),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   1070 <new_matrix2@plt>
mov    %r14d,0xc(%rsp)
test   %r14d,%r14d
jle    122f <wcov+0xaf>
mov    %rax,%r12
mov    0xc(%rsp),%r14d
xor    %r15d,%r15d
nopl   (%rax)
mov    0x20(%rsp),%rax
mov    (%rax,%r15,8),%rdx
mov    %r12,%rdi
mov    %rdx,%rsi
mov    %ebp,%ecx
mov    %ebp,%r8d
call   1080 <outer_prod@plt>
mov    (%r12),%rdi
mov    0x10(%rsp),%rax
movsd  (%rax,%r15,8),%xmm0
mov    %rdi,%rsi
mov    %r13d,%edx
call   1060 <mult@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
mov    %ebp,%r8d
call   1090 <matrix_add@plt>
add    $0x1,%r15
cmp    %r15,%r14
jne    11e0 <wcov+0x60>
mov    (%rbx),%r15
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
call   1040 <sum@plt>
movsd  0xdb8(%rip),%xmm1
divsd  %xmm0,%xmm1
mov    %r15,%rdi
mov    %r15,%rsi
movapd %xmm1,%xmm0
mov    %r13d,%edx
call   1060 <mult@plt>
mov    0x18(%rsp),%r14
test   %r14,%r14
je     12ae <wcov+0x12e>
mov    %ebp,%edi
mov    %ebp,%esi
call   1070 <new_matrix2@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
mov    %ebp,%r8d
call   1080 <outer_prod@plt>
mov    (%rbx),%rdi
mov    (%r15),%rdx
mov    %rdi,%rsi
mov    %r13d,%ecx
call   10a0 <sub@plt>
mov    %r15,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <free_matrix2@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @wcov(double** %0, double** %1, double* %2, double* %3, i32 %4, i32 %5) {
%7 = alloca double**
%8 = alloca double**
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca double**
%15 = alloca double**
store double** %0, double*** %7
store double** %1, double*** %8
store double* %2, double** %9
store double* %3, double** %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%16 = load double**, double*** %7
%17 = getelementptr inbounds double*, double** %16, i64 0
%18 = load double*, double** %17
%19 = load i32, i32* %12
%20 = load i32, i32* %12
%21 = mul nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 8
%24 = trunc i64 %23 to i32
%25 = call i32 @memset(double* %18, i32 0, i32 %24)
%26 = load i32, i32* %12
%27 = load i32, i32* %12
%28 = call double** @new_matrix2(i32 %26, i32 %27)
store double** %28, double*** %14
store i32 0, i32* %13
br label %29
29: ; preds = %69, %6
%30 = load i32, i32* %13
%31 = load i32, i32* %11
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %72
33: ; preds = %29
%34 = load double**, double*** %14
%35 = load double**, double*** %8
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds double*, double** %35, i64 %37
%39 = load double*, double** %38
%40 = load double**, double*** %8
%41 = load i32, i32* %13
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double*, double** %40, i64 %42
%44 = load double*, double** %43
%45 = load i32, i32* %12
%46 = load i32, i32* %12
%47 = call i32 @outer_prod(double** %34, double* %39, double* %44, i32 %45, i32 %46)
%48 = load double**, double*** %14
%49 = getelementptr inbounds double*, double** %48, i64 0
%50 = load double*, double** %49
%51 = load double**, double*** %14
%52 = getelementptr inbounds double*, double** %51, i64 0
%53 = load double*, double** %52
%54 = load double*, double** %9
%55 = load i32, i32* %13
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds double, double* %54, i64 %56
%58 = load double, double* %57
%59 = load i32, i32* %12
%60 = load i32, i32* %12
%61 = mul nsw i32 %59, %60
%62 = call i32 @mult(double* %50, double* %53, double %58, i32 %61)
%63 = load double**, double*** %7
%64 = load double**, double*** %7
%65 = load double**, double*** %14
%66 = load i32, i32* %12
%67 = load i32, i32* %12
%68 = call i32 @matrix_add(double** %63, double** %64, double** %65, i32 %66, i32 %67)
br label %69
69: ; preds = %33
%70 = load i32, i32* %13
%71 = add nsw i32 %70, 1
store i32 %71, i32* %13
br label %29
72: ; preds = %29
%73 = load double**, double*** %7
%74 = getelementptr inbounds double*, double** %73, i64 0
%75 = load double*, double** %74
%76 = load double**, double*** %7
%77 = getelementptr inbounds double*, double** %76, i64 0
%78 = load double*, double** %77
%79 = load double*, double** %9
%80 = load i32, i32* %11
%81 = call double @sum(double* %79, i32 %80)
%82 = fdiv double 1.000000e+00, %81
%83 = load i32, i32* %12
%84 = load i32, i32* %12
%85 = mul nsw i32 %83, %84
%86 = call i32 @mult(double* %75, double* %78, double %82, i32 %85)
%87 = load double*, double** %10
%88 = icmp ne double* %87, null
br i1 %88, label %89, label %114
89: ; preds = %72
%90 = load i32, i32* %12
%91 = load i32, i32* %12
%92 = call double** @new_matrix2(i32 %90, i32 %91)
store double** %92, double*** %15
%93 = load double**, double*** %15
%94 = load double*, double** %10
%95 = load double*, double** %10
%96 = load i32, i32* %12
%97 = load i32, i32* %12
%98 = call i32 @outer_prod(double** %93, double* %94, double* %95, i32 %96, i32 %97)
%99 = load double**, double*** %7
%100 = getelementptr inbounds double*, double** %99, i64 0
%101 = load double*, double** %100
%102 = load double**, double*** %7
%103 = getelementptr inbounds double*, double** %102, i64 0
%104 = load double*, double** %103
%105 = load double**, double*** %15
%106 = getelementptr inbounds double*, double** %105, i64 0
%107 = load double*, double** %106
%108 = load i32, i32* %12
%109 = load i32, i32* %12
%110 = mul nsw i32 %108, %109
%111 = call i32 @sub(double* %101, double* %104, double* %107, i32 %110)
%112 = load double**, double*** %15
%113 = call i32 @free_matrix2(double** %112)
br label %114
114: ; preds = %89, %72
ret void
}

LLVM IR (-O2):
define void @wcov(double** %0, double** nocapture %1, double* %2, double* %3, i32 %4, i32 %5) {
%7 = load double*, double** %0
%8 = mul nsw i32 %5, %5
%9 = shl i32 %8, 3
%10 = tail call i32 @memset(double* %7, i32 0, i32 %9)
%11 = tail call double** @new_matrix2(i32 %5, i32 %5)
%12 = icmp sgt i32 %4, 0
br i1 %12, label %13, label %27
13: ; preds = %6
%14 = zext i32 %4 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %25, %15 ]
%17 = getelementptr inbounds double*, double** %1, i64 %16
%18 = load double*, double** %17
%19 = tail call i32 @outer_prod(double** %11, double* %18, double* %18, i32 %5, i32 %5)
%20 = load double*, double** %11
%21 = getelementptr inbounds double, double* %2, i64 %16
%22 = load double, double* %21
%23 = tail call i32 @mult(double* %20, double* %20, double %22, i32 %8)
%24 = tail call i32 @matrix_add(double** %0, double** %0, double** %11, i32 %5, i32 %5)
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, %14
br i1 %26, label %27, label %15
27: ; preds = %15, %6
%28 = load double*, double** %0
%29 = tail call double @sum(double* %2, i32 %4)
%30 = fdiv double 1.000000e+00, %29
%31 = tail call i32 @mult(double* %28, double* %28, double %30, i32 %8)
%32 = icmp eq double* %3, null
br i1 %32, label %40, label %33
33: ; preds = %27
%34 = tail call double** @new_matrix2(i32 %5, i32 %5)
%35 = tail call i32 @outer_prod(double** %34, double* %3, double* %3, i32 %5, i32 %5)
%36 = load double*, double** %0
%37 = load double*, double** %34
%38 = tail call i32 @sub(double* %36, double* %36, double* %37, i32 %8)
%39 = tail call i32 @free_matrix2(double** %34)
br label %40
40: ; preds = %33, %27
ret void
}