; LLVM IR from einthomas/GVSPP :: vulkanutil.ll (ZN10VulkanUtil14findMemoryTypeEP18VkPhysicalDevice_Tjj)

; 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.VkPhysicalDeviceMemoryProperties = type { i32, [32 x %struct.VkMemoryType], i32, [16 x %struct.VkMemoryHeap] }
%struct.VkMemoryType = type { i32, i32 }
%struct.VkMemoryHeap = type { i64, i32 }

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN10VulkanUtil14findMemoryTypeEP18VkPhysicalDevice_Tjj(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.VkPhysicalDeviceMemoryProperties, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 520, ptr %8) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  call void @vkGetPhysicalDeviceMemoryProperties(ptr noundef %11, ptr noundef %8)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %38, %3
  %13 = load i32, ptr %9, align 4, !tbaa !9
  %14 = getelementptr inbounds %struct.VkPhysicalDeviceMemoryProperties, ptr %8, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = icmp ult i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %12
  store i32 2, ptr %10, align 4
  br label %41

18:                                               ; preds = %12
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = shl i32 1, %20
  %22 = and i32 %19, %21
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %37

24:                                               ; preds = %18
  %25 = getelementptr inbounds %struct.VkPhysicalDeviceMemoryProperties, ptr %8, i32 0, i32 1
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds [32 x %struct.VkMemoryType], ptr %25, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.VkMemoryType, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = and i32 %30, %31
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %24
  %36 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %36, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %41

37:                                               ; preds = %24, %18
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = add i32 %39, 1
  store i32 %40, ptr %9, align 4, !tbaa !9
  br label %12, !llvm.loop !15

41:                                               ; preds = %35, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %42 = load i32, ptr %10, align 4
  switch i32 %42, label %44 [
    i32 2, label %43
  ]

43:                                               ; preds = %41
  unreachable

44:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 520, ptr %8) #3
  %45 = load i32, ptr %4, align 4
  ret i32 %45
}

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

declare void @vkGetPhysicalDeviceMemoryProperties(ptr noundef, ptr 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 }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS32VkPhysicalDeviceMemoryProperties", !10, i64 0, !7, i64 4, !10, i64 260, !7, i64 264}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTS12VkMemoryType", !10, i64 0, !10, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"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.VkPhysicalDeviceMemoryProperties = type { i32, [32 x %struct.VkMemoryType], i32, [16 x %struct.VkMemoryHeap] }
%struct.VkMemoryType = type { i32, i32 }
%struct.VkMemoryHeap = type { i64, i32 }

; 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 uwtable
define dso_local noundef i32 @_ZN10VulkanUtil14findMemoryTypeEP18VkPhysicalDevice_Tjj(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
.lr.ph.preheader:
  %3 = alloca %struct.VkPhysicalDeviceMemoryProperties, align 8
  call void @llvm.lifetime.start.p0(i64 520, ptr nonnull %3) #4
  call void @vkGetPhysicalDeviceMemoryProperties(ptr noundef %0, ptr noundef nonnull %3)
  %4 = getelementptr inbounds i8, ptr %3, i64 4
  %5 = load i32, ptr %3, align 8, !tbaa !5
  %.not1213.not = icmp ne i32 %5, 0
  call void @llvm.assume(i1 %.not1213.not)
  %6 = zext i32 %5 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %15
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %15 ]
  %.not1215 = phi i1 [ true, %.lr.ph.preheader ], [ %.not12, %15 ]
  %7 = trunc nuw i64 %indvars.iv to i32
  %8 = shl nuw i32 1, %7
  %9 = and i32 %8, %1
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %15, label %10

10:                                               ; preds = %.lr.ph
  %11 = getelementptr inbounds [32 x %struct.VkMemoryType], ptr %4, i64 0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !10
  %13 = and i32 %12, %2
  %14 = icmp eq i32 %13, %2
  br i1 %14, label %._crit_edge, label %15

15:                                               ; preds = %.lr.ph, %10
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not12 = icmp ult i64 %indvars.iv.next, %6
  %exitcond.not = icmp eq i64 %indvars.iv.next, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %15, %10
  %.010.lcssa = phi i32 [ %7, %10 ], [ %5, %15 ]
  %.not12.lcssa = phi i1 [ %.not1215, %10 ], [ %.not12, %15 ]
  call void @llvm.assume(i1 %.not12.lcssa)
  call void @llvm.lifetime.end.p0(i64 520, ptr nonnull %3) #4
  ret i32 %.010.lcssa
}

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

declare void @vkGetPhysicalDeviceMemoryProperties(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #3

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
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, !7, i64 0}
!6 = !{!"_ZTS32VkPhysicalDeviceMemoryProperties", !7, i64 0, !8, i64 4, !7, i64 260, !8, i64 264}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"_ZTS12VkMemoryType", !7, i64 0, !7, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass