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

; 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.CrossThreadRun = type { i8 }
%struct.PoolSpace = type { i64, i32, i64, ptr }

@.str.20 = external hidden unnamed_addr constant [93 x i8], align 1
@_ZL9MaxThread = external hidden global i32, align 4
@_ZL9MinThread = external hidden global i32, align 4
@_ZL9poolSpace = external hidden global ptr, align 8
@.str.32 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [21 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 void @_Z20TestCrossThreadPoolsv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca %class.CrossThreadRun, align 1
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %5 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  store i32 %5, ptr %1, align 4, !tbaa !5
  br label %6

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

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

11:                                               ; preds = %6
  %12 = load i32, ptr %1, align 4, !tbaa !5
  call void @_ZN14CrossThreadRun11initBarrierEj(i32 noundef %12)
  %13 = load i32, ptr %1, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  call void @_ZN14CrossThreadRun4initEl(i64 noundef %14)
  %15 = load i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  call void @_ZN14CrossThreadRunC2Ev(ptr noundef nonnull align 1 dereferenceable(1) %3)
  call void @_Z17NativeParallelForIi14CrossThreadRunEvT_RKT0_(i32 noundef %15, ptr noundef nonnull align 1 dereferenceable(1) %3)
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %32, %11
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %16
  %22 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.PoolSpace, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.PoolSpace, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %30, label %29

29:                                               ; preds = %21
  br label %31

30:                                               ; preds = %21
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.20, i32 noundef 279, ptr noundef @.str.32, ptr noundef @.str.33)
  br label %31

31:                                               ; preds = %30, %29
  br label %32

32:                                               ; preds = %31
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %16, !llvm.loop !14

35:                                               ; preds = %20
  call void @_ZN14CrossThreadRun7destroyEv()
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %1, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !16

39:                                               ; preds = %10
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14CrossThreadRun4initEl(i64 noundef) #1 align 2

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14CrossThreadRun7destroyEv() #1 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS9PoolSpace", !13, i64 0, !6, i64 8, !13, i64 16, !10, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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.CrossThreadRun = type { i8 }
%struct.PoolSpace = type { i64, i32, i64, ptr }

@.str.20 = external hidden unnamed_addr constant [93 x i8], align 1
@_ZL9MaxThread = external hidden local_unnamed_addr global i32, align 4
@_ZL9MinThread = external hidden local_unnamed_addr global i32, align 4
@_ZL9poolSpace = external hidden local_unnamed_addr global ptr, align 8
@.str.32 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [21 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 void @_Z20TestCrossThreadPoolsv() local_unnamed_addr #1 {
  %1 = alloca %class.CrossThreadRun, align 1
  %2 = load i32, ptr @_ZL9MinThread, align 4, !tbaa !5
  %3 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %.not12 = icmp sgt i32 %2, %3
  br i1 %.not12, label %._crit_edge16, label %.lr.ph15.preheader

.lr.ph15.preheader:                               ; preds = %0
  %4 = sext i32 %2 to i64
  br label %.lr.ph15

._crit_edge16:                                    ; preds = %._crit_edge, %0
  ret void

.lr.ph15:                                         ; preds = %.lr.ph15.preheader, %._crit_edge
  %indvars.iv18 = phi i64 [ %4, %.lr.ph15.preheader ], [ %indvars.iv.next19, %._crit_edge ]
  %5 = trunc nsw i64 %indvars.iv18 to i32
  call void @_ZN14CrossThreadRun11initBarrierEj(i32 noundef %5)
  call void @_ZN14CrossThreadRun4initEl(i64 noundef %indvars.iv18)
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %1) #3
  call void @_ZN14CrossThreadRunC2Ev(ptr noundef nonnull align 1 dereferenceable(1) %1)
  call void @_Z17NativeParallelForIi14CrossThreadRunEvT_RKT0_(i32 noundef %5, ptr noundef nonnull align 1 dereferenceable(1) %1)
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %1) #3
  %6 = icmp sgt i64 %indvars.iv18, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph15
  %.pre21 = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  br label %.lr.ph

._crit_edge:                                      ; preds = %13, %.lr.ph15
  call void @_ZN14CrossThreadRun7destroyEv()
  %indvars.iv.next19 = add nsw i64 %indvars.iv18, 1
  %7 = load i32, ptr @_ZL9MaxThread, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %.not.not = icmp slt i64 %indvars.iv18, %8
  br i1 %.not.not, label %.lr.ph15, label %._crit_edge16, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph.preheader, %13
  %9 = phi ptr [ %.pre21, %.lr.ph.preheader ], [ %14, %13 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %13 ]
  %10 = getelementptr inbounds %struct.PoolSpace, ptr %9, i64 %indvars.iv, i32 1
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %.not10 = icmp eq i32 %11, 0
  br i1 %.not10, label %13, label %12

12:                                               ; preds = %.lr.ph
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.20, i32 noundef 279, ptr noundef nonnull @.str.32, ptr noundef nonnull @.str.33)
  %.pre = load ptr, ptr @_ZL9poolSpace, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %12, %.lr.ph
  %14 = phi ptr [ %.pre, %12 ], [ %9, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv18
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14CrossThreadRun11initBarrierEj(i32 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14CrossThreadRun4initEl(i64 noundef) local_unnamed_addr #1 align 2

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN14CrossThreadRun7destroyEv() local_unnamed_addr #1 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTS9PoolSpace", !15, i64 0, !6, i64 8, !15, i64 16, !10, i64 24}
!15 = !{!"long", !7, i64 0}
!16 = distinct !{!16, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass