; LLVM IR from GCY/wxECGAnalyzer :: embedded/stm32f407 - 360Hz/src/stm32f4/algorithm (adaptive_algorithm)

; 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"

@rms_count = external hidden global i32, align 4
@rms_sum = external hidden global float, align 4
@rms = external hidden global float, align 4

; Function Attrs: nounwind uwtable
define dso_local float @CalculateRootMeanSquare(float noundef %0) #0 {
  %2 = alloca float, align 4
  store float %0, ptr %2, align 4, !tbaa !5
  %3 = load float, ptr %2, align 4, !tbaa !5
  %4 = fdiv float %3, 1.000000e+03
  store float %4, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr @rms_count, align 4, !tbaa !9
  %6 = icmp ult i32 %5, 360
  br i1 %6, label %7, label %14

7:                                                ; preds = %1
  %8 = load float, ptr %2, align 4, !tbaa !5
  %9 = load float, ptr %2, align 4, !tbaa !5
  %10 = load float, ptr @rms_sum, align 4, !tbaa !5
  %11 = call float @llvm.fmuladd.f32(float %8, float %9, float %10)
  store float %11, ptr @rms_sum, align 4, !tbaa !5
  %12 = load i32, ptr @rms_count, align 4, !tbaa !9
  %13 = add i32 %12, 1
  store i32 %13, ptr @rms_count, align 4, !tbaa !9
  br label %20

14:                                               ; preds = %1
  %15 = load float, ptr @rms_sum, align 4, !tbaa !5
  %16 = fdiv float %15, 3.600000e+02
  %17 = fpext float %16 to double
  %18 = call double @sqrt(double noundef %17) #3
  %19 = fptrunc double %18 to float
  store float %19, ptr @rms, align 4, !tbaa !5
  store i32 0, ptr @rms_count, align 4, !tbaa !9
  store float 0.000000e+00, ptr @rms_sum, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %14, %7
  %21 = load float, ptr @rms, align 4, !tbaa !5
  %22 = fmul float %21, 1.000000e+03
  ret float %22
}

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

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


; 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"

@rms_count = external hidden local_unnamed_addr global i32, align 4
@rms_sum = external hidden local_unnamed_addr global float, align 4
@rms = external hidden local_unnamed_addr global float, align 4

; Function Attrs: mustprogress nofree nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: write) uwtable
define dso_local float @CalculateRootMeanSquare(float noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @rms_count, align 4, !tbaa !5
  %3 = icmp ult i32 %2, 360
  br i1 %3, label %4, label %9

4:                                                ; preds = %1
  %5 = fdiv float %0, 1.000000e+03
  %6 = load float, ptr @rms_sum, align 4, !tbaa !9
  %7 = tail call float @llvm.fmuladd.f32(float %5, float %5, float %6)
  %8 = add nuw nsw i32 %2, 1
  %.pre = load float, ptr @rms, align 4, !tbaa !9
  br label %12

9:                                                ; preds = %1
  %10 = load float, ptr @rms_sum, align 4, !tbaa !9
  %11 = fdiv float %10, 3.600000e+02
  %sqrtf = tail call float @sqrtf(float noundef %11) #2
  store float %sqrtf, ptr @rms, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %9, %4
  %13 = phi float [ %.pre, %4 ], [ %sqrtf, %9 ]
  %.sink3 = phi float [ %7, %4 ], [ 0.000000e+00, %9 ]
  %.sink = phi i32 [ %8, %4 ], [ 0, %9 ]
  store float %.sink3, ptr @rms_sum, align 4
  store i32 %.sink, ptr @rms_count, align 4
  %14 = fmul float %13, 1.000000e+03
  ret float %14
}

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

declare float @sqrtf(float) local_unnamed_addr

attributes #0 = { mustprogress nofree nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: write) 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 nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass