; LLVM IR from Wilson-ZheLin/Introduction-to-Digital-Image-Processing :: 10. Otus, Moving Average, and Region Growing/src/main.ll (Z15calculate_sigmaifPf)

; 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: mustprogress nounwind uwtable
declare dso_local noundef float @_Z3sumiPf(i32 noundef, ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef float @_Z15calculate_sigmaifPf(i32 noundef %0, float noundef %1, ptr noundef %2) #2 {
  %4 = alloca float, align 4
  %5 = alloca i32, align 4
  %6 = alloca float, align 4
  %7 = alloca ptr, align 8
  %8 = alloca float, align 4
  %9 = alloca i32, align 4
  %10 = alloca float, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store float %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store float 0.000000e+00, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %25, %3
  %13 = load i32, ptr %9, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %28

17:                                               ; preds = %12
  %18 = load ptr, ptr %7, align 8, !tbaa !11
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds float, ptr %18, i64 %20
  %22 = load float, ptr %21, align 4, !tbaa !9
  %23 = load float, ptr %8, align 4, !tbaa !9
  %24 = fadd float %23, %22
  store float %24, ptr %8, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %17
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %9, align 4, !tbaa !5
  br label %12, !llvm.loop !13

28:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load ptr, ptr %7, align 8, !tbaa !11
  %31 = call noundef float @_Z3sumiPf(i32 noundef %29, ptr noundef %30)
  store float %31, ptr %10, align 4, !tbaa !9
  %32 = load float, ptr %8, align 4, !tbaa !9
  %33 = fpext float %32 to double
  %34 = fcmp olt double %33, 1.000000e-10
  br i1 %34, label %40, label %35

35:                                               ; preds = %28
  %36 = load float, ptr %8, align 4, !tbaa !9
  %37 = fsub float 1.000000e+00, %36
  %38 = fpext float %37 to double
  %39 = fcmp olt double %38, 1.000000e-10
  br i1 %39, label %40, label %41

40:                                               ; preds = %35, %28
  store float 0.000000e+00, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %55

41:                                               ; preds = %35
  %42 = load float, ptr %6, align 4, !tbaa !9
  %43 = load float, ptr %8, align 4, !tbaa !9
  %44 = load float, ptr %10, align 4, !tbaa !9
  %45 = fneg float %44
  %46 = call float @llvm.fmuladd.f32(float %42, float %43, float %45)
  %47 = call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %46, i32 noundef 2)
  %48 = load float, ptr %8, align 4, !tbaa !9
  %49 = load float, ptr %8, align 4, !tbaa !9
  %50 = fsub float 1.000000e+00, %49
  %51 = fmul float %48, %50
  %52 = fpext float %51 to double
  %53 = fdiv double %47, %52
  %54 = fptrunc double %53 to float
  store float %54, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %55

55:                                               ; preds = %41, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %56 = load float, ptr %4, align 4
  ret float %56
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef, i32 noundef) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { mustprogress 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 = { inlinehint mustprogress 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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 nounwind uwtable
declare dso_local noundef float @_Z3sumiPf(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef float @_Z15calculate_sigmaifPf(i32 noundef %0, float noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %0 to i64
  %xtraiter = and i64 %wide.trip.count, 7
  %5 = icmp ult i32 %0, 8
  br i1 %5, label %._crit_edge.loopexit.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

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi float [ poison, %.lr.ph.preheader ], [ %35, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.7, %.lr.ph ]
  %.01618.unr = phi float [ 0.000000e+00, %.lr.ph.preheader ], [ %35, %.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, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01618.epil = phi float [ %8, %.lr.ph.epil ], [ %.01618.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %6 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.epil
  %7 = load float, ptr %6, align 4, !tbaa !5
  %8 = fadd float %.01618.epil, %7
  %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 !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %3
  %.016.lcssa = phi float [ 0.000000e+00, %3 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %8, %.lr.ph.epil ]
  %9 = tail call noundef float @_Z3sumiPf(i32 noundef %0, ptr noundef %2)
  %10 = fpext float %.016.lcssa to double
  %11 = fcmp olt double %10, 1.000000e-10
  br i1 %11, label %48, label %36

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.7, %.lr.ph ]
  %.01618 = phi float [ 0.000000e+00, %.lr.ph.preheader.new ], [ %35, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %12 = getelementptr inbounds float, ptr %2, i64 %indvars.iv
  %13 = load float, ptr %12, align 4, !tbaa !5
  %14 = fadd float %.01618, %13
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %15 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next
  %16 = load float, ptr %15, align 4, !tbaa !5
  %17 = fadd float %14, %16
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %18 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.1
  %19 = load float, ptr %18, align 4, !tbaa !5
  %20 = fadd float %17, %19
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %21 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.2
  %22 = load float, ptr %21, align 4, !tbaa !5
  %23 = fadd float %20, %22
  %indvars.iv.next.3 = or disjoint i64 %indvars.iv, 4
  %24 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.3
  %25 = load float, ptr %24, align 4, !tbaa !5
  %26 = fadd float %23, %25
  %indvars.iv.next.4 = or disjoint i64 %indvars.iv, 5
  %27 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.4
  %28 = load float, ptr %27, align 4, !tbaa !5
  %29 = fadd float %26, %28
  %indvars.iv.next.5 = or disjoint i64 %indvars.iv, 6
  %30 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.5
  %31 = load float, ptr %30, align 4, !tbaa !5
  %32 = fadd float %29, %31
  %indvars.iv.next.6 = or disjoint i64 %indvars.iv, 7
  %33 = getelementptr inbounds float, ptr %2, i64 %indvars.iv.next.6
  %34 = load float, ptr %33, align 4, !tbaa !5
  %35 = fadd float %32, %34
  %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.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

36:                                               ; preds = %._crit_edge
  %37 = fsub float 1.000000e+00, %.016.lcssa
  %38 = fpext float %37 to double
  %39 = fcmp olt double %38, 1.000000e-10
  br i1 %39, label %48, label %40

40:                                               ; preds = %36
  %41 = fneg float %9
  %42 = tail call float @llvm.fmuladd.f32(float %1, float %.016.lcssa, float %41)
  %43 = tail call noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef %42, i32 noundef 2)
  %44 = fmul float %.016.lcssa, %37
  %45 = fpext float %44 to double
  %46 = fdiv double %43, %45
  %47 = fptrunc double %46 to float
  br label %48

48:                                               ; preds = %._crit_edge, %36, %40
  %.0 = phi float [ %47, %40 ], [ 0.000000e+00, %36 ], [ 0.000000e+00, %._crit_edge ]
  ret float %.0
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIfiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(float noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #2

attributes #0 = { mustprogress 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 #1 = { inlinehint mustprogress 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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