; LLVM IR from narke/Aragveli :: src/kernel/memory (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.pool_t = type { i64, i64, i64, i64, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @pool_allocate(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.pool_t, ptr %5, i32 0, i32 3
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.pool_t, ptr %8, i32 0, i32 0
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = icmp ult i64 %7, %10
  br i1 %11, label %12, label %28

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.pool_t, ptr %14, i32 0, i32 3
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = trunc i64 %16 to i32
  %18 = call ptr @address_from_index(ptr noundef %13, i32 noundef %17)
  store ptr %18, ptr %3, align 8, !tbaa !5
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.pool_t, ptr %19, i32 0, i32 3
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = add i64 %21, 1
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  store i64 %22, ptr %23, align 8, !tbaa !13
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.pool_t, ptr %24, i32 0, i32 3
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = add i64 %26, 1
  store i64 %27, ptr %25, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %28

28:                                               ; preds = %12, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store ptr null, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.pool_t, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !14
  %32 = icmp ugt i64 %31, 0
  br i1 %32, label %33, label %59

33:                                               ; preds = %28
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.pool_t, ptr %34, i32 0, i32 5
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  store ptr %36, ptr %4, align 8, !tbaa !5
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.pool_t, ptr %37, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !14
  %40 = add i64 %39, -1
  store i64 %40, ptr %38, align 8, !tbaa !14
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.pool_t, ptr %41, i32 0, i32 2
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %55

45:                                               ; preds = %33
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.pool_t, ptr %47, i32 0, i32 5
  %49 = load ptr, ptr %48, align 8, !tbaa !15
  %50 = load i64, ptr %49, align 8, !tbaa !13
  %51 = trunc i64 %50 to i32
  %52 = call ptr @address_from_index(ptr noundef %46, i32 noundef %51)
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.pool_t, ptr %53, i32 0, i32 5
  store ptr %52, ptr %54, align 8, !tbaa !15
  br label %58

55:                                               ; preds = %33
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.pool_t, ptr %56, i32 0, i32 5
  store ptr null, ptr %57, align 8, !tbaa !15
  br label %58

58:                                               ; preds = %55, %45
  br label %59

59:                                               ; preds = %58, %28
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  ret ptr %60
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @address_from_index(ptr noundef, i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 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, !11, i64 24}
!10 = !{!"", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 16}
!15 = !{!10, !6, i64 40}


; 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 ptr @pool_allocate(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = load i64, ptr %0, align 8, !tbaa !11
  %5 = icmp ult i64 %3, %4
  br i1 %5, label %6, label %13

6:                                                ; preds = %1
  %7 = trunc i64 %3 to i32
  %8 = tail call ptr @address_from_index(ptr noundef nonnull %0, i32 noundef %7)
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = add i64 %9, 1
  store i64 %10, ptr %8, align 8, !tbaa !12
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = add i64 %11, 1
  store i64 %12, ptr %2, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %6, %1
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %.not = icmp eq i64 %15, 0
  br i1 %.not, label %24, label %16

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %0, i64 40
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = add i64 %15, -1
  store i64 %19, ptr %14, align 8, !tbaa !13
  %.not18 = icmp eq i64 %19, 0
  br i1 %.not18, label %.sink.split, label %20

20:                                               ; preds = %16
  %21 = load i64, ptr %18, align 8, !tbaa !12
  %22 = trunc i64 %21 to i32
  %23 = tail call ptr @address_from_index(ptr noundef nonnull %0, i32 noundef %22)
  br label %.sink.split

.sink.split:                                      ; preds = %16, %20
  %.sink = phi ptr [ %23, %20 ], [ null, %16 ]
  store ptr %.sink, ptr %17, align 8, !tbaa !14
  br label %24

24:                                               ; preds = %.sink.split, %13
  %.0 = phi ptr [ null, %13 ], [ %18, %.sink.split ]
  ret ptr %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @address_from_index(ptr noundef, i32 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" }

!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 24}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !10, i64 40}


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