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

@_gm_ = external hidden global %struct.malloc_state, 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 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 @dlrealloc_in_place(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
  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) #3
  store ptr null, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %71

12:                                               ; preds = %2
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp uge i64 %13, -128
  br i1 %14, label %15, label %17

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

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

20:                                               ; preds = %17
  br label %26

21:                                               ; preds = %17
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = add i64 %22, 8
  %24 = add i64 %23, 15
  %25 = and i64 %24, -16
  br label %26

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

35:                                               ; preds = %26
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.malloc_state, ptr %36, i32 0, i32 16
  %38 = atomicrmw xchg ptr %37, i32 1 seq_cst, align 4
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %45

40:                                               ; preds = %35
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.malloc_state, ptr %41, i32 0, i32 16
  %43 = call i32 @spin_acquire_lock(ptr noundef %42)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %69, label %47

45:                                               ; preds = %35
  br i1 false, label %69, label %47

46:                                               ; preds = %26
  br i1 false, label %69, label %47

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

57:                                               ; preds = %47
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.malloc_state, ptr %58, i32 0, i32 16
  store atomic i32 0, ptr %59 release, align 4
  br label %60

60:                                               ; preds = %57, %47
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = icmp eq ptr %61, %62
  br i1 %63, label %64, label %68

64:                                               ; preds = %60
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  call void @do_check_inuse_chunk(ptr noundef %65, ptr noundef %66)
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %67, ptr %5, align 8, !tbaa !5
  br label %68

68:                                               ; preds = %64, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %69

69:                                               ; preds = %68, %46, %45, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %70

70:                                               ; preds = %69, %15
  br label %71

71:                                               ; preds = %70, %2
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %72
}

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"

%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 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 @dlrealloc_in_place(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %27, label %3

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

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

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

15:                                               ; preds = %7
  %16 = atomicrmw xchg ptr getelementptr inbounds (i8, ptr @_gm_, i64 884), i32 1 seq_cst, align 4
  %.not22 = icmp eq i32 %16, 0
  br i1 %.not22, label %19, label %17

17:                                               ; preds = %15
  %18 = tail call i32 @spin_acquire_lock(ptr noundef nonnull getelementptr inbounds (i8, ptr @_gm_, i64 884))
  %.not23 = icmp eq i32 %18, 0
  br i1 %.not23, label %19, label %27

19:                                               ; preds = %7, %15, %17
  %20 = tail call ptr @try_realloc_chunk(ptr noundef nonnull @_gm_, ptr noundef nonnull %12, i64 noundef %11, i32 noundef 0)
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @_gm_, i64 880), align 8, !tbaa !9
  %22 = and i32 %21, 2
  %.not24 = icmp eq i32 %22, 0
  br i1 %.not24, label %24, label %23

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

24:                                               ; preds = %23, %19
  %25 = icmp eq ptr %20, %12
  br i1 %25, label %26, label %27

26:                                               ; preds = %24
  tail call void @do_check_inuse_chunk(ptr noundef nonnull @_gm_, ptr noundef %20)
  br label %27

27:                                               ; preds = %17, %26, %24, %5, %2
  %.2 = phi ptr [ null, %5 ], [ null, %2 ], [ null, %17 ], [ %0, %26 ], [ null, %24 ]
  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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass