; LLVM IR from stan-dev/math :: lib/boost_1.84.0/libs/container/src (alloc_lib)

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

%struct.malloc_state = type { i32, i32, i64, i64, ptr, ptr, ptr, i64, i64, i64, [66 x ptr], [32 x ptr], i64, i64, i64, i32, i32, %struct.malloc_segment, ptr, i64 }
%struct.malloc_segment = type { ptr, i64, ptr, i32 }
%struct.malloc_chunk = type { i64, i64, ptr, ptr }

; Function Attrs: nounwind uwtable
declare hidden i32 @spin_acquire_lock(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden void @do_check_inuse_chunk(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #2

; Function Attrs: nounwind uwtable
declare hidden ptr @try_realloc_chunk(ptr noundef, ptr noundef, i64 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @mspace_malloc(ptr noundef, i64 noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind uwtable
declare dso_local void @mspace_free(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mspace_realloc(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store ptr null, ptr %7, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %19

15:                                               ; preds = %3
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = call ptr @mspace_malloc(ptr noundef %16, i64 noundef %17)
  store ptr %18, ptr %7, align 8, !tbaa !5
  br label %112

19:                                               ; preds = %3
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = icmp uge i64 %20, -128
  br i1 %21, label %22, label %24

22:                                               ; preds = %19
  %23 = call ptr @__errno_location() #5
  store i32 12, ptr %23, align 4, !tbaa !11
  br label %111

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = icmp ult i64 %25, 23
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  br label %33

28:                                               ; preds = %24
  %29 = load i64, ptr %6, align 8, !tbaa !9
  %30 = add i64 %29, 8
  %31 = add i64 %30, 15
  %32 = and i64 %31, -16
  br label %33

33:                                               ; preds = %28, %27
  %34 = phi i64 [ 32, %27 ], [ %32, %28 ]
  store i64 %34, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i64 -16
  store ptr %36, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %37, ptr %10, align 8, !tbaa !5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.malloc_state, ptr %38, i32 0, i32 15
  %40 = load i32, ptr %39, align 8, !tbaa !13
  %41 = and i32 %40, 2
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %54

43:                                               ; preds = %33
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.malloc_state, ptr %44, i32 0, i32 16
  %46 = atomicrmw xchg ptr %45, i32 1 seq_cst, align 4
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %53

48:                                               ; preds = %43
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.malloc_state, ptr %49, i32 0, i32 16
  %51 = call i32 @spin_acquire_lock(ptr noundef %50)
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %110, label %55

53:                                               ; preds = %43
  br i1 false, label %110, label %55

54:                                               ; preds = %33
  br i1 false, label %110, label %55

55:                                               ; preds = %54, %53, %48
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i64, ptr %8, align 8, !tbaa !9
  %59 = call ptr @try_realloc_chunk(ptr noundef %56, ptr noundef %57, i64 noundef %58, i32 noundef 1)
  store ptr %59, ptr %11, align 8, !tbaa !5
  %60 = load ptr, ptr %10, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.malloc_state, ptr %60, i32 0, i32 15
  %62 = load i32, ptr %61, align 8, !tbaa !13
  %63 = and i32 %62, 2
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %68

65:                                               ; preds = %55
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.malloc_state, ptr %66, i32 0, i32 16
  store atomic i32 0, ptr %67 release, align 4
  br label %68

68:                                               ; preds = %65, %55
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = icmp ne ptr %69, null
  br i1 %70, label %71, label %76

71:                                               ; preds = %68
  %72 = load ptr, ptr %10, align 8, !tbaa !5
  %73 = load ptr, ptr %11, align 8, !tbaa !5
  call void @do_check_inuse_chunk(ptr noundef %72, ptr noundef %73)
  %74 = load ptr, ptr %11, align 8, !tbaa !5
  %75 = getelementptr inbounds i8, ptr %74, i64 16
  store ptr %75, ptr %7, align 8, !tbaa !5
  br label %109

76:                                               ; preds = %68
  %77 = load ptr, ptr %10, align 8, !tbaa !5
  %78 = load i64, ptr %6, align 8, !tbaa !9
  %79 = call ptr @mspace_malloc(ptr noundef %77, i64 noundef %78)
  store ptr %79, ptr %7, align 8, !tbaa !5
  %80 = load ptr, ptr %7, align 8, !tbaa !5
  %81 = icmp ne ptr %80, null
  br i1 %81, label %82, label %108

82:                                               ; preds = %76
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %83 = load ptr, ptr %9, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.malloc_chunk, ptr %83, i32 0, i32 1
  %85 = load i64, ptr %84, align 8, !tbaa !16
  %86 = and i64 %85, -8
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.malloc_chunk, ptr %87, i32 0, i32 1
  %89 = load i64, ptr %88, align 8, !tbaa !16
  %90 = and i64 %89, 3
  %91 = icmp eq i64 %90, 0
  %92 = zext i1 %91 to i64
  %93 = select i1 %91, i64 16, i64 8
  %94 = sub i64 %86, %93
  store i64 %94, ptr %12, align 8, !tbaa !9
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = load i64, ptr %12, align 8, !tbaa !9
  %98 = load i64, ptr %6, align 8, !tbaa !9
  %99 = icmp ult i64 %97, %98
  br i1 %99, label %100, label %102

100:                                              ; preds = %82
  %101 = load i64, ptr %12, align 8, !tbaa !9
  br label %104

102:                                              ; preds = %82
  %103 = load i64, ptr %6, align 8, !tbaa !9
  br label %104

104:                                              ; preds = %102, %100
  %105 = phi i64 [ %101, %100 ], [ %103, %102 ]
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %95, ptr align 1 %96, i64 %105, i1 false)
  %106 = load ptr, ptr %10, align 8, !tbaa !5
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  call void @mspace_free(ptr noundef %106, ptr noundef %107)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %108

108:                                              ; preds = %104, %76
  br label %109

109:                                              ; preds = %108, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %110

110:                                              ; preds = %109, %54, %53, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %111

111:                                              ; preds = %110, %22
  br label %112

112:                                              ; preds = %111, %15
  %113 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %113
}

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

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 880}
!14 = !{!"malloc_state", !12, i64 0, !12, i64 4, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !7, i64 72, !7, i64 600, !10, i64 856, !10, i64 864, !10, i64 872, !12, i64 880, !12, i64 884, !15, i64 888, !6, i64 920, !10, i64 928}
!15 = !{!"malloc_segment", !6, i64 0, !10, i64 8, !6, i64 16, !12, i64 24}
!16 = !{!17, !10, i64 8}
!17 = !{!"malloc_chunk", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 24}


; 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: nounwind uwtable
declare hidden i32 @spin_acquire_lock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @do_check_inuse_chunk(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @try_realloc_chunk(ptr noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @mspace_malloc(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare dso_local void @mspace_free(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mspace_realloc(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  br i1 %4, label %5, label %7

5:                                                ; preds = %3
  %6 = tail call ptr @mspace_malloc(ptr noundef %0, i64 noundef %2)
  br label %44

7:                                                ; preds = %3
  %8 = icmp ugt i64 %2, -129
  br i1 %8, label %9, label %11

9:                                                ; preds = %7
  %10 = tail call ptr @__errno_location() #4
  store i32 12, ptr %10, align 4, !tbaa !5
  br label %44

11:                                               ; preds = %7
  %12 = icmp ult i64 %2, 23
  %13 = add nuw i64 %2, 23
  %14 = and i64 %13, -16
  %15 = select i1 %12, i64 32, i64 %14
  %16 = getelementptr inbounds i8, ptr %1, i64 -16
  %17 = getelementptr inbounds i8, ptr %0, i64 880
  %18 = load i32, ptr %17, align 8, !tbaa !9
  %19 = and i32 %18, 2
  %.not = icmp eq i32 %19, 0
  br i1 %.not, label %25, label %20

20:                                               ; preds = %11
  %21 = getelementptr inbounds i8, ptr %0, i64 884
  %22 = atomicrmw xchg ptr %21, i32 1 seq_cst, align 4
  %.not38 = icmp eq i32 %22, 0
  br i1 %.not38, label %25, label %23

23:                                               ; preds = %20
  %24 = tail call i32 @spin_acquire_lock(ptr noundef nonnull %21)
  %.not39 = icmp eq i32 %24, 0
  br i1 %.not39, label %25, label %44

25:                                               ; preds = %11, %20, %23
  %26 = tail call ptr @try_realloc_chunk(ptr noundef nonnull %0, ptr noundef nonnull %16, i64 noundef %15, i32 noundef 1)
  %27 = load i32, ptr %17, align 8, !tbaa !9
  %28 = and i32 %27, 2
  %.not40 = icmp eq i32 %28, 0
  br i1 %.not40, label %31, label %29

29:                                               ; preds = %25
  %30 = getelementptr inbounds i8, ptr %0, i64 884
  store atomic i32 0, ptr %30 release, align 4
  br label %31

31:                                               ; preds = %29, %25
  %.not41 = icmp eq ptr %26, null
  br i1 %.not41, label %34, label %32

32:                                               ; preds = %31
  tail call void @do_check_inuse_chunk(ptr noundef nonnull %0, ptr noundef nonnull %26)
  %33 = getelementptr inbounds i8, ptr %26, i64 16
  br label %44

34:                                               ; preds = %31
  %35 = tail call ptr @mspace_malloc(ptr noundef nonnull %0, i64 noundef %2)
  %.not42 = icmp eq ptr %35, null
  br i1 %.not42, label %44, label %36

36:                                               ; preds = %34
  %37 = getelementptr inbounds i8, ptr %1, i64 -8
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = and i64 %38, -8
  %40 = and i64 %38, 3
  %41 = icmp eq i64 %40, 0
  %.neg = select i1 %41, i64 -16, i64 -8
  %42 = add i64 %.neg, %39
  %43 = tail call i64 @llvm.umin.i64(i64 %42, i64 %2)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %35, ptr nonnull align 1 %1, i64 %43, i1 false)
  tail call void @mspace_free(ptr noundef nonnull %0, ptr noundef nonnull %1)
  br label %44

44:                                               ; preds = %23, %34, %36, %32, %9, %5
  %.2 = phi ptr [ %6, %5 ], [ null, %9 ], [ null, %23 ], [ %33, %32 ], [ %35, %36 ], [ null, %34 ]
  ret ptr %.2
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #3

attributes #0 = { 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 nofree nosync nounwind willreturn memory(none) "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: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind willreturn memory(none) }

!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/C++ TBAA"}
!9 = !{!10, !6, i64 880}
!10 = !{!"malloc_state", !6, i64 0, !6, i64 4, !11, i64 8, !11, i64 16, !12, i64 24, !12, i64 32, !12, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !7, i64 72, !7, i64 600, !11, i64 856, !11, i64 864, !11, i64 872, !6, i64 880, !6, i64 884, !13, i64 888, !12, i64 920, !11, i64 928}
!11 = !{!"long", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"malloc_segment", !12, i64 0, !11, i64 8, !12, i64 16, !6, i64 24}
!14 = !{!15, !11, i64 8}
!15 = !{!"malloc_chunk", !11, i64 0, !11, i64 8, !12, i64 16, !12, i64 24}


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