; LLVM IR from stan-dev/math :: lib/tbb_2020.3/src/test/test_parallel_scan.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"

%"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 { i64 }
%"class.tbb::task_scheduler_init" = type { ptr }

@.str.17 = external hidden unnamed_addr constant [94 x i8], align 1
@NumberOfLiveStorage = external dso_local global %"struct.tbb::atomic", align 8
@_ZL9MinThread = external hidden global i32, align 4
@_ZL9MaxThread = external hidden global i32, align 4
@.str.35 = external hidden unnamed_addr constant [23 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
define dso_local noundef i32 @_Z8TestMainv() #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %"class.tbb::task_scheduler_init", align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  call void @_ZL12TestScanTagsv()
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %7 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  store i32 %7, ptr %1, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %42, %0
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %11 = icmp sle i32 %9, %10
  br i1 %11, label %13, label %12

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

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

14:                                               ; preds = %34, %13
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 3
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %41

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %19 = load i32, ptr %1, align 4, !tbaa !5
  call void @_ZN3tbb19task_scheduler_initC2Eim(ptr noundef nonnull align 8 dereferenceable(8) %4, i32 noundef %19, i64 noundef 0)
  %20 = invoke noundef i64 @_ZN3tbb6atomicIlEaSEl(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage, i64 noundef 0)
          to label %21 unwind label %37

21:                                               ; preds = %18
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = load i32, ptr %1, align 4, !tbaa !5
  invoke void @_Z15TestAccumulatorii(i32 noundef %22, i32 noundef %23)
          to label %24 unwind label %37

24:                                               ; preds = %21
  %25 = load i32, ptr %1, align 4, !tbaa !5
  invoke void @_ZL15TestCPUUserTimeii(i32 noundef %25, i32 noundef 1)
          to label %26 unwind label %37

26:                                               ; preds = %24
  %27 = invoke noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage)
          to label %28 unwind label %37

28:                                               ; preds = %26
  %29 = icmp eq i64 %27, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %28
  br label %33

31:                                               ; preds = %28
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.17, i32 noundef 455, ptr noundef @.str.35, ptr noundef null)
          to label %32 unwind label %37

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32, %30
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !5
  br label %14, !llvm.loop !9

37:                                               ; preds = %31, %26, %24, %21, %18
  %38 = landingpad { ptr, i32 }
          cleanup
  %39 = extractvalue { ptr, i32 } %38, 0
  store ptr %39, ptr %5, align 8
  %40 = extractvalue { ptr, i32 } %38, 1
  store i32 %40, ptr %6, align 4
  call void @_ZN3tbb19task_scheduler_initD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %46

41:                                               ; preds = %17
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !11

45:                                               ; preds = %12
  ret i32 0

46:                                               ; preds = %37
  %47 = load ptr, ptr %5, align 8
  %48 = load i32, ptr %6, align 4
  %49 = insertvalue { ptr, i32 } poison, ptr %47, 0
  %50 = insertvalue { ptr, i32 } %49, i32 %48, 1
  resume { ptr, i32 } %50
}

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL12TestScanTagsv() #1

; 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

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL15TestCPUUserTimeii(i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8)) #1 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 = distinct !{!11, !10}


; 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::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 { i64 }
%"class.tbb::task_scheduler_init" = type { ptr }

@.str.17 = external hidden unnamed_addr constant [94 x i8], align 1
@NumberOfLiveStorage = external dso_local global %"struct.tbb::atomic", align 8
@_ZL9MinThread = external hidden local_unnamed_addr global i32, align 4
@_ZL9MaxThread = external hidden local_unnamed_addr global i32, align 4
@.str.35 = external hidden unnamed_addr constant [23 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
define dso_local noundef i32 @_Z8TestMainv() local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.tbb::task_scheduler_init", align 8
  tail call void @_ZL12TestScanTagsv()
  %2 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  %3 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not11 = icmp sgt i32 %2, %3
  br i1 %.not11, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %0, %30
  %.012 = phi i32 [ %31, %30 ], [ %2, %0 ]
  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 %.012, i64 noundef 0)
  %4 = invoke noundef i64 @_ZN3tbb6atomicIlEaSEl(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage, i64 noundef 0)
          to label %5 unwind label %33

._crit_edge:                                      ; preds = %30, %0
  ret i32 0

5:                                                ; preds = %.preheader
  invoke void @_Z15TestAccumulatorii(i32 noundef 0, i32 noundef %.012)
          to label %6 unwind label %33

6:                                                ; preds = %5
  invoke void @_ZL15TestCPUUserTimeii(i32 noundef %.012, i32 noundef 1)
          to label %7 unwind label %33

7:                                                ; preds = %6
  %8 = invoke noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage)
          to label %9 unwind label %33

9:                                                ; preds = %7
  %10 = icmp eq i64 %8, 0
  br i1 %10, label %12, label %11

11:                                               ; preds = %9
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 455, ptr noundef nonnull @.str.35, ptr noundef null)
          to label %12 unwind label %33

12:                                               ; preds = %11, %9
  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
  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 %.012, i64 noundef 0)
  %13 = invoke noundef i64 @_ZN3tbb6atomicIlEaSEl(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage, i64 noundef 0)
          to label %14 unwind label %33

14:                                               ; preds = %12
  invoke void @_Z15TestAccumulatorii(i32 noundef 1, i32 noundef %.012)
          to label %15 unwind label %33

15:                                               ; preds = %14
  invoke void @_ZL15TestCPUUserTimeii(i32 noundef %.012, i32 noundef 1)
          to label %16 unwind label %33

16:                                               ; preds = %15
  %17 = invoke noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage)
          to label %18 unwind label %33

18:                                               ; preds = %16
  %19 = icmp eq i64 %17, 0
  br i1 %19, label %21, label %20

20:                                               ; preds = %18
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 455, ptr noundef nonnull @.str.35, ptr noundef null)
          to label %21 unwind label %33

21:                                               ; preds = %20, %18
  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
  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 %.012, i64 noundef 0)
  %22 = invoke noundef i64 @_ZN3tbb6atomicIlEaSEl(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage, i64 noundef 0)
          to label %23 unwind label %33

23:                                               ; preds = %21
  invoke void @_Z15TestAccumulatorii(i32 noundef 2, i32 noundef %.012)
          to label %24 unwind label %33

24:                                               ; preds = %23
  invoke void @_ZL15TestCPUUserTimeii(i32 noundef %.012, i32 noundef 1)
          to label %25 unwind label %33

25:                                               ; preds = %24
  %26 = invoke noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8) @NumberOfLiveStorage)
          to label %27 unwind label %33

27:                                               ; preds = %25
  %28 = icmp eq i64 %26, 0
  br i1 %28, label %30, label %29

29:                                               ; preds = %27
  invoke void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.17, i32 noundef 455, ptr noundef nonnull @.str.35, ptr noundef null)
          to label %30 unwind label %33

30:                                               ; preds = %29, %27
  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
  %31 = add nsw i32 %.012, 1
  %32 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.012, %32
  br i1 %.not.not, label %.preheader, label %._crit_edge, !llvm.loop !9

33:                                               ; preds = %29, %25, %24, %23, %21, %20, %16, %15, %14, %12, %11, %7, %6, %5, %.preheader
  %34 = landingpad { ptr, i32 }
          cleanup
  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 } %34
}

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL12TestScanTagsv() local_unnamed_addr #1

; 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

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

; Function Attrs: mustprogress uwtable
declare hidden void @_ZL15TestCPUUserTimeii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNVK3tbb8internal11atomic_implIlEcvlEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #1 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)"}
!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: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass