; LLVM IR from FedericaPaoli1/stm32-speech-recognition-and-traduction :: Drivers/CMSIS/DSP/Source/StatisticsFunctions (arm_max_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_max_q15(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds i16, ptr %13, i32 1
  store ptr %14, ptr %5, align 8, !tbaa !5
  %15 = load i16, ptr %13, align 2, !tbaa !11
  store i16 %15, ptr %10, align 2, !tbaa !11
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sub i32 %16, 1
  store i32 %17, ptr %11, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %35, %4
  %19 = load i32, ptr %11, align 4, !tbaa !9
  %20 = icmp ugt i32 %19, 0
  br i1 %20, label %21, label %38

21:                                               ; preds = %18
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds i16, ptr %22, i32 1
  store ptr %23, ptr %5, align 8, !tbaa !5
  %24 = load i16, ptr %22, align 2, !tbaa !11
  store i16 %24, ptr %9, align 2, !tbaa !11
  %25 = load i16, ptr %10, align 2, !tbaa !11
  %26 = sext i16 %25 to i32
  %27 = load i16, ptr %9, align 2, !tbaa !11
  %28 = sext i16 %27 to i32
  %29 = icmp slt i32 %26, %28
  br i1 %29, label %30, label %35

30:                                               ; preds = %21
  %31 = load i16, ptr %9, align 2, !tbaa !11
  store i16 %31, ptr %10, align 2, !tbaa !11
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = sub i32 %32, %33
  store i32 %34, ptr %12, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %30, %21
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = add i32 %36, -1
  store i32 %37, ptr %11, align 4, !tbaa !9
  br label %18, !llvm.loop !13

38:                                               ; preds = %18
  %39 = load i16, ptr %10, align 2, !tbaa !11
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  store i16 %39, ptr %40, align 2, !tbaa !11
  %41 = load i32, ptr %12, align 4, !tbaa !9
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %41, ptr %42, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  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 = { 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 = !{!"short", !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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @arm_max_q15(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = load i16, ptr %0, align 2, !tbaa !5
  %.01318 = add i32 %1, -1
  %.not19 = icmp eq i32 %.01318, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %xtraiter = and i32 %.01318, 1
  %6 = icmp eq i32 %1, 2
  br i1 %6, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %.01318, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01323 = phi i32 [ %.01318, %.lr.ph.preheader.new ], [ %.013.1, %.lr.ph ]
  %.022 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %spec.select17.1, %.lr.ph ]
  %.01421 = phi i16 [ %5, %.lr.ph.preheader.new ], [ %spec.select.1, %.lr.ph ]
  %.pn20 = phi ptr [ %0, %.lr.ph.preheader.new ], [ %.016.1, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %.016 = getelementptr inbounds i8, ptr %.pn20, i64 2
  %7 = load i16, ptr %.016, align 2, !tbaa !5
  %8 = icmp slt i16 %.01421, %7
  %9 = sub i32 %1, %.01323
  %spec.select = tail call i16 @llvm.smax.i16(i16 %.01421, i16 %7)
  %spec.select17 = select i1 %8, i32 %9, i32 %.022
  %.013 = add i32 %.01323, -1
  %.016.1 = getelementptr inbounds i8, ptr %.pn20, i64 4
  %10 = load i16, ptr %.016.1, align 2, !tbaa !5
  %11 = icmp slt i16 %spec.select, %10
  %12 = sub i32 %1, %.013
  %spec.select.1 = tail call i16 @llvm.smax.i16(i16 %spec.select, i16 %10)
  %spec.select17.1 = select i1 %11, i32 %12, i32 %spec.select17
  %.013.1 = add i32 %.01323, -2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %spec.select.lcssa.ph = phi i16 [ poison, %.lr.ph.preheader ], [ %spec.select.1, %.lr.ph ]
  %spec.select17.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %spec.select17.1, %.lr.ph ]
  %.01323.unr = phi i32 [ %.01318, %.lr.ph.preheader ], [ %.013.1, %.lr.ph ]
  %.022.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %spec.select17.1, %.lr.ph ]
  %.01421.unr = phi i16 [ %5, %.lr.ph.preheader ], [ %spec.select.1, %.lr.ph ]
  %.pn20.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %.016.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %.016.epil = getelementptr inbounds i8, ptr %.pn20.unr, i64 2
  %13 = load i16, ptr %.016.epil, align 2, !tbaa !5
  %14 = icmp slt i16 %.01421.unr, %13
  %15 = sub i32 %1, %.01323.unr
  %spec.select.epil = tail call i16 @llvm.smax.i16(i16 %.01421.unr, i16 %13)
  %spec.select17.epil = select i1 %14, i32 %15, i32 %.022.unr
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %4
  %.014.lcssa = phi i16 [ %5, %4 ], [ %spec.select.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select.epil, %.lr.ph.epil ]
  %.0.lcssa = phi i32 [ 0, %4 ], [ %spec.select17.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select17.epil, %.lr.ph.epil ]
  store i16 %.014.lcssa, ptr %2, align 2, !tbaa !5
  store i32 %.0.lcssa, ptr %3, align 4, !tbaa !11
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smax.i16(i16, i16) #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}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


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