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

@.str.29 = external hidden unnamed_addr constant [98 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [12 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 {
  %1 = alloca i8, align 1
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %1) #2
  store i8 1, ptr %1, align 1, !tbaa !5
  %6 = call noundef i64 @_Z14GetMemoryUsage14MemoryStatType(i32 noundef 0)
  %7 = icmp ne i64 %6, 0
  br i1 %7, label %8, label %17

8:                                                ; preds = %0
  %9 = call noundef zeroext i1 @_Z17TestBootstrapLeakv()
  %10 = zext i1 %9 to i32
  %11 = load i8, ptr %1, align 1, !tbaa !5, !range !9, !noundef !10
  %12 = trunc i8 %11 to i1
  %13 = zext i1 %12 to i32
  %14 = and i32 %13, %10
  %15 = icmp ne i32 %14, 0
  %16 = zext i1 %15 to i8
  store i8 %16, ptr %1, align 1, !tbaa !5
  br label %17

17:                                               ; preds = %8, %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  store i64 1, ptr %2, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 1, ptr %3, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !11
  br label %18

18:                                               ; preds = %22, %17
  %19 = load i64, ptr %4, align 8, !tbaa !11
  %20 = icmp ule i64 %19, 65536
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %18
  %23 = load i64, ptr %4, align 8, !tbaa !11
  %24 = call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef %23)
  %25 = zext i1 %24 to i32
  %26 = load i8, ptr %1, align 1, !tbaa !5, !range !9, !noundef !10
  %27 = trunc i8 %26 to i1
  %28 = zext i1 %27 to i32
  %29 = and i32 %28, %25
  %30 = icmp ne i32 %29, 0
  %31 = zext i1 %30 to i8
  store i8 %31, ptr %1, align 1, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !11
  store i64 %32, ptr %2, align 8, !tbaa !11
  %33 = load i64, ptr %4, align 8, !tbaa !11
  store i64 %33, ptr %3, align 8, !tbaa !11
  %34 = load i64, ptr %2, align 8, !tbaa !11
  %35 = load i64, ptr %3, align 8, !tbaa !11
  %36 = add i64 %34, %35
  store i64 %36, ptr %4, align 8, !tbaa !11
  br label %18, !llvm.loop !13

37:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 2, ptr %5, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %52, %37
  %39 = load i64, ptr %5, align 8, !tbaa !11
  %40 = icmp ule i64 %39, 65536
  br i1 %40, label %42, label %41

41:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %55

42:                                               ; preds = %38
  %43 = load i64, ptr %5, align 8, !tbaa !11
  %44 = call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef %43)
  %45 = zext i1 %44 to i32
  %46 = load i8, ptr %1, align 1, !tbaa !5, !range !9, !noundef !10
  %47 = trunc i8 %46 to i1
  %48 = zext i1 %47 to i32
  %49 = and i32 %48, %45
  %50 = icmp ne i32 %49, 0
  %51 = zext i1 %50 to i8
  store i8 %51, ptr %1, align 1, !tbaa !5
  br label %52

52:                                               ; preds = %42
  %53 = load i64, ptr %5, align 8, !tbaa !11
  %54 = mul i64 %53, 2
  store i64 %54, ptr %5, align 8, !tbaa !11
  br label %38, !llvm.loop !15

55:                                               ; preds = %41
  %56 = load i8, ptr %1, align 1, !tbaa !5, !range !9, !noundef !10
  %57 = trunc i8 %56 to i1
  br i1 %57, label %58, label %59

58:                                               ; preds = %55
  br label %60

59:                                               ; preds = %55
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.29, i32 noundef 181, ptr noundef @.str.45, ptr noundef @.str.46)
  br label %60

60:                                               ; preds = %59, %58
  call void @_Z16TestAlignedMsizev()
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #2
  ret i32 0
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z14GetMemoryUsage14MemoryStatType(i32 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z17TestBootstrapLeakv() #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef) #1

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

@.str.29 = external hidden unnamed_addr constant [98 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z11ReportErrorPKciS0_S0_(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8TestMainv() local_unnamed_addr #0 {
  %1 = tail call noundef i64 @_Z14GetMemoryUsage14MemoryStatType(i32 noundef 0)
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %.preheader, label %2

2:                                                ; preds = %0
  %3 = tail call noundef zeroext i1 @_Z17TestBootstrapLeakv()
  br label %.preheader

.preheader:                                       ; preds = %2, %0
  %.116.ph = phi i1 [ true, %0 ], [ %3, %2 ]
  br label %4

4:                                                ; preds = %.preheader, %4
  %.01318 = phi i64 [ %7, %4 ], [ 1, %.preheader ]
  %.01417 = phi i64 [ %.01318, %4 ], [ 1, %.preheader ]
  %.116 = phi i1 [ %6, %4 ], [ %.116.ph, %.preheader ]
  %5 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef %.01318)
  %6 = and i1 %.116, %5
  %7 = add nuw nsw i64 %.01318, %.01417
  %8 = icmp ult i64 %7, 65537
  br i1 %8, label %4, label %.preheader.preheader, !llvm.loop !5

.preheader.preheader:                             ; preds = %4
  %9 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 2)
  %10 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 4)
  %11 = and i1 %9, %10
  %12 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 8)
  %13 = and i1 %11, %12
  %14 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 16)
  %15 = and i1 %13, %14
  %16 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 32)
  %17 = and i1 %15, %16
  %18 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 64)
  %19 = and i1 %17, %18
  %20 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 128)
  %21 = and i1 %19, %20
  %22 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 256)
  %23 = and i1 %21, %22
  %24 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 512)
  %25 = and i1 %23, %24
  %26 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 1024)
  %27 = and i1 %25, %26
  %28 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 2048)
  %29 = and i1 %27, %28
  %30 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 4096)
  %31 = and i1 %29, %30
  %32 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 8192)
  %33 = and i1 %31, %32
  %34 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 16384)
  %35 = and i1 %33, %34
  %36 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 32768)
  %37 = and i1 %35, %36
  %38 = tail call noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef 65536)
  %39 = and i1 %37, %38
  %40 = and i1 %39, %6
  br i1 %40, label %42, label %41

41:                                               ; preds = %.preheader.preheader
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.29, i32 noundef 181, ptr noundef nonnull @.str.45, ptr noundef nonnull @.str.46)
  br label %42

42:                                               ; preds = %.preheader.preheader, %41
  tail call void @_Z16TestAlignedMsizev()
  ret i32 0
}

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z14GetMemoryUsage14MemoryStatType(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z17TestBootstrapLeakv() local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z16TestAlignedMsizev() local_unnamed_addr #0

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass