; LLVM IR from zchrissirhcz/pixel :: legacy/matcalc (mean_stddev)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind
declare double @sqrt(double noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @mean_stddev_u8_asimd1(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca float, align 4
  %10 = alloca double, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca float, align 4
  %14 = alloca double, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %6, align 8, !tbaa !9
  %17 = call float @array_mean_u8_asimd4(ptr noundef %15, i64 noundef %16)
  store float %17, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store double 0.000000e+00, ptr %10, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %18 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %18, ptr %12, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %38, %4
  %20 = load i64, ptr %12, align 8, !tbaa !9
  %21 = load i64, ptr %6, align 8, !tbaa !9
  %22 = icmp ult i64 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %41

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !15
  %29 = uitofp i8 %28 to float
  %30 = load float, ptr %9, align 4, !tbaa !11
  %31 = fsub float %29, %30
  store float %31, ptr %13, align 4, !tbaa !11
  %32 = load float, ptr %13, align 4, !tbaa !11
  %33 = load float, ptr %13, align 4, !tbaa !11
  %34 = fmul float %32, %33
  %35 = fpext float %34 to double
  %36 = load double, ptr %10, align 8, !tbaa !13
  %37 = fadd double %36, %35
  store double %37, ptr %10, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %38

38:                                               ; preds = %24
  %39 = load i64, ptr %12, align 8, !tbaa !9
  %40 = add i64 %39, 1
  store i64 %40, ptr %12, align 8, !tbaa !9
  br label %19, !llvm.loop !16

41:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %42 = load double, ptr %10, align 8, !tbaa !13
  %43 = load i64, ptr %6, align 8, !tbaa !9
  %44 = uitofp i64 %43 to double
  %45 = fdiv double %42, %44
  %46 = call double @sqrt(double noundef %45) #4
  store double %46, ptr %14, align 8, !tbaa !13
  %47 = load float, ptr %9, align 4, !tbaa !11
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  store float %47, ptr %48, align 4, !tbaa !11
  %49 = load double, ptr %14, align 8, !tbaa !13
  %50 = fptrunc double %49 to float
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  store float %50, ptr %51, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret void
}

declare float @array_mean_u8_asimd4(ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @mean_stddev_u8_asimd1(ptr noundef %0, i64 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #1 {
  %5 = tail call float @array_mean_u8_asimd4(ptr noundef %0, i64 noundef %1) #3
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %xtraiter = and i64 %1, 1
  %6 = icmp eq i64 %1, 1
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %1, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi double [ poison, %.lr.ph.preheader ], [ %32, %.lr.ph ]
  %.020.unr = phi double [ 0.000000e+00, %.lr.ph.preheader ], [ %32, %.lr.ph ]
  %.01719.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %33, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %7 = getelementptr inbounds i8, ptr %0, i64 %.01719.unr
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %9 = uitofp i8 %8 to float
  %10 = fsub float %9, %5
  %11 = fmul float %10, %10
  %12 = fpext float %11 to double
  %13 = fadd double %.020.unr, %12
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %4
  %.0.lcssa = phi double [ 0.000000e+00, %4 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %13, %.lr.ph.epil ]
  %14 = uitofp i64 %1 to double
  %15 = fdiv double %.0.lcssa, %14
  %16 = tail call double @sqrt(double noundef %15) #3
  store float %5, ptr %2, align 4, !tbaa !8
  %17 = fptrunc double %16 to float
  store float %17, ptr %3, align 4, !tbaa !8
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.020 = phi double [ 0.000000e+00, %.lr.ph.preheader.new ], [ %32, %.lr.ph ]
  %.01719 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %33, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %0, i64 %.01719
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = uitofp i8 %19 to float
  %21 = fsub float %20, %5
  %22 = fmul float %21, %21
  %23 = fpext float %22 to double
  %24 = fadd double %.020, %23
  %25 = or disjoint i64 %.01719, 1
  %26 = getelementptr inbounds i8, ptr %0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = uitofp i8 %27 to float
  %29 = fsub float %28, %5
  %30 = fmul float %29, %29
  %31 = fpext float %30 to double
  %32 = fadd double %24, %31
  %33 = add nuw i64 %.01719, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10
}

declare float @array_mean_u8_asimd4(ptr noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn memory(write) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"float", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass