; LLVM IR from ggml-org/ggml :: src (ggml-quants)

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

; 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: nounwind uwtable
declare dso_local void @quantize_row_q2_K_ref(ptr noalias noundef, ptr noalias noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @quantize_q2_K(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i64 %3, ptr %9, align 8, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %14 = load i64, ptr %9, align 8, !tbaa !9
  %15 = call i64 @ggml_row_size(i32 noundef 10, i64 noundef %14)
  store i64 %15, ptr %11, align 8, !tbaa !9
  %16 = load ptr, ptr %10, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %24, label %18

18:                                               ; preds = %5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load i64, ptr %8, align 8, !tbaa !9
  %22 = load i64, ptr %9, align 8, !tbaa !9
  %23 = mul nsw i64 %21, %22
  call void @quantize_row_q2_K_ref(ptr noundef %19, ptr noundef %20, i64 noundef %23)
  br label %47

24:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %25, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %43, %24
  %27 = load i64, ptr %13, align 8, !tbaa !9
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = icmp slt i64 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %46

31:                                               ; preds = %26
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load ptr, ptr %12, align 8, !tbaa !5
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = trunc i64 %34 to i32
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  call void @quantize_row_q2_K_impl(ptr noundef %32, ptr noundef %33, i32 noundef %35, ptr noundef %36)
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds float, ptr %38, i64 %37
  store ptr %39, ptr %6, align 8, !tbaa !5
  %40 = load i64, ptr %11, align 8, !tbaa !9
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 %40
  store ptr %42, ptr %12, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %31
  %44 = load i64, ptr %13, align 8, !tbaa !9
  %45 = add nsw i64 %44, 1
  store i64 %45, ptr %13, align 8, !tbaa !9
  br label %26, !llvm.loop !11

46:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %47

47:                                               ; preds = %46, %18
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = mul i64 %48, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret i64 %50
}

declare i64 @ggml_row_size(i32 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @quantize_row_q2_K_impl(ptr noalias noundef, ptr noalias noundef, i32 noundef, ptr noalias noundef) #1

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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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: nounwind uwtable
declare dso_local void @quantize_row_q2_K_ref(ptr noalias noundef, ptr noalias noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @quantize_q2_K(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = tail call i64 @ggml_row_size(i32 noundef 10, i64 noundef %3) #2
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %9, label %.preheader

.preheader:                                       ; preds = %5
  %7 = icmp sgt i64 %2, 0
  br i1 %7, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %8 = trunc i64 %3 to i32
  br label %11

9:                                                ; preds = %5
  %10 = mul nsw i64 %3, %2
  tail call void @quantize_row_q2_K_ref(ptr noundef %0, ptr noundef %1, i64 noundef %10)
  br label %.loopexit

11:                                               ; preds = %.lr.ph, %11
  %.024 = phi i64 [ 0, %.lr.ph ], [ %14, %11 ]
  %.01923 = phi ptr [ %1, %.lr.ph ], [ %13, %11 ]
  %.02022 = phi ptr [ %0, %.lr.ph ], [ %12, %11 ]
  tail call void @quantize_row_q2_K_impl(ptr noundef %.02022, ptr noundef %.01923, i32 noundef %8, ptr noundef nonnull %4)
  %12 = getelementptr inbounds float, ptr %.02022, i64 %3
  %13 = getelementptr inbounds i8, ptr %.01923, i64 %6
  %14 = add nuw nsw i64 %.024, 1
  %exitcond.not = icmp eq i64 %14, %2
  br i1 %exitcond.not, label %.loopexit, label %11, !llvm.loop !5

.loopexit:                                        ; preds = %11, %.preheader, %9
  %15 = mul i64 %6, %2
  ret i64 %15
}

declare i64 @ggml_row_size(i32 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @quantize_row_q2_K_impl(ptr noalias noundef, ptr noalias noundef, i32 noundef, ptr noalias noundef) local_unnamed_addr #0

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 = { "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 }

!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"}


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