; LLVM IR from benjinx/Toon :: Modules/Vulkan/Private/glad (vulkan)

; 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._glad_vulkan_userptr = type { ptr, ptr, ptr, ptr, ptr }

@_vulkan_handle = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @gladLoadVulkanUserPtr(ptr noundef, ptr 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
define dso_local i32 @gladLoaderLoadVulkan(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca %struct._glad_vulkan_userptr, align 8
  %11 = alloca %struct._glad_vulkan_userptr, 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr null, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 40, ptr %10) #3
  %12 = load ptr, ptr @_vulkan_handle, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  %14 = zext i1 %13 to i32
  store i32 %14, ptr %9, align 4, !tbaa !9
  %15 = call ptr @glad_vulkan_dlopen_handle()
  store ptr %15, ptr %8, align 8, !tbaa !5
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %40

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 40, ptr %11) #3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  call void @glad_vulkan_build_userptr(ptr dead_on_unwind writable sret(%struct._glad_vulkan_userptr) align 8 %11, ptr noundef %19, ptr noundef %20, ptr noundef %21)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %10, ptr align 8 %11, i64 40, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 40, ptr %11) #3
  %22 = getelementptr inbounds %struct._glad_vulkan_userptr, ptr %10, i32 0, i32 3
  %23 = load ptr, ptr %22, align 8, !tbaa !12
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %32

25:                                               ; preds = %18
  %26 = getelementptr inbounds %struct._glad_vulkan_userptr, ptr %10, i32 0, i32 4
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %32

29:                                               ; preds = %25
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = call i32 @gladLoadVulkanUserPtr(ptr noundef %30, ptr noundef @glad_vulkan_get_proc, ptr noundef %10)
  store i32 %31, ptr %7, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %29, %25, %18
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %39, label %35

35:                                               ; preds = %32
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %35
  call void @gladLoaderUnloadVulkan()
  br label %39

39:                                               ; preds = %38, %35, %32
  br label %40

40:                                               ; preds = %39, %3
  %41 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 40, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %41
}

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_vulkan_dlopen_handle() #0

; Function Attrs: nounwind uwtable
declare hidden void @glad_vulkan_build_userptr(ptr dead_on_unwind noalias writable sret(%struct._glad_vulkan_userptr) align 8, ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_vulkan_get_proc(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @gladLoaderUnloadVulkan() #0

attributes #0 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !5, i64 24, i64 8, !5, i64 32, i64 8, !5}
!12 = !{!13, !6, i64 24}
!13 = !{!"_glad_vulkan_userptr", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!14 = !{!13, !6, i64 32}


; 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._glad_vulkan_userptr = type { ptr, ptr, ptr, ptr, ptr }

@_vulkan_handle = external hidden local_unnamed_addr global ptr, align 8

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @gladLoaderLoadVulkan(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct._glad_vulkan_userptr, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %4) #2
  %5 = load ptr, ptr @_vulkan_handle, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  %7 = tail call ptr @glad_vulkan_dlopen_handle()
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %20, label %8

8:                                                ; preds = %3
  call void @glad_vulkan_build_userptr(ptr dead_on_unwind nonnull writable sret(%struct._glad_vulkan_userptr) align 8 %4, ptr noundef nonnull %7, ptr noundef %0, ptr noundef %2)
  %9 = getelementptr inbounds i8, ptr %4, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  %12 = getelementptr inbounds i8, ptr %4, i64 32
  %13 = load ptr, ptr %12, align 8
  %14 = icmp ne ptr %13, null
  %or.cond = select i1 %11, i1 %14, i1 false
  br i1 %or.cond, label %15, label %17

15:                                               ; preds = %8
  %16 = call i32 @gladLoadVulkanUserPtr(ptr noundef %1, ptr noundef nonnull @glad_vulkan_get_proc, ptr noundef nonnull %4)
  br label %17

17:                                               ; preds = %15, %8
  %.0 = phi i32 [ %16, %15 ], [ 0, %8 ]
  %18 = icmp eq i32 %.0, 0
  %or.cond4 = select i1 %18, i1 %6, i1 false
  br i1 %or.cond4, label %19, label %20

19:                                               ; preds = %17
  call void @gladLoaderUnloadVulkan()
  br label %20

20:                                               ; preds = %17, %19, %3
  %.1 = phi i32 [ 0, %19 ], [ %.0, %17 ], [ 0, %3 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #2
  ret i32 %.1
}

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_vulkan_dlopen_handle() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @glad_vulkan_build_userptr(ptr dead_on_unwind noalias writable sret(%struct._glad_vulkan_userptr) align 8, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @glad_vulkan_get_proc(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @gladLoaderUnloadVulkan() local_unnamed_addr #0

attributes #0 = { 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 24}
!10 = !{!"_glad_vulkan_userptr", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass