#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 */
double _p3dKittlerTresholding_P2(int, double *, int);
double _p3dKittlerTresholding_u2(int, double *, int);
double _p3dKittlerTresholding_s2(int t, double *h, int hist_length) {
  int i;
  double sum, p, u, x;
  sum = 0.0;
  p = _p3dKittlerTresholding_P2(t, h, hist_length);
  if (p <= 0.0)
    return 0.0;
  u = _p3dKittlerTresholding_u2(t, h, hist_length);
  for (i = t + 1; i <= hist_length; i++) {
    x = (i - u) * (i - u);
    sum += x * h[i];
  }
  return sum / p;
}


x86 assembly (clang, -O0):
<_p3dKittlerTresholding_s2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <_p3dKittlerTresholding_P2@plt>
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x30(%rbp),%xmm0
jb     1169 <_p3dKittlerTresholding_s2+0x49>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f0 <_p3dKittlerTresholding_s2+0xd0>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <_p3dKittlerTresholding_u2@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     11e1 <_p3dKittlerTresholding_s2+0xc1>
cvtsi2sdl -0x20(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
cvtsi2sdl -0x20(%rbp),%xmm1
subsd  -0x38(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1186 <_p3dKittlerTresholding_s2+0x66>
movsd  -0x28(%rbp),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_p3dKittlerTresholding_s2>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <_p3dKittlerTresholding_P2@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jae    11f7 <_p3dKittlerTresholding_s2+0xd7>
movsd  %xmm0,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <_p3dKittlerTresholding_u2@plt>
xorpd  %xmm1,%xmm1
cmp    %r14d,%ebp
jge    11f1 <_p3dKittlerTresholding_s2+0xd1>
movslq %ebp,%rcx
movslq %r14d,%rax
sub    %ebp,%r14d
mov    %rcx,%rdx
not    %rdx
test   $0x1,%r14b
jne    1183 <_p3dKittlerTresholding_s2+0x63>
xorpd  %xmm1,%xmm1
add    %rax,%rdx
jne    11b0 <_p3dKittlerTresholding_s2+0x90>
jmp    11f1 <_p3dKittlerTresholding_s2+0xd1>
lea    0x1(%rcx),%rsi
cvtsi2sd %esi,%xmm2
subsd  %xmm0,%xmm2
mulsd  %xmm2,%xmm2
mulsd  0x8(%rbx,%rcx,8),%xmm2
xorpd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
mov    %rsi,%rcx
add    %rax,%rdx
je     11f1 <_p3dKittlerTresholding_s2+0xd1>
nopl   0x0(%rax)
lea    0x1(%rcx),%edx
xorps  %xmm2,%xmm2
cvtsi2sd %edx,%xmm2
subsd  %xmm0,%xmm2
mulsd  %xmm2,%xmm2
mulsd  0x8(%rbx,%rcx,8),%xmm2
addsd  %xmm1,%xmm2
lea    0x2(%rcx),%rdx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
subsd  %xmm0,%xmm1
mulsd  %xmm1,%xmm1
mulsd  0x10(%rbx,%rcx,8),%xmm1
addsd  %xmm2,%xmm1
mov    %rdx,%rcx
cmp    %rdx,%rax
jne    11b0 <_p3dKittlerTresholding_s2+0x90>
divsd  0x8(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @_p3dKittlerTresholding_s2(i32 %0, double* %1, i32 %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
store i32 %0, i32* %5
store double* %1, double** %6
store i32 %2, i32* %7
store double 0.000000e+00, double* %9
%13 = load i32, i32* %5
%14 = load double*, double** %6
%15 = load i32, i32* %7
%16 = call double @_p3dKittlerTresholding_P2(i32 %13, double* %14, i32 %15)
store double %16, double* %10
%17 = load double, double* %10
%18 = fcmp ole double %17, 0.000000e+00
br i1 %18, label %19, label %20
19: ; preds = %3
store double 0.000000e+00, double* %4
br label %56
20: ; preds = %3
%21 = load i32, i32* %5
%22 = load double*, double** %6
%23 = load i32, i32* %7
%24 = call double @_p3dKittlerTresholding_u2(i32 %21, double* %22, i32 %23)
store double %24, double* %11
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
br label %27
27: ; preds = %49, %20
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = load i32, i32* %8
%33 = sitofp i32 %32 to double
%34 = load double, double* %11
%35 = fsub double %33, %34
%36 = load i32, i32* %8
%37 = sitofp i32 %36 to double
%38 = load double, double* %11
%39 = fsub double %37, %38
%40 = fmul double %35, %39
store double %40, double* %12
%41 = load double, double* %12
%42 = load double*, double** %6
%43 = load i32, i32* %8
%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 = call double @llvm.fmuladd.f64(double %41, double %46, double %47)
store double %48, double* %9
br label %49
49: ; preds = %31
%50 = load i32, i32* %8
%51 = add nsw i32 %50, 1
store i32 %51, i32* %8
br label %27
52: ; preds = %27
%53 = load double, double* %9
%54 = load double, double* %10
%55 = fdiv double %53, %54
store double %55, double* %4
br label %56
56: ; preds = %52, %19
%57 = load double, double* %4
ret double %57
}

LLVM IR (-O2):
define double @_p3dKittlerTresholding_s2(i32 %0, double* %1, i32 %2) {
%4 = tail call double @_p3dKittlerTresholding_P2(i32 %0, double* %1, i32 %2)
%5 = fcmp ugt double %4, 0.000000e+00
br i1 %5, label %6, label %54
6: ; preds = %3
%7 = tail call double @_p3dKittlerTresholding_u2(i32 %0, double* %1, i32 %2)
%8 = icmp slt i32 %0, %2
br i1 %8, label %9, label %51
9: ; preds = %6
%10 = sext i32 %0 to i64
%11 = sext i32 %2 to i64
%12 = sub nsw i64 %11, %10
%13 = xor i64 %10, -1
%14 = and i64 %12, 1
%15 = icmp eq i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %9
%17 = add nsw i64 %10, 1
%18 = trunc i64 %17 to i32
%19 = sitofp i32 %18 to double
%20 = fsub double %19, %7
%21 = fmul double %20, %20
%22 = getelementptr inbounds double, double* %1, i64 %17
%23 = load double, double* %22
%24 = tail call double @llvm.fmuladd.f64(double %21, double %23, double 0.000000e+00)
br label %25
25: ; preds = %16, %9
%26 = phi double [ undef, %9 ], [ %24, %16 ]
%27 = phi i64 [ %10, %9 ], [ %17, %16 ]
%28 = phi double [ 0.000000e+00, %9 ], [ %24, %16 ]
%29 = sub nsw i64 0, %11
%30 = icmp eq i64 %13, %29
br i1 %30, label %51, label %31
31: ; preds = %25, %31
%32 = phi i64 [ %42, %31 ], [ %27, %25 ]
%33 = phi double [ %49, %31 ], [ %28, %25 ]
%34 = add nsw i64 %32, 1
%35 = trunc i64 %34 to i32
%36 = sitofp i32 %35 to double
%37 = fsub double %36, %7
%38 = fmul double %37, %37
%39 = getelementptr inbounds double, double* %1, i64 %34
%40 = load double, double* %39
%41 = tail call double @llvm.fmuladd.f64(double %38, double %40, double %33)
%42 = add nsw i64 %32, 2
%43 = trunc i64 %42 to i32
%44 = sitofp i32 %43 to double
%45 = fsub double %44, %7
%46 = fmul double %45, %45
%47 = getelementptr inbounds double, double* %1, i64 %42
%48 = load double, double* %47
%49 = tail call double @llvm.fmuladd.f64(double %46, double %48, double %41)
%50 = icmp eq i64 %42, %11
br i1 %50, label %51, label %31
51: ; preds = %25, %31, %6
%52 = phi double [ 0.000000e+00, %6 ], [ %26, %25 ], [ %49, %31 ]
%53 = fdiv double %52, %4
br label %54
54: ; preds = %3, %51
%55 = phi double [ %53, %51 ], [ 0.000000e+00, %3 ]
ret double %55
}