; 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: nounwind uwtable
define dso_local void @POOL_reset(ptr noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %10 = load i8, ptr %4, align 1, !tbaa !9
  %11 = icmp ne i8 %10, 0
  br i1 %11, label %12, label %27

12:                                               ; preds = %2
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.Pool, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !10
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Pool, ptr %16, i32 0, i32 4
  %18 = load i16, ptr %17, align 2, !tbaa !13
  %19 = zext i16 %18 to i32
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Pool, ptr %20, i32 0, i32 3
  %22 = load i16, ptr %21, align 8, !tbaa !14
  %23 = zext i16 %22 to i32
  %24 = add nsw i32 %23, 2
  %25 = mul nsw i32 %19, %24
  %26 = trunc i32 %25 to i16
  call void @memset(ptr noundef %15, i8 noundef zeroext 0, i16 noundef zeroext %26)
  br label %27

27:                                               ; preds = %12, %2
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Pool, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  store ptr %30, ptr %9, align 8, !tbaa !5
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Pool, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !10
  store ptr %33, ptr %8, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Pool, ptr %34, i32 0, i32 3
  %36 = load i16, ptr %35, align 8, !tbaa !14
  %37 = zext i16 %36 to i32
  %38 = ashr i32 %37, 1
  %39 = trunc i32 %38 to i16
  store i16 %39, ptr %7, align 2, !tbaa !16
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Pool, ptr %40, i32 0, i32 4
  %42 = load i16, ptr %41, align 2, !tbaa !13
  store i16 %42, ptr %5, align 2, !tbaa !16
  store i16 0, ptr %6, align 2, !tbaa !16
  br label %43

43:                                               ; preds = %47, %27
  %44 = load i16, ptr %5, align 2, !tbaa !16
  %45 = add i16 %44, -1
  store i16 %45, ptr %5, align 2, !tbaa !16
  %46 = icmp ne i16 %44, 0
  br i1 %46, label %47, label %60

47:                                               ; preds = %43
  %48 = load i16, ptr %6, align 2, !tbaa !16
  %49 = add i16 %48, 1
  store i16 %49, ptr %6, align 2, !tbaa !16
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds i16, ptr %50, i32 1
  store ptr %51, ptr %8, align 8, !tbaa !5
  store i16 %48, ptr %50, align 2, !tbaa !16
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds ptr, ptr %53, i32 1
  store ptr %54, ptr %9, align 8, !tbaa !5
  store ptr %52, ptr %53, align 8, !tbaa !5
  %55 = load i16, ptr %7, align 2, !tbaa !16
  %56 = zext i16 %55 to i32
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = sext i32 %56 to i64
  %59 = getelementptr inbounds i16, ptr %57, i64 %58
  store ptr %59, ptr %8, align 8, !tbaa !5
  br label %43, !llvm.loop !17

60:                                               ; preds = %43
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.Pool, ptr %62, i32 0, i32 2
  store ptr %61, ptr %63, align 8, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  ret void
}

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

