; LLVM IR from tensorflow/tflite-micro :: signal/src/energy.ll (ZN6tflite11tflm_signal16SpectrumToEnergyEPK7ComplexIsEiiPj)

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

%struct.Complex = type { i16, i16 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN6tflite11tflm_signal16SpectrumToEnergyEPK7ComplexIsEiiPj(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %12 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %12, ptr %9, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %46, %4
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %49

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds %struct.Complex, ptr %19, i64 %21
  %23 = getelementptr inbounds %struct.Complex, ptr %22, i32 0, i32 0
  %24 = load i16, ptr %23, align 2, !tbaa !11
  store i16 %24, ptr %10, align 2, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.Complex, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.Complex, ptr %28, i32 0, i32 1
  %30 = load i16, ptr %29, align 2, !tbaa !15
  store i16 %30, ptr %11, align 2, !tbaa !14
  %31 = load i16, ptr %10, align 2, !tbaa !14
  %32 = sext i16 %31 to i32
  %33 = load i16, ptr %10, align 2, !tbaa !14
  %34 = sext i16 %33 to i32
  %35 = mul nsw i32 %32, %34
  %36 = load i16, ptr %11, align 2, !tbaa !14
  %37 = sext i16 %36 to i32
  %38 = load i16, ptr %11, align 2, !tbaa !14
  %39 = sext i16 %38 to i32
  %40 = mul nsw i32 %37, %39
  %41 = add nsw i32 %35, %40
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i32, ptr %42, i64 %44
  store i32 %41, ptr %45, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  br label %46

46:                                               ; preds = %18
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %9, align 4, !tbaa !9
  br label %13, !llvm.loop !16

49:                                               ; preds = %17
  ret void
}

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS7ComplexIsE", !13, i64 0, !13, i64 2}
!13 = !{!"short", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !13, i64 2}
!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"

%struct.Complex = type { i16, i16 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_ZN6tflite11tflm_signal16SpectrumToEnergyEPK7ComplexIsEiiPj(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp slt i32 %1, %2
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %6 = sext i32 %1 to i64
  %wide.trip.count = sext i32 %2 to i64
  %7 = sub nsw i64 %wide.trip.count, %6
  %min.iters.check = icmp ult i64 %7, 8
  br i1 %min.iters.check, label %.lr.ph.preheader19, label %vector.ph

.lr.ph.preheader19:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ %6, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %7, -8
  %ind.end = add nsw i64 %n.vec, %6
  %invariant.gep = getelementptr i8, ptr %0, i64 16
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %index, %6
  %8 = getelementptr inbounds %struct.Complex, ptr %0, i64 %offset.idx
  %gep = getelementptr %struct.Complex, ptr %invariant.gep, i64 %offset.idx
  %wide.vec = load <8 x i16>, ptr %8, align 2, !tbaa !5
  %wide.vec15 = load <8 x i16>, ptr %gep, align 2, !tbaa !5
  %strided.vec = shufflevector <8 x i16> %wide.vec, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec16 = shufflevector <8 x i16> %wide.vec15, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
  %strided.vec17 = shufflevector <8 x i16> %wide.vec, <8 x i16> poison, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
  %strided.vec18 = shufflevector <8 x i16> %wide.vec15, <8 x i16> poison, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
  %9 = sext <4 x i16> %strided.vec to <4 x i32>
  %10 = sext <4 x i16> %strided.vec16 to <4 x i32>
  %11 = mul nsw <4 x i32> %9, %9
  %12 = mul nsw <4 x i32> %10, %10
  %13 = sext <4 x i16> %strided.vec17 to <4 x i32>
  %14 = sext <4 x i16> %strided.vec18 to <4 x i32>
  %15 = mul nsw <4 x i32> %13, %13
  %16 = mul nsw <4 x i32> %14, %14
  %17 = add nuw nsw <4 x i32> %15, %11
  %18 = add nuw nsw <4 x i32> %16, %12
  %19 = getelementptr inbounds i32, ptr %3, i64 %offset.idx
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store <4 x i32> %17, ptr %19, align 4, !tbaa !9
  store <4 x i32> %18, ptr %20, align 4, !tbaa !9
  %index.next = add nuw i64 %index, 8
  %21 = icmp eq i64 %index.next, %n.vec
  br i1 %21, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %7, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader19

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %4
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader19, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader19 ]
  %22 = getelementptr inbounds %struct.Complex, ptr %0, i64 %indvars.iv
  %23 = load i16, ptr %22, align 2, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %22, i64 2
  %25 = load i16, ptr %24, align 2, !tbaa !17
  %26 = sext i16 %23 to i32
  %27 = mul nsw i32 %26, %26
  %28 = sext i16 %25 to i32
  %29 = mul nsw i32 %28, %28
  %30 = add nuw nsw i32 %29, %27
  %31 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  store i32 %30, ptr %31, align 4, !tbaa !9
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !18
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(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" }

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = !{!16, !6, i64 0}
!16 = !{!"_ZTS7ComplexIsE", !6, i64 0, !6, i64 2}
!17 = !{!16, !6, i64 2}
!18 = distinct !{!18, !12, !14, !13}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass