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

%struct.block_iq4_nl = type { i16, [16 x i8] }

@.str.1 = external hidden unnamed_addr constant [65 x i8], align 1
@kvalues_iq4nl = external hidden constant [16 x i8], align 16
@.str.6 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [14 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
declare void @ggml_abort(ptr noundef, i32 noundef, ptr noundef, ...) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @quantize_row_iq4_nl_ref(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca [32 x i8], align 16
  %9 = alloca [32 x float], align 16
  %10 = alloca i16, align 2
  %11 = alloca ptr, align 8
  %12 = alloca float, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = srem i64 %15, 32
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %3
  call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef @.str.1, i32 noundef 4708, ptr noundef @.str.6, ptr noundef @.str.11) #3
  unreachable

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = sdiv i64 %20, 32
  store i64 %21, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 128, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store ptr null, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %22, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  store i32 0, ptr %14, align 4, !tbaa !11
  br label %23

23:                                               ; preds = %49, %19
  %24 = load i32, ptr %14, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = icmp slt i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  br label %52

29:                                               ; preds = %23
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %14, align 4, !tbaa !11
  %32 = mul nsw i32 32, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds float, ptr %30, i64 %33
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = load i32, ptr %14, align 4, !tbaa !11
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds %struct.block_iq4_nl, ptr %35, i64 %37
  %39 = getelementptr inbounds %struct.block_iq4_nl, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %13, align 8, !tbaa !5
  %41 = load i32, ptr %14, align 4, !tbaa !11
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %struct.block_iq4_nl, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.block_iq4_nl, ptr %43, i32 0, i32 1
  %45 = getelementptr inbounds [16 x i8], ptr %44, i64 0, i64 0
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  %47 = getelementptr inbounds [32 x float], ptr %9, i64 0, i64 0
  %48 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  call void @quantize_row_iq4_nl_impl(i32 noundef 32, i32 noundef 32, ptr noundef %34, ptr noundef %39, ptr noundef %45, ptr noundef %10, ptr noundef %46, ptr noundef %12, ptr noundef %47, ptr noundef %48, ptr noundef @kvalues_iq4nl, ptr noundef null, i32 noundef -1)
  br label %49

49:                                               ; preds = %29
  %50 = load i32, ptr %14, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %14, align 4, !tbaa !11
  br label %23, !llvm.loop !13

52:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 128, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn "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 }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.block_iq4_nl = type { i16, [16 x i8] }

@.str.1 = external hidden unnamed_addr constant [65 x i8], align 1
@kvalues_iq4nl = external hidden constant [16 x i8], align 16
@.str.6 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [14 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @quantize_row_iq4_nl_ref(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [32 x i8], align 16
  %5 = alloca [32 x float], align 16
  %6 = alloca i16, align 2
  %7 = alloca float, align 4
  %8 = and i64 %2, 31
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %11, label %10

10:                                               ; preds = %3
  tail call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef nonnull @.str.1, i32 noundef 4708, ptr noundef nonnull @.str.6, ptr noundef nonnull @.str.11) #3
  unreachable

11:                                               ; preds = %3
  %12 = ashr exact i64 %2, 5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #4
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #4
  ret void

.lr.ph:                                           ; preds = %11, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %11 ]
  %.idx = shl nsw i64 %indvars.iv, 7
  %14 = getelementptr inbounds i8, ptr %0, i64 %.idx
  %15 = getelementptr inbounds %struct.block_iq4_nl, ptr %1, i64 %indvars.iv
  %16 = getelementptr inbounds i8, ptr %15, i64 2
  call void @quantize_row_iq4_nl_impl(i32 noundef 32, i32 noundef 32, ptr noundef %14, ptr noundef %15, ptr noundef nonnull %16, ptr noundef nonnull %6, ptr noundef null, ptr noundef nonnull %7, ptr noundef nonnull %5, ptr noundef nonnull %4, ptr noundef nonnull @kvalues_iq4nl, ptr noundef null, i32 noundef -1)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %12
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5
}

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


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