; LLVM IR from ggml-org/ggml :: examples/mnist/mnist-common.ll (Z14load_from_ggufPKcP12ggml_contextP12gguf_context)

; 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.9 = external hidden unnamed_addr constant [3 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: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z14load_from_ggufPKcP12ggml_contextP12gguf_context(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca i64, align 8
  %18 = alloca i64, align 8
  %19 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = call ptr @ggml_fopen(ptr noundef %20, ptr noundef @.str.9)
  store ptr %21, ptr %8, align 8, !tbaa !5
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %25, label %24

24:                                               ; preds = %3
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %121

25:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  store i64 4194304, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %26 = call noalias ptr @malloc(i64 noundef 4194304) #6
  store ptr %26, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = call i64 @gguf_get_n_tensors(ptr noundef %27)
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  store i32 0, ptr %13, align 4, !tbaa !11
  br label %30

30:                                               ; preds = %111, %25
  %31 = load i32, ptr %13, align 4, !tbaa !11
  %32 = load i32, ptr %12, align 4, !tbaa !11
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  store i32 2, ptr %9, align 4
  br label %114

35:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i32, ptr %13, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = call ptr @gguf_get_tensor_name(ptr noundef %36, i64 noundef %38)
  store ptr %39, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load ptr, ptr %14, align 8, !tbaa !5
  %42 = call ptr @ggml_get_tensor(ptr noundef %40, ptr noundef %41)
  store ptr %42, ptr %15, align 8, !tbaa !5
  %43 = load ptr, ptr %15, align 8, !tbaa !5
  %44 = icmp ne ptr %43, null
  br i1 %44, label %46, label %45

45:                                               ; preds = %35
  store i32 4, ptr %9, align 4
  br label %108

46:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = call i64 @gguf_get_data_offset(ptr noundef %47)
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load i32, ptr %13, align 4, !tbaa !11
  %51 = sext i32 %50 to i64
  %52 = call i64 @gguf_get_tensor_offset(ptr noundef %49, i64 noundef %51)
  %53 = add i64 %48, %52
  store i64 %53, ptr %16, align 8, !tbaa !9
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = load i64, ptr %16, align 8, !tbaa !9
  %56 = call i32 @fseek(ptr noundef %54, i64 noundef %55, i32 noundef 0)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %62

58:                                               ; preds = %46
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = call i32 @fclose(ptr noundef %59)
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %61) #5
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %107

62:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #5
  %63 = load ptr, ptr %15, align 8, !tbaa !5
  %64 = call i64 @ggml_nbytes(ptr noundef %63)
  store i64 %64, ptr %17, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #5
  store i64 0, ptr %18, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %100, %62
  %66 = load i64, ptr %18, align 8, !tbaa !9
  %67 = load i64, ptr %17, align 8, !tbaa !9
  %68 = icmp ult i64 %66, %67
  br i1 %68, label %70, label %69

69:                                               ; preds = %65
  store i32 5, ptr %9, align 4
  br label %103

70:                                               ; preds = %65
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #5
  %71 = load i64, ptr %17, align 8, !tbaa !9
  %72 = load i64, ptr %18, align 8, !tbaa !9
  %73 = sub i64 %71, %72
  %74 = icmp ult i64 4194304, %73
  br i1 %74, label %75, label %76

75:                                               ; preds = %70
  br label %80

76:                                               ; preds = %70
  %77 = load i64, ptr %17, align 8, !tbaa !9
  %78 = load i64, ptr %18, align 8, !tbaa !9
  %79 = sub i64 %77, %78
  br label %80

80:                                               ; preds = %76, %75
  %81 = phi i64 [ 4194304, %75 ], [ %79, %76 ]
  store i64 %81, ptr %19, align 8, !tbaa !9
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  %83 = load i64, ptr %19, align 8, !tbaa !9
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = call i64 @fread(ptr noundef %82, i64 noundef 1, i64 noundef %83, ptr noundef %84)
  %86 = load i64, ptr %19, align 8, !tbaa !9
  %87 = icmp ne i64 %85, %86
  br i1 %87, label %88, label %92

88:                                               ; preds = %80
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = call i32 @fclose(ptr noundef %89)
  %91 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %91) #5
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %97

92:                                               ; preds = %80
  %93 = load ptr, ptr %15, align 8, !tbaa !5
  %94 = load ptr, ptr %11, align 8, !tbaa !5
  %95 = load i64, ptr %18, align 8, !tbaa !9
  %96 = load i64, ptr %19, align 8, !tbaa !9
  call void @ggml_backend_tensor_set(ptr noundef %93, ptr noundef %94, i64 noundef %95, i64 noundef %96)
  store i32 0, ptr %9, align 4
  br label %97

97:                                               ; preds = %92, %88
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #5
  %98 = load i32, ptr %9, align 4
  switch i32 %98, label %103 [
    i32 0, label %99
  ]

99:                                               ; preds = %97
  br label %100

100:                                              ; preds = %99
  %101 = load i64, ptr %18, align 8, !tbaa !9
  %102 = add i64 %101, 4194304
  store i64 %102, ptr %18, align 8, !tbaa !9
  br label %65, !llvm.loop !13

103:                                              ; preds = %97, %69
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #5
  %104 = load i32, ptr %9, align 4
  switch i32 %104, label %106 [
    i32 5, label %105
  ]

105:                                              ; preds = %103
  store i32 0, ptr %9, align 4
  br label %106

106:                                              ; preds = %105, %103
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #5
  br label %107

107:                                              ; preds = %106, %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  br label %108

108:                                              ; preds = %107, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  %109 = load i32, ptr %9, align 4
  switch i32 %109, label %114 [
    i32 0, label %110
    i32 4, label %111
  ]

110:                                              ; preds = %108
  br label %111

111:                                              ; preds = %110, %108
  %112 = load i32, ptr %13, align 4, !tbaa !11
  %113 = add nsw i32 %112, 1
  store i32 %113, ptr %13, align 4, !tbaa !11
  br label %30, !llvm.loop !15

114:                                              ; preds = %108, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  %115 = load i32, ptr %9, align 4
  switch i32 %115, label %120 [
    i32 2, label %116
  ]

116:                                              ; preds = %114
  %117 = load ptr, ptr %8, align 8, !tbaa !5
  %118 = call i32 @fclose(ptr noundef %117)
  %119 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %119) #5
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %120

120:                                              ; preds = %116, %114
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %121

121:                                              ; preds = %120, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %122 = load i1, ptr %4, align 1
  ret i1 %122
}

declare ptr @ggml_fopen(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

declare i64 @gguf_get_n_tensors(ptr noundef) #2

declare ptr @gguf_get_tensor_name(ptr noundef, i64 noundef) #2

declare ptr @ggml_get_tensor(ptr noundef, ptr noundef) #2

declare i64 @gguf_get_data_offset(ptr noundef) #2

declare i64 @gguf_get_tensor_offset(ptr noundef, i64 noundef) #2

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #2

declare i32 @fclose(ptr noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

declare i64 @ggml_nbytes(ptr noundef) #2

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

declare void @ggml_backend_tensor_set(ptr noundef, ptr noundef, i64 noundef, i64 noundef) #2

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

!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++ 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"}
!15 = distinct !{!15, !14}


; 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.9 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z14load_from_ggufPKcP12ggml_contextP12gguf_context(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = tail call ptr @ggml_fopen(ptr noundef %0, ptr noundef nonnull @.str.9)
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %24, label %5

5:                                                ; preds = %3
  %6 = tail call noalias dereferenceable_or_null(4194304) ptr @malloc(i64 noundef 4194304) #6
  %7 = tail call i64 @gguf_get_n_tensors(ptr noundef %2)
  %8 = trunc i64 %7 to i32
  %.not59.not64 = icmp sgt i32 %8, 0
  br i1 %.not59.not64, label %.lr.ph66.preheader, label %.sink.split

.lr.ph66.preheader:                               ; preds = %5
  %wide.trip.count = and i64 %7, 2147483647
  br label %.lr.ph66

.lr.ph66:                                         ; preds = %.lr.ph66.preheader, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph66.preheader ], [ %indvars.iv.next, %.loopexit ]
  %9 = tail call ptr @gguf_get_tensor_name(ptr noundef %2, i64 noundef %indvars.iv)
  %10 = tail call ptr @ggml_get_tensor(ptr noundef %1, ptr noundef %9)
  %.not55 = icmp eq ptr %10, null
  br i1 %.not55, label %.loopexit, label %11

11:                                               ; preds = %.lr.ph66
  %12 = tail call i64 @gguf_get_data_offset(ptr noundef %2)
  %13 = tail call i64 @gguf_get_tensor_offset(ptr noundef %2, i64 noundef %indvars.iv)
  %14 = add i64 %13, %12
  %15 = tail call i32 @fseek(ptr noundef nonnull %4, i64 noundef %14, i32 noundef 0)
  %.not56 = icmp eq i32 %15, 0
  br i1 %.not56, label %16, label %.sink.split

16:                                               ; preds = %11
  %17 = tail call i64 @ggml_nbytes(ptr noundef nonnull %10)
  %.not5862.not = icmp eq i64 %17, 0
  br i1 %.not5862.not, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %16, %21
  %.04463 = phi i64 [ %22, %21 ], [ 0, %16 ]
  %18 = sub i64 %17, %.04463
  %19 = tail call i64 @llvm.umin.i64(i64 %18, i64 4194304)
  %20 = tail call i64 @fread(ptr noundef %6, i64 noundef 1, i64 noundef %19, ptr noundef nonnull %4)
  %.not57 = icmp eq i64 %20, %19
  br i1 %.not57, label %21, label %.sink.split

21:                                               ; preds = %.lr.ph
  tail call void @ggml_backend_tensor_set(ptr noundef nonnull %10, ptr noundef %6, i64 noundef %.04463, i64 noundef %19)
  %22 = add i64 %.04463, 4194304
  %.not58 = icmp ult i64 %22, %17
  br i1 %.not58, label %.lr.ph, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %21, %16, %.lr.ph66
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.sink.split, label %.lr.ph66, !llvm.loop !7

.sink.split:                                      ; preds = %.loopexit, %11, %.lr.ph, %5
  %.8.ph = phi i1 [ true, %5 ], [ false, %.lr.ph ], [ false, %11 ], [ true, %.loopexit ]
  %23 = tail call i32 @fclose(ptr noundef nonnull %4)
  tail call void @free(ptr noundef %6) #7
  br label %24

24:                                               ; preds = %.sink.split, %3
  %.8 = phi i1 [ false, %3 ], [ %.8.ph, %.sink.split ]
  ret i1 %.8
}

declare ptr @ggml_fopen(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

declare i64 @gguf_get_n_tensors(ptr noundef) local_unnamed_addr #1

declare ptr @gguf_get_tensor_name(ptr noundef, i64 noundef) local_unnamed_addr #1

declare ptr @ggml_get_tensor(ptr noundef, ptr noundef) local_unnamed_addr #1

declare i64 @gguf_get_data_offset(ptr noundef) local_unnamed_addr #1

declare i64 @gguf_get_tensor_offset(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

declare i64 @ggml_nbytes(ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

declare void @ggml_backend_tensor_set(ptr noundef, ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #5

attributes #0 = { mustprogress 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree 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 #4 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { 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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass