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

13:                                               ; preds = %2
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.malloc_state, ptr %14, i32 0, i32 16
  %16 = atomicrmw xchg ptr %15, i32 1 seq_cst, align 4
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %23

18:                                               ; preds = %13
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.malloc_state, ptr %19, i32 0, i32 16
  %21 = call i32 @spin_acquire_lock(ptr noundef %20)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %60, label %25

23:                                               ; preds = %13
  br i1 false, label %60, label %25

24:                                               ; preds = %2
  br i1 false, label %60, label %25

25:                                               ; preds = %24, %23, %18
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.malloc_state, ptr %26, i32 0, i32 15
  %28 = load i32, ptr %27, align 8, !tbaa !11
  %29 = zext i32 %28 to i64
  %30 = and i64 %29, 1
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %25
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %32, %25
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %43, label %36

36:                                               ; preds = %33
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.malloc_state, ptr %37, i32 0, i32 15
  %39 = load i32, ptr %38, align 8, !tbaa !11
  %40 = zext i32 %39 to i64
  %41 = or i64 %40, 1
  %42 = trunc i64 %41 to i32
  store i32 %42, ptr %38, align 8, !tbaa !11
  br label %50

43:                                               ; preds = %33
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.malloc_state, ptr %44, i32 0, i32 15
  %46 = load i32, ptr %45, align 8, !tbaa !11
  %47 = zext i32 %46 to i64
  %48 = and i64 %47, -2
  %49 = trunc i64 %48 to i32
  store i32 %49, ptr %45, align 8, !tbaa !11
  br label %50

50:                                               ; preds = %43, %36
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.malloc_state, ptr %51, i32 0, i32 15
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = and i32 %53, 2
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %56, label %59

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

59:                                               ; preds = %56, %50
  br label %60

60:                                               ; preds = %59, %24, %23, %18
  %61 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %61
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 880}
!12 = !{!"malloc_state", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !7, i64 72, !7, i64 600, !13, i64 856, !13, i64 864, !13, i64 872, !10, i64 880, !10, i64 884, !14, i64 888, !6, i64 920, !13, i64 928}
!13 = !{!"long", !7, i64 0}
!14 = !{!"malloc_segment", !6, i64 0, !13, i64 8, !6, i64 16, !10, 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
define dso_local range(i32 0, 2) i32 @mspace_track_large_chunks(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 880
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = and i32 %4, 2
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %11, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 884
  %8 = atomicrmw xchg ptr %7, i32 1 seq_cst, align 4
  %.not10 = icmp eq i32 %8, 0
  br i1 %.not10, label %11, label %9

9:                                                ; preds = %6
  %10 = tail call i32 @spin_acquire_lock(ptr noundef nonnull %7)
  %.not11 = icmp eq i32 %10, 0
  br i1 %.not11, label %11, label %18

11:                                               ; preds = %2, %6, %9
  %12 = load i32, ptr %3, align 8, !tbaa !5
  %13 = and i32 %12, 1
  %spec.select = xor i32 %13, 1
  %.not13 = icmp eq i32 %1, 0
  %14 = and i32 %12, -2
  %masksel = zext i1 %.not13 to i32
  %storemerge = or disjoint i32 %14, %masksel
  store i32 %storemerge, ptr %3, align 8, !tbaa !5
  %15 = and i32 %12, 2
  %.not14 = icmp eq i32 %15, 0
  br i1 %.not14, label %18, label %16

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 884
  store atomic i32 0, ptr %17 release, align 4
  br label %18

18:                                               ; preds = %11, %16, %9
  %.1 = phi i32 [ 0, %9 ], [ %spec.select, %16 ], [ %spec.select, %11 ]
  ret i32 %.1
}

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}


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