; LLVM IR from flashlight/flashlight :: flashlight/fl/runtime/CUDAStream.ll (ZN2fl10CUDAStream13wrapUnmanagedEiP11CUstream_st)

; 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.std::shared_ptr" = type { %"class.std::__shared_ptr" }
%"class.std::__shared_ptr" = type { ptr, %"class.std::__shared_count" }
%"class.std::__shared_count" = type { ptr }

declare noundef nonnull align 8 dereferenceable(56) ptr @_ZN2fl13DeviceManager11getInstanceEv() #0

declare i32 @__gxx_personality_v0(...)

declare noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager15getActiveDeviceENS_10DeviceTypeE(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2fl10CUDAStream21makeSharedAndRegisterERNS_10CUDADeviceEP11CUstream_stb(ptr dead_on_unwind noalias writable sret(%"class.std::shared_ptr") align 8, ptr noundef nonnull align 8 dereferenceable(92), ptr noundef, i1 noundef zeroext) #1 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt12__shared_ptrIN2fl10CUDAStreamELN9__gnu_cxx12_Lock_policyE2EED2Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #3 align 2

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef nonnull align 8 dereferenceable(92) ptr @_ZN2fl6Device4implINS_10CUDADeviceEEERT_v(ptr noundef nonnull align 8 dereferenceable(88)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2fl10CUDAStream13wrapUnmanagedEiP11CUstream_st(ptr dead_on_unwind noalias writable sret(%"class.std::shared_ptr") align 8 %0, i32 noundef %1, ptr noundef %2) #1 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i8, align 1
  %11 = alloca i1, align 1
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8
  store i32 %1, ptr %5, align 4, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %14 = call noundef nonnull align 8 dereferenceable(56) ptr @_ZN2fl13DeviceManager11getInstanceEv()
  store ptr %14, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %15 = load ptr, ptr %7, align 8, !tbaa !9
  %16 = call noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager15getActiveDeviceENS_10DeviceTypeE(ptr noundef nonnull align 8 dereferenceable(56) %15, i32 noundef 1)
  store ptr %16, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %17 = load ptr, ptr %7, align 8, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = call noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager9getDeviceENS_10DeviceTypeEi(ptr noundef nonnull align 8 dereferenceable(56) %17, i32 noundef 1, i32 noundef %18)
  %20 = call noundef nonnull align 8 dereferenceable(92) ptr @_ZN2fl6Device4implINS_10CUDADeviceEEERT_v(ptr noundef nonnull align 8 dereferenceable(88) %19)
  store ptr %20, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #4
  %21 = load ptr, ptr %8, align 8, !tbaa !9
  %22 = load ptr, ptr %9, align 8, !tbaa !9
  %23 = icmp ne ptr %21, %22
  %24 = zext i1 %23 to i8
  store i8 %24, ptr %10, align 1, !tbaa !11
  %25 = load i8, ptr %10, align 1, !tbaa !11, !range !13, !noundef !14
  %26 = trunc i8 %25 to i1
  br i1 %26, label %27, label %29

27:                                               ; preds = %3
  %28 = load ptr, ptr %9, align 8, !tbaa !9
  call void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88) %28)
  br label %29

29:                                               ; preds = %27, %3
  store i1 false, ptr %11, align 1
  %30 = load ptr, ptr %9, align 8, !tbaa !9
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  call void @_ZN2fl10CUDAStream21makeSharedAndRegisterERNS_10CUDADeviceEP11CUstream_stb(ptr dead_on_unwind writable sret(%"class.std::shared_ptr") align 8 %0, ptr noundef nonnull align 8 dereferenceable(92) %30, ptr noundef %31, i1 noundef zeroext false)
  %32 = load i8, ptr %10, align 1, !tbaa !11, !range !13, !noundef !14
  %33 = trunc i8 %32 to i1
  br i1 %33, label %34, label %41

34:                                               ; preds = %29
  %35 = load ptr, ptr %8, align 8, !tbaa !9
  invoke void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88) %35)
          to label %36 unwind label %37

36:                                               ; preds = %34
  br label %41

37:                                               ; preds = %34
  %38 = landingpad { ptr, i32 }
          cleanup
  %39 = extractvalue { ptr, i32 } %38, 0
  store ptr %39, ptr %12, align 8
  %40 = extractvalue { ptr, i32 } %38, 1
  store i32 %40, ptr %13, align 4
  call void @_ZNSt12__shared_ptrIN2fl10CUDAStreamELN9__gnu_cxx12_Lock_policyE2EED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %0) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %45

41:                                               ; preds = %36, %29
  store i1 true, ptr %11, align 1
  %42 = load i1, ptr %11, align 1
  br i1 %42, label %44, label %43

43:                                               ; preds = %41
  call void @_ZNSt12__shared_ptrIN2fl10CUDAStreamELN9__gnu_cxx12_Lock_policyE2EED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %0) #4
  br label %44

44:                                               ; preds = %43, %41
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void

45:                                               ; preds = %37
  %46 = load ptr, ptr %12, align 8
  %47 = load i32, ptr %13, align 4
  %48 = insertvalue { ptr, i32 } poison, ptr %46, 0
  %49 = insertvalue { ptr, i32 } %48, i32 %47, 1
  resume { ptr, i32 } %49
}

declare noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager9getDeviceENS_10DeviceTypeEi(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef, i32 noundef) #0

declare void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88)) #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 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}


; 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.std::shared_ptr" = type { %"class.std::__shared_ptr" }
%"class.std::__shared_ptr" = type { ptr, %"class.std::__shared_count" }
%"class.std::__shared_count" = type { ptr }

declare noundef nonnull align 8 dereferenceable(56) ptr @_ZN2fl13DeviceManager11getInstanceEv() local_unnamed_addr #0

declare i32 @__gxx_personality_v0(...)

declare noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager15getActiveDeviceENS_10DeviceTypeE(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2fl10CUDAStream21makeSharedAndRegisterERNS_10CUDADeviceEP11CUstream_stb(ptr dead_on_unwind noalias writable sret(%"class.std::shared_ptr") align 8, ptr noundef nonnull align 8 dereferenceable(92), ptr noundef, i1 noundef zeroext) local_unnamed_addr #1 align 2

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef nonnull align 8 dereferenceable(92) ptr @_ZN2fl6Device4implINS_10CUDADeviceEEERT_v(ptr noundef nonnull align 8 dereferenceable(88)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2fl10CUDAStream13wrapUnmanagedEiP11CUstream_st(ptr dead_on_unwind noalias writable sret(%"class.std::shared_ptr") align 8 %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #1 align 2 personality ptr @__gxx_personality_v0 {
  %4 = tail call noundef nonnull align 8 dereferenceable(56) ptr @_ZN2fl13DeviceManager11getInstanceEv()
  %5 = tail call noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager15getActiveDeviceENS_10DeviceTypeE(ptr noundef nonnull align 8 dereferenceable(56) %4, i32 noundef 1)
  %6 = tail call noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager9getDeviceENS_10DeviceTypeEi(ptr noundef nonnull align 8 dereferenceable(56) %4, i32 noundef 1, i32 noundef %1)
  %7 = tail call noundef nonnull align 8 dereferenceable(92) ptr @_ZN2fl6Device4implINS_10CUDADeviceEEERT_v(ptr noundef nonnull align 8 dereferenceable(88) %6)
  %.not = icmp eq ptr %5, %7
  br i1 %.not, label %.critedge, label %8

8:                                                ; preds = %3
  tail call void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88) %7)
  tail call void @_ZN2fl10CUDAStream21makeSharedAndRegisterERNS_10CUDADeviceEP11CUstream_stb(ptr dead_on_unwind writable sret(%"class.std::shared_ptr") align 8 %0, ptr noundef nonnull align 8 dereferenceable(92) %7, ptr noundef %2, i1 noundef zeroext false)
  invoke void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88) %5)
          to label %11 unwind label %9

9:                                                ; preds = %8
  %10 = landingpad { ptr, i32 }
          cleanup
  tail call void @_ZNSt12__shared_ptrIN2fl10CUDAStreamELN9__gnu_cxx12_Lock_policyE2EED2Ev(ptr noundef nonnull align 8 dereferenceable(16) %0) #3
  resume { ptr, i32 } %10

.critedge:                                        ; preds = %3
  tail call void @_ZN2fl10CUDAStream21makeSharedAndRegisterERNS_10CUDADeviceEP11CUstream_stb(ptr dead_on_unwind writable sret(%"class.std::shared_ptr") align 8 %0, ptr noundef nonnull align 8 dereferenceable(92) %7, ptr noundef %2, i1 noundef zeroext false)
  br label %11

11:                                               ; preds = %8, %.critedge
  ret void
}

declare noundef nonnull align 8 dereferenceable(88) ptr @_ZNK2fl13DeviceManager9getDeviceENS_10DeviceTypeEi(ptr noundef nonnull align 8 dereferenceable(56), i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @_ZNK2fl6Device9setActiveEv(ptr noundef nonnull align 8 dereferenceable(88)) 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 = { 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 = { 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 #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)"}


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