; LLVM IR from kala13x/libxutils :: src/sys (pool)

; 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.XPool = type { ptr, ptr, i64, i64, i8 }

; Function Attrs: nounwind uwtable
declare dso_local ptr @XPool_Create(i64 noundef) #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 @XPool_Alloc(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %78

12:                                               ; preds = %2
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store ptr null, ptr %3, align 8
  br label %78

16:                                               ; preds = %12
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.XPool, ptr %17, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = add i64 %19, %20
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.XPool, ptr %22, i32 0, i32 3
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp ugt i64 %21, %24
  br i1 %25, label %26, label %64

26:                                               ; preds = %16
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.XPool, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %58

31:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.XPool, ptr %33, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !13
  %36 = icmp ugt i64 %32, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %31
  %38 = load i64, ptr %5, align 8, !tbaa !9
  br label %43

39:                                               ; preds = %31
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.XPool, ptr %40, i32 0, i32 3
  %42 = load i64, ptr %41, align 8, !tbaa !13
  br label %43

43:                                               ; preds = %39, %37
  %44 = phi i64 [ %38, %37 ], [ %42, %39 ]
  store i64 %44, ptr %6, align 8, !tbaa !9
  %45 = load i64, ptr %6, align 8, !tbaa !9
  %46 = call ptr @XPool_Create(i64 noundef %45)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.XPool, ptr %47, i32 0, i32 0
  store ptr %46, ptr %48, align 8, !tbaa !14
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.XPool, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !14
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %54

53:                                               ; preds = %43
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %55

54:                                               ; preds = %43
  store i32 0, ptr %7, align 4
  br label %55

55:                                               ; preds = %54, %53
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %56 = load i32, ptr %7, align 4
  switch i32 %56, label %80 [
    i32 0, label %57
    i32 1, label %78
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57, %26
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.XPool, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  %62 = load i64, ptr %5, align 8, !tbaa !9
  %63 = call ptr @XPool_Alloc(ptr noundef %61, i64 noundef %62)
  store ptr %63, ptr %3, align 8
  br label %78

64:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.XPool, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !15
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.XPool, ptr %68, i32 0, i32 2
  %70 = load i64, ptr %69, align 8, !tbaa !11
  %71 = getelementptr inbounds i8, ptr %67, i64 %70
  store ptr %71, ptr %8, align 8, !tbaa !5
  %72 = load i64, ptr %5, align 8, !tbaa !9
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.XPool, ptr %73, i32 0, i32 2
  %75 = load i64, ptr %74, align 8, !tbaa !11
  %76 = add i64 %75, %72
  store i64 %76, ptr %74, align 8, !tbaa !11
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %77, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %78

78:                                               ; preds = %64, %58, %55, %15, %11
  %79 = load ptr, ptr %3, align 8
  ret ptr %79

80:                                               ; preds = %55
  unreachable
}

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 }

!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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"XPool", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !7, i64 32}
!13 = !{!12, !10, i64 24}
!14 = !{!12, !6, i64 0}
!15 = !{!12, !6, i64 8}


; 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
declare dso_local ptr @XPool_Create(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @XPool_Alloc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not25 = icmp eq i64 %1, 0
  %.not31 = icmp eq ptr %0, null
  %or.cond32 = or i1 %.not25, %.not31
  br i1 %or.cond32, label %.loopexit, label %.lr.ph.split.preheader

.lr.ph.split.preheader:                           ; preds = %2
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = add i64 %4, %1
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load i64, ptr %6, align 8, !tbaa !11
  %8 = icmp ugt i64 %5, %7
  br i1 %8, label %.lr.ph, label %.split

.lr.ph:                                           ; preds = %.lr.ph.split.preheader, %tailrecurse
  %9 = phi i64 [ %19, %tailrecurse ], [ %7, %.lr.ph.split.preheader ]
  %.tr3352 = phi ptr [ %14, %tailrecurse ], [ %0, %.lr.ph.split.preheader ]
  %10 = load ptr, ptr %.tr3352, align 8, !tbaa !12
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %tailrecurse

12:                                               ; preds = %.lr.ph
  %. = tail call i64 @llvm.umax.i64(i64 %9, i64 %1)
  %13 = tail call ptr @XPool_Create(i64 noundef %.)
  store ptr %13, ptr %.tr3352, align 8, !tbaa !12
  %.not26 = icmp eq ptr %13, null
  br i1 %.not26, label %.loopexit, label %tailrecurse

tailrecurse:                                      ; preds = %12, %.lr.ph
  %14 = phi ptr [ %13, %12 ], [ %10, %.lr.ph ]
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = add i64 %16, %1
  %18 = getelementptr inbounds i8, ptr %14, i64 24
  %19 = load i64, ptr %18, align 8, !tbaa !11
  %20 = icmp ugt i64 %17, %19
  br i1 %20, label %.lr.ph, label %.split

.split:                                           ; preds = %tailrecurse, %.lr.ph.split.preheader
  %.tr33.lcssa = phi ptr [ %0, %.lr.ph.split.preheader ], [ %14, %tailrecurse ]
  %.lcssa49 = phi i64 [ %4, %.lr.ph.split.preheader ], [ %16, %tailrecurse ]
  %.lcssa = phi i64 [ %5, %.lr.ph.split.preheader ], [ %17, %tailrecurse ]
  %21 = getelementptr inbounds i8, ptr %.tr33.lcssa, i64 16
  %22 = getelementptr inbounds i8, ptr %.tr33.lcssa, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %23, i64 %.lcssa49
  store i64 %.lcssa, ptr %21, align 8, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %12, %2, %.split
  %.1 = phi ptr [ %24, %.split ], [ null, %2 ], [ null, %12 ]
  ret ptr %.1
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #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 = { 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, !10, i64 16}
!6 = !{!"XPool", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !8, i64 32}
!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 24}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 8}


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