; LLVM IR from hydrabus/hydrafw :: src/common (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.pool = type { ptr, i32, i8, i8, [128 x i8] }

@ram_pool = external hidden global %struct.pool, 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 ptr @pool_alloc_blocks(i8 noundef zeroext %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store i8 %0, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  %8 = load i8, ptr %3, align 1, !tbaa !5
  %9 = zext i8 %8 to i32
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %91

12:                                               ; preds = %1
  %13 = load i8, ptr %3, align 1, !tbaa !5
  %14 = zext i8 %13 to i32
  %15 = icmp sgt i32 %14, 128
  br i1 %15, label %16, label %17

16:                                               ; preds = %12
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %91

17:                                               ; preds = %12
  store i8 1, ptr %6, align 1, !tbaa !5
  store i32 0, ptr %4, align 4, !tbaa !8
  br label %18

18:                                               ; preds = %87, %17
  %19 = load i32, ptr %4, align 4, !tbaa !8
  %20 = load i8, ptr %3, align 1, !tbaa !5
  %21 = zext i8 %20 to i32
  %22 = sub i32 128, %21
  %23 = icmp ule i32 %19, %22
  br i1 %23, label %24, label %90

24:                                               ; preds = %18
  %25 = load i32, ptr %4, align 4, !tbaa !8
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 4), i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = zext i8 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %86

31:                                               ; preds = %24
  store i32 1, ptr %5, align 4, !tbaa !8
  br label %32

32:                                               ; preds = %49, %31
  %33 = load i32, ptr %5, align 4, !tbaa !8
  %34 = load i8, ptr %3, align 1, !tbaa !5
  %35 = zext i8 %34 to i32
  %36 = icmp ult i32 %33, %35
  br i1 %36, label %37, label %52

37:                                               ; preds = %32
  %38 = load i32, ptr %4, align 4, !tbaa !8
  %39 = load i32, ptr %5, align 4, !tbaa !8
  %40 = add i32 %38, %39
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 4), i64 0, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !5
  %44 = zext i8 %43 to i32
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %47

46:                                               ; preds = %37
  store i8 0, ptr %6, align 1, !tbaa !5
  br label %52

47:                                               ; preds = %37
  store i8 1, ptr %6, align 1, !tbaa !5
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %5, align 4, !tbaa !8
  %51 = add i32 %50, 1
  store i32 %51, ptr %5, align 4, !tbaa !8
  br label %32, !llvm.loop !10

52:                                               ; preds = %46, %32
  %53 = load i8, ptr %6, align 1, !tbaa !5
  %54 = zext i8 %53 to i32
  %55 = icmp eq i32 %54, 1
  br i1 %55, label %56, label %85

56:                                               ; preds = %52
  store i32 0, ptr %5, align 4, !tbaa !8
  br label %57

57:                                               ; preds = %69, %56
  %58 = load i32, ptr %5, align 4, !tbaa !8
  %59 = load i8, ptr %3, align 1, !tbaa !5
  %60 = zext i8 %59 to i32
  %61 = icmp ult i32 %58, %60
  br i1 %61, label %62, label %72

62:                                               ; preds = %57
  %63 = load i8, ptr %3, align 1, !tbaa !5
  %64 = load i32, ptr %4, align 4, !tbaa !8
  %65 = load i32, ptr %5, align 4, !tbaa !8
  %66 = add i32 %64, %65
  %67 = zext i32 %66 to i64
  %68 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 4), i64 0, i64 %67
  store i8 %63, ptr %68, align 1, !tbaa !5
  br label %69

69:                                               ; preds = %62
  %70 = load i32, ptr %5, align 4, !tbaa !8
  %71 = add i32 %70, 1
  store i32 %71, ptr %5, align 4, !tbaa !8
  br label %57, !llvm.loop !12

72:                                               ; preds = %57
  %73 = load i8, ptr %3, align 1, !tbaa !5
  %74 = zext i8 %73 to i32
  %75 = load i8, ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 3), align 1, !tbaa !13
  %76 = zext i8 %75 to i32
  %77 = add nsw i32 %76, %74
  %78 = trunc i32 %77 to i8
  store i8 %78, ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 3), align 1, !tbaa !13
  %79 = load ptr, ptr @ram_pool, align 8, !tbaa !16
  %80 = load i32, ptr getelementptr inbounds (%struct.pool, ptr @ram_pool, i32 0, i32 1), align 8, !tbaa !17
  %81 = load i32, ptr %4, align 4, !tbaa !8
  %82 = mul i32 %80, %81
  %83 = zext i32 %82 to i64
  %84 = getelementptr inbounds i8, ptr %79, i64 %83
  store ptr %84, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %91

85:                                               ; preds = %52
  br label %86

86:                                               ; preds = %85, %24
  br label %87

87:                                               ; preds = %86
  %88 = load i32, ptr %4, align 4, !tbaa !8
  %89 = add i32 %88, 1
  store i32 %89, ptr %4, align 4, !tbaa !8
  br label %18, !llvm.loop !18

90:                                               ; preds = %18
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %91

91:                                               ; preds = %90, %72, %16, %11
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %92 = load ptr, ptr %2, align 8
  ret ptr %92
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = !{!14, !6, i64 13}
!14 = !{!"pool", !15, i64 0, !9, i64 8, !6, i64 12, !6, i64 13, !6, i64 14}
!15 = !{!"any pointer", !6, i64 0}
!16 = !{!14, !15, i64 0}
!17 = !{!14, !9, i64 8}
!18 = distinct !{!18, !11}


