; LLVM IR from NVIDIA/DALI :: dali/core/cuda_stream_pool.ll (ZN4dali14CUDAStreamPool3GetEi)

; 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"

%"class.dali::CUDAStreamLease" = type { %"class.dali::CUDAStream", i32, ptr }
%"class.dali::CUDAStream" = type { %"class.dali::UniqueHandle" }
%"class.dali::UniqueHandle" = type { ptr }

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4dali14CUDAStreamPool3GetEi(ptr dead_on_unwind noalias writable sret(%"class.dali::CUDAStreamLease") align 8 %0, ptr noundef nonnull align 8 dereferenceable(41) %1, i32 noundef %2) #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %"class.dali::CUDAStream", align 8
  %8 = alloca %"class.dali::CUDAStream", align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %11 = load ptr, ptr %5, align 8
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %16

14:                                               ; preds = %3
  %15 = call i32 @cudaGetDevice(ptr noundef %6)
  call void @_Z9CUDA_CALLI9cudaErrorEvT_(i32 noundef %15)
  br label %16

16:                                               ; preds = %14, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %17 = load i32, ptr %6, align 4, !tbaa !9
  call void @_ZN4dali14CUDAStreamPool11GetFromPoolEi(ptr dead_on_unwind writable sret(%"class.dali::CUDAStream") align 8 %7, ptr noundef nonnull align 8 dereferenceable(41) %11, i32 noundef %17)
  %18 = call noundef zeroext i1 @_ZNK4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEEcvbEv(ptr noundef nonnull align 8 dereferenceable(8) %7) #6
  br i1 %18, label %33, label %19

19:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %20 = load i32, ptr %6, align 4, !tbaa !9
  invoke void @_ZN4dali10CUDAStream6CreateEbi(ptr dead_on_unwind writable sret(%"class.dali::CUDAStream") align 8 %8, i1 noundef zeroext true, i32 noundef %20)
          to label %21 unwind label %24

21:                                               ; preds = %19
  %22 = invoke noundef nonnull align 8 dereferenceable(8) ptr @_ZN4dali10CUDAStreamaSEOS0_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull align 8 dereferenceable(8) %8)
          to label %23 unwind label %28

23:                                               ; preds = %21
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %33

24:                                               ; preds = %19
  %25 = landingpad { ptr, i32 }
          cleanup
  %26 = extractvalue { ptr, i32 } %25, 0
  store ptr %26, ptr %9, align 8
  %27 = extractvalue { ptr, i32 } %25, 1
  store i32 %27, ptr %10, align 4
  br label %32

28:                                               ; preds = %21
  %29 = landingpad { ptr, i32 }
          cleanup
  %30 = extractvalue { ptr, i32 } %29, 0
  store ptr %30, ptr %9, align 8
  %31 = extractvalue { ptr, i32 } %29, 1
  store i32 %31, ptr %10, align 4
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %8) #6
  br label %32

32:                                               ; preds = %28, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  br label %40

33:                                               ; preds = %23, %16
  %34 = load i32, ptr %6, align 4, !tbaa !9
  invoke void @_ZN4dali15CUDAStreamLeaseC2EONS_10CUDAStreamEiPNS_14CUDAStreamPoolE(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(8) %7, i32 noundef %34, ptr noundef %11)
          to label %35 unwind label %36

35:                                               ; preds = %33
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  ret void

36:                                               ; preds = %33
  %37 = landingpad { ptr, i32 }
          cleanup
  %38 = extractvalue { ptr, i32 } %37, 0
  store ptr %38, ptr %9, align 8
  %39 = extractvalue { ptr, i32 } %37, 1
  store i32 %39, ptr %10, align 4
  br label %40

40:                                               ; preds = %36, %32
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  br label %41

41:                                               ; preds = %40
  %42 = load ptr, ptr %9, align 8
  %43 = load i32, ptr %10, align 4
  %44 = insertvalue { ptr, i32 } poison, ptr %42, 0
  %45 = insertvalue { ptr, i32 } %44, i32 %43, 1
  resume { ptr, i32 } %45
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z9CUDA_CALLI9cudaErrorEvT_(i32 noundef) #1

declare i32 @cudaGetDevice(ptr noundef) #2

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4dali14CUDAStreamPool11GetFromPoolEi(ptr dead_on_unwind noalias writable sret(%"class.dali::CUDAStream") align 8, ptr noundef nonnull align 8 dereferenceable(41), i32 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEEcvbEv(ptr noundef nonnull align 8 dereferenceable(8)) #4 align 2

declare void @_ZN4dali10CUDAStream6CreateEbi(ptr dead_on_unwind writable sret(%"class.dali::CUDAStream") align 8, i1 noundef zeroext, i32 noundef) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZN4dali10CUDAStreamaSEOS0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #1 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #5 align 2

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4dali15CUDAStreamLeaseC2EONS_10CUDAStreamEiPNS_14CUDAStreamPoolE(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(8), i32 noundef, ptr noundef) unnamed_addr #0 align 2

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 = { inlinehint 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(argmem: readwrite) }
attributes #4 = { 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 #5 = { inlinehint 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 #6 = { 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}


; 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"

%"class.dali::CUDAStreamLease" = type { %"class.dali::CUDAStream", i32, ptr }
%"class.dali::CUDAStream" = type { %"class.dali::UniqueHandle" }
%"class.dali::UniqueHandle" = type { ptr }

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4dali14CUDAStreamPool3GetEi(ptr dead_on_unwind noalias writable sret(%"class.dali::CUDAStreamLease") align 8 %0, ptr noundef nonnull align 8 dereferenceable(41) %1, i32 noundef %2) local_unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca i32, align 4
  %5 = alloca %"class.dali::CUDAStream", align 8
  %6 = alloca %"class.dali::CUDAStream", align 8
  store i32 %2, ptr %4, align 4, !tbaa !5
  %7 = icmp slt i32 %2, 0
  br i1 %7, label %8, label %10

8:                                                ; preds = %3
  %9 = call i32 @cudaGetDevice(ptr noundef nonnull %4)
  call void @_Z9CUDA_CALLI9cudaErrorEvT_(i32 noundef %9)
  %.pre = load i32, ptr %4, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %8, %3
  %11 = phi i32 [ %.pre, %8 ], [ %2, %3 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #6
  call void @_ZN4dali14CUDAStreamPool11GetFromPoolEi(ptr dead_on_unwind nonnull writable sret(%"class.dali::CUDAStream") align 8 %5, ptr noundef nonnull align 8 dereferenceable(41) %1, i32 noundef %11)
  %12 = call noundef zeroext i1 @_ZNK4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEEcvbEv(ptr noundef nonnull align 8 dereferenceable(8) %5) #6
  br i1 %12, label %23, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #6
  %14 = load i32, ptr %4, align 4, !tbaa !5
  invoke void @_ZN4dali10CUDAStream6CreateEbi(ptr dead_on_unwind nonnull writable sret(%"class.dali::CUDAStream") align 8 %6, i1 noundef zeroext true, i32 noundef %14)
          to label %15 unwind label %18

15:                                               ; preds = %13
  %16 = invoke noundef nonnull align 8 dereferenceable(8) ptr @_ZN4dali10CUDAStreamaSEOS0_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %6)
          to label %17 unwind label %20

17:                                               ; preds = %15
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #6
  br label %23

18:                                               ; preds = %13
  %19 = landingpad { ptr, i32 }
          cleanup
  br label %22

20:                                               ; preds = %15
  %21 = landingpad { ptr, i32 }
          cleanup
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %6) #6
  br label %22

22:                                               ; preds = %20, %18
  %.pn = phi { ptr, i32 } [ %21, %20 ], [ %19, %18 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #6
  br label %28

23:                                               ; preds = %17, %10
  %24 = load i32, ptr %4, align 4, !tbaa !5
  invoke void @_ZN4dali15CUDAStreamLeaseC2EONS_10CUDAStreamEiPNS_14CUDAStreamPoolE(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 8 dereferenceable(8) %5, i32 noundef %24, ptr noundef nonnull %1)
          to label %25 unwind label %26

25:                                               ; preds = %23
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #6
  ret void

26:                                               ; preds = %23
  %27 = landingpad { ptr, i32 }
          cleanup
  br label %28

28:                                               ; preds = %26, %22
  %.pn5 = phi { ptr, i32 } [ %27, %26 ], [ %.pn, %22 ]
  call void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8) %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #6
  resume { ptr, i32 } %.pn5
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z9CUDA_CALLI9cudaErrorEvT_(i32 noundef) local_unnamed_addr #1

declare i32 @cudaGetDevice(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4dali14CUDAStreamPool11GetFromPoolEi(ptr dead_on_unwind noalias writable sret(%"class.dali::CUDAStream") align 8, ptr noundef nonnull align 8 dereferenceable(41), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEEcvbEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #4 align 2

declare void @_ZN4dali10CUDAStream6CreateEbi(ptr dead_on_unwind writable sret(%"class.dali::CUDAStream") align 8, i1 noundef zeroext, i32 noundef) local_unnamed_addr #2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZN4dali12UniqueHandleIP11CUstream_stNS_10CUDAStreamEED2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #5 align 2

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4dali15CUDAStreamLeaseC2EONS_10CUDAStreamEiPNS_14CUDAStreamPoolE(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 8 dereferenceable(8), i32 noundef, ptr noundef) unnamed_addr #0 align 2

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 = { inlinehint 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { inlinehint 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 #6 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass