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

; 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.tbb::internal::allocate_root_proxy" = type { i8 }

; 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 nounwind uwtable
declare dso_local void @_ZN3tbb4task13allocate_rootEv() #1 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZnwmRKN3tbb8internal19allocate_root_proxyE(i64 noundef, ptr noundef nonnull align 1 dereferenceable(1)) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZdlPvRKN3tbb8internal19allocate_root_proxyE(ptr noundef, ptr noundef nonnull align 1 dereferenceable(1)) #3

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN3tbb10interface58internal9task_base5spawnERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8)) #2 align 2

; Function Attrs: nounwind
declare i32 @sched_yield() #4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb4task12wait_for_allEv(ptr noundef nonnull align 8 dereferenceable(8)) #5 align 2

declare void @_ZN3tbb10interface58internal9task_base7destroyERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8)) #6

; Function Attrs: mustprogress uwtable
define dso_local void @_Z16TestWaitableTaskv() #5 personality ptr @__gxx_personality_v0 {
  %1 = alloca ptr, align 8
  %2 = alloca %"class.tbb::internal::allocate_root_proxy", align 1
  %3 = alloca %"class.tbb::internal::allocate_root_proxy", align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #7
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #7
  call void @_ZN3tbb4task13allocate_rootEv()
  %7 = call noundef ptr @_ZnwmRKN3tbb8internal19allocate_root_proxyE(i64 noundef 8, ptr noundef nonnull align 1 dereferenceable(1) %2)
  invoke void @_ZN13waitable_taskC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %7)
          to label %8 unwind label %13

8:                                                ; preds = %0
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #7
  store ptr %7, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #7
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %28, %8
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 100000
  br i1 %11, label %17, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #7
  br label %31

13:                                               ; preds = %0
  %14 = landingpad { ptr, i32 }
          cleanup
  %15 = extractvalue { ptr, i32 } %14, 0
  store ptr %15, ptr %4, align 8
  %16 = extractvalue { ptr, i32 } %14, 1
  store i32 %16, ptr %5, align 4
  call void @_ZdlPvRKN3tbb8internal19allocate_root_proxyE(ptr noundef %7, ptr noundef nonnull align 1 dereferenceable(1) %2) #7
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #7
  br label %34

17:                                               ; preds = %9
  %18 = load ptr, ptr %1, align 8, !tbaa !5
  call void @_ZN3tbb4task13set_ref_countEi(ptr noundef nonnull align 8 dereferenceable(8) %18, i32 noundef 2)
  %19 = load ptr, ptr %1, align 8, !tbaa !5
  %20 = load ptr, ptr %1, align 8, !tbaa !5
  call void @_ZN3tbb10interface58internal9task_base5spawnERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8) %20)
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = and i32 %21, 1
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %17
  %25 = call i32 @sched_yield() #7
  br label %26

26:                                               ; preds = %24, %17
  %27 = load ptr, ptr %1, align 8, !tbaa !5
  call void @_ZN3tbb4task12wait_for_allEv(ptr noundef nonnull align 8 dereferenceable(8) %27)
  br label %28

28:                                               ; preds = %26
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %6, align 4, !tbaa !9
  br label %9, !llvm.loop !11

31:                                               ; preds = %12
  %32 = load ptr, ptr %1, align 8, !tbaa !5
  call void @_ZN3tbb4task10set_parentEPS0_(ptr noundef nonnull align 8 dereferenceable(8) %32, ptr noundef null)
  %33 = load ptr, ptr %1, align 8, !tbaa !5
  call void @_ZN3tbb10interface58internal9task_base7destroyERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8) %33)
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #7
  ret void

34:                                               ; preds = %13
  %35 = load ptr, ptr %4, align 8
  %36 = load i32, ptr %5, align 4
  %37 = insertvalue { ptr, i32 } poison, ptr %35, 0
  %38 = insertvalue { ptr, i32 } %37, i32 %36, 1
  resume { ptr, i32 } %38
}

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 = { inlinehint 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 "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 = { 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 #6 = { "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 #7 = { 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 = distinct !{!11, !12}
!12 = !{!"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.tbb::internal::allocate_root_proxy" = type { i8 }

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb4task13allocate_rootEv() local_unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZnwmRKN3tbb8internal19allocate_root_proxyE(i64 noundef, ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZdlPvRKN3tbb8internal19allocate_root_proxyE(ptr noundef, ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #3

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN3tbb10interface58internal9task_base5spawnERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: nounwind
declare i32 @sched_yield() local_unnamed_addr #4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN3tbb4task12wait_for_allEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #5 align 2

declare void @_ZN3tbb10interface58internal9task_base7destroyERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #6

; Function Attrs: mustprogress uwtable
define dso_local void @_Z16TestWaitableTaskv() local_unnamed_addr #5 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.tbb::internal::allocate_root_proxy", align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %1) #7
  tail call void @_ZN3tbb4task13allocate_rootEv()
  %2 = call noundef ptr @_ZnwmRKN3tbb8internal19allocate_root_proxyE(i64 noundef 8, ptr noundef nonnull align 1 dereferenceable(1) %1)
  invoke void @_ZN13waitable_taskC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %2)
          to label %3 unwind label %5

3:                                                ; preds = %0
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %1) #7
  br label %7

4:                                                ; preds = %11
  call void @_ZN3tbb4task10set_parentEPS0_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef null)
  call void @_ZN3tbb10interface58internal9task_base7destroyERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8) %2)
  ret void

5:                                                ; preds = %0
  %6 = landingpad { ptr, i32 }
          cleanup
  call void @_ZdlPvRKN3tbb8internal19allocate_root_proxyE(ptr noundef %2, ptr noundef nonnull align 1 dereferenceable(1) %1) #7
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %1) #7
  resume { ptr, i32 } %6

7:                                                ; preds = %3, %11
  %.010 = phi i32 [ 0, %3 ], [ %12, %11 ]
  call void @_ZN3tbb4task13set_ref_countEi(ptr noundef nonnull align 8 dereferenceable(8) %2, i32 noundef 2)
  call void @_ZN3tbb10interface58internal9task_base5spawnERNS_4taskE(ptr noundef nonnull align 8 dereferenceable(8) %2)
  %8 = and i32 %.010, 1
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %11, label %9

9:                                                ; preds = %7
  %10 = call i32 @sched_yield() #7
  br label %11

11:                                               ; preds = %9, %7
  call void @_ZN3tbb4task12wait_for_allEv(ptr noundef nonnull align 8 dereferenceable(8) %2)
  %12 = add nuw nsw i32 %.010, 1
  %exitcond.not = icmp eq i32 %12, 100000
  br i1 %exitcond.not, label %4, label %7, !llvm.loop !5
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb4task10set_parentEPS0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #1 align 2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { inlinehint 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 = { inlinehint 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 "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 = { 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 #6 = { "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 #7 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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