; 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.pool = type { ptr, i32, i8, i8, [128 x i8] }

@ram_pool = external hidden local_unnamed_addr global %struct.pool, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local ptr @pool_alloc_blocks(i8 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = add i8 %0, -1
  %or.cond = icmp slt i8 %2, 0
  br i1 %or.cond, label %.loopexit, label %.preheader28

.preheader28:                                     ; preds = %1
  %3 = zext i8 %0 to i64
  %4 = icmp ugt i8 %0, 1
  %5 = sub nsw i64 129, %3
  %wide.trip.count48 = and i64 %5, 4294967295
  br i1 %4, label %.preheader28.split.us.preheader, label %.preheader28.split

.preheader28.split.us.preheader:                  ; preds = %.preheader28
  %wide.trip.count43 = zext i8 %0 to i64
  br label %.preheader28.split.us

.preheader28.split.us:                            ; preds = %.preheader28.split.us.preheader, %.thread.us
  %indvars.iv45 = phi i64 [ 0, %.preheader28.split.us.preheader ], [ %indvars.iv.next46, %.thread.us ]
  %6 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %indvars.iv45
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = icmp eq i8 %7, 0
  br i1 %8, label %.preheader27.us, label %.thread.us

9:                                                ; preds = %.preheader27.us
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %exitcond44.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count43
  br i1 %exitcond44.not, label %.preheader, label %.preheader27.us, !llvm.loop !8

.preheader27.us:                                  ; preds = %.preheader28.split.us, %9
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %9 ], [ 1, %.preheader28.split.us ]
  %10 = add nuw nsw i64 %indvars.iv40, %indvars.iv45
  %11 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %.not25.us = icmp eq i8 %12, 0
  br i1 %.not25.us, label %9, label %.thread.us

.thread.us:                                       ; preds = %.preheader27.us, %.preheader28.split.us
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %exitcond49.not = icmp eq i64 %indvars.iv.next46, %wide.trip.count48
  br i1 %exitcond49.not, label %.loopexit, label %.preheader28.split.us, !llvm.loop !10

.preheader28.split:                               ; preds = %.preheader28, %.thread.3
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.thread.3 ], [ 0, %.preheader28 ]
  %13 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %indvars.iv
  %14 = load i8, ptr %13, align 2, !tbaa !5
  %15 = icmp eq i8 %14, 0
  br i1 %15, label %.preheader, label %.thread

.preheader:                                       ; preds = %.preheader28.split, %.thread, %.thread.1, %.thread.2, %9
  %.us-phi.in = phi i64 [ %indvars.iv45, %9 ], [ %indvars.iv, %.preheader28.split ], [ %indvars.iv.next, %.thread ], [ %indvars.iv.next.1, %.thread.1 ], [ %indvars.iv.next.2, %.thread.2 ]
  %.us-phi = trunc i64 %.us-phi.in to i32
  %.not = icmp eq i8 %0, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %16 = and i64 %.us-phi.in, 4294967295
  %17 = getelementptr i8, ptr @ram_pool, i64 %16
  %scevgep = getelementptr i8, ptr %17, i64 14
  %18 = zext i8 %0 to i64
  tail call void @llvm.memset.p0.i64(ptr align 1 %scevgep, i8 %0, i64 %18, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %.preheader
  %19 = load i8, ptr getelementptr inbounds (i8, ptr @ram_pool, i64 13), align 1, !tbaa !11
  %20 = add i8 %19, %0
  store i8 %20, ptr getelementptr inbounds (i8, ptr @ram_pool, i64 13), align 1, !tbaa !11
  %21 = load ptr, ptr @ram_pool, align 8, !tbaa !15
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @ram_pool, i64 8), align 8, !tbaa !16
  %23 = mul i32 %22, %.us-phi
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %21, i64 %24
  br label %.loopexit

.thread:                                          ; preds = %.preheader28.split
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %26 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %indvars.iv.next
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = icmp eq i8 %27, 0
  br i1 %28, label %.preheader, label %.thread.1

.thread.1:                                        ; preds = %.thread
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %29 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %indvars.iv.next.1
  %30 = load i8, ptr %29, align 4, !tbaa !5
  %31 = icmp eq i8 %30, 0
  br i1 %31, label %.preheader, label %.thread.2

.thread.2:                                        ; preds = %.thread.1
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %32 = getelementptr inbounds [128 x i8], ptr getelementptr inbounds (i8, ptr @ram_pool, i64 14), i64 0, i64 %indvars.iv.next.2
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %34 = icmp eq i8 %33, 0
  br i1 %34, label %.preheader, label %.thread.3

.thread.3:                                        ; preds = %.thread.2
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count48
  br i1 %exitcond.not.3, label %.loopexit, label %.preheader28.split, !llvm.loop !10

.loopexit:                                        ; preds = %.thread.3, %.thread.us, %1, %._crit_edge
  %.023 = phi ptr [ %25, %._crit_edge ], [ null, %1 ], [ null, %.thread.us ], [ null, %.thread.3 ]
  ret ptr %.023
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}
!11 = !{!12, !6, i64 13}
!12 = !{!"pool", !13, i64 0, !14, i64 8, !6, i64 12, !6, i64 13, !6, i64 14}
!13 = !{!"any pointer", !6, i64 0}
!14 = !{!"int", !6, i64 0}
!15 = !{!12, !13, i64 0}
!16 = !{!12, !14, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass