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

; 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
define dso_local ptr @mspace_realloc_in_place(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
  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) #3
  store ptr null, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %74

14:                                               ; preds = %3
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = icmp uge i64 %15, -128
  br i1 %16, label %17, label %19

17:                                               ; preds = %14
  %18 = call ptr @__errno_location() #4
  store i32 12, ptr %18, align 4, !tbaa !11
  br label %73

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

22:                                               ; preds = %19
  br label %28

23:                                               ; preds = %19
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = add i64 %24, 8
  %26 = add i64 %25, 15
  %27 = and i64 %26, -16
  br label %28

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

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

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

48:                                               ; preds = %38
  br i1 false, label %72, label %50

49:                                               ; preds = %28
  br i1 false, label %72, label %50

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

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

63:                                               ; preds = %60, %50
  %64 = load ptr, ptr %11, align 8, !tbaa !5
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = icmp eq ptr %64, %65
  br i1 %66, label %67, label %71

67:                                               ; preds = %63
  %68 = load ptr, ptr %10, align 8, !tbaa !5
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  call void @do_check_inuse_chunk(ptr noundef %68, ptr noundef %69)
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %70, ptr %7, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %67, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %72

72:                                               ; preds = %71, %49, %48, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %73

73:                                               ; preds = %72, %17
  br label %74

74:                                               ; preds = %73, %3
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %75
}

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 = { nounwind }
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 = !{!"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}


; 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
define dso_local noundef ptr @mspace_realloc_in_place(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %31, label %4

4:                                                ; preds = %3
  %5 = icmp ugt i64 %2, -129
  br i1 %5, label %6, label %8

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

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

17:                                               ; preds = %8
  %18 = getelementptr inbounds i8, ptr %0, i64 884
  %19 = atomicrmw xchg ptr %18, i32 1 seq_cst, align 4
  %.not23 = icmp eq i32 %19, 0
  br i1 %.not23, label %22, label %20

20:                                               ; preds = %17
  %21 = tail call i32 @spin_acquire_lock(ptr noundef nonnull %18)
  %.not24 = icmp eq i32 %21, 0
  br i1 %.not24, label %22, label %31

22:                                               ; preds = %8, %17, %20
  %23 = tail call ptr @try_realloc_chunk(ptr noundef nonnull %0, ptr noundef nonnull %13, i64 noundef %12, i32 noundef 0)
  %24 = load i32, ptr %14, align 8, !tbaa !9
  %25 = and i32 %24, 2
  %.not25 = icmp eq i32 %25, 0
  br i1 %.not25, label %28, label %26

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

28:                                               ; preds = %26, %22
  %29 = icmp eq ptr %23, %13
  br i1 %29, label %30, label %31

30:                                               ; preds = %28
  tail call void @do_check_inuse_chunk(ptr noundef nonnull %0, ptr noundef %23)
  br label %31

31:                                               ; preds = %20, %30, %28, %6, %3
  %.2 = phi ptr [ null, %6 ], [ null, %3 ], [ null, %20 ], [ %1, %30 ], [ null, %28 ]
  ret ptr %.2
}

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 = { 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}


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