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

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

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z24allocatorRandomThrashingv() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca [1000 x ptr], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #5
  store i32 1000, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 10000, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 10485760, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8000, ptr %4) #5
  call void @llvm.memset.p0.i64(ptr align 16 %4, i8 0, i64 8000, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %50, %0
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = call i32 @rand() #5
  %12 = srem i32 %11, 10000
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

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

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

16:                                               ; preds = %30, %15
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = call i32 @rand() #5
  %19 = srem i32 %18, 1000
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %33

22:                                               ; preds = %16
  %23 = call i32 @rand() #5
  %24 = srem i32 %23, 10485760
  %25 = sext i32 %24 to i64
  %26 = call ptr @scalable_malloc(i64 noundef %25)
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %28
  store ptr %26, ptr %29, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %22
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %7, align 4, !tbaa !5
  br label %16, !llvm.loop !11

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

34:                                               ; preds = %46, %33
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = icmp slt i32 %35, 1000
  br i1 %36, label %38, label %37

37:                                               ; preds = %34
  store i32 8, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %49

38:                                               ; preds = %34
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  call void @scalable_free(ptr noundef %42)
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %44
  store ptr null, ptr %45, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %38
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %8, align 4, !tbaa !5
  br label %34, !llvm.loop !13

49:                                               ; preds = %37
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %5, align 4, !tbaa !5
  br label %9, !llvm.loop !14

53:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 8000, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #5
  ret void
}

; Function Attrs: nounwind
declare i32 @rand() #3

declare ptr @scalable_malloc(i64 noundef) #4

declare void @scalable_free(ptr noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = { 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 = { "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 }

!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}
!14 = distinct !{!14, !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"

; 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 nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z24allocatorRandomThrashingv() local_unnamed_addr #2 {
  %1 = alloca [1000 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 8000, ptr nonnull %1) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(8000) %1, i8 0, i64 8000, i1 false)
  %2 = tail call i32 @rand() #5
  %3 = srem i32 %2, 10000
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.preheader11, label %._crit_edge

.preheader11:                                     ; preds = %0, %17
  %.01014 = phi i32 [ %18, %17 ], [ 0, %0 ]
  %5 = tail call i32 @rand() #5
  %6 = srem i32 %5, 1000
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.lr.ph, %.preheader11
  br label %.preheader

._crit_edge:                                      ; preds = %17, %0
  call void @llvm.lifetime.end.p0(i64 8000, ptr nonnull %1) #5
  ret void

.lr.ph:                                           ; preds = %.preheader11, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader11 ]
  %8 = tail call i32 @rand() #5
  %9 = srem i32 %8, 10485760
  %10 = sext i32 %9 to i64
  %11 = tail call ptr @scalable_malloc(i64 noundef %10)
  %12 = getelementptr inbounds [1000 x ptr], ptr %1, i64 0, i64 %indvars.iv
  store ptr %11, ptr %12, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %13 = tail call i32 @rand() #5
  %14 = srem i32 %13, 1000
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph, label %.preheader.preheader, !llvm.loop !9

17:                                               ; preds = %.preheader
  %18 = add nuw nsw i32 %.01014, 1
  %19 = tail call i32 @rand() #5
  %20 = srem i32 %19, 10000
  %21 = icmp slt i32 %18, %20
  br i1 %21, label %.preheader11, label %._crit_edge, !llvm.loop !11

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %indvars.iv16 = phi i64 [ %indvars.iv.next17, %.preheader ], [ 0, %.preheader.preheader ]
  %22 = getelementptr inbounds [1000 x ptr], ptr %1, i64 0, i64 %indvars.iv16
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  tail call void @scalable_free(ptr noundef %23)
  store ptr null, ptr %22, align 8, !tbaa !5
  %indvars.iv.next17 = add nuw nsw i64 %indvars.iv16, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next17, 1000
  br i1 %exitcond.not, label %17, label %.preheader, !llvm.loop !12
}

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #3

declare ptr @scalable_malloc(i64 noundef) local_unnamed_addr #4

declare void @scalable_free(ptr noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = { 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 = { "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 }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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