; LLVM IR from ubaldot/defrecs :: stm32f4x/ADC_Readings/Drivers/CMSIS/DSP/Source/StatisticsFunctions (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_absmax_no_idx_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 i16, align 2
  %8 = alloca i16, align 2
  %9 = 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 2, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = load i16, ptr %10, align 2, !tbaa !11
  %12 = sext i16 %11 to i32
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %18

14:                                               ; preds = %3
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i16, ptr %15, align 2, !tbaa !11
  %17 = sext i16 %16 to i32
  br label %31

18:                                               ; preds = %3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i16, ptr %19, align 2, !tbaa !11
  %21 = sext i16 %20 to i32
  %22 = icmp eq i32 %21, -32768
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  br label %29

24:                                               ; preds = %18
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = sext i16 %26 to i32
  %28 = sub nsw i32 0, %27
  br label %29

29:                                               ; preds = %24, %23
  %30 = phi i32 [ 32767, %23 ], [ %28, %24 ]
  br label %31

31:                                               ; preds = %29, %14
  %32 = phi i32 [ %17, %14 ], [ %30, %29 ]
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %8, align 2, !tbaa !11
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds i16, ptr %34, i32 1
  store ptr %35, ptr %4, align 8, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = sub i32 %36, 1
  store i32 %37, ptr %9, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %75, %31
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = icmp ugt i32 %39, 0
  br i1 %40, label %41, label %78

41:                                               ; preds = %38
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i16, ptr %42, align 2, !tbaa !11
  %44 = sext i16 %43 to i32
  %45 = icmp sgt i32 %44, 0
  br i1 %45, label %46, label %50

46:                                               ; preds = %41
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load i16, ptr %47, align 2, !tbaa !11
  %49 = sext i16 %48 to i32
  br label %63

50:                                               ; preds = %41
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i16, ptr %51, align 2, !tbaa !11
  %53 = sext i16 %52 to i32
  %54 = icmp eq i32 %53, -32768
  br i1 %54, label %55, label %56

55:                                               ; preds = %50
  br label %61

56:                                               ; preds = %50
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load i16, ptr %57, align 2, !tbaa !11
  %59 = sext i16 %58 to i32
  %60 = sub nsw i32 0, %59
  br label %61

61:                                               ; preds = %56, %55
  %62 = phi i32 [ 32767, %55 ], [ %60, %56 ]
  br label %63

63:                                               ; preds = %61, %46
  %64 = phi i32 [ %49, %46 ], [ %62, %61 ]
  %65 = trunc i32 %64 to i16
  store i16 %65, ptr %7, align 2, !tbaa !11
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds i16, ptr %66, i32 1
  store ptr %67, ptr %4, align 8, !tbaa !5
  %68 = load i16, ptr %8, align 2, !tbaa !11
  %69 = sext i16 %68 to i32
  %70 = load i16, ptr %7, align 2, !tbaa !11
  %71 = sext i16 %70 to i32
  %72 = icmp slt i32 %69, %71
  br i1 %72, label %73, label %75

73:                                               ; preds = %63
  %74 = load i16, ptr %7, align 2, !tbaa !11
  store i16 %74, ptr %8, align 2, !tbaa !11
  br label %75

75:                                               ; preds = %73, %63
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = add i32 %76, -1
  store i32 %77, ptr %9, align 4, !tbaa !9
  br label %38, !llvm.loop !13

78:                                               ; preds = %38
  %79 = load i16, ptr %8, align 2, !tbaa !11
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  store i16 %79, ptr %80, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #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_absmax_no_idx_q15(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load i16, ptr %0, align 2, !tbaa !5
  %5 = icmp sgt i16 %4, 0
  %6 = icmp eq i16 %4, -32768
  %7 = sub i16 0, %4
  %spec.select = select i1 %6, i16 32767, i16 %7
  %8 = select i1 %5, i16 %4, i16 %spec.select
  %.025 = add i32 %1, -1
  %.not26 = icmp eq i32 %.025, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %9 = zext i32 %.025 to i64
  %min.iters.check = icmp ult i32 %.025, 16
  br i1 %min.iters.check, label %.lr.ph.preheader35, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %9, 4294967280
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end = sub i32 %.025, %.cast
  %10 = shl nuw nsw i64 %n.vec, 1
  %ind.end30 = getelementptr i8, ptr %0, i64 %10
  %minmax.ident.splatinsert = insertelement <8 x i16> poison, i16 %8, i64 0
  %minmax.ident.splat = shufflevector <8 x i16> %minmax.ident.splatinsert, <8 x i16> poison, <8 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <8 x i16> [ %minmax.ident.splat, %vector.ph ], [ %23, %vector.body ]
  %vec.phi32 = phi <8 x i16> [ %minmax.ident.splat, %vector.ph ], [ %24, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %11 = getelementptr inbounds i8, ptr %next.gep, i64 2
  %12 = getelementptr inbounds i8, ptr %next.gep, i64 18
  %wide.load = load <8 x i16>, ptr %11, align 2, !tbaa !5
  %wide.load34 = load <8 x i16>, ptr %12, align 2, !tbaa !5
  %13 = icmp sgt <8 x i16> %wide.load, zeroinitializer
  %14 = icmp sgt <8 x i16> %wide.load34, zeroinitializer
  %15 = icmp eq <8 x i16> %wide.load, <i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768>
  %16 = icmp eq <8 x i16> %wide.load34, <i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768, i16 -32768>
  %17 = sub <8 x i16> zeroinitializer, %wide.load
  %18 = sub <8 x i16> zeroinitializer, %wide.load34
  %19 = select <8 x i1> %15, <8 x i16> <i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767>, <8 x i16> %17
  %20 = select <8 x i1> %16, <8 x i16> <i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767, i16 32767>, <8 x i16> %18
  %21 = select <8 x i1> %13, <8 x i16> %wide.load, <8 x i16> %19
  %22 = select <8 x i1> %14, <8 x i16> %wide.load34, <8 x i16> %20
  %23 = tail call <8 x i16> @llvm.umax.v8i16(<8 x i16> %vec.phi, <8 x i16> %21)
  %24 = tail call <8 x i16> @llvm.umax.v8i16(<8 x i16> %vec.phi32, <8 x i16> %22)
  %index.next = add nuw i64 %index, 16
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <8 x i16> @llvm.umax.v8i16(<8 x i16> %23, <8 x i16> %24)
  %26 = tail call i16 @llvm.vector.reduce.umax.v8i16(<8 x i16> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %9
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader35

.lr.ph.preheader35:                               ; preds = %middle.block, %.lr.ph.preheader
  %.029.ph = phi i32 [ %.025, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01728.ph = phi i16 [ %8, %.lr.ph.preheader ], [ %26, %middle.block ]
  %.pn27.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end30, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader35, %.lr.ph
  %.029 = phi i32 [ %.0, %.lr.ph ], [ %.029.ph, %.lr.ph.preheader35 ]
  %.01728 = phi i16 [ %spec.select24, %.lr.ph ], [ %.01728.ph, %.lr.ph.preheader35 ]
  %.pn27 = phi ptr [ %.018, %.lr.ph ], [ %.pn27.ph, %.lr.ph.preheader35 ]
  %.018 = getelementptr inbounds i8, ptr %.pn27, i64 2
  %27 = load i16, ptr %.018, align 2, !tbaa !5
  %28 = icmp sgt i16 %27, 0
  %29 = icmp eq i16 %27, -32768
  %30 = sub i16 0, %27
  %spec.select23 = select i1 %29, i16 32767, i16 %30
  %31 = select i1 %28, i16 %27, i16 %spec.select23
  %spec.select24 = tail call i16 @llvm.umax.i16(i16 %.01728, i16 %31)
  %.0 = add i32 %.029, -1
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  %.017.lcssa = phi i16 [ %8, %3 ], [ %26, %middle.block ], [ %spec.select24, %.lr.ph ]
  store i16 %.017.lcssa, ptr %2, align 2, !tbaa !5
  ret void
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <8 x i16> @llvm.umax.v8i16(<8 x i16>, <8 x i16>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.vector.reduce.umax.v8i16(<8 x 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, !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,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass