; 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

; Function Attrs: nounwind uwtable
declare hidden i32 @init_mparams() #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 ptr @create_mspace(i64 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store ptr null, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load i64, ptr @mparams, align 8, !tbaa !13
  %11 = icmp ne i64 %10, 0
  br i1 %11, label %15, label %12

12:                                               ; preds = %2
  %13 = call i32 @init_mparams()
  %14 = icmp ne i32 %13, 0
  br label %15

15:                                               ; preds = %12, %2
  %16 = phi i1 [ true, %2 ], [ %14, %12 ]
  %17 = zext i1 %16 to i32
  store i64 944, ptr %6, align 8, !tbaa !5
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = load i64, ptr %6, align 8, !tbaa !5
  %20 = add i64 %19, 80
  %21 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 1), align 8, !tbaa !15
  %22 = add i64 %20, %21
  %23 = sub i64 0, %22
  %24 = icmp ult i64 %18, %23
  br i1 %24, label %25, label %73

25:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %25
  %29 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !16
  br label %35

30:                                               ; preds = %25
  %31 = load i64, ptr %3, align 8, !tbaa !5
  %32 = add i64 %31, 80
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = add i64 %32, %33
  br label %35

35:                                               ; preds = %30, %28
  %36 = phi i64 [ %29, %28 ], [ %34, %30 ]
  store i64 %36, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %37 = load i64, ptr %7, align 8, !tbaa !5
  %38 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !16
  %39 = sub i64 %38, 1
  %40 = add i64 %37, %39
  %41 = load i64, ptr getelementptr inbounds (%struct.malloc_params, ptr @mparams, i32 0, i32 2), align 8, !tbaa !16
  %42 = sub i64 %41, 1
  %43 = xor i64 %42, -1
  %44 = and i64 %40, %43
  store i64 %44, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %45 = load i64, ptr %8, align 8, !tbaa !5
  %46 = call ptr @mmap(ptr noundef null, i64 noundef %45, i32 noundef 3, i32 noundef 34, i32 noundef -1, i64 noundef 0) #3
  store ptr %46, ptr %9, align 8, !tbaa !11
  %47 = load ptr, ptr %9, align 8, !tbaa !11
  %48 = icmp ne ptr %47, inttoptr (i64 -1 to ptr)
  br i1 %48, label %49, label %72

49:                                               ; preds = %35
  %50 = load ptr, ptr %9, align 8, !tbaa !11
  %51 = load i64, ptr %8, align 8, !tbaa !5
  %52 = call ptr @init_user_mstate(ptr noundef %50, i64 noundef %51)
  store ptr %52, ptr %5, align 8, !tbaa !11
  %53 = load ptr, ptr %5, align 8, !tbaa !11
  %54 = getelementptr inbounds %struct.malloc_state, ptr %53, i32 0, i32 17
  %55 = getelementptr inbounds %struct.malloc_segment, ptr %54, i32 0, i32 3
  store i32 1, ptr %55, align 8, !tbaa !17
  %56 = load i32, ptr %4, align 4, !tbaa !9
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %63

58:                                               ; preds = %49
  %59 = load ptr, ptr %5, align 8, !tbaa !11
  %60 = getelementptr inbounds %struct.malloc_state, ptr %59, i32 0, i32 15
  %61 = load i32, ptr %60, align 8, !tbaa !20
  %62 = or i32 %61, 2
  br label %68

63:                                               ; preds = %49
  %64 = load ptr, ptr %5, align 8, !tbaa !11
  %65 = getelementptr inbounds %struct.malloc_state, ptr %64, i32 0, i32 15
  %66 = load i32, ptr %65, align 8, !tbaa !20
  %67 = and i32 %66, -3
  br label %68

68:                                               ; preds = %63, %58
  %69 = phi i32 [ %62, %58 ], [ %67, %63 ]
  %70 = load ptr, ptr %5, align 8, !tbaa !11
  %71 = getelementptr inbounds %struct.malloc_state, ptr %70, i32 0, i32 15
  store i32 %69, ptr %71, align 8, !tbaa !20
  br label %72

72:                                               ; preds = %68, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %73

73:                                               ; preds = %72, %15
  %74 = load ptr, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %74
}

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @init_user_mstate(ptr noundef, i64 noundef) #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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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 }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"malloc_params", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !6, i64 16}
!17 = !{!18, !10, i64 912}
!18 = !{!"malloc_state", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16, !12, i64 24, !12, i64 32, !12, 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, !10, i64 880, !10, i64 884, !19, i64 888, !12, i64 920, !6, i64 928}
!19 = !{!"malloc_segment", !12, i64 0, !6, i64 8, !12, i64 16, !10, i64 24}
!20 = !{!18, !10, i64 880}


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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @init_mparams() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @create_mspace(i64 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @mparams, align 8, !tbaa !5
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call i32 @init_mparams()
  br label %6

6:                                                ; preds = %4, %2
  %7 = load i64, ptr getelementptr inbounds (i8, ptr @mparams, i64 8), align 8, !tbaa !11
  %8 = sub i64 -1024, %7
  %9 = icmp ugt i64 %8, %0
  br i1 %9, label %10, label %28

10:                                               ; preds = %6
  %11 = icmp eq i64 %0, 0
  %12 = load i64, ptr getelementptr inbounds (i8, ptr @mparams, i64 16), align 8
  %13 = add i64 %0, 1024
  %14 = select i1 %11, i64 %12, i64 %13
  %15 = add i64 %12, -1
  %16 = add i64 %15, %14
  %17 = sub i64 0, %12
  %18 = and i64 %16, %17
  %19 = tail call ptr @mmap(ptr noundef null, i64 noundef %18, i32 noundef 3, i32 noundef 34, i32 noundef -1, i64 noundef 0) #2
  %.not17 = icmp eq ptr %19, inttoptr (i64 -1 to ptr)
  br i1 %.not17, label %28, label %20

20:                                               ; preds = %10
  %21 = tail call ptr @init_user_mstate(ptr noundef %19, i64 noundef %18)
  %22 = getelementptr inbounds i8, ptr %21, i64 912
  store i32 1, ptr %22, align 8, !tbaa !12
  %.not18 = icmp eq i32 %1, 0
  %23 = getelementptr inbounds i8, ptr %21, i64 880
  %24 = load i32, ptr %23, align 8, !tbaa !16
  %25 = and i32 %24, -3
  %masksel = select i1 %.not18, i32 0, i32 2
  %26 = or disjoint i32 %25, %masksel
  %27 = getelementptr inbounds i8, ptr %21, i64 880
  store i32 %26, ptr %27, align 8, !tbaa !16
  br label %28

28:                                               ; preds = %10, %20, %6
  %.1 = phi ptr [ null, %6 ], [ %21, %20 ], [ null, %10 ]
  ret ptr %.1
}

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @init_user_mstate(ptr noundef, i64 noundef) local_unnamed_addr #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" }
attributes #1 = { nounwind "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, !7, i64 0}
!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 = !{!6, !7, i64 8}
!12 = !{!13, !10, i64 912}
!13 = !{!"malloc_state", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 16, !14, i64 24, !14, i64 32, !14, 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, !15, i64 888, !14, i64 920, !7, i64 928}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!"malloc_segment", !14, i64 0, !7, i64 8, !14, i64 16, !10, i64 24}
!16 = !{!13, !10, i64 880}


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