; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/edge-impulse-sdk/CMSIS/NN/Source/SoftmaxFunctions (arm_softmax_q7)

; 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_softmax_q7(ptr noundef %0, i16 noundef zeroext %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i16, align 2
  %9 = alloca i8, align 1
  %10 = alloca i16, align 2
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  store i16 -128, ptr %10, align 2, !tbaa !9
  store i16 0, ptr %8, align 2, !tbaa !9
  br label %12

12:                                               ; preds = %36, %3
  %13 = load i16, ptr %8, align 2, !tbaa !9
  %14 = sext i16 %13 to i32
  %15 = load i16, ptr %5, align 2, !tbaa !9
  %16 = zext i16 %15 to i32
  %17 = icmp slt i32 %14, %16
  br i1 %17, label %18, label %39

18:                                               ; preds = %12
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i16, ptr %8, align 2, !tbaa !9
  %21 = sext i16 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = sext i8 %23 to i32
  %25 = load i16, ptr %10, align 2, !tbaa !9
  %26 = sext i16 %25 to i32
  %27 = icmp sgt i32 %24, %26
  br i1 %27, label %28, label %35

28:                                               ; preds = %18
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i16, ptr %8, align 2, !tbaa !9
  %31 = sext i16 %30 to i64
  %32 = getelementptr inbounds i8, ptr %29, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = sext i8 %33 to i16
  store i16 %34, ptr %10, align 2, !tbaa !9
  br label %35

35:                                               ; preds = %28, %18
  br label %36

36:                                               ; preds = %35
  %37 = load i16, ptr %8, align 2, !tbaa !9
  %38 = add i16 %37, 1
  store i16 %38, ptr %8, align 2, !tbaa !9
  br label %12, !llvm.loop !12

39:                                               ; preds = %12
  %40 = load i16, ptr %10, align 2, !tbaa !9
  %41 = sext i16 %40 to i32
  %42 = sub nsw i32 %41, 8
  %43 = trunc i32 %42 to i16
  store i16 %43, ptr %10, align 2, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !14
  store i16 0, ptr %8, align 2, !tbaa !9
  br label %44

44:                                               ; preds = %67, %39
  %45 = load i16, ptr %8, align 2, !tbaa !9
  %46 = sext i16 %45 to i32
  %47 = load i16, ptr %5, align 2, !tbaa !9
  %48 = zext i16 %47 to i32
  %49 = icmp slt i32 %46, %48
  br i1 %49, label %50, label %70

50:                                               ; preds = %44
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i16, ptr %8, align 2, !tbaa !9
  %53 = sext i16 %52 to i64
  %54 = getelementptr inbounds i8, ptr %51, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !11
  %56 = sext i8 %55 to i32
  %57 = load i16, ptr %10, align 2, !tbaa !9
  %58 = sext i16 %57 to i32
  %59 = sub nsw i32 %56, %58
  %60 = call i32 @__USAT(i32 noundef %59, i32 noundef 3)
  %61 = trunc i32 %60 to i8
  store i8 %61, ptr %9, align 1, !tbaa !11
  %62 = load i8, ptr %9, align 1, !tbaa !11
  %63 = zext i8 %62 to i32
  %64 = shl i32 1, %63
  %65 = load i32, ptr %7, align 4, !tbaa !14
  %66 = add nsw i32 %65, %64
  store i32 %66, ptr %7, align 4, !tbaa !14
  br label %67

67:                                               ; preds = %50
  %68 = load i16, ptr %8, align 2, !tbaa !9
  %69 = add i16 %68, 1
  store i16 %69, ptr %8, align 2, !tbaa !9
  br label %44, !llvm.loop !16

70:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %71 = load i32, ptr %7, align 4, !tbaa !14
  %72 = sdiv i32 1048576, %71
  store i32 %72, ptr %11, align 4, !tbaa !14
  store i16 0, ptr %8, align 2, !tbaa !9
  br label %73

73:                                               ; preds = %102, %70
  %74 = load i16, ptr %8, align 2, !tbaa !9
  %75 = sext i16 %74 to i32
  %76 = load i16, ptr %5, align 2, !tbaa !9
  %77 = zext i16 %76 to i32
  %78 = icmp slt i32 %75, %77
  br i1 %78, label %79, label %105

79:                                               ; preds = %73
  %80 = load i16, ptr %10, align 2, !tbaa !9
  %81 = sext i16 %80 to i32
  %82 = add nsw i32 13, %81
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = load i16, ptr %8, align 2, !tbaa !9
  %85 = sext i16 %84 to i64
  %86 = getelementptr inbounds i8, ptr %83, i64 %85
  %87 = load i8, ptr %86, align 1, !tbaa !11
  %88 = sext i8 %87 to i32
  %89 = sub nsw i32 %82, %88
  %90 = call i32 @__USAT(i32 noundef %89, i32 noundef 5)
  %91 = trunc i32 %90 to i8
  store i8 %91, ptr %9, align 1, !tbaa !11
  %92 = load i32, ptr %11, align 4, !tbaa !14
  %93 = load i8, ptr %9, align 1, !tbaa !11
  %94 = zext i8 %93 to i32
  %95 = ashr i32 %92, %94
  %96 = call i32 @__SSAT(i32 noundef %95, i32 noundef 8)
  %97 = trunc i32 %96 to i8
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = load i16, ptr %8, align 2, !tbaa !9
  %100 = sext i16 %99 to i64
  %101 = getelementptr inbounds i8, ptr %98, i64 %100
  store i8 %97, ptr %101, align 1, !tbaa !11
  br label %102

102:                                              ; preds = %79
  %103 = load i16, ptr %8, align 2, !tbaa !9
  %104 = add i16 %103, 1
  store i16 %104, ptr %8, align 2, !tbaa !9
  br label %73, !llvm.loop !17

105:                                              ; preds = %73
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  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: alwaysinline nounwind uwtable
declare hidden i32 @__USAT(i32 noundef, i32 noundef) #2

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

; 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 = { 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 = { 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 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !13}
!17 = distinct !{!17, !13}


; 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_softmax_q7(ptr nocapture noundef readonly %0, i16 noundef zeroext %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = zext i16 %1 to i32
  %.not = icmp eq i16 %1, 0
  br i1 %.not, label %._crit_edge43, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %min.iters.check = icmp ult i16 %1, 16
  br i1 %min.iters.check, label %.lr.ph.preheader52, label %vector.scevcheck

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %5 = add nsw i32 %4, -1
  %6 = trunc i32 %5 to i16
  %7 = icmp ugt i16 %6, 32766
  %8 = icmp ugt i32 %5, 32767
  %9 = or i1 %7, %8
  br i1 %9, label %.lr.ph.preheader52, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i32 %4, 65520
  %ind.end = trunc nuw i32 %n.vec to i16
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <8 x i16> [ <i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128>, %vector.ph ], [ %15, %vector.body ]
  %vec.phi50 = phi <8 x i16> [ <i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128, i16 -128>, %vector.ph ], [ %16, %vector.body ]
  %offset.idx = zext i32 %index to i64
  %sext = shl i64 %offset.idx, 48
  %10 = ashr exact i64 %sext, 48
  %11 = getelementptr inbounds i8, ptr %0, i64 %10
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %wide.load = load <8 x i8>, ptr %11, align 1, !tbaa !5
  %wide.load51 = load <8 x i8>, ptr %12, align 1, !tbaa !5
  %13 = sext <8 x i8> %wide.load to <8 x i16>
  %14 = sext <8 x i8> %wide.load51 to <8 x i16>
  %15 = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %vec.phi, <8 x i16> %13)
  %16 = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %vec.phi50, <8 x i16> %14)
  %index.next = add nuw i32 %index, 16
  %17 = icmp eq i32 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <8 x i16> @llvm.smax.v8i16(<8 x i16> %15, <8 x i16> %16)
  %18 = tail call i16 @llvm.vector.reduce.smax.v8i16(<8 x i16> %rdx.minmax)
  %cmp.n = icmp eq i32 %n.vec, %4
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader52

.lr.ph.preheader52:                               ; preds = %middle.block, %vector.scevcheck, %.lr.ph.preheader
  %.02732.ph = phi i16 [ -128, %vector.scevcheck ], [ -128, %.lr.ph.preheader ], [ %18, %middle.block ]
  %.02831.ph = phi i16 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader52, %.lr.ph
  %.02732 = phi i16 [ %spec.select, %.lr.ph ], [ %.02732.ph, %.lr.ph.preheader52 ]
  %.02831 = phi i16 [ %23, %.lr.ph ], [ %.02831.ph, %.lr.ph.preheader52 ]
  %19 = sext i16 %.02831 to i64
  %20 = getelementptr inbounds i8, ptr %0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = sext i8 %21 to i16
  %spec.select = tail call i16 @llvm.smax.i16(i16 %.02732, i16 %22)
  %23 = add i16 %.02831, 1
  %24 = sext i16 %23 to i32
  %25 = icmp slt i32 %24, %4
  br i1 %25, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %middle.block
  %spec.select.lcssa = phi i16 [ %18, %middle.block ], [ %spec.select, %.lr.ph ]
  %26 = add nsw i16 %spec.select.lcssa, -8
  %27 = sext i16 %26 to i32
  br i1 %.not, label %._crit_edge43, label %.lr.ph37

.lr.ph37:                                         ; preds = %._crit_edge, %.lr.ph37
  %.035 = phi i32 [ %36, %.lr.ph37 ], [ 0, %._crit_edge ]
  %.12934 = phi i16 [ %37, %.lr.ph37 ], [ 0, %._crit_edge ]
  %28 = sext i16 %.12934 to i64
  %29 = getelementptr inbounds i8, ptr %0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = sext i8 %30 to i32
  %32 = sub nsw i32 %31, %27
  %33 = tail call i32 @__USAT(i32 noundef %32, i32 noundef 3)
  %34 = and i32 %33, 255
  %35 = shl nuw i32 1, %34
  %36 = add nsw i32 %35, %.035
  %37 = add i16 %.12934, 1
  %38 = sext i16 %37 to i32
  %39 = icmp slt i32 %38, %4
  br i1 %39, label %.lr.ph37, label %._crit_edge38, !llvm.loop !13

._crit_edge38:                                    ; preds = %.lr.ph37
  %40 = sdiv i32 1048576, %36
  br i1 %.not, label %._crit_edge43, label %.lr.ph42

.lr.ph42:                                         ; preds = %._crit_edge38
  %41 = add nsw i32 %27, 13
  br label %42

42:                                               ; preds = %.lr.ph42, %42
  %.240 = phi i16 [ 0, %.lr.ph42 ], [ %54, %42 ]
  %43 = sext i16 %.240 to i64
  %44 = getelementptr inbounds i8, ptr %0, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !5
  %46 = sext i8 %45 to i32
  %47 = sub nsw i32 %41, %46
  %48 = tail call i32 @__USAT(i32 noundef %47, i32 noundef 5)
  %49 = and i32 %48, 255
  %50 = ashr i32 %40, %49
  %51 = tail call i32 @__SSAT(i32 noundef %50, i32 noundef 8)
  %52 = trunc i32 %51 to i8
  %53 = getelementptr inbounds i8, ptr %2, i64 %43
  store i8 %52, ptr %53, align 1, !tbaa !5
  %54 = add i16 %.240, 1
  %55 = sext i16 %54 to i32
  %56 = icmp slt i32 %55, %4
  br i1 %56, label %42, label %._crit_edge43, !llvm.loop !14

._crit_edge43:                                    ; preds = %42, %3, %._crit_edge, %._crit_edge38
  ret void
}

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.vector.reduce.smax.v8i16(<8 x i16>) #2

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


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