; LLVM IR from stan-dev/math :: lib/boost_1.84.0/libs/container/src (monotonic_buffer_resource)

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

%"class.boost::container::pmr::monotonic_buffer_resource" = type { %"class.boost::container::pmr::memory_resource", %"class.boost::container::pmr::block_slist", ptr, i64, i64, ptr, i64 }
%"class.boost::container::pmr::memory_resource" = type { ptr }
%"class.boost::container::pmr::block_slist" = type { %"class.boost::container::pmr::block_slist_base", ptr }
%"class.boost::container::pmr::block_slist_base" = type { %"struct.boost::container::pmr::slist_node" }
%"struct.boost::container::pmr::slist_node" = type { ptr }

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZNK5boost9container3pmr25monotonic_buffer_resource17remaining_storageEmRm(ptr noundef nonnull align 8 dereferenceable(64) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(8) %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = sub i64 %12, 1
  store i64 %13, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = xor i64 %14, -1
  store i64 %15, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %16 = getelementptr inbounds %"class.boost::container::pmr::monotonic_buffer_resource", ptr %11, i32 0, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = ptrtoint ptr %17 to i64
  store i64 %18, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %19 = load i64, ptr %9, align 8, !tbaa !9
  %20 = load i64, ptr %7, align 8, !tbaa !9
  %21 = add i64 %19, %20
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = and i64 %21, %22
  store i64 %23, ptr %10, align 8, !tbaa !9
  %24 = load i64, ptr %10, align 8, !tbaa !9
  %25 = load i64, ptr %9, align 8, !tbaa !9
  %26 = sub i64 %24, %25
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %26, ptr %27, align 8, !tbaa !9
  %28 = getelementptr inbounds %"class.boost::container::pmr::monotonic_buffer_resource", ptr %11, i32 0, i32 3
  %29 = load i64, ptr %28, align 8, !tbaa !17
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i64, ptr %30, align 8, !tbaa !9
  %32 = icmp ule i64 %29, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %3
  br label %40

34:                                               ; preds = %3
  %35 = getelementptr inbounds %"class.boost::container::pmr::monotonic_buffer_resource", ptr %11, i32 0, i32 3
  %36 = load i64, ptr %35, align 8, !tbaa !17
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = sub i64 %36, %38
  br label %40

40:                                               ; preds = %34, %33
  %41 = phi i64 [ 0, %33 ], [ %39, %34 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %41
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"_ZTSN5boost9container3pmr25monotonic_buffer_resourceE", !13, i64 0, !14, i64 8, !6, i64 24, !10, i64 32, !10, i64 40, !6, i64 48, !10, i64 56}
!13 = !{!"_ZTSN5boost9container3pmr15memory_resourceE"}
!14 = !{!"_ZTSN5boost9container3pmr11block_slistE", !15, i64 0, !6, i64 8}
!15 = !{!"_ZTSN5boost9container3pmr16block_slist_baseINS1_18block_slist_headerEEE", !16, i64 0}
!16 = !{!"_ZTSN5boost9container3pmr10slist_nodeE", !6, i64 0}
!17 = !{!12, !10, i64 32}


; 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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local noundef i64 @_ZNK5boost9container3pmr25monotonic_buffer_resource17remaining_storageEmRm(ptr nocapture noundef nonnull readonly align 8 dereferenceable(64) %0, i64 noundef %1, ptr nocapture noundef nonnull writeonly align 8 dereferenceable(8) %2) local_unnamed_addr #0 align 2 {
  %4 = add i64 %1, -1
  %5 = sub i64 0, %1
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = ptrtoint ptr %7 to i64
  %9 = add i64 %4, %8
  %10 = and i64 %9, %5
  %11 = sub i64 %10, %8
  store i64 %11, ptr %2, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %0, i64 32
  %13 = load i64, ptr %12, align 8, !tbaa !16
  %spec.select = tail call i64 @llvm.usub.sat.i64(i64 %13, i64 %11)
  ret i64 %spec.select
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.usub.sat.i64(i64, i64) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 speculatable 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, !11, i64 24}
!6 = !{!"_ZTSN5boost9container3pmr25monotonic_buffer_resourceE", !7, i64 0, !8, i64 8, !11, i64 24, !14, i64 32, !14, i64 40, !11, i64 48, !14, i64 56}
!7 = !{!"_ZTSN5boost9container3pmr15memory_resourceE"}
!8 = !{!"_ZTSN5boost9container3pmr11block_slistE", !9, i64 0, !11, i64 8}
!9 = !{!"_ZTSN5boost9container3pmr16block_slist_baseINS1_18block_slist_headerEEE", !10, i64 0}
!10 = !{!"_ZTSN5boost9container3pmr10slist_nodeE", !11, i64 0}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"long", !12, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!6, !14, i64 32}


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