; LLVM IR from Water-Melon/Melon :: src (mln_alloc)

; 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.mln_alloc_s = type { ptr, i64, i64, ptr, ptr, ptr, ptr, [35 x %struct.mln_alloc_mgr_s], ptr, ptr, ptr, ptr }
%struct.mln_alloc_mgr_s = type { i64, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.mln_alloc_shm_s = type { ptr, ptr, ptr, ptr, i64, i32, i32, [4096 x i8] }

; 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 @mln_alloc_available_capacity(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.mln_alloc_s, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %28

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mln_alloc_s, ptr %13, i32 0, i32 1
  %15 = load i64, ptr %14, align 8, !tbaa !12
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %17, label %25

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mln_alloc_s, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !12
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mln_alloc_s, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !13
  %24 = sub i64 %20, %23
  br label %26

25:                                               ; preds = %12
  br label %26

26:                                               ; preds = %25, %17
  %27 = phi i64 [ %24, %17 ], [ -1, %25 ]
  store i64 %27, ptr %2, align 8
  br label %65

28:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mln_alloc_s, ptr %29, i32 0, i32 10
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  store ptr %31, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !15
  br label %32

32:                                               ; preds = %56, %28
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %35, label %60

35:                                               ; preds = %32
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mln_alloc_shm_s, ptr %36, i32 0, i32 7
  %38 = getelementptr inbounds [4096 x i8], ptr %37, i64 0, i64 0
  store ptr %38, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i64 4096
  store ptr %40, ptr %6, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %52, %35
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = icmp ult ptr %42, %43
  br i1 %44, label %45, label %55

45:                                               ; preds = %41
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load i8, ptr %46, align 1, !tbaa !16
  %48 = call zeroext i8 @mln_alloc_shm_count_bits(i8 noundef zeroext %47)
  %49 = zext i8 %48 to i64
  %50 = load i64, ptr %7, align 8, !tbaa !15
  %51 = add i64 %50, %49
  store i64 %51, ptr %7, align 8, !tbaa !15
  br label %52

52:                                               ; preds = %45
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i32 1
  store ptr %54, ptr %5, align 8, !tbaa !5
  br label %41, !llvm.loop !17

55:                                               ; preds = %41
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.mln_alloc_shm_s, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !19
  store ptr %59, ptr %4, align 8, !tbaa !5
  br label %32, !llvm.loop !22

60:                                               ; preds = %32
  %61 = load i64, ptr %7, align 8, !tbaa !15
  %62 = sub i64 32768, %61
  store i64 %62, ptr %7, align 8, !tbaa !15
  %63 = load i64, ptr %7, align 8, !tbaa !15
  %64 = mul i64 %63, 64
  store i64 %64, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %65

65:                                               ; preds = %60, %26
  %66 = load i64, ptr %2, align 8
  ret i64 %66
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i8 @mln_alloc_shm_count_bits(i8 noundef zeroext) #2

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 = { inlinehint 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"mln_alloc_s", !6, i64 0, !11, i64 8, !11, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 2016, !6, i64 2024, !6, i64 2032, !6, i64 2040}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}
!14 = !{!10, !6, i64 2032}
!15 = !{!11, !11, i64 0}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!20, !6, i64 8}
!20 = !{!"mln_alloc_shm_s", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !21, i64 40, !21, i64 44, !21, i64 47, !7, i64 48}
!21 = !{!"int", !7, i64 0}
!22 = distinct !{!22, !18}


; 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
define dso_local i64 @mln_alloc_available_capacity(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %3 = icmp eq ptr %2, null
  br i1 %3, label %4, label %11

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %.not21 = icmp eq i64 %6, 0
  br i1 %.not21, label %._crit_edge, label %7

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %10 = sub i64 %6, %9
  br label %._crit_edge

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %0, i64 2032
  %.01824 = load ptr, ptr %12, align 8, !tbaa !13
  %.not25 = icmp eq ptr %.01824, null
  br i1 %.not25, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %11, %18
  %.01827 = phi ptr [ %.018, %18 ], [ %.01824, %11 ]
  %.026 = phi i64 [ %17, %18 ], [ 0, %11 ]
  br label %13

13:                                               ; preds = %.preheader, %13
  %.123 = phi i64 [ %.026, %.preheader ], [ %17, %13 ]
  %.017.idx22 = phi i64 [ 48, %.preheader ], [ %.017.add, %13 ]
  %.017.ptr = getelementptr inbounds i8, ptr %.01827, i64 %.017.idx22
  %14 = load i8, ptr %.017.ptr, align 1, !tbaa !14
  %15 = tail call zeroext i8 @mln_alloc_shm_count_bits(i8 noundef zeroext %14)
  %16 = zext i8 %15 to i64
  %17 = add i64 %.123, %16
  %.017.add = add nuw nsw i64 %.017.idx22, 1
  %exitcond.not = icmp eq i64 %.017.add, 4144
  br i1 %exitcond.not, label %18, label %13, !llvm.loop !15

18:                                               ; preds = %13
  %19 = getelementptr inbounds i8, ptr %.01827, i64 8
  %.018 = load ptr, ptr %19, align 8, !tbaa !13
  %.not = icmp eq ptr %.018, null
  br i1 %.not, label %._crit_edge.loopexit, label %.preheader, !llvm.loop !17

._crit_edge.loopexit:                             ; preds = %18
  %20 = shl i64 %17, 6
  %21 = sub i64 2097152, %20
  br label %._crit_edge

._crit_edge:                                      ; preds = %11, %._crit_edge.loopexit, %7, %4
  %.016 = phi i64 [ %10, %7 ], [ -1, %4 ], [ 2097152, %11 ], [ %21, %._crit_edge.loopexit ]
  ret i64 %.016
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i8 @mln_alloc_shm_count_bits(i8 noundef zeroext) local_unnamed_addr #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" }
attributes #1 = { inlinehint 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 0}
!6 = !{!"mln_alloc_s", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !8, i64 56, !7, i64 2016, !7, i64 2024, !7, i64 2032, !7, i64 2040}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!7, !7, i64 0}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass