; 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
@s_allocated_memory = external hidden global i64, align 8

; 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 uwtable
define dso_local i32 @boost_cont_grow(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, 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
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store ptr @_gm_, ptr %10, align 8, !tbaa !5
  %14 = load ptr, ptr %10, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.malloc_state, ptr %14, i32 0, i32 15
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = and i32 %16, 2
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %30

19:                                               ; preds = %4
  %20 = load ptr, ptr %10, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.malloc_state, ptr %20, i32 0, i32 16
  %22 = atomicrmw xchg ptr %21, i32 1 seq_cst, align 4
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %29

24:                                               ; preds = %19
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.malloc_state, ptr %25, i32 0, i32 16
  %27 = call i32 @spin_acquire_lock(ptr noundef %26)
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %102, label %31

29:                                               ; preds = %19
  br i1 false, label %102, label %31

30:                                               ; preds = %4
  br i1 false, label %102, label %31

31:                                               ; preds = %30, %29, %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 -16
  store ptr %33, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %34 = load ptr, ptr %11, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.malloc_chunk, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = and i64 %36, -8
  store i64 %37, ptr %12, align 8, !tbaa !9
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = icmp ult i64 %40, 23
  br i1 %41, label %42, label %43

42:                                               ; preds = %31
  br label %48

43:                                               ; preds = %31
  %44 = load i64, ptr %7, align 8, !tbaa !9
  %45 = add i64 %44, 8
  %46 = add i64 %45, 15
  %47 = and i64 %46, -16
  br label %48

48:                                               ; preds = %43, %42
  %49 = phi i64 [ 32, %42 ], [ %47, %43 ]
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = icmp ult i64 %50, 23
  br i1 %51, label %52, label %53

52:                                               ; preds = %48
  br label %58

53:                                               ; preds = %48
  %54 = load i64, ptr %8, align 8, !tbaa !9
  %55 = add i64 %54, 8
  %56 = add i64 %55, 15
  %57 = and i64 %56, -16
  br label %58

58:                                               ; preds = %53, %52
  %59 = phi i64 [ 32, %52 ], [ %57, %53 ]
  %60 = call ptr @try_realloc_chunk_with_min(ptr noundef %38, ptr noundef %39, i64 noundef %49, i64 noundef %59, i32 noundef 0)
  store ptr %60, ptr %11, align 8, !tbaa !5
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.malloc_state, ptr %61, i32 0, i32 15
  %63 = load i32, ptr %62, align 8, !tbaa !11
  %64 = and i32 %63, 2
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %69

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

69:                                               ; preds = %66, %58
  %70 = load ptr, ptr %11, align 8, !tbaa !5
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %98

72:                                               ; preds = %69
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  %74 = load ptr, ptr %11, align 8, !tbaa !5
  call void @do_check_inuse_chunk(ptr noundef %73, ptr noundef %74)
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds i8, ptr %75, i64 -16
  %77 = getelementptr inbounds %struct.malloc_chunk, ptr %76, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !15
  %79 = and i64 %78, -8
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds i8, ptr %80, i64 -16
  %82 = getelementptr inbounds %struct.malloc_chunk, ptr %81, i32 0, i32 1
  %83 = load i64, ptr %82, align 8, !tbaa !15
  %84 = and i64 %83, 3
  %85 = icmp eq i64 %84, 0
  %86 = zext i1 %85 to i64
  %87 = select i1 %85, i64 16, i64 8
  %88 = sub i64 %79, %87
  %89 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %88, ptr %89, align 8, !tbaa !9
  %90 = load ptr, ptr %11, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.malloc_chunk, ptr %90, i32 0, i32 1
  %92 = load i64, ptr %91, align 8, !tbaa !15
  %93 = and i64 %92, -8
  %94 = load i64, ptr %12, align 8, !tbaa !9
  %95 = sub i64 %93, %94
  %96 = load i64, ptr @s_allocated_memory, align 8, !tbaa !9
  %97 = add i64 %96, %95
  store i64 %97, ptr @s_allocated_memory, align 8, !tbaa !9
  br label %98

98:                                               ; preds = %72, %69
  %99 = load ptr, ptr %11, align 8, !tbaa !5
  %100 = icmp ne ptr null, %99
  %101 = zext i1 %100 to i32
  store i32 %101, ptr %5, align 4
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %103

102:                                              ; preds = %30, %29, %24
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %103

103:                                              ; preds = %102, %98
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %104 = load i32, ptr %5, align 4
  ret i32 %104
}

; Function Attrs: nounwind uwtable
declare hidden ptr @try_realloc_chunk_with_min(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i32 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 }

!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, !13, i64 880}
!12 = !{!"malloc_state", !13, i64 0, !13, 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, !13, i64 880, !13, i64 884, !14, i64 888, !6, i64 920, !10, i64 928}
!13 = !{!"int", !7, i64 0}
!14 = !{!"malloc_segment", !6, i64 0, !10, i64 8, !6, i64 16, !13, i64 24}
!15 = !{!16, !10, i64 8}
!16 = !{!"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
@s_allocated_memory = external hidden local_unnamed_addr global i64, align 8

; 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: nounwind uwtable
define dso_local range(i32 0, 2) i32 @boost_cont_grow(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @_gm_, i64 880), align 8, !tbaa !5
  %6 = and i32 %5, 2
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %11, label %7

7:                                                ; preds = %4
  %8 = atomicrmw xchg ptr getelementptr inbounds (i8, ptr @_gm_, i64 884), i32 1 seq_cst, align 4
  %.not23 = icmp eq i32 %8, 0
  br i1 %.not23, label %11, label %9

9:                                                ; preds = %7
  %10 = tail call i32 @spin_acquire_lock(ptr noundef nonnull getelementptr inbounds (i8, ptr @_gm_, i64 884))
  %.not24 = icmp eq i32 %10, 0
  br i1 %.not24, label %11, label %44

11:                                               ; preds = %4, %7, %9
  %12 = getelementptr inbounds i8, ptr %0, i64 -16
  %13 = getelementptr inbounds i8, ptr %0, i64 -8
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = and i64 %14, -8
  %16 = icmp ult i64 %1, 23
  %17 = add i64 %1, 23
  %18 = and i64 %17, -16
  %19 = select i1 %16, i64 32, i64 %18
  %20 = icmp ult i64 %2, 23
  %21 = add i64 %2, 23
  %22 = and i64 %21, -16
  %23 = select i1 %20, i64 32, i64 %22
  %24 = tail call ptr @try_realloc_chunk_with_min(ptr noundef nonnull @_gm_, ptr noundef nonnull %12, i64 noundef %19, i64 noundef %23, i32 noundef 0)
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @_gm_, i64 880), align 8, !tbaa !5
  %26 = and i32 %25, 2
  %.not25 = icmp eq i32 %26, 0
  br i1 %.not25, label %28, label %27

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

28:                                               ; preds = %27, %11
  %29 = icmp ne ptr %24, null
  br i1 %29, label %30, label %42

30:                                               ; preds = %28
  tail call void @do_check_inuse_chunk(ptr noundef nonnull @_gm_, ptr noundef nonnull %24)
  %31 = load i64, ptr %13, align 8, !tbaa !13
  %32 = and i64 %31, -8
  %33 = and i64 %31, 3
  %34 = icmp eq i64 %33, 0
  %.neg = select i1 %34, i64 -16, i64 -8
  %35 = add i64 %.neg, %32
  store i64 %35, ptr %3, align 8, !tbaa !15
  %36 = getelementptr inbounds i8, ptr %24, i64 8
  %37 = load i64, ptr %36, align 8, !tbaa !13
  %38 = and i64 %37, -8
  %39 = sub i64 %38, %15
  %40 = load i64, ptr @s_allocated_memory, align 8, !tbaa !15
  %41 = add i64 %39, %40
  store i64 %41, ptr @s_allocated_memory, align 8, !tbaa !15
  br label %42

42:                                               ; preds = %30, %28
  %43 = zext i1 %29 to i32
  br label %44

44:                                               ; preds = %9, %42
  %.0 = phi i32 [ %43, %42 ], [ 0, %9 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @try_realloc_chunk_with_min(ptr noundef, ptr noundef, i64 noundef, i64 noundef, i32 noundef) local_unnamed_addr #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" }

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


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