; 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_params = type { i64, i64, i64, i64, i64, i32 }
%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 }

@mparams = external hidden global %struct.malloc_params, align 8
@_gm_ = external hidden global %struct.malloc_state, align 8

; 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: nounwind uwtable
define dso_local i64 @dlmalloc_set_footprint_limit(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %4 = load i64, ptr %2, align 8, !tbaa !5
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %6, label %14

6:                                                ; preds = %1
  %7 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !9
  %8 = sub i64 %7, 1
  %9 = add i64 1, %8
  %10 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !9
  %11 = sub i64 %10, 1
  %12 = xor i64 %11, -1
  %13 = and i64 %9, %12
  store i64 %13, ptr %3, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %6, %1
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = icmp eq i64 %15, -1
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %27

18:                                               ; preds = %14
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !9
  %21 = sub i64 %20, 1
  %22 = add i64 %19, %21
  %23 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !9
  %24 = sub i64 %23, 1
  %25 = xor i64 %24, -1
  %26 = and i64 %22, %25
  store i64 %26, ptr %3, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %18, %17
  %28 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %28, ptr getelementptr inbounds (%struct.malloc_state, ptr @_gm_, i32 0, i32 14), align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 %28
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"malloc_params", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 872}
!13 = !{!"malloc_state", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !7, i64 72, !7, i64 600, !6, i64 856, !6, i64 864, !6, i64 872, !11, i64 880, !11, i64 884, !15, i64 888, !14, i64 920, !6, i64 928}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!"malloc_segment", !14, i64 0, !6, i64 8, !14, i64 16, !11, 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_params = type { i64, i64, i64, i64, i64, i32 }
%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 }

@mparams = external hidden local_unnamed_addr global %struct.malloc_params, align 8
@_gm_ = external hidden local_unnamed_addr global %struct.malloc_state, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local i64 @dlmalloc_set_footprint_limit(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i64 %0, -1
  br i1 %2, label %9, label %3

3:                                                ; preds = %1
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @mparams, i64 16), align 8, !tbaa !5
  %5 = add i64 %0, -1
  %6 = add i64 %5, %4
  %7 = sub i64 0, %4
  %8 = and i64 %6, %7
  br label %9

9:                                                ; preds = %1, %3
  %.0 = phi i64 [ %8, %3 ], [ 0, %1 ]
  store i64 %.0, ptr getelementptr inbounds (i8, ptr @_gm_, i64 872), align 8, !tbaa !11
  ret i64 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) 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 16}
!6 = !{!"malloc_params", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 872}
!12 = !{!"malloc_state", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !8, i64 72, !8, i64 600, !7, i64 856, !7, i64 864, !7, i64 872, !10, i64 880, !10, i64 884, !14, i64 888, !13, i64 920, !7, i64 928}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!"malloc_segment", !13, i64 0, !7, i64 8, !13, i64 16, !10, i64 24}


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