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

; 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.std::nothrow_t" = type { i8 }
%"class.tbb::task_scheduler_init" = type { ptr }
%class.RunInNativeThread = type { i8, i8 }

@.str.19 = external hidden unnamed_addr constant [89 x i8], align 1
@_ZSt7nothrow = external global %"struct.std::nothrow_t", align 1
@.str.21 = external hidden unnamed_addr constant [3 x i8], align 1

; 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 @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17TestTasksInThreadv() #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.tbb::task_scheduler_init", align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca %class.RunInNativeThread, align 1
  %6 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @_ZN3tbb19task_scheduler_initC2Eim(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef 2, i64 noundef 0)
  invoke void @_Z15CallParallelForv()
          to label %7 unwind label %12

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

8:                                                ; preds = %21, %7
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 2
  br i1 %10, label %16, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %28

12:                                               ; preds = %0
  %13 = landingpad { ptr, i32 }
          cleanup
  %14 = extractvalue { ptr, i32 } %13, 0
  store ptr %14, ptr %2, align 8
  %15 = extractvalue { ptr, i32 } %13, 1
  store i32 %15, ptr %3, align 4
  br label %41

16:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = icmp eq i32 1, %17
  invoke void @_ZN17RunInNativeThreadC2Ebb(ptr noundef nonnull align 1 dereferenceable(2) %5, i1 noundef zeroext %18, i1 noundef zeroext false)
          to label %19 unwind label %24

19:                                               ; preds = %16
  invoke void @_Z17NativeParallelForIi17RunInNativeThreadEvT_RKT0_(i32 noundef 2, ptr noundef nonnull align 1 dereferenceable(2) %5)
          to label %20 unwind label %24

20:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  br label %21

21:                                               ; preds = %20
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !9

24:                                               ; preds = %19, %16
  %25 = landingpad { ptr, i32 }
          cleanup
  %26 = extractvalue { ptr, i32 } %25, 0
  store ptr %26, ptr %2, align 8
  %27 = extractvalue { ptr, i32 } %25, 1
  store i32 %27, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %41

28:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  %29 = call noundef zeroext i1 @_ZN3tbb19task_scheduler_init18blocking_terminateERKSt9nothrow_t(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow) #3
  %30 = zext i1 %29 to i8
  store i8 %30, ptr %6, align 1, !tbaa !11
  %31 = load i8, ptr %6, align 1, !tbaa !11, !range !13, !noundef !14
  %32 = trunc i8 %31 to i1
  br i1 %32, label %33, label %34

33:                                               ; preds = %28
  br label %36

34:                                               ; preds = %28
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.19, i32 noundef 154, ptr noundef @.str.21, ptr noundef null)
          to label %35 unwind label %37

35:                                               ; preds = %34
  br label %36

36:                                               ; preds = %35, %33
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %1) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void

37:                                               ; preds = %34
  %38 = landingpad { ptr, i32 }
          cleanup
  %39 = extractvalue { ptr, i32 } %38, 0
  store ptr %39, ptr %2, align 8
  %40 = extractvalue { ptr, i32 } %38, 1
  store i32 %40, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  br label %41

41:                                               ; preds = %37, %24, %12
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %1) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %42

42:                                               ; preds = %41
  %43 = load ptr, ptr %2, align 8
  %44 = load i32, ptr %3, align 4
  %45 = insertvalue { ptr, i32 } poison, ptr %43, 0
  %46 = insertvalue { ptr, i32 } %45, i32 %44, 1
  resume { ptr, i32 } %46
}

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

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN17RunInNativeThreadC2Ebb(ptr noundef nonnull align 1 dereferenceable(2), i1 noundef zeroext, i1 noundef zeroext) unnamed_addr #2 align 2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2 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 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}


; 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.std::nothrow_t" = type { i8 }
%"class.tbb::task_scheduler_init" = type { ptr }
%class.RunInNativeThread = type { i8, i8 }

@.str.19 = external hidden unnamed_addr constant [89 x i8], align 1
@_ZSt7nothrow = external global %"struct.std::nothrow_t", align 1
@.str.21 = external hidden unnamed_addr constant [3 x i8], align 1

; 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 uwtable
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z17TestTasksInThreadv() local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.tbb::task_scheduler_init", align 8
  %2 = alloca %class.RunInNativeThread, align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  call void @_ZN3tbb19task_scheduler_initC2Eim(ptr noundef nonnull align 8 dereferenceable(8) %1, i32 noundef 2, i64 noundef 0)
  invoke void @_Z15CallParallelForv()
          to label %.preheader.preheader unwind label %3

.preheader.preheader:                             ; preds = %0
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %2) #3
  call void @_ZN17RunInNativeThreadC2Ebb(ptr noundef nonnull align 1 dereferenceable(2) %2, i1 noundef zeroext false, i1 noundef zeroext false)
  invoke void @_Z17NativeParallelForIi17RunInNativeThreadEvT_RKT0_(i32 noundef 2, ptr noundef nonnull align 1 dereferenceable(2) %2)
          to label %.preheader.1 unwind label %7

3:                                                ; preds = %0
  %4 = landingpad { ptr, i32 }
          cleanup
  br label %13

.preheader.1:                                     ; preds = %.preheader.preheader
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %2) #3
  call void @_ZN17RunInNativeThreadC2Ebb(ptr noundef nonnull align 1 dereferenceable(2) %2, i1 noundef zeroext true, i1 noundef zeroext false)
  invoke void @_Z17NativeParallelForIi17RunInNativeThreadEvT_RKT0_(i32 noundef 2, ptr noundef nonnull align 1 dereferenceable(2) %2)
          to label %5 unwind label %7

5:                                                ; preds = %.preheader.1
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %2) #3
  %6 = call noundef zeroext i1 @_ZN3tbb19task_scheduler_init18blocking_terminateERKSt9nothrow_t(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull align 1 dereferenceable(1) @_ZSt7nothrow) #3
  br i1 %6, label %10, label %9

7:                                                ; preds = %.preheader.1, %.preheader.preheader
  %8 = landingpad { ptr, i32 }
          cleanup
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %2) #3
  br label %13

9:                                                ; preds = %5
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.19, i32 noundef 154, ptr noundef nonnull @.str.21, ptr noundef null)
          to label %10 unwind label %11

10:                                               ; preds = %9, %5
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %1) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  ret void

11:                                               ; preds = %9
  %12 = landingpad { ptr, i32 }
          cleanup
  br label %13

13:                                               ; preds = %11, %7, %3
  %.pn = phi { ptr, i32 } [ %8, %7 ], [ %12, %11 ], [ %4, %3 ]
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %1) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  resume { ptr, i32 } %.pn
}

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

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN17RunInNativeThreadC2Ebb(ptr noundef nonnull align 1 dereferenceable(2), i1 noundef zeroext, i1 noundef zeroext) unnamed_addr #2 align 2

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

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

attributes #0 = { mustprogress 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 = { 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 #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)"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass