#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 closest_centroid(double *, double *, int, int);
scalar_t__ distance_metric(double *, double *, int);
double nextafter(int /*<<< orphan*/, int);
double model_variance(double *data, double *centroids, int k, int N, int D) {
  double variance_distance = 0.0;
  for (int i = 0; i < N; i++) {
    int c = closest_centroid(data + i * D, centroids, k, D);
    variance_distance += distance_metric(data + i * D, centroids + c * D, D);
  }
  double variance = variance_distance / (double)(N - k);
  if (variance == 0) {
    variance = nextafter(0, 1);
  }
  return variance;
}


x86 assembly (clang, -O0):
<model_variance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11d8 <model_variance+0xa8>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1030 <closest_centroid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1050 <distance_metric@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1159 <model_variance+0x29>
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1219 <model_variance+0xe9>
jp     1219 <model_variance+0xe9>
xor    %edi,%edi
mov    $0x1,%esi
call   1040 <nextafter@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<model_variance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
xorpd  %xmm0,%xmm0
mov    %ecx,0xc(%rsp)
test   %ecx,%ecx
jle    11b4 <model_variance+0x84>
mov    %r8d,%ebx
mov    %rsi,%r12
mov    %rdi,%rbp
movslq %r8d,%r13
mov    0xc(%rsp),%r14d
shl    $0x3,%r13
xorpd  %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm0,0x10(%rsp)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
call   1030 <closest_centroid@plt>
imul   %ebx,%eax
cltq
lea    (%r12,%rax,8),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
call   1050 <distance_metric@plt>
movsd  0x10(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
addsd  %xmm1,%xmm0
add    %r13,%rbp
add    $0xffffffffffffffff,%r14
jne    1170 <model_variance+0x40>
mov    0xc(%rsp),%eax
sub    %r15d,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
ucomisd 0xe32(%rip),%xmm0
jne    11ec <model_variance+0xbc>
jp     11ec <model_variance+0xbc>
xor    %edi,%edi
mov    $0x1,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <nextafter@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @model_variance(double* %0, double* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca double*
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
%13 = alloca i32
%14 = alloca double
store double* %0, double** %6
store double* %1, double** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store double 0.000000e+00, double* %11
store i32 0, i32* %12
br label %15
15: ; preds = %47, %5
%16 = load i32, i32* %12
%17 = load i32, i32* %9
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %50
19: ; preds = %15
%20 = load double*, double** %6
%21 = load i32, i32* %12
%22 = load i32, i32* %10
%23 = mul nsw i32 %21, %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %20, i64 %24
%26 = load double*, double** %7
%27 = load i32, i32* %8
%28 = load i32, i32* %10
%29 = call i32 @closest_centroid(double* %25, double* %26, i32 %27, i32 %28)
store i32 %29, i32* %13
%30 = load double*, double** %6
%31 = load i32, i32* %12
%32 = load i32, i32* %10
%33 = mul nsw i32 %31, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %30, i64 %34
%36 = load double*, double** %7
%37 = load i32, i32* %13
%38 = load i32, i32* %10
%39 = mul nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %36, i64 %40
%42 = load i32, i32* %10
%43 = call i64 @distance_metric(double* %35, double* %41, i32 %42)
%44 = sitofp i64 %43 to double
%45 = load double, double* %11
%46 = fadd double %45, %44
store double %46, double* %11
br label %47
47: ; preds = %19
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 1
store i32 %49, i32* %12
br label %15
50: ; preds = %15
%51 = load double, double* %11
%52 = load i32, i32* %9
%53 = load i32, i32* %8
%54 = sub nsw i32 %52, %53
%55 = sitofp i32 %54 to double
%56 = fdiv double %51, %55
store double %56, double* %14
%57 = load double, double* %14
%58 = fcmp oeq double %57, 0.000000e+00
br i1 %58, label %59, label %61
59: ; preds = %50
%60 = call double @nextafter(i32 0, i32 1)
store double %60, double* %14
br label %61
61: ; preds = %59, %50
%62 = load double, double* %14
ret double %62
}

LLVM IR (-O2):
define double @model_variance(double* %0, double* %1, i32 %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = sext i32 %4 to i64
%9 = zext i32 %3 to i64
br label %16
10: ; preds = %16, %5
%11 = phi double [ 0.000000e+00, %5 ], [ %27, %16 ]
%12 = sub nsw i32 %3, %2
%13 = sitofp i32 %12 to double
%14 = fdiv double %11, %13
%15 = fcmp oeq double %14, 0.000000e+00
br i1 %15, label %30, label %32
16: ; preds = %7, %16
%17 = phi i64 [ 0, %7 ], [ %28, %16 ]
%18 = phi double [ 0.000000e+00, %7 ], [ %27, %16 ]
%19 = mul nsw i64 %17, %8
%20 = getelementptr inbounds double, double* %0, i64 %19
%21 = tail call i32 @closest_centroid(double* %20, double* %1, i32 %2, i32 %4)
%22 = mul nsw i32 %21, %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %1, i64 %23
%25 = tail call i64 @distance_metric(double* %20, double* %24, i32 %4)
%26 = sitofp i64 %25 to double
%27 = fadd double %18, %26
%28 = add nuw nsw i64 %17, 1
%29 = icmp eq i64 %28, %9
br i1 %29, label %10, label %16
30: ; preds = %10
%31 = tail call double @nextafter(i32 0, i32 1)
br label %32
32: ; preds = %30, %10
%33 = phi double [ %31, %30 ], [ %14, %10 ]
ret double %33
}