; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/test/test_task_enqueue.ll (Z8TestMainv)

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

@NumRepeats = external dso_local global i32, align 4
@_ZL9MinThread = external hidden global i32, align 4
@_ZL9MaxThread = external hidden global i32, 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
define dso_local noundef i32 @_Z8TestMainv() #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i1, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  call void @_Z11TestWakeupsv()
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %55, %0
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 2
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  br label %58

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  store i1 false, ptr %5, align 1
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  store ptr %17, ptr %4, align 8
  store i1 true, ptr %5, align 1
  invoke void @_ZN3tbb10interface914global_controlC2ENS1_9parameterEm(ptr noundef nonnull align 8 dereferenceable(20) %17, i32 noundef 0, i64 noundef 1)
          to label %18 unwind label %25

18:                                               ; preds = %16
  br label %20

19:                                               ; preds = %13
  br label %20

20:                                               ; preds = %19, %18
  %21 = phi ptr [ %17, %18 ], [ null, %19 ]
  store ptr %21, ptr %3, align 8, !tbaa !9
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %33

24:                                               ; preds = %20
  store i32 10, ptr @NumRepeats, align 4, !tbaa !5
  br label %33

25:                                               ; preds = %16
  %26 = landingpad { ptr, i32 }
          cleanup
  %27 = extractvalue { ptr, i32 } %26, 0
  store ptr %27, ptr %6, align 8
  %28 = extractvalue { ptr, i32 } %26, 1
  store i32 %28, ptr %7, align 4
  %29 = load i1, ptr %5, align 1
  br i1 %29, label %30, label %32

30:                                               ; preds = %25
  %31 = load ptr, ptr %4, align 8
  call void @_ZdlPvm(ptr noundef %31, i64 noundef 24) #7
  br label %32

32:                                               ; preds = %30, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  br label %59

33:                                               ; preds = %24, %20
  call void @_Z19TestCascadedEnqueuev()
  %34 = load ptr, ptr %3, align 8, !tbaa !9
  %35 = icmp ne ptr %34, null
  br i1 %35, label %37, label %36

36:                                               ; preds = %33
  call void @_Z19TestDequeueByMasterv()
  br label %37

37:                                               ; preds = %36, %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %38 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  store i32 %38, ptr %8, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %47, %37
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %42 = icmp sle i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %50

44:                                               ; preds = %39
  %45 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z11TestEnqueuei(i32 noundef %45)
  %46 = load i32, ptr %8, align 4, !tbaa !5
  call void @_Z14TestSharedRooti(i32 noundef %46)
  br label %47

47:                                               ; preds = %44
  %48 = load i32, ptr %8, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %8, align 4, !tbaa !5
  br label %39, !llvm.loop !11

50:                                               ; preds = %43
  %51 = load ptr, ptr %3, align 8, !tbaa !9
  %52 = icmp eq ptr %51, null
  br i1 %52, label %54, label %53

53:                                               ; preds = %50
  call void @_ZN3tbb10interface914global_controlD2Ev(ptr noundef nonnull align 8 dereferenceable(20) %51) #5
  call void @_ZdlPvm(ptr noundef %51, i64 noundef 24) #7
  br label %54

54:                                               ; preds = %53, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %1, align 4, !tbaa !5
  br label %9, !llvm.loop !13

58:                                               ; preds = %12
  ret i32 0

59:                                               ; preds = %32
  %60 = load ptr, ptr %6, align 8
  %61 = load i32, ptr %7, align 4
  %62 = insertvalue { ptr, i32 } poison, ptr %60, 0
  %63 = insertvalue { ptr, i32 } %62, i32 %61, 1
  resume { ptr, i32 } %63
}

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

declare i32 @__gxx_personality_v0(...)

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

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

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

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb10interface914global_controlC2ENS1_9parameterEm(ptr noundef nonnull align 8 dereferenceable(20), i32 noundef, i64 noundef) unnamed_addr #1 align 2

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #3

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

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 = { nobuiltin allocsize(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 = { nobuiltin nounwind "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 = { 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 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@NumRepeats = external dso_local local_unnamed_addr global i32, align 4
@_ZL9MinThread = external hidden local_unnamed_addr global i32, align 4
@_ZL9MaxThread = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8TestMainv() local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  tail call void @_Z11TestWakeupsv()
  tail call void @_Z19TestCascadedEnqueuev()
  tail call void @_Z19TestDequeueByMasterv()
  %.pre = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  %.pre24 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not1621 = icmp sgt i32 %.pre, %.pre24
  br i1 %.not1621, label %._crit_edge, label %.lr.ph

1:                                                ; preds = %._crit_edge
  %2 = landingpad { ptr, i32 }
          cleanup
  tail call void @_ZdlPvm(ptr noundef nonnull %5, i64 noundef 24) #4
  resume { ptr, i32 } %2

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.022 = phi i32 [ %3, %.lr.ph ], [ %.pre, %0 ]
  tail call void @_Z11TestEnqueuei(i32 noundef %.022)
  tail call void @_Z14TestSharedRooti(i32 noundef %.022)
  %3 = add nsw i32 %.022, 1
  %4 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not16.not = icmp slt i32 %.022, %4
  br i1 %.not16.not, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %0
  %5 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #5
  invoke void @_ZN3tbb10interface914global_controlC2ENS1_9parameterEm(ptr noundef nonnull align 8 dereferenceable(20) %5, i32 noundef 0, i64 noundef 1)
          to label %6 unwind label %1

6:                                                ; preds = %._crit_edge
  store i32 10, ptr @NumRepeats, align 4, !tbaa !5
  tail call void @_Z19TestCascadedEnqueuev()
  %.pre25 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  %.pre26 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not1621.1 = icmp sgt i32 %.pre25, %.pre26
  br i1 %.not1621.1, label %.loopexit, label %.lr.ph.1

.lr.ph.1:                                         ; preds = %6, %.lr.ph.1
  %.022.1 = phi i32 [ %7, %.lr.ph.1 ], [ %.pre25, %6 ]
  tail call void @_Z11TestEnqueuei(i32 noundef %.022.1)
  tail call void @_Z14TestSharedRooti(i32 noundef %.022.1)
  %7 = add nsw i32 %.022.1, 1
  %8 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not16.not.1 = icmp slt i32 %.022.1, %8
  br i1 %.not16.not.1, label %.lr.ph.1, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph.1, %6
  tail call void @_ZN3tbb10interface914global_controlD2Ev(ptr noundef nonnull align 8 dereferenceable(20) %5) #6
  tail call void @_ZdlPvm(ptr noundef nonnull %5, i64 noundef 24) #4
  ret i32 0
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11TestEnqueuei(i32 noundef) local_unnamed_addr #0

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z14TestSharedRooti(i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #1

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

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb10interface914global_controlD2Ev(ptr noundef nonnull align 8 dereferenceable(20)) unnamed_addr #3 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 = { nobuiltin allocsize(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 = { nobuiltin nounwind "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 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 = { builtin nounwind }
attributes #5 = { builtin allocsize(0) }
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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass