; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/edge-impulse-sdk/CMSIS/NN/Source/ActivationFunctions (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"

@sigmoidTable_q7 = external constant [256 x i8], align 16
@tanhTable_q7 = external constant [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @arm_nn_activations_direct_q7(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca i16, align 2
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i8, align 1
  %13 = alloca i8, align 1
  %14 = alloca i16, align 2
  %15 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !9
  store i16 %2, ptr %7, align 2, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  %16 = load i16, ptr %6, align 2, !tbaa !9
  store i16 %16, ptr %9, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %17, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %18, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #2
  %19 = load i16, ptr %7, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = sub nsw i32 3, %20
  %22 = trunc i32 %21 to i16
  store i16 %22, ptr %14, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %23 = load i32, ptr %8, align 4, !tbaa !11
  switch i32 %23, label %26 [
    i32 0, label %24
    i32 1, label %25
  ]

24:                                               ; preds = %4
  store ptr @sigmoidTable_q7, ptr %15, align 8, !tbaa !5
  br label %27

25:                                               ; preds = %4
  br label %26

26:                                               ; preds = %25, %4
  store ptr @tanhTable_q7, ptr %15, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %26, %24
  br label %28

28:                                               ; preds = %31, %27
  %29 = load i16, ptr %9, align 2, !tbaa !9
  %30 = icmp ne i16 %29, 0
  br i1 %30, label %31, label %50

31:                                               ; preds = %28
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i32 1
  store ptr %33, ptr %10, align 8, !tbaa !5
  %34 = load i8, ptr %32, align 1, !tbaa !13
  store i8 %34, ptr %12, align 1, !tbaa !13
  %35 = load ptr, ptr %15, align 8, !tbaa !5
  %36 = load i8, ptr %12, align 1, !tbaa !13
  %37 = sext i8 %36 to i32
  %38 = load i16, ptr %14, align 2, !tbaa !9
  %39 = zext i16 %38 to i32
  %40 = ashr i32 %37, %39
  %41 = trunc i32 %40 to i8
  %42 = zext i8 %41 to i64
  %43 = getelementptr inbounds i8, ptr %35, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !13
  store i8 %44, ptr %13, align 1, !tbaa !13
  %45 = load i8, ptr %13, align 1, !tbaa !13
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i32 1
  store ptr %47, ptr %11, align 8, !tbaa !5
  store i8 %45, ptr %46, align 1, !tbaa !13
  %48 = load i16, ptr %9, align 2, !tbaa !9
  %49 = add i16 %48, -1
  store i16 %49, ptr %9, align 2, !tbaa !9
  br label %28, !llvm.loop !14

50:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, 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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@sigmoidTable_q7 = external local_unnamed_addr constant [256 x i8], align 16
@tanhTable_q7 = external local_unnamed_addr constant [256 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @arm_nn_activations_direct_q7(ptr nocapture noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i32 noundef %3) local_unnamed_addr #0 {
  %cond = icmp eq i32 %3, 0
  %sigmoidTable_q7.tanhTable_q7 = select i1 %cond, ptr @sigmoidTable_q7, ptr @tanhTable_q7
  %.not15 = icmp eq i16 %1, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %5 = sub i16 3, %2
  %6 = zext nneg i16 %5 to i32
  %xtraiter = and i16 %1, 3
  %lcmp.mod.not = icmp eq i16 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %.lr.ph, %.prol.preheader
  %.01218.prol = phi i16 [ %15, %.prol.preheader ], [ %1, %.lr.ph ]
  %.01317.prol = phi ptr [ %7, %.prol.preheader ], [ %0, %.lr.ph ]
  %prol.iter = phi i16 [ %prol.iter.next, %.prol.preheader ], [ 0, %.lr.ph ]
  %7 = getelementptr i8, ptr %.01317.prol, i64 1
  %8 = load i8, ptr %.01317.prol, align 1, !tbaa !5
  %9 = sext i8 %8 to i32
  %10 = ashr i32 %9, %6
  %11 = and i32 %10, 255
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %sigmoidTable_q7.tanhTable_q7, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !5
  store i8 %14, ptr %.01317.prol, align 1, !tbaa !5
  %15 = add i16 %.01218.prol, -1
  %prol.iter.next = add i16 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i16 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !8

.prol.loopexit:                                   ; preds = %.prol.preheader, %.lr.ph
  %.01218.unr = phi i16 [ %1, %.lr.ph ], [ %15, %.prol.preheader ]
  %.01317.unr = phi ptr [ %0, %.lr.ph ], [ %7, %.prol.preheader ]
  %16 = icmp ult i16 %1, 4
  br i1 %16, label %._crit_edge, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit, %.lr.ph.new
  %.01218 = phi i16 [ %49, %.lr.ph.new ], [ %.01218.unr, %.prol.loopexit ]
  %.01317 = phi ptr [ %41, %.lr.ph.new ], [ %.01317.unr, %.prol.loopexit ]
  %17 = getelementptr i8, ptr %.01317, i64 1
  %18 = load i8, ptr %.01317, align 1, !tbaa !5
  %19 = sext i8 %18 to i32
  %20 = ashr i32 %19, %6
  %21 = and i32 %20, 255
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %sigmoidTable_q7.tanhTable_q7, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !5
  store i8 %24, ptr %.01317, align 1, !tbaa !5
  %25 = getelementptr i8, ptr %.01317, i64 2
  %26 = load i8, ptr %17, align 1, !tbaa !5
  %27 = sext i8 %26 to i32
  %28 = ashr i32 %27, %6
  %29 = and i32 %28, 255
  %30 = zext nneg i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %sigmoidTable_q7.tanhTable_q7, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !5
  store i8 %32, ptr %17, align 1, !tbaa !5
  %33 = getelementptr i8, ptr %.01317, i64 3
  %34 = load i8, ptr %25, align 1, !tbaa !5
  %35 = sext i8 %34 to i32
  %36 = ashr i32 %35, %6
  %37 = and i32 %36, 255
  %38 = zext nneg i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %sigmoidTable_q7.tanhTable_q7, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !5
  store i8 %40, ptr %25, align 1, !tbaa !5
  %41 = getelementptr i8, ptr %.01317, i64 4
  %42 = load i8, ptr %33, align 1, !tbaa !5
  %43 = sext i8 %42 to i32
  %44 = ashr i32 %43, %6
  %45 = and i32 %44, 255
  %46 = zext nneg i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %sigmoidTable_q7.tanhTable_q7, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !5
  store i8 %48, ptr %33, align 1, !tbaa !5
  %49 = add i16 %.01218, -4
  %.not.3 = icmp eq i16 %49, 0
  br i1 %.not.3, label %._crit_edge, label %.lr.ph.new, !llvm.loop !10

._crit_edge:                                      ; preds = %.prol.loopexit, %.lr.ph.new, %4
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(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" }

!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}
!9 = !{!"llvm.loop.unroll.disable"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass