; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/rml/test/test_rml_mixed.ll (Z22TBBOutSideOpenMPInsidev)

; 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.RunTime = type { %"class.tbb::internal::rml::tbb_factory", ptr, ptr }
%"class.tbb::internal::rml::tbb_factory" = type { %"class.rml::factory", ptr, ptr }
%"class.rml::factory" = type { ptr, ptr, ptr }
%class.ThreadLevelRecorder = type { %"struct.tbb::atomic", %"struct.tbb::atomic.0", [1048576 x %"struct.ThreadLevelRecorder::record"] }
%"struct.tbb::atomic" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic" }
%"struct.tbb::internal::atomic_impl_with_arithmetic" = type { %"struct.tbb::internal::atomic_impl" }
%"struct.tbb::internal::atomic_impl" = type { %"struct.tbb::internal::aligned_storage" }
%"struct.tbb::internal::aligned_storage" = type { i32 }
%"struct.tbb::atomic.0" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic.1" }
%"struct.tbb::internal::atomic_impl_with_arithmetic.1" = type { %"struct.tbb::internal::atomic_impl.2" }
%"struct.tbb::internal::atomic_impl.2" = type { %"struct.tbb::internal::aligned_storage.3" }
%"struct.tbb::internal::aligned_storage.3" = type { i32 }
%"struct.ThreadLevelRecorder::record" = type <{ %"class.tbb::tick_count", i32, [4 x i8] }>
%"class.tbb::tick_count" = type { i64 }

@TBB_MaxThread = external dso_local global i32, align 4
@TBB_RunTime = external dso_local global %class.RunTime, align 8
@TotalThreadLevel = external dso_local global %class.ThreadLevelRecorder, align 8
@AvailWork = external dso_local global %"struct.tbb::atomic", align 4
@CompletionCount = external dso_local global %"struct.tbb::atomic", align 4

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7TBBWorkv() #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z22TBBOutSideOpenMPInsidev() #1 {
  %1 = alloca i32, align 4
  call void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224) @TotalThreadLevel, i32 noundef 1)
  %2 = call noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4) @CompletionCount, i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 32, ptr %1, align 4, !tbaa !5
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = call noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4) @AvailWork, i32 noundef %3)
  %5 = load ptr, ptr getelementptr inbounds (%class.RunTime, ptr @TBB_RunTime, i32 0, i32 2), align 8, !tbaa !9
  %6 = load i32, ptr @TBB_MaxThread, align 4, !tbaa !5
  %7 = sub nsw i32 %6, 1
  %8 = load ptr, ptr %5, align 8, !tbaa !14
  %9 = getelementptr inbounds ptr, ptr %8, i64 5
  %10 = load ptr, ptr %9, align 8
  call void %10(ptr noundef nonnull align 8 dereferenceable(8) %5, i32 noundef %7)
  br label %11

11:                                               ; preds = %16, %0
  %12 = call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) @CompletionCount)
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  %15 = icmp ne i32 %12, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %11
  call void @_Z7TBBWorkv()
  br label %11, !llvm.loop !16

17:                                               ; preds = %11
  call void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224) @TotalThreadLevel, i32 noundef -1)
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !13, i64 48}
!10 = !{!"_ZTS7RunTimeIN3tbb8internal3rml11tbb_factoryE10TBB_ClientE", !11, i64 0, !13, i64 40, !13, i64 48}
!11 = !{!"_ZTSN3tbb8internal3rml11tbb_factoryE", !12, i64 0, !13, i64 24, !13, i64 32}
!12 = !{!"_ZTSN3rml7factoryE", !13, i64 0, !13, i64 8, !13, i64 16}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"vtable pointer", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; 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.RunTime = type { %"class.tbb::internal::rml::tbb_factory", ptr, ptr }
%"class.tbb::internal::rml::tbb_factory" = type { %"class.rml::factory", ptr, ptr }
%"class.rml::factory" = type { ptr, ptr, ptr }
%class.ThreadLevelRecorder = type { %"struct.tbb::atomic", %"struct.tbb::atomic.0", [1048576 x %"struct.ThreadLevelRecorder::record"] }
%"struct.tbb::atomic" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic" }
%"struct.tbb::internal::atomic_impl_with_arithmetic" = type { %"struct.tbb::internal::atomic_impl" }
%"struct.tbb::internal::atomic_impl" = type { %"struct.tbb::internal::aligned_storage" }
%"struct.tbb::internal::aligned_storage" = type { i32 }
%"struct.tbb::atomic.0" = type { %"struct.tbb::internal::atomic_impl_with_arithmetic.1" }
%"struct.tbb::internal::atomic_impl_with_arithmetic.1" = type { %"struct.tbb::internal::atomic_impl.2" }
%"struct.tbb::internal::atomic_impl.2" = type { %"struct.tbb::internal::aligned_storage.3" }
%"struct.tbb::internal::aligned_storage.3" = type { i32 }
%"struct.ThreadLevelRecorder::record" = type <{ %"class.tbb::tick_count", i32, [4 x i8] }>
%"class.tbb::tick_count" = type { i64 }

@TBB_MaxThread = external dso_local local_unnamed_addr global i32, align 4
@TBB_RunTime = external dso_local local_unnamed_addr global %class.RunTime, align 8
@TotalThreadLevel = external dso_local global %class.ThreadLevelRecorder, align 8
@AvailWork = external dso_local global %"struct.tbb::atomic", align 4
@CompletionCount = external dso_local global %"struct.tbb::atomic", align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z7TBBWorkv() local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z22TBBOutSideOpenMPInsidev() local_unnamed_addr #0 {
  tail call void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224) @TotalThreadLevel, i32 noundef 1)
  %1 = tail call noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4) @CompletionCount, i32 noundef 0)
  %2 = tail call noundef i32 @_ZN3tbb6atomicIiEaSEi(ptr noundef nonnull align 4 dereferenceable(4) @AvailWork, i32 noundef 32)
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @TBB_RunTime, i64 48), align 8, !tbaa !5
  %4 = load i32, ptr @TBB_MaxThread, align 4, !tbaa !12
  %5 = add nsw i32 %4, -1
  %6 = load ptr, ptr %3, align 8, !tbaa !14
  %7 = getelementptr inbounds i8, ptr %6, i64 40
  %8 = load ptr, ptr %7, align 8
  tail call void %8(ptr noundef nonnull align 8 dereferenceable(8) %3, i32 noundef %5)
  %9 = tail call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) @CompletionCount)
  %.not2 = icmp eq i32 %9, 33
  br i1 %.not2, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  tail call void @_Z7TBBWorkv()
  %10 = tail call noundef i32 @_ZNVK3tbb8internal11atomic_implIiEcviEv(ptr noundef nonnull align 4 dereferenceable(4) @CompletionCount)
  %.not = icmp eq i32 %10, 33
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %0
  tail call void @_ZN19ThreadLevelRecorder12change_levelEi(ptr noundef nonnull align 8 dereferenceable(16777224) @TotalThreadLevel, i32 noundef -1)
  ret void
}

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

!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, !9, i64 48}
!6 = !{!"_ZTS7RunTimeIN3tbb8internal3rml11tbb_factoryE10TBB_ClientE", !7, i64 0, !9, i64 40, !9, i64 48}
!7 = !{!"_ZTSN3tbb8internal3rml11tbb_factoryE", !8, i64 0, !9, i64 24, !9, i64 32}
!8 = !{!"_ZTSN3rml7factoryE", !9, i64 0, !9, i64 8, !9, i64 16}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"vtable pointer", !11, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass