; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/rml/client/rml_tbb.ll (ZN3tbb8internal3rml11tbb_factory4openEv)

; 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.tbb::internal::dynamic_link_descriptor" = type { ptr, ptr, ptr }
%"class.rml::factory" = type { ptr, ptr, ptr }
%"class.tbb::internal::rml::tbb_factory" = type { %"class.rml::factory", ptr, ptr }

@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN3tbb8internal3rml11tbb_factory4openEv(ptr noundef nonnull align 8 dereferenceable(40) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca [4 x %"struct.tbb::internal::dynamic_link_descriptor"], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  %8 = getelementptr inbounds %"class.rml::factory", ptr %7, i32 0, i32 2
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  %11 = xor i1 %10, true
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 96, ptr %4) #3
  %12 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i32 0, i32 0
  store ptr @.str, ptr %12, align 8, !tbaa !11
  %13 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i32 0, i32 1
  store ptr %3, ptr %13, align 8, !tbaa !13
  %14 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i32 0, i32 2
  store ptr @__RML_open_factory, ptr %14, align 8, !tbaa !14
  %15 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i64 1
  %16 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %15, i32 0, i32 0
  store ptr @.str.1, ptr %16, align 8, !tbaa !11
  %17 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %15, i32 0, i32 1
  %18 = getelementptr inbounds %"class.tbb::internal::rml::tbb_factory", ptr %7, i32 0, i32 1
  store ptr %18, ptr %17, align 8, !tbaa !13
  %19 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %15, i32 0, i32 2
  store ptr @__TBB_make_rml_server, ptr %19, align 8, !tbaa !14
  %20 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i64 2
  %21 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %20, i32 0, i32 0
  store ptr @.str.2, ptr %21, align 8, !tbaa !11
  %22 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %20, i32 0, i32 1
  %23 = getelementptr inbounds %"class.rml::factory", ptr %7, i32 0, i32 1
  store ptr %23, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %20, i32 0, i32 2
  store ptr @__RML_close_factory, ptr %24, align 8, !tbaa !14
  %25 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %4, i64 3
  %26 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %25, i32 0, i32 0
  store ptr @.str.3, ptr %26, align 8, !tbaa !11
  %27 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %25, i32 0, i32 1
  %28 = getelementptr inbounds %"class.tbb::internal::rml::tbb_factory", ptr %7, i32 0, i32 2
  store ptr %28, ptr %27, align 8, !tbaa !13
  %29 = getelementptr inbounds %"struct.tbb::internal::dynamic_link_descriptor", ptr %25, i32 0, i32 2
  store ptr @__TBB_call_with_my_server_info, ptr %29, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %30 = getelementptr inbounds [4 x %"struct.tbb::internal::dynamic_link_descriptor"], ptr %4, i64 0, i64 0
  %31 = getelementptr inbounds %"class.rml::factory", ptr %7, i32 0, i32 2
  %32 = call noundef zeroext i1 @_ZN3tbb8internal12dynamic_linkEPKcPKNS0_23dynamic_link_descriptorEmPPvi(ptr noundef @.str.4, ptr noundef %30, i64 noundef 4, ptr noundef %31, i32 noundef 7)
  br i1 %32, label %33, label %36

33:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = call noundef i32 %34(ptr noundef nonnull align 8 dereferenceable(24) %7, ptr noundef nonnull align 4 dereferenceable(4) %6, i32 noundef 2)
  store i32 %35, ptr %5, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %38

36:                                               ; preds = %1
  %37 = getelementptr inbounds %"class.rml::factory", ptr %7, i32 0, i32 2
  store ptr null, ptr %37, align 8, !tbaa !9
  store i32 2, ptr %5, align 4, !tbaa !15
  br label %38

38:                                               ; preds = %36, %33
  %39 = load i32, ptr %5, align 4, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 96, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i32 %39
}

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

