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

; 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_tallocr = type { ptr, ptr, i64, i64 }
%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.1 = external hidden unnamed_addr constant [64 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [79 x i8], align 1
@__func__.ggml_tallocr_alloc = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [43 x i8], align 1
@__PRETTY_FUNCTION__.ggml_tallocr_alloc = external hidden unnamed_addr constant [81 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

declare ptr @ggml_backend_buffer_get_base(ptr noundef) #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; 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
define dso_local i32 @ggml_tallocr_alloc(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.ggml_tallocr, ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = call i64 @ggml_backend_buffer_get_alloc_size(ptr noundef %9, ptr noundef %10)
  store i64 %11, ptr %5, align 8, !tbaa !12
  %12 = load i64, ptr %5, align 8, !tbaa !12
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.ggml_tallocr, ptr %13, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %16 = add i64 %12, %15
  %17 = sub i64 %16, 1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.ggml_tallocr, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = sub i64 %20, 1
  %22 = xor i64 %21, -1
  %23 = and i64 %17, %22
  store i64 %23, ptr %5, align 8, !tbaa !12
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.ggml_tallocr, ptr %24, i32 0, i32 3
  %26 = load i64, ptr %25, align 8, !tbaa !14
  %27 = load i64, ptr %5, align 8, !tbaa !12
  %28 = add i64 %26, %27
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ggml_tallocr, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  %32 = call i64 @ggml_backend_buffer_get_size(ptr noundef %31)
  %33 = icmp ugt i64 %28, %32
  br i1 %33, label %34, label %47

34:                                               ; preds = %2
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.ggml_tensor, ptr %35, i32 0, i32 11
  %37 = getelementptr inbounds [64 x i8], ptr %36, i64 0, i64 0
  %38 = load i64, ptr %5, align 8, !tbaa !12
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.ggml_tallocr, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = call i64 @ggml_backend_buffer_get_size(ptr noundef %41)
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.ggml_tallocr, ptr %43, i32 0, i32 3
  %45 = load i64, ptr %44, align 8, !tbaa !14
  %46 = sub i64 %42, %45
  call void (i32, ptr, ...) @ggml_log_internal(i32 noundef 4, ptr noundef @.str.2, ptr noundef @__func__.ggml_tallocr_alloc, ptr noundef %37, i64 noundef %38, i64 noundef %46)
  call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef @.str.1, i32 noundef 99, ptr noundef @.str.3) #6
  unreachable

47:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.ggml_tallocr, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !9
  %51 = call ptr @ggml_backend_buffer_get_base(ptr noundef %50)
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ggml_tallocr, ptr %52, i32 0, i32 3
  %54 = load i64, ptr %53, align 8, !tbaa !14
  %55 = getelementptr inbounds i8, ptr %51, i64 %54
  store ptr %55, ptr %6, align 8, !tbaa !5
  %56 = load i64, ptr %5, align 8, !tbaa !12
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ggml_tallocr, ptr %57, i32 0, i32 3
  %59 = load i64, ptr %58, align 8, !tbaa !14
  %60 = add i64 %59, %56
  store i64 %60, ptr %58, align 8, !tbaa !14
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = ptrtoint ptr %61 to i64
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.ggml_tallocr, ptr %63, i32 0, i32 2
  %65 = load i64, ptr %64, align 8, !tbaa !13
  %66 = urem i64 %62, %65
  %67 = icmp eq i64 %66, 0
  br i1 %67, label %68, label %69

68:                                               ; preds = %47
  br label %70

69:                                               ; preds = %47
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.1, i32 noundef 105, ptr noundef @__PRETTY_FUNCTION__.ggml_tallocr_alloc) #7
  unreachable

70:                                               ; preds = %68
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.ggml_tallocr, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !9
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = call i32 @ggml_backend_tensor_alloc(ptr noundef %73, ptr noundef %74, ptr noundef %75)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret i32 %76
}

declare i64 @ggml_backend_buffer_get_alloc_size(ptr noundef, ptr noundef) #1

declare i64 @ggml_backend_buffer_get_size(ptr noundef) #1

declare void @ggml_log_internal(i32 noundef, ptr noundef, ...) #1

; Function Attrs: noreturn
declare void @ggml_abort(ptr noundef, i32 noundef, ptr noundef, ...) #4

declare i32 @ggml_backend_tensor_alloc(ptr noundef, ptr noundef, ptr noundef) #1

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 = { noreturn 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 #3 = { 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn }
attributes #7 = { noreturn 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, !6, i64 0}
!10 = !{!"ggml_tallocr", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 24}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 16}
!14 = !{!10, !11, i64 24}


; 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.1 = external hidden unnamed_addr constant [64 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [79 x i8], align 1
@__func__.ggml_tallocr_alloc = external hidden unnamed_addr constant [19 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [43 x i8], align 1
@__PRETTY_FUNCTION__.ggml_tallocr_alloc = external hidden unnamed_addr constant [81 x i8], align 1

declare ptr @ggml_backend_buffer_get_base(ptr noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @ggml_tallocr_alloc(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = tail call i64 @ggml_backend_buffer_get_alloc_size(ptr noundef %3, ptr noundef %1) #4
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = add i64 %4, -1
  %8 = add i64 %7, %6
  %9 = sub i64 0, %6
  %10 = and i64 %8, %9
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load i64, ptr %11, align 8, !tbaa !12
  %13 = add i64 %10, %12
  %14 = load ptr, ptr %0, align 8, !tbaa !5
  %15 = tail call i64 @ggml_backend_buffer_get_size(ptr noundef %14) #4
  %16 = icmp ugt i64 %13, %15
  br i1 %16, label %17, label %23

17:                                               ; preds = %2
  %18 = getelementptr inbounds i8, ptr %1, i64 256
  %19 = load ptr, ptr %0, align 8, !tbaa !5
  %20 = tail call i64 @ggml_backend_buffer_get_size(ptr noundef %19) #4
  %21 = load i64, ptr %11, align 8, !tbaa !12
  %22 = sub i64 %20, %21
  tail call void (i32, ptr, ...) @ggml_log_internal(i32 noundef 4, ptr noundef nonnull @.str.2, ptr noundef nonnull @__func__.ggml_tallocr_alloc, ptr noundef nonnull %18, i64 noundef %10, i64 noundef %22) #4
  tail call void (ptr, i32, ptr, ...) @ggml_abort(ptr noundef nonnull @.str.1, i32 noundef 99, ptr noundef nonnull @.str.3) #5
  unreachable

23:                                               ; preds = %2
  %24 = load ptr, ptr %0, align 8, !tbaa !5
  %25 = tail call ptr @ggml_backend_buffer_get_base(ptr noundef %24) #4
  %26 = load i64, ptr %11, align 8, !tbaa !12
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  %28 = add i64 %26, %10
  store i64 %28, ptr %11, align 8, !tbaa !12
  %29 = ptrtoint ptr %27 to i64
  %30 = load i64, ptr %5, align 8, !tbaa !11
  %31 = urem i64 %29, %30
  %32 = icmp eq i64 %31, 0
  br i1 %32, label %34, label %33

33:                                               ; preds = %23
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i32 noundef 105, ptr noundef nonnull @__PRETTY_FUNCTION__.ggml_tallocr_alloc) #5
  unreachable

34:                                               ; preds = %23
  %35 = load ptr, ptr %0, align 8, !tbaa !5
  %36 = tail call i32 @ggml_backend_tensor_alloc(ptr noundef %35, ptr noundef %1, ptr noundef %27) #4
  ret i32 %36
}

declare i64 @ggml_backend_buffer_get_alloc_size(ptr noundef, ptr noundef) local_unnamed_addr #0

declare i64 @ggml_backend_buffer_get_size(ptr noundef) local_unnamed_addr #0

declare void @ggml_log_internal(i32 noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: noreturn
declare void @ggml_abort(ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #3

declare i32 @ggml_backend_tensor_alloc(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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 = { noreturn 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 #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 "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 }
attributes #5 = { noreturn 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, !7, i64 0}
!6 = !{!"ggml_tallocr", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!6, !10, i64 24}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass