; LLVM IR from Stephane-D/SGDK :: src (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 = type { ptr, ptr, ptr, i16, i16 }

; 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
declare dso_local zeroext i16 @POOL_getNumAllocated(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local signext i16 @POOL_find(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Pool, ptr %9, i32 0, i32 2
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %11, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = call zeroext i16 @POOL_getNumAllocated(ptr noundef %12)
  store i16 %13, ptr %7, align 2, !tbaa !12
  br label %14

14:                                               ; preds = %40, %2
  %15 = load i16, ptr %7, align 2, !tbaa !12
  %16 = add i16 %15, -1
  store i16 %16, ptr %7, align 2, !tbaa !12
  %17 = icmp ne i16 %15, 0
  br i1 %17, label %18, label %43

18:                                               ; preds = %14
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = icmp eq ptr %20, %21
  br i1 %22, label %23, label %40

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Pool, ptr %24, i32 0, i32 4
  %26 = load i16, ptr %25, align 2, !tbaa !13
  %27 = zext i16 %26 to i32
  %28 = sub nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Pool, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = ptrtoint ptr %30 to i64
  %35 = ptrtoint ptr %33 to i64
  %36 = sub i64 %34, %35
  %37 = sdiv exact i64 %36, 8
  %38 = sub nsw i64 %29, %37
  %39 = trunc i64 %38 to i16
  store i16 %39, ptr %3, align 2
  store i32 1, ptr %8, align 4
  br label %44

40:                                               ; preds = %18
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds ptr, ptr %41, i32 1
  store ptr %42, ptr %6, align 8, !tbaa !5
  br label %14, !llvm.loop !15

43:                                               ; preds = %14
  store i16 -1, ptr %3, align 2
  store i32 1, ptr %8, align 4
  br label %44

44:                                               ; preds = %43, %23
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %45 = load i16, ptr %3, align 2
  ret i16 %45
}

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


; 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 zeroext i16 @POOL_getNumAllocated(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local signext i16 @POOL_find(ptr noundef %0, ptr noundef readnone %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = tail call zeroext i16 @POOL_getNumAllocated(ptr noundef %0)
  %.not12 = icmp eq i16 %5, 0
  br i1 %.not12, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2, %20
  %.in = phi i16 [ %21, %20 ], [ %5, %2 ]
  %.0913 = phi ptr [ %22, %20 ], [ %4, %2 ]
  %6 = load ptr, ptr %.0913, align 8, !tbaa !11
  %7 = icmp eq ptr %6, %1
  br i1 %7, label %8, label %20

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %0, i64 26
  %10 = load i16, ptr %9, align 2, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  %13 = ptrtoint ptr %.0913 to i64
  %14 = ptrtoint ptr %12 to i64
  %15 = sub i64 %13, %14
  %16 = lshr exact i64 %15, 3
  %17 = trunc i64 %16 to i16
  %18 = xor i16 %17, -1
  %19 = add i16 %10, %18
  br label %.loopexit

20:                                               ; preds = %.lr.ph
  %21 = add i16 %.in, -1
  %22 = getelementptr inbounds i8, ptr %.0913, i64 8
  %.not = icmp eq i16 %21, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !14

.loopexit:                                        ; preds = %20, %2, %8
  %.010 = phi i16 [ %19, %8 ], [ -1, %2 ], [ -1, %20 ]
  ret i16 %.010
}

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


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