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

%union.val = type { double }
%struct.matrix_index = type { i32, i32 }
%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: nounwind uwtable
declare hidden i32 @sizeof_dtype(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @copy_matrix(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @matrix_find_val(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @matrix_argsort(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca %union.val, align 8
  %9 = alloca %struct.matrix_index, 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call ptr @copy_matrix(ptr noundef %10)
  store ptr %11, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.matrix, ptr %12, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.matrix, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !10
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.matrix, ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = mul nsw i32 %17, %20
  %22 = sext i32 %21 to i64
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.matrix, ptr %23, i32 0, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !14
  %26 = call i32 @sizeof_dtype(i32 noundef %25)
  %27 = sext i32 %26 to i64
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  call void @kml_sort(ptr noundef %14, i64 noundef %22, i64 noundef %27, ptr noundef %28)
  store i32 0, ptr %5, align 4, !tbaa !15
  br label %29

29:                                               ; preds = %76, %2
  %30 = load i32, ptr %5, align 4, !tbaa !15
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.matrix, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !10
  %34 = icmp slt i32 %30, %33
  br i1 %34, label %35, label %79

35:                                               ; preds = %29
  store i32 0, ptr %6, align 4, !tbaa !15
  br label %36

36:                                               ; preds = %72, %35
  %37 = load i32, ptr %6, align 4, !tbaa !15
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.matrix, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !13
  %41 = icmp slt i32 %37, %40
  br i1 %41, label %42, label %75

42:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.matrix, ptr %43, i32 0, i32 3
  %45 = load ptr, ptr %44, align 8, !tbaa !9
  %46 = load i32, ptr %5, align 4, !tbaa !15
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.matrix, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !13
  %50 = mul nsw i32 %46, %49
  %51 = load i32, ptr %6, align 4, !tbaa !15
  %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 %8, align 8, !tbaa !9
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  call void @matrix_find_val(ptr noundef %56, ptr noundef %8, ptr noundef %9)
  %57 = getelementptr inbounds %struct.matrix_index, ptr %9, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !18
  %59 = sitofp i32 %58 to float
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.matrix, ptr %60, i32 0, i32 3
  %62 = load ptr, ptr %61, align 8, !tbaa !9
  %63 = load i32, ptr %5, align 4, !tbaa !15
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.matrix, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !13
  %67 = mul nsw i32 %63, %66
  %68 = load i32, ptr %6, align 4, !tbaa !15
  %69 = add nsw i32 %67, %68
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds float, ptr %62, i64 %70
  store float %59, ptr %71, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %72

72:                                               ; preds = %42
  %73 = load i32, ptr %6, align 4, !tbaa !15
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %6, align 4, !tbaa !15
  br label %36, !llvm.loop !20

75:                                               ; preds = %36
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %5, align 4, !tbaa !15
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %5, align 4, !tbaa !15
  br label %29, !llvm.loop !22

79:                                               ; preds = %29
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret ptr %80
}

declare void @kml_sort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

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


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

%union.val = type { double }
%struct.matrix_index = type { i32, i32 }

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

; Function Attrs: nounwind uwtable
declare hidden i32 @sizeof_dtype(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @copy_matrix(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @matrix_find_val(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @matrix_argsort(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca %union.val, align 8
  %4 = alloca %struct.matrix_index, align 4
  %5 = tail call ptr @copy_matrix(ptr noundef %0)
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = load i32, ptr %0, align 8, !tbaa !8
  %9 = getelementptr inbounds i8, ptr %0, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = mul nsw i32 %10, %8
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !12
  %15 = tail call i32 @sizeof_dtype(i32 noundef %14)
  %16 = sext i32 %15 to i64
  tail call void @kml_sort(ptr noundef %7, i64 noundef %12, i64 noundef %16, ptr noundef %1) #3
  %17 = load i32, ptr %5, align 8, !tbaa !8
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %.preheader.lr.ph, label %._crit_edge24

.preheader.lr.ph:                                 ; preds = %2
  %19 = getelementptr inbounds i8, ptr %5, i64 4
  %20 = getelementptr inbounds i8, ptr %4, i64 4
  %21 = load i32, ptr %19, align 4, !tbaa !11
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %.preheader, label %._crit_edge24

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %23 = phi i32 [ %44, %._crit_edge ], [ %17, %.preheader.lr.ph ]
  %24 = phi i32 [ %45, %._crit_edge ], [ %21, %.preheader.lr.ph ]
  %.023 = phi i32 [ %46, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %.pre = load i32, ptr %9, align 4, !tbaa !11
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %26 = phi i32 [ %36, %.lr.ph ], [ %.pre, %.lr.ph.preheader ]
  %.02122 = phi i32 [ %41, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = mul nsw i32 %26, %.023
  %29 = add nsw i32 %28, %.02122
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds float, ptr %27, i64 %30
  %32 = load float, ptr %31, align 4, !tbaa !13
  store float %32, ptr %3, align 8, !tbaa !5
  call void @matrix_find_val(ptr noundef nonnull %0, ptr noundef nonnull %3, ptr noundef nonnull %4)
  %33 = load i32, ptr %20, align 4, !tbaa !15
  %34 = sitofp i32 %33 to float
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !11
  %37 = mul nsw i32 %36, %.023
  %38 = add nsw i32 %37, %.02122
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds float, ptr %35, i64 %39
  store float %34, ptr %40, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  %41 = add nuw nsw i32 %.02122, 1
  %42 = load i32, ptr %19, align 4, !tbaa !11
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !17

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre26 = load i32, ptr %5, align 8, !tbaa !8
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %44 = phi i32 [ %.pre26, %._crit_edge.loopexit ], [ %23, %.preheader ]
  %45 = phi i32 [ %42, %._crit_edge.loopexit ], [ %24, %.preheader ]
  %46 = add nuw nsw i32 %.023, 1
  %47 = icmp slt i32 %46, %44
  br i1 %47, label %.preheader, label %._crit_edge24, !llvm.loop !19

._crit_edge24:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %2
  ret ptr %5
}

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

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


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