; LLVM IR from khushmmehta/Scorch-V :: src/ScorchVkEngine/Abstractions (VulkanMemoryAllocator)

; 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.VmaDefragmentationContext_T::StateBalanced" = type { i64, i64 }
%class.VmaDeviceMemoryBlock = type { ptr, ptr, i32, i32, ptr, %class.VmaMutex, %class.VmaMappingHysteresis, i32, ptr }
%class.VmaMutex = type { %"class.std::mutex" }
%"class.std::mutex" = type { %"class.std::__mutex_base" }
%"class.std::__mutex_base" = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%class.VmaMappingHysteresis = type { i32, i32, i32 }

; 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 nounwind uwtable
declare dso_local noundef i64 @_ZNK16VmaBlockMetadata7GetSizeEv(ptr noundef nonnull align 8 dereferenceable(33)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK14VmaBlockVector13GetBlockCountEv(ptr noundef nonnull align 8 dereferenceable(181)) #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZNK14VmaBlockVector8GetBlockEm(ptr noundef nonnull align 8 dereferenceable(181), i64 noundef) #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN27VmaDefragmentationContext_T22UpdateVectorStatisticsER14VmaBlockVectorRNS_13StateBalancedE(ptr noundef nonnull align 8 dereferenceable(168) %0, ptr noundef nonnull align 8 dereferenceable(181) %1, ptr noundef nonnull align 8 dereferenceable(16) %2) #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %12, i32 0, i32 0
  store i64 0, ptr %13, align 8, !tbaa !11
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %14, i32 0, i32 1
  store i64 0, ptr %15, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %57, %3
  %17 = load i64, ptr %9, align 8, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = call noundef i64 @_ZNK14VmaBlockVector13GetBlockCountEv(ptr noundef nonnull align 8 dereferenceable(181) %18)
  %20 = icmp ult i64 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %60

22:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = call noundef ptr @_ZNK14VmaBlockVector8GetBlockEm(ptr noundef nonnull align 8 dereferenceable(181) %23, i64 noundef %24)
  %26 = getelementptr inbounds %class.VmaDeviceMemoryBlock, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  store ptr %27, ptr %10, align 8, !tbaa !5
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = load ptr, ptr %28, align 8, !tbaa !21
  %30 = getelementptr inbounds ptr, ptr %29, i64 4
  %31 = load ptr, ptr %30, align 8
  %32 = call noundef i64 %31(ptr noundef nonnull align 8 dereferenceable(33) %28)
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = add i64 %33, %32
  store i64 %34, ptr %7, align 8, !tbaa !9
  %35 = load ptr, ptr %10, align 8, !tbaa !5
  %36 = load ptr, ptr %35, align 8, !tbaa !21
  %37 = getelementptr inbounds ptr, ptr %36, i64 5
  %38 = load ptr, ptr %37, align 8
  %39 = call noundef i64 %38(ptr noundef nonnull align 8 dereferenceable(33) %35)
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = add i64 %40, %39
  store i64 %41, ptr %8, align 8, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = load ptr, ptr %42, align 8, !tbaa !21
  %44 = getelementptr inbounds ptr, ptr %43, i64 6
  %45 = load ptr, ptr %44, align 8
  %46 = call noundef i64 %45(ptr noundef nonnull align 8 dereferenceable(33) %42)
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = add i64 %49, %46
  store i64 %50, ptr %48, align 8, !tbaa !11
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = call noundef i64 @_ZNK16VmaBlockMetadata7GetSizeEv(ptr noundef nonnull align 8 dereferenceable(33) %51)
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %53, i32 0, i32 1
  %55 = load i64, ptr %54, align 8, !tbaa !13
  %56 = add i64 %55, %52
  store i64 %56, ptr %54, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %57

57:                                               ; preds = %22
  %58 = load i64, ptr %9, align 8, !tbaa !9
  %59 = add i64 %58, 1
  store i64 %59, ptr %9, align 8, !tbaa !9
  br label %16, !llvm.loop !23

60:                                               ; preds = %21
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %61, i32 0, i32 1
  %63 = load i64, ptr %62, align 8, !tbaa !13
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %64, i32 0, i32 0
  %66 = load i64, ptr %65, align 8, !tbaa !11
  %67 = sub i64 %63, %66
  %68 = load i64, ptr %7, align 8, !tbaa !9
  %69 = udiv i64 %67, %68
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %70, i32 0, i32 1
  store i64 %69, ptr %71, align 8, !tbaa !13
  %72 = load i64, ptr %8, align 8, !tbaa !9
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %"struct.VmaDefragmentationContext_T::StateBalanced", ptr %73, i32 0, i32 0
  %75 = load i64, ptr %74, align 8, !tbaa !11
  %76 = udiv i64 %75, %72
  store i64 %76, ptr %74, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTSN27VmaDefragmentationContext_T13StateBalancedE", !10, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"_ZTS20VmaDeviceMemoryBlock", !6, i64 0, !6, i64 8, !16, i64 16, !16, i64 20, !6, i64 24, !17, i64 32, !20, i64 72, !16, i64 84, !6, i64 88}
!16 = !{!"int", !7, i64 0}
!17 = !{!"_ZTS8VmaMutex", !18, i64 0}
!18 = !{!"_ZTSSt5mutex", !19, i64 0}
!19 = !{!"_ZTSSt12__mutex_base", !7, i64 0}
!20 = !{!"_ZTS20VmaMappingHysteresis", !16, i64 0, !16, i64 4, !16, i64 8}
!21 = !{!22, !22, i64 0}
!22 = !{!"vtable pointer", !8, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK16VmaBlockMetadata7GetSizeEv(ptr noundef nonnull align 8 dereferenceable(33)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK14VmaBlockVector13GetBlockCountEv(ptr noundef nonnull align 8 dereferenceable(181)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZNK14VmaBlockVector8GetBlockEm(ptr noundef nonnull align 8 dereferenceable(181), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN27VmaDefragmentationContext_T22UpdateVectorStatisticsER14VmaBlockVectorRNS_13StateBalancedE(ptr nocapture noundef nonnull readnone align 8 dereferenceable(168) %0, ptr noundef nonnull align 8 dereferenceable(181) %1, ptr nocapture noundef nonnull align 8 dereferenceable(16) %2) local_unnamed_addr #1 align 2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 0, i64 16, i1 false)
  %5 = tail call noundef i64 @_ZNK14VmaBlockVector13GetBlockCountEv(ptr noundef nonnull align 8 dereferenceable(181) %1)
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.022.lcssa = phi i64 [ 0, %3 ], [ %22, %.lr.ph ]
  %.0.lcssa = phi i64 [ 0, %3 ], [ %17, %.lr.ph ]
  %6 = load i64, ptr %4, align 8, !tbaa !5
  %7 = load i64, ptr %2, align 8, !tbaa !10
  %8 = sub i64 %6, %7
  %9 = udiv i64 %8, %.0.lcssa
  store i64 %9, ptr %4, align 8, !tbaa !5
  %10 = udiv i64 %7, %.022.lcssa
  store i64 %10, ptr %2, align 8, !tbaa !10
  ret void

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.025 = phi i64 [ %17, %.lr.ph ], [ 0, %3 ]
  %.02124 = phi i64 [ %32, %.lr.ph ], [ 0, %3 ]
  %.02223 = phi i64 [ %22, %.lr.ph ], [ 0, %3 ]
  %11 = tail call noundef ptr @_ZNK14VmaBlockVector8GetBlockEm(ptr noundef nonnull align 8 dereferenceable(181) %1, i64 noundef %.02124)
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = load ptr, ptr %12, align 8, !tbaa !19
  %14 = getelementptr inbounds i8, ptr %13, i64 32
  %15 = load ptr, ptr %14, align 8
  %16 = tail call noundef i64 %15(ptr noundef nonnull align 8 dereferenceable(33) %12)
  %17 = add i64 %16, %.025
  %18 = load ptr, ptr %12, align 8, !tbaa !19
  %19 = getelementptr inbounds i8, ptr %18, i64 40
  %20 = load ptr, ptr %19, align 8
  %21 = tail call noundef i64 %20(ptr noundef nonnull align 8 dereferenceable(33) %12)
  %22 = add i64 %21, %.02223
  %23 = load ptr, ptr %12, align 8, !tbaa !19
  %24 = getelementptr inbounds i8, ptr %23, i64 48
  %25 = load ptr, ptr %24, align 8
  %26 = tail call noundef i64 %25(ptr noundef nonnull align 8 dereferenceable(33) %12)
  %27 = load i64, ptr %2, align 8, !tbaa !10
  %28 = add i64 %27, %26
  store i64 %28, ptr %2, align 8, !tbaa !10
  %29 = tail call noundef i64 @_ZNK16VmaBlockMetadata7GetSizeEv(ptr noundef nonnull align 8 dereferenceable(33) %12)
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = add i64 %30, %29
  store i64 %31, ptr %4, align 8, !tbaa !5
  %32 = add nuw i64 %.02124, 1
  %33 = tail call noundef i64 @_ZNK14VmaBlockVector13GetBlockCountEv(ptr noundef nonnull align 8 dereferenceable(181) %1)
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %.lr.ph, label %._crit_edge, !llvm.loop !21
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

!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 8}
!6 = !{!"_ZTSN27VmaDefragmentationContext_T13StateBalancedE", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS20VmaDeviceMemoryBlock", !13, i64 0, !13, i64 8, !14, i64 16, !14, i64 20, !13, i64 24, !15, i64 32, !18, i64 72, !14, i64 84, !13, i64 88}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!"int", !8, i64 0}
!15 = !{!"_ZTS8VmaMutex", !16, i64 0}
!16 = !{!"_ZTSSt5mutex", !17, i64 0}
!17 = !{!"_ZTSSt12__mutex_base", !8, i64 0}
!18 = !{!"_ZTS20VmaMappingHysteresis", !14, i64 0, !14, i64 4, !14, i64 8}
!19 = !{!20, !20, i64 0}
!20 = !{!"vtable pointer", !9, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass