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

@_gm_ = external hidden global %struct.malloc_state, align 8

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

; 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 dso_local void @dlfree(ptr noundef) #0

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

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

13:                                               ; preds = %2
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = call ptr @dlmalloc(i64 noundef %14)
  store ptr %15, ptr %5, align 8, !tbaa !5
  br label %122

16:                                               ; preds = %2
  %17 = load i64, ptr %4, align 8, !tbaa !9
  %18 = icmp uge i64 %17, -128
  br i1 %18, label %19, label %21

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

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

24:                                               ; preds = %21
  br label %30

25:                                               ; preds = %21
  %26 = load i64, ptr %4, align 8, !tbaa !9
  %27 = add i64 %26, 8
  %28 = add i64 %27, 15
  %29 = and i64 %28, -16
  br label %30

30:                                               ; preds = %25, %24
  %31 = phi i64 [ 32, %24 ], [ %29, %25 ]
  store i64 %31, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 -16
  store ptr %33, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store ptr @_gm_, ptr %8, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.malloc_state, ptr %34, i32 0, i32 15
  %36 = load i32, ptr %35, align 8, !tbaa !13
  %37 = and i32 %36, 2
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %50

39:                                               ; preds = %30
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.malloc_state, ptr %40, i32 0, i32 16
  %42 = atomicrmw xchg ptr %41, i32 1 seq_cst, align 4
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %49

44:                                               ; preds = %39
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.malloc_state, ptr %45, i32 0, i32 16
  %47 = call i32 @spin_acquire_lock(ptr noundef %46)
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %120, label %51

49:                                               ; preds = %39
  br i1 false, label %120, label %51

50:                                               ; preds = %30
  br i1 false, label %120, label %51

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

61:                                               ; preds = %51
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.malloc_state, ptr %62, i32 0, i32 16
  store atomic i32 0, ptr %63 release, align 4
  br label %64

64:                                               ; preds = %61, %51
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = icmp ne ptr %65, null
  br i1 %66, label %67, label %72

67:                                               ; preds = %64
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  call void @do_check_inuse_chunk(ptr noundef %68, ptr noundef %69)
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i64 16
  store ptr %71, ptr %5, align 8, !tbaa !5
  br label %119

72:                                               ; preds = %64
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = icmp eq ptr %73, @_gm_
  br i1 %74, label %75, label %78

75:                                               ; preds = %72
  %76 = load i64, ptr %4, align 8, !tbaa !9
  %77 = call ptr @dlmalloc(i64 noundef %76)
  br label %82

78:                                               ; preds = %72
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = load i64, ptr %4, align 8, !tbaa !9
  %81 = call ptr @mspace_malloc(ptr noundef %79, i64 noundef %80)
  br label %82

82:                                               ; preds = %78, %75
  %83 = phi ptr [ %77, %75 ], [ %81, %78 ]
  store ptr %83, ptr %5, align 8, !tbaa !5
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = icmp ne ptr %84, null
  br i1 %85, label %86, label %118

86:                                               ; preds = %82
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %87 = load ptr, ptr %7, 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, -8
  %91 = load ptr, ptr %7, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.malloc_chunk, ptr %91, i32 0, i32 1
  %93 = load i64, ptr %92, align 8, !tbaa !16
  %94 = and i64 %93, 3
  %95 = icmp eq i64 %94, 0
  %96 = zext i1 %95 to i64
  %97 = select i1 %95, i64 16, i64 8
  %98 = sub i64 %90, %97
  store i64 %98, ptr %10, align 8, !tbaa !9
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = load i64, ptr %10, align 8, !tbaa !9
  %102 = load i64, ptr %4, align 8, !tbaa !9
  %103 = icmp ult i64 %101, %102
  br i1 %103, label %104, label %106

104:                                              ; preds = %86
  %105 = load i64, ptr %10, align 8, !tbaa !9
  br label %108

106:                                              ; preds = %86
  %107 = load i64, ptr %4, align 8, !tbaa !9
  br label %108

108:                                              ; preds = %106, %104
  %109 = phi i64 [ %105, %104 ], [ %107, %106 ]
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %99, ptr align 1 %100, i64 %109, i1 false)
  %110 = load ptr, ptr %8, align 8, !tbaa !5
  %111 = icmp eq ptr %110, @_gm_
  br i1 %111, label %112, label %114

112:                                              ; preds = %108
  %113 = load ptr, ptr %3, align 8, !tbaa !5
  call void @dlfree(ptr noundef %113)
  br label %117

114:                                              ; preds = %108
  %115 = load ptr, ptr %8, align 8, !tbaa !5
  %116 = load ptr, ptr %3, align 8, !tbaa !5
  call void @mspace_free(ptr noundef %115, ptr noundef %116)
  br label %117

117:                                              ; preds = %114, %112
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %118

118:                                              ; preds = %117, %82
  br label %119

119:                                              ; preds = %118, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %120

120:                                              ; preds = %119, %50, %49, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %121

121:                                              ; preds = %120, %19
  br label %122

122:                                              ; preds = %121, %13
  %123 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret ptr %123
}

; 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

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"

%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 }

@_gm_ = external hidden global %struct.malloc_state, align 8

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @dlfree(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: nounwind uwtable
define dso_local ptr @dlrealloc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call ptr @dlmalloc(i64 noundef %1)
  br label %40

6:                                                ; preds = %2
  %7 = icmp ugt i64 %1, -129
  br i1 %7, label %8, label %10

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

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

18:                                               ; preds = %10
  %19 = atomicrmw xchg ptr getelementptr inbounds (i8, ptr @_gm_, i64 884), i32 1 seq_cst, align 4
  %.not40 = icmp eq i32 %19, 0
  br i1 %.not40, label %22, label %20

20:                                               ; preds = %18
  %21 = tail call i32 @spin_acquire_lock(ptr noundef nonnull getelementptr inbounds (i8, ptr @_gm_, i64 884))
  %.not41 = icmp eq i32 %21, 0
  br i1 %.not41, label %22, label %40

22:                                               ; preds = %10, %18, %20
  %23 = tail call ptr @try_realloc_chunk(ptr noundef nonnull @_gm_, ptr noundef nonnull %15, i64 noundef %14, i32 noundef 1)
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @_gm_, i64 880), align 8, !tbaa !9
  %25 = and i32 %24, 2
  %.not42 = icmp eq i32 %25, 0
  br i1 %.not42, label %27, label %26

26:                                               ; preds = %22
  store atomic i32 0, ptr getelementptr inbounds (i8, ptr @_gm_, i64 884) release, align 4
  br label %27

27:                                               ; preds = %26, %22
  %.not43 = icmp eq ptr %23, null
  br i1 %.not43, label %30, label %28

28:                                               ; preds = %27
  tail call void @do_check_inuse_chunk(ptr noundef nonnull @_gm_, ptr noundef nonnull %23)
  %29 = getelementptr inbounds i8, ptr %23, i64 16
  br label %40

30:                                               ; preds = %27
  %31 = tail call ptr @dlmalloc(i64 noundef %1)
  %.not44 = icmp eq ptr %31, null
  br i1 %.not44, label %40, label %32

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

40:                                               ; preds = %20, %30, %32, %28, %8, %4
  %.2 = phi ptr [ %5, %4 ], [ null, %8 ], [ null, %20 ], [ %29, %28 ], [ %31, %32 ], [ null, %30 ]
  ret ptr %.2
}

; 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: 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: 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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass