; LLVM IR from ubaldot/defrecs :: stm32f4x/ADC_Readings/Drivers/CMSIS/NN/Source/FullyConnectedFunctions (s8)

; 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"

%struct.cmsis_nn_fc_params = type { i32, i32, i32, %struct.cmsis_nn_activation }
%struct.cmsis_nn_activation = type { i32, i32 }
%struct.cmsis_nn_dims = type { i32, i32, i32, i32 }
%struct.cmsis_nn_per_tensor_quant_params = type { i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @arm_fully_connected_s8(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6, ptr noundef %7, ptr noundef %8, ptr noundef %9, ptr noundef %10) #0 {
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  %19 = alloca ptr, align 8
  %20 = alloca ptr, align 8
  %21 = alloca ptr, align 8
  %22 = alloca ptr, align 8
  %23 = alloca i32, align 4
  store ptr %0, ptr %12, align 8, !tbaa !5
  store ptr %1, ptr %13, align 8, !tbaa !5
  store ptr %2, ptr %14, align 8, !tbaa !5
  store ptr %3, ptr %15, align 8, !tbaa !5
  store ptr %4, ptr %16, align 8, !tbaa !5
  store ptr %5, ptr %17, align 8, !tbaa !5
  store ptr %6, ptr %18, align 8, !tbaa !5
  store ptr %7, ptr %19, align 8, !tbaa !5
  store ptr %8, ptr %20, align 8, !tbaa !5
  store ptr %9, ptr %21, align 8, !tbaa !5
  store ptr %10, ptr %22, align 8, !tbaa !5
  %24 = load ptr, ptr %19, align 8, !tbaa !5
  %25 = load ptr, ptr %12, align 8, !tbaa !5
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %23) #3
  %29 = load ptr, ptr %15, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 4, !tbaa !13
  store i32 %31, ptr %23, align 4, !tbaa !15
  br label %32

32:                                               ; preds = %35, %11
  %33 = load i32, ptr %23, align 4, !tbaa !15
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %81

35:                                               ; preds = %32
  %36 = load ptr, ptr %16, align 8, !tbaa !5
  %37 = load ptr, ptr %18, align 8, !tbaa !5
  %38 = load ptr, ptr %20, align 8, !tbaa !5
  %39 = load ptr, ptr %22, align 8, !tbaa !5
  %40 = load ptr, ptr %13, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 4, !tbaa !16
  %43 = load ptr, ptr %13, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 4, !tbaa !17
  %46 = load ptr, ptr %14, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.cmsis_nn_per_tensor_quant_params, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 4, !tbaa !18
  %49 = load ptr, ptr %14, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.cmsis_nn_per_tensor_quant_params, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 4, !tbaa !19
  %52 = load ptr, ptr %17, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 4, !tbaa !13
  %55 = load ptr, ptr %21, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %55, i32 0, i32 3
  %57 = load i32, ptr %56, align 4, !tbaa !20
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %58, i32 0, i32 3
  %60 = getelementptr inbounds %struct.cmsis_nn_activation, ptr %59, i32 0, i32 0
  %61 = load i32, ptr %60, align 4, !tbaa !21
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %62, i32 0, i32 3
  %64 = getelementptr inbounds %struct.cmsis_nn_activation, ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 4, !tbaa !22
  %66 = call i32 @arm_nn_vec_mat_mult_t_s8(ptr noundef %36, ptr noundef %37, ptr noundef %38, ptr noundef %39, i32 noundef %42, i32 noundef 0, i32 noundef %45, i32 noundef %48, i32 noundef %51, i32 noundef %54, i32 noundef %57, i32 noundef %61, i32 noundef %65, i32 noundef 1)
  %67 = load ptr, ptr %17, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %67, i32 0, i32 0
  %69 = load i32, ptr %68, align 4, !tbaa !13
  %70 = load ptr, ptr %16, align 8, !tbaa !5
  %71 = sext i32 %69 to i64
  %72 = getelementptr inbounds i8, ptr %70, i64 %71
  store ptr %72, ptr %16, align 8, !tbaa !5
  %73 = load ptr, ptr %21, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %73, i32 0, i32 3
  %75 = load i32, ptr %74, align 4, !tbaa !20
  %76 = load ptr, ptr %22, align 8, !tbaa !5
  %77 = sext i32 %75 to i64
  %78 = getelementptr inbounds i8, ptr %76, i64 %77
  store ptr %78, ptr %22, align 8, !tbaa !5
  %79 = load i32, ptr %23, align 4, !tbaa !15
  %80 = add nsw i32 %79, -1
  store i32 %80, ptr %23, align 4, !tbaa !15
  br label %32, !llvm.loop !23

81:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %23) #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

declare i32 @arm_nn_vec_mat_mult_t_s8(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, 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 = { "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, !11, i64 4}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !12, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 4}
!13 = !{!14, !11, i64 0}
!14 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 0}
!17 = !{!10, !11, i64 8}
!18 = !{!12, !11, i64 0}
!19 = !{!12, !11, i64 4}
!20 = !{!14, !11, i64 12}
!21 = !{!10, !11, i64 12}
!22 = !{!10, !11, i64 16}
!23 = distinct !{!23, !24}
!24 = !{!"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: nounwind uwtable
define dso_local noundef i32 @arm_fully_connected_s8(ptr nocapture noundef readnone %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, ptr nocapture noundef readonly %3, ptr noundef %4, ptr nocapture noundef readonly %5, ptr noundef %6, ptr nocapture noundef readnone %7, ptr noundef %8, ptr nocapture noundef readonly %9, ptr noundef %10) local_unnamed_addr #0 {
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %.not23 = icmp eq i32 %12, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %13 = getelementptr inbounds i8, ptr %1, i64 8
  %14 = getelementptr inbounds i8, ptr %2, i64 4
  %15 = getelementptr inbounds i8, ptr %9, i64 12
  %16 = getelementptr inbounds i8, ptr %1, i64 12
  %17 = getelementptr inbounds i8, ptr %1, i64 16
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  %.pre27 = load i32, ptr %15, align 4, !tbaa !10
  br label %18

18:                                               ; preds = %.lr.ph, %18
  %19 = phi i32 [ %.pre27, %.lr.ph ], [ %31, %18 ]
  %20 = phi i32 [ %.pre, %.lr.ph ], [ %28, %18 ]
  %.026 = phi i32 [ %12, %.lr.ph ], [ %34, %18 ]
  %.02125 = phi ptr [ %10, %.lr.ph ], [ %33, %18 ]
  %.02224 = phi ptr [ %4, %.lr.ph ], [ %30, %18 ]
  %21 = load i32, ptr %1, align 4, !tbaa !11
  %22 = load i32, ptr %13, align 4, !tbaa !14
  %23 = load i32, ptr %2, align 4, !tbaa !15
  %24 = load i32, ptr %14, align 4, !tbaa !16
  %25 = load i32, ptr %16, align 4, !tbaa !17
  %26 = load i32, ptr %17, align 4, !tbaa !18
  %27 = tail call i32 @arm_nn_vec_mat_mult_t_s8(ptr noundef %.02224, ptr noundef %6, ptr noundef %8, ptr noundef %.02125, i32 noundef %21, i32 noundef 0, i32 noundef %22, i32 noundef %23, i32 noundef %24, i32 noundef %20, i32 noundef %19, i32 noundef %25, i32 noundef %26, i32 noundef 1) #2
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %.02224, i64 %29
  %31 = load i32, ptr %15, align 4, !tbaa !10
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %.02125, i64 %32
  %34 = add nsw i32 %.026, -1
  %.not = icmp eq i32 %34, 0
  br i1 %.not, label %._crit_edge, label %18, !llvm.loop !19

._crit_edge:                                      ; preds = %18, %11
  ret i32 0
}

declare i32 @arm_nn_vec_mat_mult_t_s8(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #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 = { "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 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 12}
!11 = !{!12, !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !13, i64 12}
!13 = !{!"", !7, i64 0, !7, i64 4}
!14 = !{!12, !7, i64 8}
!15 = !{!13, !7, i64 0}
!16 = !{!13, !7, i64 4}
!17 = !{!12, !7, i64 12}
!18 = !{!12, !7, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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