; LLVM IR from ahampriyanshu/algo-ds-101 :: Maths/Statistics/Standard_Deviation (Standard_Deviation)

; 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: nounwind uwtable
define dso_local float @calculateSD(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca float, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store float 0.000000e+00, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store float 0.000000e+00, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %21, %2
  %10 = load i32, ptr %8, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %24

13:                                               ; preds = %9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds float, ptr %14, i64 %16
  %18 = load float, ptr %17, align 4, !tbaa !11
  %19 = load float, ptr %5, align 4, !tbaa !11
  %20 = fadd float %19, %18
  store float %20, ptr %5, align 4, !tbaa !11
  br label %21

21:                                               ; preds = %13
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %8, align 4, !tbaa !9
  br label %9, !llvm.loop !13

24:                                               ; preds = %9
  %25 = load float, ptr %5, align 4, !tbaa !11
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = sitofp i32 %26 to float
  %28 = fdiv float %25, %27
  store float %28, ptr %6, align 4, !tbaa !11
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %47, %24
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %50

33:                                               ; preds = %29
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds float, ptr %34, i64 %36
  %38 = load float, ptr %37, align 4, !tbaa !11
  %39 = load float, ptr %6, align 4, !tbaa !11
  %40 = fsub float %38, %39
  %41 = fpext float %40 to double
  %42 = call double @pow(double noundef %41, double noundef 2.000000e+00) #3
  %43 = load float, ptr %7, align 4, !tbaa !11
  %44 = fpext float %43 to double
  %45 = fadd double %44, %42
  %46 = fptrunc double %45 to float
  store float %46, ptr %7, align 4, !tbaa !11
  br label %47

47:                                               ; preds = %33
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %8, align 4, !tbaa !9
  br label %29, !llvm.loop !15

50:                                               ; preds = %29
  %51 = load float, ptr %7, align 4, !tbaa !11
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = sitofp i32 %52 to float
  %54 = fdiv float %51, %53
  %55 = fpext float %54 to double
  %56 = call double @sqrt(double noundef %55) #3
  %57 = fptrunc double %56 to float
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret float %57
}

; 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 @pow(double noundef, double noundef) #2

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: nofree nounwind memory(write, argmem: readwrite) uwtable
define dso_local float @calculateSD(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2
  %4 = sitofp i32 %1 to float
  br label %._crit_edge25

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 7
  %5 = icmp ult i32 %1, 8
  br i1 %5, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483640
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.7, %.lr.ph ]
  %.01719 = phi float [ 0.000000e+00, %.lr.ph.preheader.new ], [ %29, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %6 = getelementptr inbounds float, ptr %0, i64 %indvars.iv
  %7 = load float, ptr %6, align 4, !tbaa !5
  %8 = fadd float %.01719, %7
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %9 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next
  %10 = load float, ptr %9, align 4, !tbaa !5
  %11 = fadd float %8, %10
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %12 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.1
  %13 = load float, ptr %12, align 4, !tbaa !5
  %14 = fadd float %11, %13
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %15 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.2
  %16 = load float, ptr %15, align 4, !tbaa !5
  %17 = fadd float %14, %16
  %indvars.iv.next.3 = or disjoint i64 %indvars.iv, 4
  %18 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.3
  %19 = load float, ptr %18, align 4, !tbaa !5
  %20 = fadd float %17, %19
  %indvars.iv.next.4 = or disjoint i64 %indvars.iv, 5
  %21 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.4
  %22 = load float, ptr %21, align 4, !tbaa !5
  %23 = fadd float %20, %22
  %indvars.iv.next.5 = or disjoint i64 %indvars.iv, 6
  %24 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.5
  %25 = load float, ptr %24, align 4, !tbaa !5
  %26 = fadd float %23, %25
  %indvars.iv.next.6 = or disjoint i64 %indvars.iv, 7
  %27 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next.6
  %28 = load float, ptr %27, align 4, !tbaa !5
  %29 = fadd float %26, %28
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge.unr-lcssa:                            ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa37.ph = phi float [ poison, %.lr.ph.preheader ], [ %29, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.7, %.lr.ph ]
  %.01719.unr = phi float [ 0.000000e+00, %.lr.ph.preheader ], [ %29, %.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.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.unr-lcssa ]
  %.01719.epil = phi float [ %32, %.lr.ph.epil ], [ %.01719.unr, %._crit_edge.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.unr-lcssa ]
  %30 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.epil
  %31 = load float, ptr %30, align 4, !tbaa !5
  %32 = fadd float %.01719.epil, %31
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.unr-lcssa
  %.lcssa37 = phi float [ %.lcssa37.ph, %._crit_edge.unr-lcssa ], [ %32, %.lr.ph.epil ]
  %33 = sitofp i32 %1 to float
  %34 = fdiv float %.lcssa37, %33
  br i1 %3, label %.lr.ph24.preheader, label %._crit_edge25

.lr.ph24.preheader:                               ; preds = %._crit_edge
  %xtraiter39 = and i64 %wide.trip.count, 1
  %35 = icmp eq i32 %1, 1
  br i1 %35, label %._crit_edge25.loopexit.unr-lcssa, label %.lr.ph24.preheader.new

.lr.ph24.preheader.new:                           ; preds = %.lr.ph24.preheader
  %unroll_iter43 = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph24

.lr.ph24:                                         ; preds = %.lr.ph24, %.lr.ph24.preheader.new
  %indvars.iv29 = phi i64 [ 0, %.lr.ph24.preheader.new ], [ %indvars.iv.next30.1, %.lr.ph24 ]
  %.01621 = phi float [ 0.000000e+00, %.lr.ph24.preheader.new ], [ %49, %.lr.ph24 ]
  %niter44 = phi i64 [ 0, %.lr.ph24.preheader.new ], [ %niter44.next.1, %.lr.ph24 ]
  %36 = getelementptr inbounds float, ptr %0, i64 %indvars.iv29
  %37 = load float, ptr %36, align 4, !tbaa !5
  %38 = fsub float %37, %34
  %39 = fpext float %38 to double
  %square = fmul double %39, %39
  %40 = fpext float %.01621 to double
  %41 = fadd double %square, %40
  %42 = fptrunc double %41 to float
  %indvars.iv.next30 = or disjoint i64 %indvars.iv29, 1
  %43 = getelementptr inbounds float, ptr %0, i64 %indvars.iv.next30
  %44 = load float, ptr %43, align 4, !tbaa !5
  %45 = fsub float %44, %34
  %46 = fpext float %45 to double
  %square.1 = fmul double %46, %46
  %47 = fpext float %42 to double
  %48 = fadd double %square.1, %47
  %49 = fptrunc double %48 to float
  %indvars.iv.next30.1 = add nuw nsw i64 %indvars.iv29, 2
  %niter44.next.1 = add i64 %niter44, 2
  %niter44.ncmp.1 = icmp eq i64 %niter44.next.1, %unroll_iter43
  br i1 %niter44.ncmp.1, label %._crit_edge25.loopexit.unr-lcssa.loopexit, label %.lr.ph24, !llvm.loop !13

._crit_edge25.loopexit.unr-lcssa.loopexit:        ; preds = %.lr.ph24
  %50 = fpext float %49 to double
  br label %._crit_edge25.loopexit.unr-lcssa

._crit_edge25.loopexit.unr-lcssa:                 ; preds = %._crit_edge25.loopexit.unr-lcssa.loopexit, %.lr.ph24.preheader
  %.lcssa.ph = phi float [ poison, %.lr.ph24.preheader ], [ %49, %._crit_edge25.loopexit.unr-lcssa.loopexit ]
  %indvars.iv29.unr = phi i64 [ 0, %.lr.ph24.preheader ], [ %indvars.iv.next30.1, %._crit_edge25.loopexit.unr-lcssa.loopexit ]
  %.01621.unr = phi double [ 0.000000e+00, %.lr.ph24.preheader ], [ %50, %._crit_edge25.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod41.not = icmp eq i64 %xtraiter39, 0
  br i1 %lcmp.mod41.not, label %._crit_edge25, label %.lr.ph24.epil

.lr.ph24.epil:                                    ; preds = %._crit_edge25.loopexit.unr-lcssa
  %51 = getelementptr inbounds float, ptr %0, i64 %indvars.iv29.unr
  %52 = load float, ptr %51, align 4, !tbaa !5
  %53 = fsub float %52, %34
  %54 = fpext float %53 to double
  %square.epil = fmul double %54, %54
  %55 = fadd double %square.epil, %.01621.unr
  %56 = fptrunc double %55 to float
  br label %._crit_edge25

._crit_edge25:                                    ; preds = %.lr.ph24.epil, %._crit_edge25.loopexit.unr-lcssa, %._crit_edge.thread, %._crit_edge
  %57 = phi float [ %33, %._crit_edge ], [ %4, %._crit_edge.thread ], [ %33, %._crit_edge25.loopexit.unr-lcssa ], [ %33, %.lr.ph24.epil ]
  %.016.lcssa = phi float [ 0.000000e+00, %._crit_edge ], [ 0.000000e+00, %._crit_edge.thread ], [ %.lcssa.ph, %._crit_edge25.loopexit.unr-lcssa ], [ %56, %.lr.ph24.epil ]
  %58 = fdiv float %.016.lcssa, %57
  %sqrtf = tail call float @sqrtf(float noundef %58) #1
  ret float %sqrtf
}

declare float @sqrtf(float) local_unnamed_addr

attributes #0 = { nofree nounwind memory(write, argmem: readwrite) 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 #1 = { 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" }

!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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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