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

; 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.ggml_tensor = type { i32, ptr, [4 x i64], [4 x i64], i32, [16 x i32], i32, [10 x ptr], ptr, i64, ptr, [64 x i8], ptr, [8 x i8] }

@.str.12 = external hidden unnamed_addr constant [58 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.97 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
declare dso_local void @ggml_abort(ptr noundef, i32 noundef, ptr noundef, ...) #0

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @ggml_argsort(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @ggml_top_k(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.ggml_tensor, ptr %8, i32 0, i32 2
  %10 = getelementptr inbounds [4 x i64], ptr %9, i64 0, i64 0
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = icmp sge i64 %11, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %3
  call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef @.str.12, i32 noundef 4341, ptr noundef @.str.13, ptr noundef @.str.97) #3
  unreachable

16:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = call ptr @ggml_argsort(ptr noundef %17, ptr noundef %18, i32 noundef 1)
  store ptr %19, ptr %7, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.ggml_tensor, ptr %24, i32 0, i32 2
  %26 = getelementptr inbounds [4 x i64], ptr %25, i64 0, i64 1
  %27 = load i64, ptr %26, align 8, !tbaa !11
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.ggml_tensor, ptr %28, i32 0, i32 2
  %30 = getelementptr inbounds [4 x i64], ptr %29, i64 0, i64 2
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ggml_tensor, ptr %32, i32 0, i32 2
  %34 = getelementptr inbounds [4 x i64], ptr %33, i64 0, i64 3
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ggml_tensor, ptr %36, i32 0, i32 3
  %38 = getelementptr inbounds [4 x i64], ptr %37, i64 0, i64 1
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ggml_tensor, ptr %40, i32 0, i32 3
  %42 = getelementptr inbounds [4 x i64], ptr %41, i64 0, i64 2
  %43 = load i64, ptr %42, align 8, !tbaa !11
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ggml_tensor, ptr %44, i32 0, i32 3
  %46 = getelementptr inbounds [4 x i64], ptr %45, i64 0, i64 3
  %47 = load i64, ptr %46, align 8, !tbaa !11
  %48 = call ptr @ggml_view_4d(ptr noundef %20, ptr noundef %21, i64 noundef %23, i64 noundef %27, i64 noundef %31, i64 noundef %35, i64 noundef %39, i64 noundef %43, i64 noundef %47, i64 noundef 0)
  store ptr %48, ptr %7, align 8, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %49
}

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


; 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.12 = external hidden unnamed_addr constant [58 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.97 = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
declare dso_local void @ggml_abort(ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @ggml_top_k(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %1, i64 16
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = sext i32 %2 to i64
  %.not = icmp slt i64 %5, %6
  br i1 %.not, label %7, label %8

7:                                                ; preds = %3
  tail call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef nonnull @.str.12, i32 noundef 4341, ptr noundef nonnull @.str.13, ptr noundef nonnull @.str.97) #2
  unreachable

8:                                                ; preds = %3
  %9 = tail call ptr @ggml_argsort(ptr noundef %0, ptr noundef nonnull %1, i32 noundef 1)
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %9, i64 32
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %9, i64 40
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %9, i64 56
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %9, i64 64
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %9, i64 72
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = tail call ptr @ggml_view_4d(ptr noundef %0, ptr noundef %9, i64 noundef %6, i64 noundef %11, i64 noundef %13, i64 noundef %15, i64 noundef %17, i64 noundef %19, i64 noundef %21, i64 noundef 0)
  ret ptr %22
}

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass