#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 */
float mean_array(float *, int);
float sqrt(float);
float variance_array(float *, int);
void calculate_loss(float *output, float *delta, int n, float thresh) {
  int i;
  float mean = mean_array(output, n);
  float var = variance_array(output, n);
  for (i = 0; i < n; ++i) {
    if (delta[i] > mean + thresh * sqrt(var))
      delta[i] = output[i];
    else
      delta[i] = 0;
  }
}


x86 assembly (clang, -O0):
<calculate_loss>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movss  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mean_array@plt>
movss  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <variance_array@plt>
movss  %xmm0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1212 <calculate_loss+0xe2>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x28(%rbp)
movss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
movss  -0x24(%rbp),%xmm0
call   1040 <sqrt@plt>
movss  -0x30(%rbp),%xmm1
movss  -0x2c(%rbp),%xmm2
movaps %xmm0,%xmm3
movss  -0x28(%rbp),%xmm0
mulss  %xmm3,%xmm1
addss  %xmm2,%xmm1
ucomiss %xmm1,%xmm0
jbe    11ef <calculate_loss+0xbf>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
jmp    11ff <calculate_loss+0xcf>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax,%rcx,4)
jmp    1204 <calculate_loss+0xd4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <calculate_loss+0x41>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_loss>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movss  %xmm0,0x18(%rsp)
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %edx,%esi
call   1030 <mean_array@plt>
movss  %xmm0,0x14(%rsp)
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <variance_array@plt>
test   %ebp,%ebp
jle    12aa <calculate_loss+0x17a>
movaps %xmm0,%xmm2
mov    %ebp,%r15d
cmp    $0x1,%ebp
jne    119d <calculate_loss+0x6d>
xor    %ebp,%ebp
test   $0x1,%r15b
je     12aa <calculate_loss+0x17a>
movss  (%rbx,%rbp,4),%xmm4
xorps  %xmm3,%xmm3
ucomiss %xmm3,%xmm2
jb     1273 <calculate_loss+0x143>
xorps  %xmm0,%xmm0
sqrtss %xmm2,%xmm0
jmp    128a <calculate_loss+0x15a>
mov    %r15d,%r12d
and    $0xfffffffe,%r12d
xor    %ebp,%ebp
sqrtss %xmm2,%xmm3
xorps  %xmm4,%xmm4
movss  %xmm2,0xc(%rsp)
movss  %xmm3,0x1c(%rsp)
jmp    11cf <calculate_loss+0x9f>
nopl   0x0(%rax,%rax,1)
movss  %xmm1,0x4(%rbx,%rbp,4)
add    $0x2,%rbp
cmp    %rbp,%r12
je     1176 <calculate_loss+0x46>
movss  (%rbx,%rbp,4),%xmm5
ucomiss %xmm4,%xmm2
movaps %xmm3,%xmm0
jae    11ff <calculate_loss+0xcf>
movaps %xmm2,%xmm0
movss  %xmm5,0x10(%rsp)
call   1040 <sqrt@plt>
movss  0x10(%rsp),%xmm5
xorps  %xmm4,%xmm4
movss  0x1c(%rsp),%xmm3
movss  0xc(%rsp),%xmm2
mulss  0x18(%rsp),%xmm0
addss  0x14(%rsp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm5
jbe    1219 <calculate_loss+0xe9>
movss  (%r14,%rbp,4),%xmm1
movss  %xmm1,(%rbx,%rbp,4)
movss  0x4(%rbx,%rbp,4),%xmm5
ucomiss %xmm4,%xmm2
movaps %xmm3,%xmm0
jae    124f <calculate_loss+0x11f>
movaps %xmm2,%xmm0
movss  %xmm5,0x10(%rsp)
call   1040 <sqrt@plt>
movss  0x10(%rsp),%xmm5
xorps  %xmm4,%xmm4
movss  0x1c(%rsp),%xmm3
movss  0xc(%rsp),%xmm2
mulss  0x18(%rsp),%xmm0
addss  0x14(%rsp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm5
jbe    11c0 <calculate_loss+0x90>
movss  0x4(%r14,%rbp,4),%xmm1
jmp    11c0 <calculate_loss+0x90>
movaps %xmm2,%xmm0
movss  %xmm4,0xc(%rsp)
call   1040 <sqrt@plt>
movss  0xc(%rsp),%xmm4
xorps  %xmm3,%xmm3
movss  0x18(%rsp),%xmm1
mulss  %xmm0,%xmm1
addss  0x14(%rsp),%xmm1
ucomiss %xmm1,%xmm4
jbe    12a5 <calculate_loss+0x175>
movss  (%r14,%rbp,4),%xmm3
movss  %xmm3,(%rbx,%rbp,4)
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @calculate_loss(float* %0, float* %1, i32 %2, float %3) {
%5 = alloca float*
%6 = alloca float*
%7 = alloca i32
%8 = alloca float
%9 = alloca i32
%10 = alloca float
%11 = alloca float
store float* %0, float** %5
store float* %1, float** %6
store i32 %2, i32* %7
store float %3, float* %8
%12 = load float*, float** %5
%13 = load i32, i32* %7
%14 = call float @mean_array(float* %12, i32 %13)
store float %14, float* %10
%15 = load float*, float** %5
%16 = load i32, i32* %7
%17 = call float @variance_array(float* %15, i32 %16)
store float %17, float* %11
store i32 0, i32* %9
br label %18
18: ; preds = %50, %4
%19 = load i32, i32* %9
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %53
22: ; preds = %18
%23 = load float*, float** %6
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds float, float* %23, i64 %25
%27 = load float, float* %26
%28 = load float, float* %10
%29 = load float, float* %8
%30 = load float, float* %11
%31 = call float @sqrt(float %30)
%32 = call float @llvm.fmuladd.f32(float %29, float %31, float %28)
%33 = fcmp ogt float %27, %32
br i1 %33, label %34, label %44
34: ; preds = %22
%35 = load float*, float** %5
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds float, float* %35, i64 %37
%39 = load float, float* %38
%40 = load float*, float** %6
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds float, float* %40, i64 %42
store float %39, float* %43
br label %49
44: ; preds = %22
%45 = load float*, float** %6
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds float, float* %45, i64 %47
store float 0.000000e+00, float* %48
br label %49
49: ; preds = %44, %34
br label %50
50: ; preds = %49
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %18
53: ; preds = %18
ret void
}

LLVM IR (-O2):
define void @calculate_loss(float* %0, float* nocapture %1, i32 %2, float %3) {
%5 = tail call float @mean_array(float* %0, i32 %2)
%6 = tail call float @variance_array(float* %0, i32 %2)
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %55
8: ; preds = %4
%9 = zext i32 %2 to i64
%10 = and i64 %9, 1
%11 = icmp eq i32 %2, 1
br i1 %11, label %41, label %12
12: ; preds = %8
%13 = and i64 %9, 4294967294
br label %14
14: ; preds = %36, %12
%15 = phi i64 [ 0, %12 ], [ %38, %36 ]
%16 = phi i64 [ 0, %12 ], [ %39, %36 ]
%17 = getelementptr inbounds float, float* %1, i64 %15
%18 = load float, float* %17
%19 = tail call float @sqrt(float %6)
%20 = tail call float @llvm.fmuladd.f32(float %3, float %19, float %5)
%21 = fcmp ogt float %18, %20
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = getelementptr inbounds float, float* %0, i64 %15
%24 = load float, float* %23
br label %25
25: ; preds = %14, %22
%26 = phi float [ %24, %22 ], [ 0.000000e+00, %14 ]
store float %26, float* %17
%27 = or i64 %15, 1
%28 = getelementptr inbounds float, float* %1, i64 %27
%29 = load float, float* %28
%30 = tail call float @sqrt(float %6)
%31 = tail call float @llvm.fmuladd.f32(float %3, float %30, float %5)
%32 = fcmp ogt float %29, %31
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = getelementptr inbounds float, float* %0, i64 %27
%35 = load float, float* %34
br label %36
36: ; preds = %33, %25
%37 = phi float [ %35, %33 ], [ 0.000000e+00, %25 ]
store float %37, float* %28
%38 = add nuw nsw i64 %15, 2
%39 = add i64 %16, 2
%40 = icmp eq i64 %39, %13
br i1 %40, label %41, label %14
41: ; preds = %36, %8
%42 = phi i64 [ 0, %8 ], [ %38, %36 ]
%43 = icmp eq i64 %10, 0
br i1 %43, label %55, label %44
44: ; preds = %41
%45 = getelementptr inbounds float, float* %1, i64 %42
%46 = load float, float* %45
%47 = tail call float @sqrt(float %6)
%48 = tail call float @llvm.fmuladd.f32(float %3, float %47, float %5)
%49 = fcmp ogt float %46, %48
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = getelementptr inbounds float, float* %0, i64 %42
%52 = load float, float* %51
br label %53
53: ; preds = %50, %44
%54 = phi float [ %52, %50 ], [ 0.000000e+00, %44 ]
store float %54, float* %45
br label %55
55: ; preds = %53, %41, %4
ret void
}