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

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

@.str = external hidden unnamed_addr constant [98 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [36 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [49 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 @_Z12ReallocParamv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [1000 x ptr], align 16
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 1000, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8000, ptr %3) #2
  %5 = call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  %6 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 0
  store ptr %5, ptr %6, align 16, !tbaa !9
  %7 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 0
  %8 = load ptr, ptr %7, align 16, !tbaa !9
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %0
  br label %12

11:                                               ; preds = %0
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str, i32 noundef 282, ptr noundef @.str.19, ptr noundef @.str.20)
  br label %12

12:                                               ; preds = %11, %10
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %28, %12
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 1000
  br i1 %15, label %16, label %31

16:                                               ; preds = %13
  %17 = call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 %19
  store ptr %17, ptr %20, align 8, !tbaa !9
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !9
  %25 = icmp eq ptr null, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %16
  br label %31

27:                                               ; preds = %16
  br label %28

28:                                               ; preds = %27
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %13, !llvm.loop !11

31:                                               ; preds = %26, %13
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = icmp slt i32 %32, 1000
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  br label %36

35:                                               ; preds = %31
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str, i32 noundef 290, ptr noundef @.str.21, ptr noundef @.str.22)
  br label %36

36:                                               ; preds = %35, %34
  %37 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 0
  %38 = load ptr, ptr %37, align 16, !tbaa !9
  %39 = call noundef ptr @_Z8TreallocPvm(ptr noundef %38, i64 noundef 0)
  %40 = call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  %41 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 0
  store ptr %40, ptr %41, align 16, !tbaa !9
  %42 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 0
  %43 = load ptr, ptr %42, align 16, !tbaa !9
  %44 = icmp ne ptr %43, null
  br i1 %44, label %45, label %46

45:                                               ; preds = %36
  br label %47

46:                                               ; preds = %36
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str, i32 noundef 297, ptr noundef @.str.19, ptr noundef null)
  br label %47

47:                                               ; preds = %46, %45
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %59, %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = load i32, ptr %2, align 4, !tbaa !5
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %62

53:                                               ; preds = %48
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1000 x ptr], ptr %3, i64 0, i64 %55
  %57 = load ptr, ptr %56, align 8, !tbaa !9
  %58 = call noundef ptr @_Z8TreallocPvm(ptr noundef %57, i64 noundef 0)
  br label %59

59:                                               ; preds = %53
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %4, align 4, !tbaa !5
  br label %48, !llvm.loop !13

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z8TreallocPvm(ptr noundef, i64 noundef) #1

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 = { 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}


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

@.str = external hidden unnamed_addr constant [98 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [36 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.22 = external hidden unnamed_addr constant [49 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 @_Z12ReallocParamv() local_unnamed_addr #1 {
  %1 = alloca [1000 x ptr], align 16
  call void @llvm.lifetime.start.p0(i64 8000, ptr nonnull %1) #2
  %2 = tail call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %.preheader23

3:                                                ; preds = %0
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str, i32 noundef 282, ptr noundef nonnull @.str.19, ptr noundef nonnull @.str.20)
  br label %.preheader23

.preheader23:                                     ; preds = %0, %3
  br label %4

4:                                                ; preds = %.preheader23, %8
  %indvars.iv = phi i64 [ %indvars.iv.next, %8 ], [ 1, %.preheader23 ]
  %5 = tail call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  %6 = getelementptr inbounds [1000 x ptr], ptr %1, i64 0, i64 %indvars.iv
  store ptr %5, ptr %6, align 8, !tbaa !5
  %7 = icmp eq ptr %5, null
  br i1 %7, label %.loopexit.loopexit, label %8

8:                                                ; preds = %4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 1000
  br i1 %exitcond.not, label %.critedge, label %4, !llvm.loop !9

.critedge:                                        ; preds = %8
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str, i32 noundef 290, ptr noundef nonnull @.str.21, ptr noundef nonnull @.str.22)
  br label %.loopexit

.loopexit.loopexit:                               ; preds = %4
  %9 = and i64 %indvars.iv, 4294967295
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.critedge
  %.0912 = phi i64 [ 1000, %.critedge ], [ %9, %.loopexit.loopexit ]
  %10 = tail call noundef ptr @_Z8TreallocPvm(ptr noundef %2, i64 noundef 0)
  %11 = tail call noundef ptr @_Z8TreallocPvm(ptr noundef null, i64 noundef 31457280)
  store ptr %11, ptr %1, align 16, !tbaa !5
  %.not10 = icmp eq ptr %11, null
  br i1 %.not10, label %12, label %.preheader

12:                                               ; preds = %.loopexit
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str, i32 noundef 297, ptr noundef nonnull @.str.19, ptr noundef null)
  br label %.preheader

.preheader:                                       ; preds = %.loopexit, %12
  br label %14

13:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 8000, ptr nonnull %1) #2
  ret void

14:                                               ; preds = %.preheader, %14
  %indvars.iv17 = phi i64 [ %indvars.iv.next18, %14 ], [ 0, %.preheader ]
  %15 = getelementptr inbounds [1000 x ptr], ptr %1, i64 0, i64 %indvars.iv17
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = tail call noundef ptr @_Z8TreallocPvm(ptr noundef %16, i64 noundef 0)
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, 1
  %exitcond20.not = icmp eq i64 %indvars.iv.next18, %.0912
  br i1 %exitcond20.not, label %13, label %14, !llvm.loop !11
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z8TreallocPvm(ptr noundef, i64 noundef) local_unnamed_addr #1

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 = { 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}


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