declare void @memset(ptr noundef, i8 noundef zeroext, i16 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 = { "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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !12, i64 24, !12, i64 26}
!12 = !{!"short", !7, i64 0}
!13 = !{!11, !12, i64 26}
!14 = !{!11, !12, i64 24}
!15 = !{!11, !6, i64 8}
!16 = !{!12, !12, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!11, !6, i64 16}


; 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 void @POOL_reset(ptr nocapture noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %.not = icmp eq i8 %1, 0
  br i1 %.not, label %11, label %3

3:                                                ; preds = %2
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 26
  %6 = load i16, ptr %5, align 2, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load i16, ptr %7, align 8, !tbaa !12
  %9 = add i16 %8, 2
  %10 = mul i16 %9, %6
  tail call void @memset(ptr noundef %4, i8 noundef zeroext 0, i16 noundef zeroext %10) #2
  br label %11

11:                                               ; preds = %3, %2
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %0, i64 26
  %15 = load i16, ptr %14, align 2, !tbaa !11
  %.not1920 = icmp eq i16 %15, 0
  br i1 %.not1920, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = load i16, ptr %16, align 8, !tbaa !12
  %18 = lshr i16 %17, 1
  %19 = load ptr, ptr %0, align 8, !tbaa !5
  %20 = zext nneg i16 %18 to i64
  %xtraiter = and i16 %15, 3
  %21 = icmp ult i16 %15, 4
  br i1 %21, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i16 %15, -4
  br label %22

22:                                               ; preds = %22, %.lr.ph.new
  %.024 = phi ptr [ %13, %.lr.ph.new ], [ %37, %22 ]
  %.01623 = phi ptr [ %19, %.lr.ph.new ], [ %38, %22 ]
  %.01821 = phi i16 [ 0, %.lr.ph.new ], [ %35, %22 ]
  %niter = phi i16 [ 0, %.lr.ph.new ], [ %niter.next.3, %22 ]
  %23 = or disjoint i16 %.01821, 1
  %24 = getelementptr inbounds i8, ptr %.01623, i64 2
  store i16 %.01821, ptr %.01623, align 2, !tbaa !14
  %25 = getelementptr inbounds i8, ptr %.024, i64 8
  store ptr %24, ptr %.024, align 8, !tbaa !15
  %26 = getelementptr inbounds i16, ptr %24, i64 %20
  %27 = or disjoint i16 %.01821, 2
  %28 = getelementptr inbounds i8, ptr %26, i64 2
  store i16 %23, ptr %26, align 2, !tbaa !14
  %29 = getelementptr inbounds i8, ptr %.024, i64 16
  store ptr %28, ptr %25, align 8, !tbaa !15
  %30 = getelementptr inbounds i16, ptr %28, i64 %20
  %31 = or disjoint i16 %.01821, 3
  %32 = getelementptr inbounds i8, ptr %30, i64 2
  store i16 %27, ptr %30, align 2, !tbaa !14
  %33 = getelementptr inbounds i8, ptr %.024, i64 24
  store ptr %32, ptr %29, align 8, !tbaa !15
  %34 = getelementptr inbounds i16, ptr %32, i64 %20
  %35 = add nuw i16 %.01821, 4
  %36 = getelementptr inbounds i8, ptr %34, i64 2
  store i16 %31, ptr %34, align 2, !tbaa !14
  %37 = getelementptr inbounds i8, ptr %.024, i64 32
  store ptr %36, ptr %33, align 8, !tbaa !15
  %38 = getelementptr inbounds i16, ptr %36, i64 %20
  %niter.next.3 = add i16 %niter, 4
  %niter.ncmp.3 = icmp eq i16 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %22, !llvm.loop !16

._crit_edge.loopexit.unr-lcssa:                   ; preds = %22, %.lr.ph
  %.lcssa.ph = phi ptr [ poison, %.lr.ph ], [ %37, %22 ]
  %.024.unr = phi ptr [ %13, %.lr.ph ], [ %37, %22 ]
  %.01623.unr = phi ptr [ %19, %.lr.ph ], [ %38, %22 ]
  %.01821.unr = phi i16 [ 0, %.lr.ph ], [ %35, %22 ]
  %lcmp.mod.not = icmp eq i16 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %.024.epil = phi ptr [ %41, %.epil.preheader ], [ %.024.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01623.epil = phi ptr [ %42, %.epil.preheader ], [ %.01623.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01821.epil = phi i16 [ %39, %.epil.preheader ], [ %.01821.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i16 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %39 = add nuw i16 %.01821.epil, 1
  %40 = getelementptr inbounds i8, ptr %.01623.epil, i64 2
  store i16 %.01821.epil, ptr %.01623.epil, align 2, !tbaa !14
  %41 = getelementptr inbounds i8, ptr %.024.epil, i64 8
  store ptr %40, ptr %.024.epil, align 8, !tbaa !15
  %42 = getelementptr inbounds i16, ptr %40, i64 %20
  %epil.iter.next = add i16 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i16 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !18

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %11
  %.0.lcssa = phi ptr [ %13, %11 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %41, %.epil.preheader ]
  %43 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %.0.lcssa, ptr %43, align 8, !tbaa !20
  ret void
}

declare void @memset(ptr noundef, i8 noundef zeroext, i16 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 = { "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, !7, i64 0}
!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 = !{!6, !10, i64 26}
!12 = !{!6, !10, i64 24}
!13 = !{!6, !7, i64 8}
!14 = !{!10, !10, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.unroll.disable"}
!20 = !{!6, !7, i64 16}


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