; LLVM IR from sbu-fsl/kernel-ml :: src/math (linear_algebra)

; 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.matrix = type { i32, i32, i32, %union.anon }
%union.anon = type { ptr }

; Function Attrs: nounwind uwtable
define dso_local void @square_loss_vector(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %102, %2
  %7 = load i32, ptr %5, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.matrix, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp slt i32 %7, %10
  br i1 %11, label %12, label %105

12:                                               ; preds = %6
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.matrix, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 8, !tbaa !13
  switch i32 %15, label %101 [
    i32 0, label %16
    i32 1, label %45
    i32 2, label %73
  ]

16:                                               ; preds = %12
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.matrix, ptr %17, i32 0, i32 3
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.matrix, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !15
  %24 = mul nsw i32 %20, %23
  %25 = add nsw i32 %24, 0
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %19, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.matrix, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.matrix, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !15
  %36 = mul nsw i32 %32, %35
  %37 = add nsw i32 %36, 0
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %31, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = mul nsw i32 %28, %40
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %42, align 8, !tbaa !14
  %44 = add nsw i32 %43, %41
  store i32 %44, ptr %42, align 8, !tbaa !14
  br label %101

45:                                               ; preds = %12
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.matrix, ptr %46, i32 0, i32 3
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.matrix, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 4, !tbaa !15
  %53 = mul nsw i32 %49, %52
  %54 = add nsw i32 %53, 0
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds float, ptr %48, i64 %55
  %57 = load float, ptr %56, align 4, !tbaa !16
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.matrix, ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !14
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.matrix, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !15
  %65 = mul nsw i32 %61, %64
  %66 = add nsw i32 %65, 0
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds float, ptr %60, i64 %67
  %69 = load float, ptr %68, align 4, !tbaa !16
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = load float, ptr %70, align 8, !tbaa !14
  %72 = call float @llvm.fmuladd.f32(float %57, float %69, float %71)
  store float %72, ptr %70, align 8, !tbaa !14
  br label %101

73:                                               ; preds = %12
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.matrix, ptr %74, i32 0, i32 3
  %76 = load ptr, ptr %75, align 8, !tbaa !14
  %77 = load i32, ptr %5, align 4, !tbaa !9
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.matrix, ptr %78, i32 0, i32 1
  %80 = load i32, ptr %79, align 4, !tbaa !15
  %81 = mul nsw i32 %77, %80
  %82 = add nsw i32 %81, 0
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds double, ptr %76, i64 %83
  %85 = load double, ptr %84, align 8, !tbaa !18
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.matrix, ptr %86, i32 0, i32 3
  %88 = load ptr, ptr %87, align 8, !tbaa !14
  %89 = load i32, ptr %5, align 4, !tbaa !9
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.matrix, ptr %90, i32 0, i32 1
  %92 = load i32, ptr %91, align 4, !tbaa !15
  %93 = mul nsw i32 %89, %92
  %94 = add nsw i32 %93, 0
  %95 = sext i32 %94 to i64
  %96 = getelementptr inbounds double, ptr %88, i64 %95
  %97 = load double, ptr %96, align 8, !tbaa !18
  %98 = load ptr, ptr %4, align 8, !tbaa !5
  %99 = load double, ptr %98, align 8, !tbaa !14
  %100 = call double @llvm.fmuladd.f64(double %85, double %97, double %99)
  store double %100, ptr %98, align 8, !tbaa !14
  br label %101

101:                                              ; preds = %73, %45, %16, %12
  br label %102

102:                                              ; preds = %101
  %103 = load i32, ptr %5, align 4, !tbaa !9
  %104 = add nsw i32 %103, 1
  store i32 %104, ptr %5, align 4, !tbaa !9
  br label %6, !llvm.loop !20

105:                                              ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"matrix", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16}
!13 = !{!12, !10, i64 8}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !10, i64 4}
!16 = !{!17, !17, i64 0}
!17 = !{!"float", !7, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @square_loss_vector(ptr nocapture noundef readonly %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  br label %8

8:                                                ; preds = %.lr.ph, %38
  %.024 = phi i32 [ 0, %.lr.ph ], [ %39, %38 ]
  %9 = load i32, ptr %5, align 8, !tbaa !10
  switch i32 %9, label %38 [
    i32 0, label %10
    i32 1, label %20
    i32 2, label %29
  ]

10:                                               ; preds = %8
  %11 = load ptr, ptr %6, align 8, !tbaa !11
  %12 = load i32, ptr %7, align 4, !tbaa !12
  %13 = mul nsw i32 %12, %.024
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr %11, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = mul nsw i32 %16, %16
  %18 = load i32, ptr %1, align 8, !tbaa !11
  %19 = add nsw i32 %18, %17
  store i32 %19, ptr %1, align 8, !tbaa !11
  br label %38

20:                                               ; preds = %8
  %21 = load ptr, ptr %6, align 8, !tbaa !11
  %22 = load i32, ptr %7, align 4, !tbaa !12
  %23 = mul nsw i32 %22, %.024
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds float, ptr %21, i64 %24
  %26 = load float, ptr %25, align 4, !tbaa !14
  %27 = load float, ptr %1, align 8, !tbaa !11
  %28 = tail call float @llvm.fmuladd.f32(float %26, float %26, float %27)
  store float %28, ptr %1, align 8, !tbaa !11
  br label %38

29:                                               ; preds = %8
  %30 = load ptr, ptr %6, align 8, !tbaa !11
  %31 = load i32, ptr %7, align 4, !tbaa !12
  %32 = mul nsw i32 %31, %.024
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds double, ptr %30, i64 %33
  %35 = load double, ptr %34, align 8, !tbaa !16
  %36 = load double, ptr %1, align 8, !tbaa !11
  %37 = tail call double @llvm.fmuladd.f64(double %35, double %35, double %36)
  store double %37, ptr %1, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %8, %10, %20, %29
  %39 = add nuw nsw i32 %.024, 1
  %40 = load i32, ptr %0, align 8, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %8, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %38, %2
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare float @llvm.fmuladd.f32(float, float, float) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: 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" }
attributes #1 = { mustprogress 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, !7, i64 0}
!6 = !{!"matrix", !7, i64 0, !7, i64 4, !7, i64 8, !8, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!8, !8, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"float", !8, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !8, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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