; LLVM IR from FedericaPaoli1/stm32-speech-recognition-and-traduction :: Drivers/CMSIS/DSP/Source/StatisticsFunctions (arm_var_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_var_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) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store i64 0, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #2
  %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 %66

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 = trunc i32 %63 to i16
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  store i16 %64, ptr %65, align 2, !tbaa !13
  store i32 0, ptr %13, align 4
  br label %66

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

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

69:                                               ; preds = %66
  unreachable
}

; 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 = { 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/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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @arm_var_q15(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = icmp ult i32 %1, 2
  br i1 %4, label %42, 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

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

.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

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

.loopexit:                                        ; preds = %.preheader, %middle.block
  %.lcssa29 = phi i64 [ %19, %middle.block ], [ %26, %.preheader ]
  %.lcssa = phi i32 [ %20, %middle.block ], [ %27, %.preheader ]
  %29 = add i32 %1, -1
  %30 = zext i32 %29 to i64
  %31 = udiv i64 %.lcssa29, %30
  %32 = trunc i64 %31 to i32
  %33 = sext i32 %.lcssa to i64
  %34 = mul nsw i64 %33, %33
  %35 = mul i32 %29, %1
  %36 = zext i32 %35 to i64
  %37 = udiv i64 %34, %36
  %38 = trunc i64 %37 to i32
  %39 = sub nsw i32 %32, %38
  %40 = lshr i32 %39, 15
  %41 = trunc i32 %40 to i16
  br label %42

42:                                               ; preds = %3, %.loopexit
  %storemerge = phi i16 [ %41, %.loopexit ], [ 0, %3 ]
  store i16 %storemerge, ptr %2, align 2, !tbaa !5
  ret void
}

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

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

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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) }

!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