declare extern_weak i32 @__RML_open_factory(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #2

declare extern_weak i32 @__TBB_make_rml_server(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

declare extern_weak void @__RML_close_factory(ptr noundef nonnull align 8 dereferenceable(24)) #2

declare extern_weak void @__TBB_call_with_my_server_info(ptr noundef, ptr noundef) #2

declare noundef zeroext i1 @_ZN3tbb8internal12dynamic_linkEPKcPKNS0_23dynamic_link_descriptorEmPPvi(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i32 noundef) #2

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

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 = { 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 = { 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, !6, i64 16}
!10 = !{!"_ZTSN3rml7factoryE", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSN3tbb8internal23dynamic_link_descriptorE", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = !{!16, !16, i64 0}
!16 = !{!"_ZTSN3rml7factory11status_typeE", !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"

%"struct.tbb::internal::dynamic_link_descriptor" = type { ptr, ptr, ptr }

@.str = external hidden unnamed_addr constant [19 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN3tbb8internal3rml11tbb_factory4openEv(ptr noundef nonnull align 8 dereferenceable(40) %0) local_unnamed_addr #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca [4 x %"struct.tbb::internal::dynamic_link_descriptor"], align 16
  %4 = alloca i32, align 4
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %3) #3
  store ptr @.str, ptr %3, align 16, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %3, i64 8
  store ptr %2, ptr %6, align 8, !tbaa !10
  %7 = getelementptr inbounds i8, ptr %3, i64 16
  store ptr @__RML_open_factory, ptr %7, align 16, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %3, i64 24
  store ptr @.str.1, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %3, i64 32
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %10, ptr %9, align 16, !tbaa !10
  %11 = getelementptr inbounds i8, ptr %3, i64 40
  store ptr @__TBB_make_rml_server, ptr %11, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %3, i64 48
  store ptr @.str.2, ptr %12, align 16, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %3, i64 56
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %14, ptr %13, align 8, !tbaa !10
  %15 = getelementptr inbounds i8, ptr %3, i64 64
  store ptr @__RML_close_factory, ptr %15, align 16, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %3, i64 72
  store ptr @.str.3, ptr %16, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %3, i64 80
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %18, ptr %17, align 16, !tbaa !10
  %19 = getelementptr inbounds i8, ptr %3, i64 88
  store ptr @__TBB_call_with_my_server_info, ptr %19, align 8, !tbaa !11
  %20 = call noundef zeroext i1 @_ZN3tbb8internal12dynamic_linkEPKcPKNS0_23dynamic_link_descriptorEmPPvi(ptr noundef nonnull @.str.4, ptr noundef nonnull %3, i64 noundef 4, ptr noundef nonnull %5, i32 noundef 7)
  br i1 %20, label %21, label %24

21:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %22 = load ptr, ptr %2, align 8, !tbaa !12
  %23 = call noundef i32 %22(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef nonnull align 4 dereferenceable(4) %4, i32 noundef 2)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %25

24:                                               ; preds = %1
  store ptr null, ptr %5, align 8, !tbaa !13
  br label %25

25:                                               ; preds = %24, %21
  %.0 = phi i32 [ %23, %21 ], [ 2, %24 ]
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret i32 %.0
}

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

declare extern_weak i32 @__RML_open_factory(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #2

declare extern_weak i32 @__TBB_make_rml_server(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

declare extern_weak void @__RML_close_factory(ptr noundef nonnull align 8 dereferenceable(24)) #2

declare extern_weak void @__TBB_call_with_my_server_info(ptr noundef, ptr noundef) #2

declare noundef zeroext i1 @_ZN3tbb8internal12dynamic_linkEPKcPKNS0_23dynamic_link_descriptorEmPPvi(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

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 = { mustprogress 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 = { 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 = !{!"_ZTSN3tbb8internal23dynamic_link_descriptorE", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 16}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !7, i64 16}
!14 = !{!"_ZTSN3rml7factoryE", !7, i64 0, !7, i64 8, !7, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,SROAPass