; LLVM IR from ggml-org/ggml :: tests (test-mul-mat2)

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

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [69 x i8], align 1
@__PRETTY_FUNCTION__.mul_mat_gq_4 = external hidden unnamed_addr constant [70 x i8], align 1

; 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @quantize_4_blocks_per_row(i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @quantize_4_row_size(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @vec_dot_gq_4(i32 noundef, ptr noalias noundef, ptr noalias noundef, ptr noalias noundef) #3

; Function Attrs: nounwind uwtable
define dso_local void @mul_mat_gq_4(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #3 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !9
  %17 = load i32, ptr %12, align 4, !tbaa !9
  %18 = srem i32 %17, 64
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %6
  br label %22

21:                                               ; preds = %6
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 1597, ptr noundef @__PRETTY_FUNCTION__.mul_mat_gq_4) #4
  unreachable

22:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %23 = load i32, ptr %12, align 4, !tbaa !9
  %24 = call i32 @quantize_4_blocks_per_row(i32 noundef %23)
  store i32 %24, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %70, %22
  %26 = load i32, ptr %14, align 4, !tbaa !9
  %27 = load i32, ptr %10, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 2, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %73

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %49, %30
  %32 = load i32, ptr %16, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  store i32 5, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %52

36:                                               ; preds = %31
  %37 = load i32, ptr %12, align 4, !tbaa !9
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = load i32, ptr %16, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds float, ptr %38, i64 %40
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  call void @vec_dot_gq_4(i32 noundef %37, ptr noundef %41, ptr noundef %42, ptr noundef %43)
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = load i32, ptr %12, align 4, !tbaa !9
  %46 = call i32 @quantize_4_row_size(i32 noundef %45)
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %44, i64 %47
  store ptr %48, ptr %8, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %36
  %50 = load i32, ptr %16, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %16, align 4, !tbaa !9
  br label %31, !llvm.loop !11

52:                                               ; preds = %35
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = call i32 @quantize_4_row_size(i32 noundef %54)
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %53, i64 %56
  store ptr %57, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = load i32, ptr %12, align 4, !tbaa !9
  %61 = call i32 @quantize_4_row_size(i32 noundef %60)
  %62 = mul nsw i32 %59, %61
  %63 = sext i32 %62 to i64
  %64 = sub i64 0, %63
  %65 = getelementptr inbounds i8, ptr %58, i64 %64
  store ptr %65, ptr %8, align 8, !tbaa !5
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds float, ptr %66, i64 %68
  store ptr %69, ptr %9, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %52
  %71 = load i32, ptr %14, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %14, align 4, !tbaa !9
  br label %25, !llvm.loop !13

73:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn nounwind "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 = { inlinehint 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 = { 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 = { noreturn nounwind }
attributes #5 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [69 x i8], align 1
@__PRETTY_FUNCTION__.mul_mat_gq_4 = external hidden unnamed_addr constant [70 x i8], align 1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @vec_dot_gq_4(i32 noundef, ptr noalias noundef, ptr noalias noundef, ptr noalias noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @mul_mat_gq_4(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #2 {
  %7 = and i32 %5, 63
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 1597, ptr noundef nonnull @__PRETTY_FUNCTION__.mul_mat_gq_4) #3
  unreachable

10:                                               ; preds = %6
  %11 = tail call i32 @quantize_4_blocks_per_row(i32 noundef %5)
  %12 = icmp sgt i32 %3, 0
  br i1 %12, label %.preheader.lr.ph, label %._crit_edge32

.preheader.lr.ph:                                 ; preds = %10
  %13 = icmp sgt i32 %4, 0
  %14 = sext i32 %4 to i64
  br i1 %13, label %.preheader.us.preheader, label %.preheader

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.02131.us = phi i32 [ %29, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %.02230.us = phi ptr [ %22, %._crit_edge.us ], [ %0, %.preheader.us.preheader ]
  %.02329.us = phi ptr [ %27, %._crit_edge.us ], [ %1, %.preheader.us.preheader ]
  %.02428.us = phi ptr [ %28, %._crit_edge.us ], [ %2, %.preheader.us.preheader ]
  br label %15

15:                                               ; preds = %.preheader.us, %15
  %indvars.iv = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next, %15 ]
  %.126.us = phi ptr [ %.02329.us, %.preheader.us ], [ %19, %15 ]
  %16 = getelementptr inbounds float, ptr %.02428.us, i64 %indvars.iv
  tail call void @vec_dot_gq_4(i32 noundef %5, ptr noundef %16, ptr noundef %.02230.us, ptr noundef %.126.us)
  %17 = tail call i32 @quantize_4_row_size(i32 noundef %5)
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %.126.us, i64 %18
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond35.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond35.not, label %._crit_edge.us, label %15, !llvm.loop !5

._crit_edge.us:                                   ; preds = %15
  %20 = tail call i32 @quantize_4_row_size(i32 noundef %5)
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %.02230.us, i64 %21
  %23 = tail call i32 @quantize_4_row_size(i32 noundef %5)
  %24 = mul nsw i32 %23, %4
  %25 = sext i32 %24 to i64
  %26 = sub nsw i64 0, %25
  %27 = getelementptr inbounds i8, ptr %19, i64 %26
  %28 = getelementptr inbounds float, ptr %.02428.us, i64 %14
  %29 = add nuw nsw i32 %.02131.us, 1
  %exitcond36.not = icmp eq i32 %29, %3
  br i1 %exitcond36.not, label %._crit_edge32, label %.preheader.us, !llvm.loop !7

.preheader:                                       ; preds = %.preheader.lr.ph, %.preheader
  %.02131 = phi i32 [ %32, %.preheader ], [ 0, %.preheader.lr.ph ]
  %30 = tail call i32 @quantize_4_row_size(i32 noundef %5)
  %31 = tail call i32 @quantize_4_row_size(i32 noundef %5)
  %32 = add nuw nsw i32 %.02131, 1
  %exitcond.not = icmp eq i32 %32, %3
  br i1 %exitcond.not, label %._crit_edge32, label %.preheader, !llvm.loop !7

._crit_edge32:                                    ; preds = %.preheader, %._crit_edge.us, %10
  ret void
}

attributes #0 = { noreturn nounwind "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 = { inlinehint 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 = { 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 = { noreturn 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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