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

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @load_matrix_from_file(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca [16 x i8], align 16
  %8 = alloca i64, align 8
  %9 = alloca float, 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) #4
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #4
  call void @llvm.memset.p0.i64(ptr align 16 %7, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !11
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %81, %2
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.matrix, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !13
  %15 = icmp slt i32 %11, %14
  br i1 %15, label %16, label %84

16:                                               ; preds = %10
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %77, %16
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.matrix, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !15
  %22 = icmp slt i32 %18, %21
  br i1 %22, label %23, label %80

23:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds [16 x i8], ptr %7, i64 0, i64 0
  %26 = call i32 @kml_file_read(ptr noundef %24, ptr noundef %25, i64 noundef 16, ptr noundef %8)
  %27 = getelementptr inbounds [16 x i8], ptr %7, i64 0, i64 0
  %28 = call float @convert_string_to_float(ptr noundef %27)
  store float %28, ptr %9, align 4, !tbaa !16
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.matrix, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !18
  switch i32 %31, label %76 [
    i32 0, label %32
    i32 1, label %47
    i32 2, label %61
  ]

32:                                               ; preds = %23
  %33 = load float, ptr %9, align 4, !tbaa !16
  %34 = fptosi float %33 to i32
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.matrix, ptr %35, i32 0, i32 3
  %37 = load ptr, ptr %36, align 8, !tbaa !19
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.matrix, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !15
  %42 = mul nsw i32 %38, %41
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = add nsw i32 %42, %43
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %37, i64 %45
  store i32 %34, ptr %46, align 4, !tbaa !9
  br label %76

47:                                               ; preds = %23
  %48 = load float, ptr %9, align 4, !tbaa !16
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.matrix, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !19
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.matrix, ptr %53, i32 0, i32 1
  %55 = load i32, ptr %54, align 4, !tbaa !15
  %56 = mul nsw i32 %52, %55
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %56, %57
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds float, ptr %51, i64 %59
  store float %48, ptr %60, align 4, !tbaa !16
  br label %76

61:                                               ; preds = %23
  %62 = load float, ptr %9, align 4, !tbaa !16
  %63 = fpext float %62 to double
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.matrix, ptr %64, i32 0, i32 3
  %66 = load ptr, ptr %65, align 8, !tbaa !19
  %67 = load i32, ptr %5, align 4, !tbaa !9
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.matrix, ptr %68, i32 0, i32 1
  %70 = load i32, ptr %69, align 4, !tbaa !15
  %71 = mul nsw i32 %67, %70
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = add nsw i32 %71, %72
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds double, ptr %66, i64 %74
  store double %63, ptr %75, align 8, !tbaa !20
  br label %76

76:                                               ; preds = %61, %47, %32, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %6, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %6, align 4, !tbaa !9
  br label %17, !llvm.loop !22

80:                                               ; preds = %17
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %5, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %5, align 4, !tbaa !9
  br label %10, !llvm.loop !24

84:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

declare i32 @kml_file_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #3

declare float @convert_string_to_float(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = { "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 #4 = { 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, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"matrix", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16}
!15 = !{!14, !10, i64 4}
!16 = !{!17, !17, i64 0}
!17 = !{!"float", !7, i64 0}
!18 = !{!14, !10, i64 8}
!19 = !{!7, !7, i64 0}
!20 = !{!21, !21, i64 0}
!21 = !{!"double", !7, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}
!24 = distinct !{!24, !23}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @load_matrix_from_file(ptr noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #2 {
  %3 = alloca [16 x i8], align 16
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %3, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  store i64 0, ptr %4, align 8, !tbaa !5
  %5 = load i32, ptr %1, align 8, !tbaa !9
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.preheader.lr.ph, label %._crit_edge25

.preheader.lr.ph:                                 ; preds = %2
  %7 = getelementptr inbounds i8, ptr %1, i64 4
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = getelementptr inbounds i8, ptr %1, i64 16
  %10 = load i32, ptr %7, align 4, !tbaa !12
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.preheader, label %._crit_edge25

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %12 = phi i32 [ %45, %._crit_edge ], [ %5, %.preheader.lr.ph ]
  %13 = phi i32 [ %46, %._crit_edge ], [ %10, %.preheader.lr.ph ]
  %.024 = phi i32 [ %47, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %41
  %.02223 = phi i32 [ %42, %41 ], [ 0, %.preheader ]
  %15 = call i32 @kml_file_read(ptr noundef %0, ptr noundef nonnull %3, i64 noundef 16, ptr noundef nonnull %4) #4
  %16 = call float @convert_string_to_float(ptr noundef nonnull %3) #4
  %17 = load i32, ptr %8, align 8, !tbaa !13
  switch i32 %17, label %41 [
    i32 0, label %18
    i32 1, label %26
    i32 2, label %33
  ]

18:                                               ; preds = %.lr.ph
  %19 = fptosi float %16 to i32
  %20 = load ptr, ptr %9, align 8, !tbaa !14
  %21 = load i32, ptr %7, align 4, !tbaa !12
  %22 = mul nsw i32 %21, %.024
  %23 = add nsw i32 %22, %.02223
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %20, i64 %24
  store i32 %19, ptr %25, align 4, !tbaa !15
  br label %41

26:                                               ; preds = %.lr.ph
  %27 = load ptr, ptr %9, align 8, !tbaa !14
  %28 = load i32, ptr %7, align 4, !tbaa !12
  %29 = mul nsw i32 %28, %.024
  %30 = add nsw i32 %29, %.02223
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds float, ptr %27, i64 %31
  store float %16, ptr %32, align 4, !tbaa !16
  br label %41

33:                                               ; preds = %.lr.ph
  %34 = fpext float %16 to double
  %35 = load ptr, ptr %9, align 8, !tbaa !14
  %36 = load i32, ptr %7, align 4, !tbaa !12
  %37 = mul nsw i32 %36, %.024
  %38 = add nsw i32 %37, %.02223
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds double, ptr %35, i64 %39
  store double %34, ptr %40, align 8, !tbaa !18
  br label %41

41:                                               ; preds = %33, %26, %18, %.lr.ph
  %42 = add nuw nsw i32 %.02223, 1
  %43 = load i32, ptr %7, align 4, !tbaa !12
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !20

._crit_edge.loopexit:                             ; preds = %41
  %.pre = load i32, ptr %1, align 8, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %45 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %12, %.preheader ]
  %46 = phi i32 [ %43, %._crit_edge.loopexit ], [ %13, %.preheader ]
  %47 = add nuw nsw i32 %.024, 1
  %48 = icmp slt i32 %47, %45
  br i1 %48, label %.preheader, label %._crit_edge25, !llvm.loop !22

._crit_edge25:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret void
}

declare i32 @kml_file_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

declare float @convert_string_to_float(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = { "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 #4 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"matrix", !11, i64 0, !11, i64 4, !11, i64 8, !7, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!10, !11, i64 8}
!14 = !{!7, !7, i64 0}
!15 = !{!11, !11, i64 0}
!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"}
!22 = distinct !{!22, !21, !23}
!23 = !{!"llvm.loop.unswitch.partial.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass