; 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

declare void @get_float_str(ptr noundef, i64 noundef, float noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @save_matrix_to_file(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca [16 x i8], align 16
  %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 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !11
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %92, %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 %95

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

17:                                               ; preds = %88, %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 %91

23:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store float 0.000000e+00, ptr %9, align 4, !tbaa !16
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.matrix, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !18
  switch i32 %26, label %71 [
    i32 0, label %27
    i32 1, label %42
    i32 2, label %56
  ]

27:                                               ; preds = %23
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.matrix, ptr %28, i32 0, i32 3
  %30 = load ptr, ptr %29, align 8, !tbaa !19
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.matrix, ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 4, !tbaa !15
  %35 = mul nsw i32 %31, %34
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = add nsw i32 %35, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %30, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = sitofp i32 %40 to float
  store float %41, ptr %9, align 4, !tbaa !16
  br label %71

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

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

71:                                               ; preds = %56, %42, %27, %23
  %72 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %73 = load float, ptr %9, align 4, !tbaa !16
  call void @get_float_str(ptr noundef %72, i64 noundef 16, float noundef %73)
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.matrix, ptr %75, i32 0, i32 1
  %77 = load i32, ptr %76, align 4, !tbaa !15
  %78 = sub nsw i32 %77, 1
  %79 = icmp slt i32 %74, %78
  br i1 %79, label %80, label %82

80:                                               ; preds = %71
  %81 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 15
  store i8 32, ptr %81, align 1, !tbaa !19
  br label %84

82:                                               ; preds = %71
  %83 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 15
  store i8 10, ptr %83, align 1, !tbaa !19
  br label %84

84:                                               ; preds = %82, %80
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %87 = call i32 @kml_file_write(ptr noundef %85, ptr noundef %86, i64 noundef 16, ptr noundef %7)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  br label %88

88:                                               ; preds = %84
  %89 = load i32, ptr %6, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %6, align 4, !tbaa !9
  br label %17, !llvm.loop !22

91:                                               ; preds = %17
  br label %92

92:                                               ; preds = %91
  %93 = load i32, ptr %5, align 4, !tbaa !9
  %94 = add nsw i32 %93, 1
  store i32 %94, ptr %5, align 4, !tbaa !9
  br label %10, !llvm.loop !24

95:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, 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_write(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 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 #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

declare void @get_float_str(ptr noundef, i64 noundef, float noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @save_matrix_to_file(ptr noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #3 {
  %3 = alloca i64, align 8
  %4 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  store i64 0, ptr %3, 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_edge26

.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 = getelementptr inbounds i8, ptr %4, i64 15
  %11 = load i32, ptr %7, align 4, !tbaa !12
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.preheader, label %._crit_edge26

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %13 = phi i32 [ %49, %._crit_edge ], [ %5, %.preheader.lr.ph ]
  %14 = phi i32 [ %50, %._crit_edge ], [ %11, %.preheader.lr.ph ]
  %.02325 = phi i32 [ %51, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %41
  %16 = phi i32 [ %47, %41 ], [ %14, %.preheader ]
  %.02224 = phi i32 [ %46, %41 ], [ 0, %.preheader ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  %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 = load ptr, ptr %9, align 8, !tbaa !14
  %20 = mul nsw i32 %16, %.02325
  %21 = add nsw i32 %20, %.02224
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %19, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !15
  %25 = sitofp i32 %24 to float
  br label %41

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

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

41:                                               ; preds = %33, %26, %18, %.lr.ph
  %.0 = phi float [ 0.000000e+00, %.lr.ph ], [ %40, %33 ], [ %32, %26 ], [ %25, %18 ]
  call void @get_float_str(ptr noundef nonnull %4, i64 noundef 16, float noundef %.0) #4
  %42 = load i32, ptr %7, align 4, !tbaa !12
  %43 = add nsw i32 %42, -1
  %44 = icmp slt i32 %.02224, %43
  %spec.select = select i1 %44, i8 32, i8 10
  store i8 %spec.select, ptr %10, align 1, !tbaa !14
  %45 = call i32 @kml_file_write(ptr noundef %0, ptr noundef nonnull %4, i64 noundef 16, ptr noundef nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #4
  %46 = add nuw nsw i32 %.02224, 1
  %47 = load i32, ptr %7, align 4, !tbaa !12
  %48 = icmp slt i32 %46, %47
  br i1 %48, 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
  %49 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %13, %.preheader ]
  %50 = phi i32 [ %47, %._crit_edge.loopexit ], [ %14, %.preheader ]
  %51 = add nuw nsw i32 %.02325, 1
  %52 = icmp slt i32 %51, %49
  br i1 %52, label %.preheader, label %._crit_edge26, !llvm.loop !22

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

declare i32 @kml_file_write(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 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 #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