; LLVM IR from FedericaPaoli1/stm32-speech-recognition-and-traduction :: Drivers/CMSIS/DSP/Source/StatisticsFunctions (arm_std_q15)

; 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: nounwind uwtable
define dso_local void @arm_std_q15(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i16, align 2
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #4
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp ule i32 %14, 1
  br i1 %15, label %16, label %18

16:                                               ; preds = %3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  store i16 0, ptr %17, align 2, !tbaa !13
  store i32 1, ptr %13, align 4
  br label %68

18:                                               ; preds = %3
  %19 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %19, ptr %7, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %23, %18
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp ugt i32 %21, 0
  br i1 %22, label %23, label %41

23:                                               ; preds = %20
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i16, ptr %24, i32 1
  store ptr %25, ptr %4, align 8, !tbaa !5
  %26 = load i16, ptr %24, align 2, !tbaa !13
  store i16 %26, ptr %12, align 2, !tbaa !13
  %27 = load i16, ptr %12, align 2, !tbaa !13
  %28 = sext i16 %27 to i32
  %29 = load i16, ptr %12, align 2, !tbaa !13
  %30 = sext i16 %29 to i32
  %31 = mul nsw i32 %28, %30
  %32 = sext i32 %31 to i64
  %33 = load i64, ptr %11, align 8, !tbaa !11
  %34 = add nsw i64 %33, %32
  store i64 %34, ptr %11, align 8, !tbaa !11
  %35 = load i16, ptr %12, align 2, !tbaa !13
  %36 = sext i16 %35 to i32
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = add nsw i32 %37, %36
  store i32 %38, ptr %8, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = add i32 %39, -1
  store i32 %40, ptr %7, align 4, !tbaa !9
  br label %20, !llvm.loop !15

41:                                               ; preds = %20
  %42 = load i64, ptr %11, align 8, !tbaa !11
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = sub i32 %43, 1
  %45 = zext i32 %44 to i64
  %46 = sdiv i64 %42, %45
  %47 = trunc i64 %46 to i32
  store i32 %47, ptr %9, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = mul nsw i64 %49, %51
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = sub i32 %54, 1
  %56 = mul i32 %53, %55
  %57 = zext i32 %56 to i64
  %58 = sdiv i64 %52, %57
  %59 = trunc i64 %58 to i32
  store i32 %59, ptr %10, align 4, !tbaa !9
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = load i32, ptr %10, align 4, !tbaa !9
  %62 = sub nsw i32 %60, %61
  %63 = ashr i32 %62, 15
  %64 = call i32 @__SSAT(i32 noundef %63, i32 noundef 16)
  %65 = trunc i32 %64 to i16
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = call i32 @arm_sqrt_q15(i16 noundef signext %65, ptr noundef %66)
  store i32 0, ptr %13, align 4
  br label %68

68:                                               ; preds = %41, %16
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %69 = load i32, ptr %13, align 4
  switch i32 %69, label %71 [
    i32 0, label %70
    i32 1, label %70
  ]

70:                                               ; preds = %68, %68
  ret void

71:                                               ; preds = %68
  unreachable
}

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

declare i32 @arm_sqrt_q15(i16 noundef signext, ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @__SSAT(i32 noundef, i32 noundef) #3

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

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 willreturn memory(argmem: readwrite) }
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 = { alwaysinline 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 = { 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 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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: nounwind uwtable
define dso_local void @arm_std_q15(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = icmp ult i32 %1, 2
  br i1 %4, label %21, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %5 = zext i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 4
  br i1 %min.iters.check, label %.preheader.preheader39, label %vector.ph

.preheader.preheader39:                           ; preds = %middle.block, %.preheader.preheader
  %.026.ph = phi ptr [ %0, %.preheader.preheader ], [ %ind.end, %middle.block ]
  %.01925.ph = phi i64 [ 0, %.preheader.preheader ], [ %19, %middle.block ]
  %.02024.ph = phi i32 [ %1, %.preheader.preheader ], [ %ind.end30, %middle.block ]
  %.02123.ph = phi i32 [ 0, %.preheader.preheader ], [ %20, %middle.block ]
  br label %.preheader

vector.ph:                                        ; preds = %.preheader.preheader
  %n.vec = and i64 %5, 4294967292
  %6 = shl nuw nsw i64 %n.vec, 1
  %ind.end = getelementptr i8, ptr %0, i64 %6
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end30 = sub i32 %1, %.cast
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %14, %vector.body ]
  %vec.phi32 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %15, %vector.body ]
  %vec.phi33 = phi <2 x i32> [ zeroinitializer, %vector.ph ], [ %16, %vector.body ]
  %vec.phi34 = phi <2 x i32> [ zeroinitializer, %vector.ph ], [ %17, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %7 = getelementptr i8, ptr %next.gep, i64 4
  %wide.load = load <2 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load36 = load <2 x i16>, ptr %7, align 2, !tbaa !5
  %8 = sext <2 x i16> %wide.load to <2 x i32>
  %9 = sext <2 x i16> %wide.load36 to <2 x i32>
  %10 = mul nsw <2 x i32> %8, %8
  %11 = mul nsw <2 x i32> %9, %9
  %12 = zext nneg <2 x i32> %10 to <2 x i64>
  %13 = zext nneg <2 x i32> %11 to <2 x i64>
  %14 = add <2 x i64> %vec.phi, %12
  %15 = add <2 x i64> %vec.phi32, %13
  %16 = add <2 x i32> %vec.phi33, %8
  %17 = add <2 x i32> %vec.phi34, %9
  %index.next = add nuw i64 %index, 4
  %18 = icmp eq i64 %index.next, %n.vec
  br i1 %18, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %15, %14
  %19 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %bin.rdx37 = add <2 x i32> %17, %16
  %20 = tail call i32 @llvm.vector.reduce.add.v2i32(<2 x i32> %bin.rdx37)
  %cmp.n = icmp eq i64 %n.vec, %5
  br i1 %cmp.n, label %.loopexit, label %.preheader.preheader39

21:                                               ; preds = %3
  store i16 0, ptr %2, align 2, !tbaa !5
  br label %45

.preheader:                                       ; preds = %.preheader.preheader39, %.preheader
  %.026 = phi ptr [ %22, %.preheader ], [ %.026.ph, %.preheader.preheader39 ]
  %.01925 = phi i64 [ %27, %.preheader ], [ %.01925.ph, %.preheader.preheader39 ]
  %.02024 = phi i32 [ %29, %.preheader ], [ %.02024.ph, %.preheader.preheader39 ]
  %.02123 = phi i32 [ %28, %.preheader ], [ %.02123.ph, %.preheader.preheader39 ]
  %22 = getelementptr inbounds i8, ptr %.026, i64 2
  %23 = load i16, ptr %.026, align 2, !tbaa !5
  %24 = sext i16 %23 to i32
  %25 = mul nsw i32 %24, %24
  %26 = zext nneg i32 %25 to i64
  %27 = add nuw nsw i64 %.01925, %26
  %28 = add nsw i32 %.02123, %24
  %29 = add i32 %.02024, -1
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !13

.loopexit:                                        ; preds = %.preheader, %middle.block
  %.lcssa29 = phi i64 [ %19, %middle.block ], [ %27, %.preheader ]
  %.lcssa = phi i32 [ %20, %middle.block ], [ %28, %.preheader ]
  %30 = add i32 %1, -1
  %31 = zext i32 %30 to i64
  %32 = udiv i64 %.lcssa29, %31
  %33 = trunc i64 %32 to i32
  %34 = sext i32 %.lcssa to i64
  %35 = mul nsw i64 %34, %34
  %36 = mul i32 %30, %1
  %37 = zext i32 %36 to i64
  %38 = udiv i64 %35, %37
  %39 = trunc i64 %38 to i32
  %40 = sub nsw i32 %33, %39
  %41 = ashr i32 %40, 15
  %42 = tail call i32 @__SSAT(i32 noundef %41, i32 noundef 16)
  %43 = trunc i32 %42 to i16
  %44 = tail call i32 @arm_sqrt_q15(i16 noundef signext %43, ptr noundef %2) #4
  br label %45

45:                                               ; preds = %.loopexit, %21
  ret void
}

declare i32 @arm_sqrt_q15(i16 noundef signext, ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @__SSAT(i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v2i32(<2 x i32>) #3

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 = { "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 = { alwaysinline 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass