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

; 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.Harness::internal::Tracer" = type <{ i32, [4 x i8], ptr, ptr, i64, [8 x i8] }>

@_ZN7Harness8internalL6tracerE = external hidden global %"class.Harness::internal::Tracer", align 8
@.str.29 = external hidden unnamed_addr constant [98 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [1 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.34 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.35 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.36 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.37 = external hidden unnamed_addr constant [2 x i8], align 1
@__FUNCTION__._Z16TestReallocMsizem = external hidden unnamed_addr constant [17 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [41 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: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7Harness8internal6Tracer14set_trace_infoEiPKcmS3_(ptr noundef nonnull align 8 dereferenceable(33), i32 noundef, ptr noundef, i64 noundef, ptr noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7Harness8internal6Tracer5traceEPKcz(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, ...) #2 align 2

; 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) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef %0) #2 {
  %2 = alloca i64, align 8
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #6
  store i8 1, ptr %3, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = call ptr @scalable_malloc(i64 noundef %7)
  store ptr %8, ptr %4, align 8, !tbaa !11
  %9 = load ptr, ptr %4, align 8, !tbaa !11
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  br label %13

12:                                               ; preds = %1
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.29, i32 noundef 99, ptr noundef @.str.31, ptr noundef @.str.32)
  br label %13

13:                                               ; preds = %12, %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %14 = load ptr, ptr %4, align 8, !tbaa !11
  %15 = call i64 @scalable_msize(ptr noundef %14)
  store i64 %15, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = icmp uge i64 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  br label %21

20:                                               ; preds = %13
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.29, i32 noundef 101, ptr noundef @.str.33, ptr noundef @.str.34)
  br label %21

21:                                               ; preds = %20, %19
  %22 = load ptr, ptr %4, align 8, !tbaa !11
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = sub i64 %23, 1
  call void @llvm.memset.p0.i64(ptr align 1 %22, i8 97, i64 %24, i1 false)
  %25 = load ptr, ptr %4, align 8, !tbaa !11
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = sub i64 %26, 1
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  store i8 0, ptr %28, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %29 = load ptr, ptr %4, align 8, !tbaa !11
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = mul i64 2, %30
  %32 = call ptr @scalable_realloc(ptr noundef %29, i64 noundef %31)
  store ptr %32, ptr %6, align 8, !tbaa !11
  %33 = load ptr, ptr %6, align 8, !tbaa !11
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %36

35:                                               ; preds = %21
  br label %37

36:                                               ; preds = %21
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.29, i32 noundef 105, ptr noundef @.str.35, ptr noundef @.str.32)
  br label %37

37:                                               ; preds = %36, %35
  %38 = load ptr, ptr %6, align 8, !tbaa !11
  %39 = call i64 @scalable_msize(ptr noundef %38)
  %40 = load i64, ptr %5, align 8, !tbaa !5
  %41 = mul i64 2, %40
  %42 = icmp uge i64 %39, %41
  br i1 %42, label %43, label %44

43:                                               ; preds = %37
  br label %45

44:                                               ; preds = %37
  call void @_Z11ReportErrorPKciS0_S0_(ptr noundef @.str.29, i32 noundef 107, ptr noundef @.str.36, ptr noundef @.str.34)
  br label %45

45:                                               ; preds = %44, %43
  %46 = load ptr, ptr %6, align 8, !tbaa !11
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = mul i64 2, %47
  %49 = sub i64 %48, 1
  %50 = getelementptr inbounds i8, ptr %46, i64 %49
  store i8 0, ptr %50, align 1, !tbaa !13
  %51 = load ptr, ptr %6, align 8, !tbaa !11
  %52 = call i64 @strspn(ptr noundef %51, ptr noundef @.str.37) #7
  %53 = load i64, ptr %5, align 8, !tbaa !5
  %54 = sub i64 %53, 1
  %55 = icmp ult i64 %52, %54
  br i1 %55, label %56, label %59

56:                                               ; preds = %45
  %57 = call noundef ptr @_ZN7Harness8internal6Tracer14set_trace_infoEiPKcmS3_(ptr noundef nonnull align 8 dereferenceable(33) @_ZN7Harness8internalL6tracerE, i32 noundef 0, ptr noundef @.str.29, i64 noundef 110, ptr noundef @__FUNCTION__._Z16TestReallocMsizem)
  %58 = load i64, ptr %2, align 8, !tbaa !5
  call void (ptr, ptr, ...) @_ZN7Harness8internal6Tracer5traceEPKcz(ptr noundef nonnull align 8 dereferenceable(33) %57, ptr noundef @.str.38, i64 noundef %58)
  store i8 0, ptr %3, align 1, !tbaa !9
  br label %59

59:                                               ; preds = %56, %45
  %60 = load ptr, ptr %6, align 8, !tbaa !11
  call void @scalable_free(ptr noundef %60)
  %61 = load i8, ptr %3, align 1, !tbaa !9, !range !14, !noundef !15
  %62 = trunc i8 %61 to i1
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #6
  ret i1 %62
}

declare ptr @scalable_malloc(i64 noundef) #4

declare i64 @scalable_msize(ptr noundef) #4

declare ptr @scalable_realloc(ptr noundef, i64 noundef) #4

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strspn(ptr noundef, ptr noundef) #5

declare void @scalable_free(ptr noundef) #4

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{i8 0, i8 2}
!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.Harness::internal::Tracer" = type <{ i32, [4 x i8], ptr, ptr, i64, [8 x i8] }>

@_ZN7Harness8internalL6tracerE = external hidden global %"class.Harness::internal::Tracer", align 8
@.str.29 = external hidden unnamed_addr constant [98 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [1 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.34 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.35 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.36 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.37 = external hidden unnamed_addr constant [2 x i8], align 1
@__FUNCTION__._Z16TestReallocMsizem = external hidden unnamed_addr constant [17 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7Harness8internal6Tracer14set_trace_infoEiPKcmS3_(ptr noundef nonnull align 8 dereferenceable(33), i32 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7Harness8internal6Tracer5traceEPKcz(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, ...) local_unnamed_addr #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z16TestReallocMsizem(i64 noundef %0) local_unnamed_addr #1 {
  %2 = tail call ptr @scalable_malloc(i64 noundef %0)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %4

3:                                                ; preds = %1
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.29, i32 noundef 99, ptr noundef nonnull @.str.31, ptr noundef nonnull @.str.32)
  br label %4

4:                                                ; preds = %1, %3
  %5 = tail call i64 @scalable_msize(ptr noundef %2)
  %.not20 = icmp ult i64 %5, %0
  br i1 %.not20, label %6, label %7

6:                                                ; preds = %4
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.29, i32 noundef 101, ptr noundef nonnull @.str.33, ptr noundef nonnull @.str.34)
  br label %7

7:                                                ; preds = %4, %6
  %8 = add i64 %5, -1
  tail call void @llvm.memset.p0.i64(ptr align 1 %2, i8 97, i64 %8, i1 false)
  %9 = getelementptr inbounds i8, ptr %2, i64 %8
  store i8 0, ptr %9, align 1, !tbaa !5
  %10 = shl i64 %5, 1
  %11 = tail call ptr @scalable_realloc(ptr noundef %2, i64 noundef %10)
  %.not21 = icmp eq ptr %11, null
  br i1 %.not21, label %12, label %13

12:                                               ; preds = %7
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.29, i32 noundef 105, ptr noundef nonnull @.str.35, ptr noundef nonnull @.str.32)
  br label %13

13:                                               ; preds = %7, %12
  %14 = tail call i64 @scalable_msize(ptr noundef %11)
  %.not22 = icmp ult i64 %14, %10
  br i1 %.not22, label %15, label %16

15:                                               ; preds = %13
  tail call void @_Z11ReportErrorPKciS0_S0_(ptr noundef nonnull @.str.29, i32 noundef 107, ptr noundef nonnull @.str.36, ptr noundef nonnull @.str.34)
  br label %16

16:                                               ; preds = %13, %15
  %17 = getelementptr i8, ptr %11, i64 %10
  %18 = getelementptr i8, ptr %17, i64 -1
  store i8 0, ptr %18, align 1, !tbaa !5
  %19 = tail call i64 @strspn(ptr noundef %11, ptr noundef nonnull @.str.37) #5
  %20 = icmp uge i64 %19, %8
  br i1 %20, label %23, label %21

21:                                               ; preds = %16
  %22 = tail call noundef ptr @_ZN7Harness8internal6Tracer14set_trace_infoEiPKcmS3_(ptr noundef nonnull align 8 dereferenceable(33) @_ZN7Harness8internalL6tracerE, i32 noundef 0, ptr noundef nonnull @.str.29, i64 noundef 110, ptr noundef nonnull @__FUNCTION__._Z16TestReallocMsizem)
  tail call void (ptr, ptr, ...) @_ZN7Harness8internal6Tracer5traceEPKcz(ptr noundef nonnull align 8 dereferenceable(33) %22, ptr noundef nonnull @.str.38, i64 noundef %0)
  br label %23

23:                                               ; preds = %21, %16
  tail call void @scalable_free(ptr noundef nonnull %11)
  ret i1 %20
}

declare ptr @scalable_malloc(i64 noundef) local_unnamed_addr #3

declare i64 @scalable_msize(ptr noundef) local_unnamed_addr #3

declare ptr @scalable_realloc(ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strspn(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #4

declare void @scalable_free(ptr noundef) local_unnamed_addr #3

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass