; 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 i32 @sys_trim(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mspace_trim(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = 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 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !11
  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 !13
  %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 %38, label %25

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

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

25:                                               ; preds = %24, %23, %18
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load i64, ptr %4, align 8, !tbaa !9
  %28 = call i32 @sys_trim(ptr noundef %26, i64 noundef %27)
  store i32 %28, ptr %5, align 4, !tbaa !11
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.malloc_state, ptr %29, i32 0, i32 15
  %31 = load i32, ptr %30, align 8, !tbaa !13
  %32 = and i32 %31, 2
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %25
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.malloc_state, ptr %35, i32 0, i32 16
  store atomic i32 0, ptr %36 release, align 4
  br label %37

37:                                               ; preds = %34, %25
  br label %38

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

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, !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 i32 @sys_trim(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mspace_trim(ptr noundef %0, i64 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
  %.not8 = icmp eq i32 %8, 0
  br i1 %.not8, label %11, label %9

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

11:                                               ; preds = %2, %6, %9
  %12 = tail call i32 @sys_trim(ptr noundef nonnull %0, i64 noundef %1)
  %13 = load i32, ptr %3, align 8, !tbaa !5
  %14 = and i32 %13, 2
  %.not10 = icmp eq i32 %14, 0
  br i1 %.not10, label %17, label %15

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

17:                                               ; preds = %11, %15, %9
  %.0 = phi i32 [ 0, %9 ], [ %12, %15 ], [ %12, %11 ]
  ret i32 %.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}


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