; LLVM IR from AdaptiveCpp/AdaptiveCpp :: src/runtime/pcuda (pcuda_runtime_api)

; 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.hipsycl::rt::device_id" = type { %"struct.hipsycl::rt::backend_descriptor", i32 }
%"struct.hipsycl::rt::backend_descriptor" = type { i32, i32, i32 }

; 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) #0

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

declare noundef nonnull align 8 dereferenceable(104) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application3getEv() #2

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

declare noundef i32 @_ZN7hipsycl2rt5pcuda27get_most_recent_pcuda_errorEv() #2

declare noundef nonnull align 8 dereferenceable(40) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application8pcuda_rtEv(ptr noundef nonnull align 8 dereferenceable(104)) #2

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZN7hipsycl2rt5pcuda12_GLOBAL__N_121get_current_device_idEv() #3

; Function Attrs: mustprogress uwtable
define dso_local i32 @pcudaStreamCreateWithPriority(ptr noundef %0, i32 noundef %1, i32 noundef %2) #3 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca %"class.hipsycl::rt::device_id", 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 4, ptr %8) #4
  %14 = call noundef i32 @_ZN7hipsycl2rt5pcuda27get_most_recent_pcuda_errorEv()
  store i32 %14, ptr %8, align 4, !tbaa !11
  %15 = load i32, ptr %8, align 4, !tbaa !11
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %3
  %18 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %18, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %54

19:                                               ; preds = %3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %23, label %22

22:                                               ; preds = %19
  store i32 11, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %54

23:                                               ; preds = %19
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = icmp ne i32 %27, 1
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  store i32 11, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %54

30:                                               ; preds = %26, %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %31 = call noundef ptr @_ZN7hipsycl2rt5pcuda12_GLOBAL__N_121get_current_device_idEv()
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  store i32 38, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %53

35:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %36 = call noundef nonnull align 8 dereferenceable(104) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application3getEv()
  %37 = call noundef nonnull align 8 dereferenceable(40) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application8pcuda_rtEv(ptr noundef nonnull align 8 dereferenceable(104) %36)
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %13, ptr align 4 %38, i64 16, i1 false), !tbaa.struct !13
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 0
  %42 = load i64, ptr %41, align 4
  %43 = getelementptr inbounds { i64, i64 }, ptr %13, i32 0, i32 1
  %44 = load i64, ptr %43, align 4
  %45 = call noundef i32 @_ZN7hipsycl2rt5pcuda6stream6createERPS2_PNS1_13pcuda_runtimeENS0_9device_idEji(ptr noundef nonnull align 8 dereferenceable(8) %11, ptr noundef %37, i64 %42, i64 %44, i32 noundef %39, i32 noundef %40)
  store i32 %45, ptr %12, align 4, !tbaa !11
  %46 = load i32, ptr %12, align 4, !tbaa !11
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %35
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %49, ptr %50, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %48, %35
  %52 = load i32, ptr %12, align 4, !tbaa !11
  store i32 %52, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %53

53:                                               ; preds = %51, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %54

54:                                               ; preds = %53, %29, %22, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %55 = load i32, ptr %4, align 4
  ret i32 %55
}

declare noundef i32 @_ZN7hipsycl2rt5pcuda6stream6createERPS2_PNS1_13pcuda_runtimeENS0_9device_idEji(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i64, i64, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"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, !12, i64 0}
!12 = !{!"_ZTS10pcudaError", !7, i64 0}
!13 = !{i64 0, i64 4, !14, i64 4, i64 4, !16, i64 8, i64 4, !18, i64 12, i64 4, !9}
!14 = !{!15, !15, i64 0}
!15 = !{!"_ZTSN7hipsycl2rt10backend_idE", !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"_ZTSN7hipsycl2rt17hardware_platformE", !7, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"_ZTSN7hipsycl2rt12api_platformE", !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"

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

declare noundef nonnull align 8 dereferenceable(104) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application3getEv() local_unnamed_addr #1

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

declare noundef i32 @_ZN7hipsycl2rt5pcuda27get_most_recent_pcuda_errorEv() local_unnamed_addr #1

declare noundef nonnull align 8 dereferenceable(40) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application8pcuda_rtEv(ptr noundef nonnull align 8 dereferenceable(104)) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZN7hipsycl2rt5pcuda12_GLOBAL__N_121get_current_device_idEv() local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @pcudaStreamCreateWithPriority(ptr noundef writeonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  %5 = tail call noundef i32 @_ZN7hipsycl2rt5pcuda27get_most_recent_pcuda_errorEv()
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %17

6:                                                ; preds = %3
  %.not16 = icmp eq ptr %0, null
  %or.cond = icmp ugt i32 %1, 1
  %or.cond18 = or i1 %.not16, %or.cond
  br i1 %or.cond18, label %17, label %7

7:                                                ; preds = %6
  %8 = tail call noundef ptr @_ZN7hipsycl2rt5pcuda12_GLOBAL__N_121get_current_device_idEv()
  %.not17 = icmp eq ptr %8, null
  br i1 %.not17, label %17, label %9

9:                                                ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %10 = tail call noundef nonnull align 8 dereferenceable(104) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application3getEv()
  %11 = tail call noundef nonnull align 8 dereferenceable(40) ptr @_ZN7hipsycl2rt5pcuda17pcuda_application8pcuda_rtEv(ptr noundef nonnull align 8 dereferenceable(104) %10)
  %.sroa.0.0.copyload = load i64, ptr %8, align 4
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %8, i64 8
  %.sroa.2.0.copyload = load i64, ptr %.sroa.2.0..sroa_idx, align 4
  %12 = call noundef i32 @_ZN7hipsycl2rt5pcuda6stream6createERPS2_PNS1_13pcuda_runtimeENS0_9device_idEji(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull %11, i64 %.sroa.0.0.copyload, i64 %.sroa.2.0.copyload, i32 noundef %1, i32 noundef %2)
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %16

14:                                               ; preds = %9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %15, ptr %0, align 8, !tbaa !5
  br label %16

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

17:                                               ; preds = %16, %7, %6, %3
  %.1 = phi i32 [ %5, %3 ], [ 11, %6 ], [ %12, %16 ], [ 38, %7 ]
  ret i32 %.1
}

declare noundef i32 @_ZN7hipsycl2rt5pcuda6stream6createERPS2_PNS1_13pcuda_runtimeENS0_9device_idEji(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i64, i64, i32 noundef, i32 noundef) local_unnamed_addr #1

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


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