; LLVM IR from itchio/go-brotli :: enc (backward_references_hq)

; 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.ZopfliNode = type { i32, i32, i32, %union.anon }
%union.anon = type { float }

; Function Attrs: nounwind uwtable
define hidden void @BrotliInitZopfliNodes(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca %struct.ZopfliNode, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %7 = getelementptr inbounds %struct.ZopfliNode, ptr %5, i32 0, i32 0
  store i32 1, ptr %7, align 4, !tbaa !11
  %8 = getelementptr inbounds %struct.ZopfliNode, ptr %5, i32 0, i32 1
  store i32 0, ptr %8, align 4, !tbaa !14
  %9 = getelementptr inbounds %struct.ZopfliNode, ptr %5, i32 0, i32 2
  store i32 0, ptr %9, align 4, !tbaa !15
  %10 = getelementptr inbounds %struct.ZopfliNode, ptr %5, i32 0, i32 3
  store float 0x47DFF933C0000000, ptr %10, align 4, !tbaa !16
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %19, %2
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %22

15:                                               ; preds = %11
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.ZopfliNode, ptr %16, i64 %17
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %18, ptr align 4 %5, i64 16, i1 false), !tbaa.struct !17
  br label %19

19:                                               ; preds = %15
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = add i64 %20, 1
  store i64 %21, ptr %6, align 8, !tbaa !9
  br label %11, !llvm.loop !19

22:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  ret void
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"ZopfliNode", !13, i64 0, !13, i64 4, !13, i64 8, !7, i64 12}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !13, i64 8}
!16 = !{!7, !7, i64 0}
!17 = !{i64 0, i64 4, !18, i64 4, i64 4, !18, i64 8, i64 4, !18, i64 12, i64 4, !16}
!18 = !{!13, !13, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%struct.ZopfliNode = type { i32, i32, i32, %union.anon }
%union.anon = type { float }

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: write) uwtable
define hidden void @BrotliInitZopfliNodes(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %xtraiter = and i64 %1, 1
  %3 = icmp eq i64 %1, 1
  br i1 %3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %1, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.04 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %7, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %4 = getelementptr inbounds %struct.ZopfliNode, ptr %0, i64 %.04
  store i32 1, ptr %4, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %4, i64 4
  store i32 0, ptr %.sroa.4.0..sroa_idx, align 4, !tbaa !5
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %4, i64 8
  store i32 0, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !5
  %.sroa.6.0..sroa_idx = getelementptr inbounds i8, ptr %4, i64 12
  store float 0x47DFF933C0000000, ptr %.sroa.6.0..sroa_idx, align 4, !tbaa !9
  %5 = or disjoint i64 %.04, 1
  %6 = getelementptr inbounds %struct.ZopfliNode, ptr %0, i64 %5
  store i32 1, ptr %6, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx.1 = getelementptr inbounds i8, ptr %6, i64 4
  store i32 0, ptr %.sroa.4.0..sroa_idx.1, align 4, !tbaa !5
  %.sroa.5.0..sroa_idx.1 = getelementptr inbounds i8, ptr %6, i64 8
  store i32 0, ptr %.sroa.5.0..sroa_idx.1, align 4, !tbaa !5
  %.sroa.6.0..sroa_idx.1 = getelementptr inbounds i8, ptr %6, i64 12
  store float 0x47DFF933C0000000, ptr %.sroa.6.0..sroa_idx.1, align 4, !tbaa !9
  %7 = add nuw i64 %.04, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.04.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %7, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %8 = getelementptr inbounds %struct.ZopfliNode, ptr %0, i64 %.04.unr
  store i32 1, ptr %8, align 4, !tbaa !5
  %.sroa.4.0..sroa_idx.epil = getelementptr inbounds i8, ptr %8, i64 4
  store i32 0, ptr %.sroa.4.0..sroa_idx.epil, align 4, !tbaa !5
  %.sroa.5.0..sroa_idx.epil = getelementptr inbounds i8, ptr %8, i64 8
  store i32 0, ptr %.sroa.5.0..sroa_idx.epil, align 4, !tbaa !5
  %.sroa.6.0..sroa_idx.epil = getelementptr inbounds i8, ptr %8, i64 12
  store float 0x47DFF933C0000000, ptr %.sroa.6.0..sroa_idx.epil, align 4, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %2
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: write) 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass