; 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

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

; Function Attrs: nounwind uwtable
declare dso_local void @quantize_row_q3_K_ref(ptr noalias noundef, ptr noalias noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @quantize_q3_K(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4) #2 {
  %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 11, 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_q3_K_ref(ptr noundef %19, ptr noundef %20, i64 noundef %23)
  br label %46

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 = %42, %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 %45

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 = load ptr, ptr %10, align 8, !tbaa !5
  call void @quantize_row_q3_K_impl(ptr noundef %32, ptr noundef %33, i64 noundef %34, ptr noundef %35)
  %36 = load i64, ptr %9, align 8, !tbaa !9
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds float, ptr %37, i64 %36
  store ptr %38, ptr %6, align 8, !tbaa !5
  %39 = load i64, ptr %11, align 8, !tbaa !9
  %40 = load ptr, ptr %12, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 %39
  store ptr %41, ptr %12, align 8, !tbaa !5
  br label %42

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @quantize_row_q3_K_impl(ptr noalias noundef, ptr noalias noundef, i64 noundef, ptr noalias noundef) #2

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

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

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

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

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

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

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

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

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %8
  %13 = mul i64 %6, %2
  ret i64 %13
}

; Function Attrs: nounwind uwtable
declare hidden void @quantize_row_q3_K_impl(ptr noalias noundef, ptr noalias noundef, i64 noundef, ptr noalias noundef) local_unnamed_addr #1

attributes #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 = { 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 }

!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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass