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

; 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_s16(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
  %24 = 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
  %25 = load ptr, ptr %19, align 8, !tbaa !5
  %26 = load ptr, ptr %12, align 8, !tbaa !5
  %27 = load ptr, ptr %13, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %23) #3
  %30 = load ptr, ptr %15, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !13
  store i32 %32, ptr %23, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %24) #3
  %33 = load ptr, ptr %14, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.cmsis_nn_per_tensor_quant_params, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 4, !tbaa !16
  %36 = icmp slt i32 %35, 2147418112
  br i1 %36, label %37, label %43

37:                                               ; preds = %11
  %38 = load ptr, ptr %14, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.cmsis_nn_per_tensor_quant_params, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 4, !tbaa !16
  %41 = add nsw i32 %40, 32768
  %42 = ashr i32 %41, 16
  br label %44

43:                                               ; preds = %11
  br label %44

44:                                               ; preds = %43, %37
  %45 = phi i32 [ %42, %37 ], [ 32767, %43 ]
  store i32 %45, ptr %24, align 4, !tbaa !15
  br label %46

46:                                               ; preds = %49, %44
  %47 = load i32, ptr %23, align 4, !tbaa !15
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %87

49:                                               ; preds = %46
  %50 = load ptr, ptr %16, align 8, !tbaa !5
  %51 = load ptr, ptr %18, align 8, !tbaa !5
  %52 = load ptr, ptr %20, align 8, !tbaa !5
  %53 = load ptr, ptr %22, align 8, !tbaa !5
  %54 = load i32, ptr %24, align 4, !tbaa !15
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.cmsis_nn_per_tensor_quant_params, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !17
  %58 = load ptr, ptr %17, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %58, i32 0, i32 0
  %60 = load i32, ptr %59, align 4, !tbaa !13
  %61 = load ptr, ptr %21, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %61, i32 0, i32 3
  %63 = load i32, ptr %62, align 4, !tbaa !18
  %64 = load ptr, ptr %13, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %64, i32 0, i32 3
  %66 = getelementptr inbounds %struct.cmsis_nn_activation, ptr %65, i32 0, i32 0
  %67 = load i32, ptr %66, align 4, !tbaa !19
  %68 = load ptr, ptr %13, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.cmsis_nn_fc_params, ptr %68, i32 0, i32 3
  %70 = getelementptr inbounds %struct.cmsis_nn_activation, ptr %69, i32 0, i32 1
  %71 = load i32, ptr %70, align 4, !tbaa !20
  %72 = call i32 @arm_nn_vec_mat_mult_t_s16(ptr noundef %50, ptr noundef %51, ptr noundef %52, ptr noundef %53, i32 noundef %54, i32 noundef %57, i32 noundef %60, i32 noundef %63, i32 noundef %67, i32 noundef %71)
  %73 = load ptr, ptr %17, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %73, i32 0, i32 0
  %75 = load i32, ptr %74, align 4, !tbaa !13
  %76 = load ptr, ptr %16, align 8, !tbaa !5
  %77 = sext i32 %75 to i64
  %78 = getelementptr inbounds i16, ptr %76, i64 %77
  store ptr %78, ptr %16, align 8, !tbaa !5
  %79 = load ptr, ptr %21, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.cmsis_nn_dims, ptr %79, i32 0, i32 3
  %81 = load i32, ptr %80, align 4, !tbaa !18
  %82 = load ptr, ptr %22, align 8, !tbaa !5
  %83 = sext i32 %81 to i64
  %84 = getelementptr inbounds i16, ptr %82, i64 %83
  store ptr %84, ptr %22, align 8, !tbaa !5
  %85 = load i32, ptr %23, align 4, !tbaa !15
  %86 = add nsw i32 %85, -1
  store i32 %86, ptr %23, align 4, !tbaa !15
  br label %46, !llvm.loop !21

87:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #3
  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_s16(ptr noundef, ptr noundef, ptr noundef, ptr 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 = !{!12, !11, i64 0}
!17 = !{!12, !11, i64 4}
!18 = !{!14, !11, i64 12}
!19 = !{!10, !11, i64 12}
!20 = !{!10, !11, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"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_s16(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
  %13 = load i32, ptr %2, align 4, !tbaa !10
  %14 = icmp slt i32 %13, 2147418112
  %15 = add nsw i32 %13, 32768
  %16 = ashr i32 %15, 16
  %17 = select i1 %14, i32 %16, i32 32767
  %.not24 = icmp eq i32 %12, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %18 = getelementptr inbounds i8, ptr %2, i64 4
  %19 = getelementptr inbounds i8, ptr %9, i64 12
  %20 = getelementptr inbounds i8, ptr %1, i64 12
  %21 = getelementptr inbounds i8, ptr %1, i64 16
  %.pre = load i32, ptr %5, align 4, !tbaa !5
  %.pre28 = load i32, ptr %19, align 4, !tbaa !12
  br label %22

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

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

declare i32 @arm_nn_vec_mat_mult_t_s16(ptr noundef, ptr noundef, ptr noundef, ptr 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 = !{!11, !7, i64 0}
!11 = !{!"", !7, i64 0, !7, i64 4}
!12 = !{!6, !7, i64 12}
!13 = !{!11, !7, i64 4}
!14 = !{!15, !7, i64 12}
!15 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !11, i64 12}
!16 = !{!15, !7, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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