; LLVM IR from Seeed-Studio/Seeed_Arduino_edgeimpulse :: src/edge-impulse-sdk/CMSIS/NN/Source/FullyConnectedFunctions (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 i32 @arm_fully_connected_q7(ptr noundef %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %5, ptr noundef %6, ptr noundef %7, ptr noundef %8) #0 {
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i16, align 2
  %13 = alloca i16, align 2
  %14 = alloca i16, align 2
  %15 = alloca i16, align 2
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  %19 = alloca i32, align 4
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  store ptr %0, ptr %10, align 8, !tbaa !5
  store ptr %1, ptr %11, align 8, !tbaa !5
  store i16 %2, ptr %12, align 2, !tbaa !9
  store i16 %3, ptr %13, align 2, !tbaa !9
  store i16 %4, ptr %14, align 2, !tbaa !9
  store i16 %5, ptr %15, align 2, !tbaa !9
  store ptr %6, ptr %16, align 8, !tbaa !5
  store ptr %7, ptr %17, align 8, !tbaa !5
  store ptr %8, ptr %18, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #3
  store i32 0, ptr %19, align 4, !tbaa !11
  br label %22

22:                                               ; preds = %82, %9
  %23 = load i32, ptr %19, align 4, !tbaa !11
  %24 = load i16, ptr %13, align 2, !tbaa !9
  %25 = zext i16 %24 to i32
  %26 = icmp slt i32 %23, %25
  br i1 %26, label %27, label %85

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #3
  %28 = load ptr, ptr %16, align 8, !tbaa !5
  %29 = load i32, ptr %19, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %28, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = sext i8 %32 to i32
  %34 = load i16, ptr %14, align 2, !tbaa !9
  %35 = zext i16 %34 to i32
  %36 = shl i32 %33, %35
  %37 = load i16, ptr %15, align 2, !tbaa !9
  %38 = zext i16 %37 to i32
  %39 = shl i32 1, %38
  %40 = lshr i32 %39, 1
  %41 = add i32 %36, %40
  store i32 %41, ptr %21, align 4, !tbaa !11
  store i32 0, ptr %20, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %68, %27
  %43 = load i32, ptr %20, align 4, !tbaa !11
  %44 = load i16, ptr %12, align 2, !tbaa !9
  %45 = zext i16 %44 to i32
  %46 = icmp slt i32 %43, %45
  br i1 %46, label %47, label %71

47:                                               ; preds = %42
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = load i32, ptr %20, align 4, !tbaa !11
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !13
  %53 = sext i8 %52 to i32
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = load i32, ptr %19, align 4, !tbaa !11
  %56 = load i16, ptr %12, align 2, !tbaa !9
  %57 = zext i16 %56 to i32
  %58 = mul nsw i32 %55, %57
  %59 = load i32, ptr %20, align 4, !tbaa !11
  %60 = add nsw i32 %58, %59
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %54, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !13
  %64 = sext i8 %63 to i32
  %65 = mul nsw i32 %53, %64
  %66 = load i32, ptr %21, align 4, !tbaa !11
  %67 = add nsw i32 %66, %65
  store i32 %67, ptr %21, align 4, !tbaa !11
  br label %68

68:                                               ; preds = %47
  %69 = load i32, ptr %20, align 4, !tbaa !11
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %20, align 4, !tbaa !11
  br label %42, !llvm.loop !14

71:                                               ; preds = %42
  %72 = load i32, ptr %21, align 4, !tbaa !11
  %73 = load i16, ptr %15, align 2, !tbaa !9
  %74 = zext i16 %73 to i32
  %75 = ashr i32 %72, %74
  %76 = call i32 @__SSAT(i32 noundef %75, i32 noundef 8)
  %77 = trunc i32 %76 to i8
  %78 = load ptr, ptr %17, align 8, !tbaa !5
  %79 = load i32, ptr %19, align 4, !tbaa !11
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i8, ptr %78, i64 %80
  store i8 %77, ptr %81, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #3
  br label %82

82:                                               ; preds = %71
  %83 = load i32, ptr %19, align 4, !tbaa !11
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %19, align 4, !tbaa !11
  br label %22, !llvm.loop !16

85:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #3
  ret i32 0
}

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


; 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 noundef i32 @arm_fully_connected_q7(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i16 noundef zeroext %5, ptr nocapture noundef readonly %6, ptr nocapture noundef writeonly %7, ptr nocapture noundef readnone %8) local_unnamed_addr #0 {
  %.not = icmp eq i16 %3, 0
  br i1 %.not, label %._crit_edge28, label %.lr.ph27

.lr.ph27:                                         ; preds = %9
  %10 = zext nneg i16 %4 to i32
  %11 = zext i16 %5 to i32
  %12 = shl nuw i32 1, %11
  %13 = lshr i32 %12, 1
  %.not29 = icmp eq i16 %2, 0
  br i1 %.not29, label %.lr.ph27.split.preheader, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %.lr.ph27
  %14 = zext i16 %2 to i64
  %wide.trip.count35 = zext i16 %3 to i64
  %min.iters.check = icmp ult i16 %2, 8
  %n.vec = and i64 %14, 65528
  %cmp.n = icmp eq i64 %n.vec, %14
  br label %.lr.ph.us

.lr.ph27.split.preheader:                         ; preds = %.lr.ph27
  %wide.trip.count40 = zext i16 %3 to i64
  br label %.lr.ph27.split

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %indvars.iv32 = phi i64 [ 0, %.lr.ph.us.preheader ], [ %indvars.iv.next33, %._crit_edge.us ]
  %15 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv32
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = sext i8 %16 to i32
  %18 = shl i32 %17, %10
  %19 = add i32 %18, %13
  %20 = mul nuw nsw i64 %indvars.iv32, %14
  %invariant.gep = getelementptr inbounds i8, ptr %1, i64 %20
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.us
  %21 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %19, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %21, %vector.ph ], [ %32, %vector.body ]
  %vec.phi44 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %33, %vector.body ]
  %22 = getelementptr inbounds i8, ptr %0, i64 %index
  %23 = getelementptr inbounds i8, ptr %22, i64 4
  %wide.load = load <4 x i8>, ptr %22, align 1, !tbaa !5
  %wide.load45 = load <4 x i8>, ptr %23, align 1, !tbaa !5
  %24 = sext <4 x i8> %wide.load to <4 x i32>
  %25 = sext <4 x i8> %wide.load45 to <4 x i32>
  %26 = getelementptr inbounds i8, ptr %invariant.gep, i64 %index
  %27 = getelementptr inbounds i8, ptr %26, i64 4
  %wide.load46 = load <4 x i8>, ptr %26, align 1, !tbaa !5
  %wide.load47 = load <4 x i8>, ptr %27, align 1, !tbaa !5
  %28 = sext <4 x i8> %wide.load46 to <4 x i32>
  %29 = sext <4 x i8> %wide.load47 to <4 x i32>
  %30 = mul nsw <4 x i32> %28, %24
  %31 = mul nsw <4 x i32> %29, %25
  %32 = add <4 x i32> %30, %vec.phi
  %33 = add <4 x i32> %31, %vec.phi44
  %index.next = add nuw i64 %index, 8
  %34 = icmp eq i64 %index.next, %n.vec
  br i1 %34, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %33, %32
  %35 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.us ], [ %n.vec, %middle.block ]
  %.024.us.ph = phi i32 [ %19, %.lr.ph.us ], [ %35, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.024.us = phi i32 [ %42, %scalar.ph ], [ %.024.us.ph, %scalar.ph.preheader ]
  %36 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %37 = load i8, ptr %36, align 1, !tbaa !5
  %38 = sext i8 %37 to i32
  %gep = getelementptr inbounds i8, ptr %invariant.gep, i64 %indvars.iv
  %39 = load i8, ptr %gep, align 1, !tbaa !5
  %40 = sext i8 %39 to i32
  %41 = mul nsw i32 %40, %38
  %42 = add nsw i32 %41, %.024.us
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %14
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !12

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %.lcssa = phi i32 [ %35, %middle.block ], [ %42, %scalar.ph ]
  %43 = ashr i32 %.lcssa, %11
  %44 = tail call i32 @__SSAT(i32 noundef %43, i32 noundef 8)
  %45 = trunc i32 %44 to i8
  %46 = getelementptr inbounds i8, ptr %7, i64 %indvars.iv32
  store i8 %45, ptr %46, align 1, !tbaa !5
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %._crit_edge28, label %.lr.ph.us, !llvm.loop !13

.lr.ph27.split:                                   ; preds = %.lr.ph27.split.preheader, %.lr.ph27.split
  %indvars.iv37 = phi i64 [ 0, %.lr.ph27.split.preheader ], [ %indvars.iv.next38, %.lr.ph27.split ]
  %47 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv37
  %48 = load i8, ptr %47, align 1, !tbaa !5
  %49 = sext i8 %48 to i32
  %50 = shl i32 %49, %10
  %51 = add i32 %50, %13
  %52 = ashr i32 %51, %11
  %53 = tail call i32 @__SSAT(i32 noundef %52, i32 noundef 8)
  %54 = trunc i32 %53 to i8
  %55 = getelementptr inbounds i8, ptr %7, i64 %indvars.iv37
  store i8 %54, ptr %55, align 1, !tbaa !5
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next38, %wide.trip.count40
  br i1 %exitcond41.not, label %._crit_edge28, label %.lr.ph27.split, !llvm.loop !13

._crit_edge28:                                    ; preds = %._crit_edge.us, %.lr.ph27.split, %9
  ret i32 0
}

; 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 i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #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, !11, !10}
!13 = distinct !{!13, !9}


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