; 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 }
%struct.Command = type { i32, i32, i32, 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
define hidden void @BrotliZopfliCreateCommands(i64 noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6, ptr noundef %7, ptr noundef %8) #1 {
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  %19 = alloca i64, align 8
  %20 = alloca i32, align 4
  %21 = alloca i64, align 8
  %22 = alloca i64, align 8
  %23 = alloca ptr, align 8
  %24 = alloca i64, align 8
  %25 = alloca i64, align 8
  %26 = alloca i64, align 8
  %27 = alloca i64, align 8
  %28 = alloca i64, align 8
  %29 = alloca i32, align 4
  %30 = alloca i64, align 8
  store i64 %0, ptr %10, align 8, !tbaa !5
  store i64 %1, ptr %11, align 8, !tbaa !5
  store i64 %2, ptr %12, align 8, !tbaa !5
  store ptr %3, ptr %13, align 8, !tbaa !9
  store ptr %4, ptr %14, align 8, !tbaa !9
  store ptr %5, ptr %15, align 8, !tbaa !9
  store ptr %6, ptr %16, align 8, !tbaa !9
  store ptr %7, ptr %17, align 8, !tbaa !9
  store ptr %8, ptr %18, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #3
  store i64 0, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #3
  %31 = load ptr, ptr %13, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.ZopfliNode, ptr %31, i64 0
  %33 = getelementptr inbounds %struct.ZopfliNode, ptr %32, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !11
  store i32 %34, ptr %20, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %22) #3
  store i64 0, ptr %22, align 8, !tbaa !5
  %35 = load ptr, ptr %16, align 8, !tbaa !9
  store i64 0, ptr %21, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %135, %9
  %37 = load i32, ptr %20, align 4, !tbaa !12
  %38 = icmp ne i32 %37, -1
  br i1 %38, label %39, label %138

39:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %23) #3
  %40 = load ptr, ptr %13, align 8, !tbaa !9
  %41 = load i64, ptr %19, align 8, !tbaa !5
  %42 = load i32, ptr %20, align 4, !tbaa !12
  %43 = zext i32 %42 to i64
  %44 = add i64 %41, %43
  %45 = getelementptr inbounds %struct.ZopfliNode, ptr %40, i64 %44
  store ptr %45, ptr %23, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %24) #3
  %46 = load ptr, ptr %23, align 8, !tbaa !9
  %47 = call i32 @ZopfliNodeCopyLength(ptr noundef %46)
  %48 = zext i32 %47 to i64
  store i64 %48, ptr %24, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %25) #3
  %49 = load ptr, ptr %23, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.ZopfliNode, ptr %49, i32 0, i32 2
  %51 = load i32, ptr %50, align 4, !tbaa !14
  %52 = zext i32 %51 to i64
  store i64 %52, ptr %25, align 8, !tbaa !5
  %53 = load i64, ptr %25, align 8, !tbaa !5
  %54 = load i64, ptr %19, align 8, !tbaa !5
  %55 = add i64 %54, %53
  store i64 %55, ptr %19, align 8, !tbaa !5
  %56 = load ptr, ptr %23, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.ZopfliNode, ptr %56, i32 0, i32 3
  %58 = load i32, ptr %57, align 4, !tbaa !11
  store i32 %58, ptr %20, align 4, !tbaa !12
  %59 = load i64, ptr %21, align 8, !tbaa !5
  %60 = icmp eq i64 %59, 0
  br i1 %60, label %61, label %67

61:                                               ; preds = %39
  %62 = load ptr, ptr %15, align 8, !tbaa !9
  %63 = load i64, ptr %62, align 8, !tbaa !5
  %64 = load i64, ptr %25, align 8, !tbaa !5
  %65 = add i64 %64, %63
  store i64 %65, ptr %25, align 8, !tbaa !5
  %66 = load ptr, ptr %15, align 8, !tbaa !9
  store i64 0, ptr %66, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %61, %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %26) #3
  %68 = load ptr, ptr %23, align 8, !tbaa !9
  %69 = call i32 @ZopfliNodeCopyDistance(ptr noundef %68)
  %70 = zext i32 %69 to i64
  store i64 %70, ptr %26, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %27) #3
  %71 = load ptr, ptr %23, align 8, !tbaa !9
  %72 = call i32 @ZopfliNodeLengthCode(ptr noundef %71)
  %73 = zext i32 %72 to i64
  store i64 %73, ptr %27, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %28) #3
  %74 = load i64, ptr %11, align 8, !tbaa !5
  %75 = load i64, ptr %19, align 8, !tbaa !5
  %76 = add i64 %74, %75
  %77 = load i64, ptr %12, align 8, !tbaa !5
  %78 = call i64 @brotli_min_size_t(i64 noundef %76, i64 noundef %77)
  store i64 %78, ptr %28, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %29) #3
  %79 = load i64, ptr %26, align 8, !tbaa !5
  %80 = load i64, ptr %28, align 8, !tbaa !5
  %81 = load i64, ptr %22, align 8, !tbaa !5
  %82 = add i64 %80, %81
  %83 = icmp ugt i64 %79, %82
  %84 = xor i1 %83, true
  %85 = xor i1 %84, true
  %86 = zext i1 %85 to i64
  %87 = select i1 %85, i32 1, i32 0
  store i32 %87, ptr %29, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %30) #3
  %88 = load ptr, ptr %23, align 8, !tbaa !9
  %89 = call i32 @ZopfliNodeDistanceCode(ptr noundef %88)
  %90 = zext i32 %89 to i64
  store i64 %90, ptr %30, align 8, !tbaa !5
  %91 = load ptr, ptr %17, align 8, !tbaa !9
  %92 = load i64, ptr %21, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.Command, ptr %91, i64 %92
  %94 = load i64, ptr %25, align 8, !tbaa !5
  %95 = load i64, ptr %24, align 8, !tbaa !5
  %96 = load i64, ptr %27, align 8, !tbaa !5
  %97 = trunc i64 %96 to i32
  %98 = load i64, ptr %24, align 8, !tbaa !5
  %99 = trunc i64 %98 to i32
  %100 = sub nsw i32 %97, %99
  %101 = load i64, ptr %30, align 8, !tbaa !5
  call void @InitCommand(ptr noundef %93, i64 noundef %94, i64 noundef %95, i32 noundef %100, i64 noundef %101)
  %102 = load i32, ptr %29, align 4, !tbaa !12
  %103 = icmp ne i32 %102, 0
  br i1 %103, label %127, label %104

104:                                              ; preds = %67
  %105 = load i64, ptr %30, align 8, !tbaa !5
  %106 = icmp ugt i64 %105, 0
  br i1 %106, label %107, label %127

107:                                              ; preds = %104
  %108 = load ptr, ptr %14, align 8, !tbaa !9
  %109 = getelementptr inbounds i32, ptr %108, i64 2
  %110 = load i32, ptr %109, align 4, !tbaa !12
  %111 = load ptr, ptr %14, align 8, !tbaa !9
  %112 = getelementptr inbounds i32, ptr %111, i64 3
  store i32 %110, ptr %112, align 4, !tbaa !12
  %113 = load ptr, ptr %14, align 8, !tbaa !9
  %114 = getelementptr inbounds i32, ptr %113, i64 1
  %115 = load i32, ptr %114, align 4, !tbaa !12
  %116 = load ptr, ptr %14, align 8, !tbaa !9
  %117 = getelementptr inbounds i32, ptr %116, i64 2
  store i32 %115, ptr %117, align 4, !tbaa !12
  %118 = load ptr, ptr %14, align 8, !tbaa !9
  %119 = getelementptr inbounds i32, ptr %118, i64 0
  %120 = load i32, ptr %119, align 4, !tbaa !12
  %121 = load ptr, ptr %14, align 8, !tbaa !9
  %122 = getelementptr inbounds i32, ptr %121, i64 1
  store i32 %120, ptr %122, align 4, !tbaa !12
  %123 = load i64, ptr %26, align 8, !tbaa !5
  %124 = trunc i64 %123 to i32
  %125 = load ptr, ptr %14, align 8, !tbaa !9
  %126 = getelementptr inbounds i32, ptr %125, i64 0
  store i32 %124, ptr %126, align 4, !tbaa !12
  br label %127

127:                                              ; preds = %107, %104, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %30) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %29) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %28) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %27) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %26) #3
  %128 = load i64, ptr %25, align 8, !tbaa !5
  %129 = load ptr, ptr %18, align 8, !tbaa !9
  %130 = load i64, ptr %129, align 8, !tbaa !5
  %131 = add i64 %130, %128
  store i64 %131, ptr %129, align 8, !tbaa !5
  %132 = load i64, ptr %24, align 8, !tbaa !5
  %133 = load i64, ptr %19, align 8, !tbaa !5
  %134 = add i64 %133, %132
  store i64 %134, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %25) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %24) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %23) #3
  br label %135

135:                                              ; preds = %127
  %136 = load i64, ptr %21, align 8, !tbaa !5
  %137 = add i64 %136, 1
  store i64 %137, ptr %21, align 8, !tbaa !5
  br label %36, !llvm.loop !16

138:                                              ; preds = %36
  %139 = load i64, ptr %10, align 8, !tbaa !5
  %140 = load i64, ptr %19, align 8, !tbaa !5
  %141 = sub i64 %139, %140
  %142 = load ptr, ptr %15, align 8, !tbaa !9
  %143 = load i64, ptr %142, align 8, !tbaa !5
  %144 = add i64 %143, %141
  store i64 %144, ptr %142, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #3
  ret void
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeCopyLength(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeCopyDistance(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeLengthCode(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @brotli_min_size_t(i64 noundef, i64 noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeDistanceCode(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @InitCommand(ptr noundef, i64 noundef, i64 noundef, i32 noundef, i64 noundef) #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 = { alwaysinline 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"ZopfliNode", !13, i64 0, !13, i64 4, !13, i64 8, !7, i64 12}
!16 = distinct !{!16, !17}
!17 = !{!"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 }
%struct.Command = type { i32, i32, i32, i16, i16 }

; Function Attrs: nounwind uwtable
define hidden void @BrotliZopfliCreateCommands(i64 noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3, ptr nocapture noundef %4, ptr nocapture noundef %5, ptr nocapture noundef readnone %6, ptr noundef %7, ptr nocapture noundef %8) local_unnamed_addr #0 {
  %10 = getelementptr inbounds i8, ptr %3, i64 12
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %.not52 = icmp eq i32 %11, -1
  br i1 %.not52, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %9
  %12 = getelementptr inbounds i8, ptr %4, i64 8
  %13 = getelementptr inbounds i8, ptr %4, i64 12
  %14 = getelementptr inbounds i8, ptr %4, i64 4
  br label %15

15:                                               ; preds = %.lr.ph, %46
  %.055 = phi i64 [ 0, %.lr.ph ], [ %49, %46 ]
  %.04954 = phi i32 [ %11, %.lr.ph ], [ %26, %46 ]
  %.05053 = phi i64 [ 0, %.lr.ph ], [ %50, %46 ]
  %16 = zext i32 %.04954 to i64
  %17 = getelementptr %struct.ZopfliNode, ptr %3, i64 %.055
  %18 = getelementptr %struct.ZopfliNode, ptr %17, i64 %16
  %19 = tail call i32 @ZopfliNodeCopyLength(ptr noundef %18)
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds i8, ptr %18, i64 8
  %22 = load i32, ptr %21, align 4, !tbaa !8
  %23 = zext i32 %22 to i64
  %24 = add i64 %.055, %23
  %25 = getelementptr inbounds i8, ptr %18, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp eq i64 %.05053, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %15
  %29 = load i64, ptr %5, align 8, !tbaa !11
  %30 = add i64 %29, %23
  store i64 0, ptr %5, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %28, %15
  %.051 = phi i64 [ %30, %28 ], [ %23, %15 ]
  %32 = tail call i32 @ZopfliNodeCopyDistance(ptr noundef %18)
  %33 = zext i32 %32 to i64
  %34 = tail call i32 @ZopfliNodeLengthCode(ptr noundef %18)
  %35 = add i64 %24, %1
  %36 = tail call i64 @brotli_min_size_t(i64 noundef %35, i64 noundef %2)
  %37 = icmp uge i64 %36, %33
  %38 = tail call i32 @ZopfliNodeDistanceCode(ptr noundef %18)
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds %struct.Command, ptr %7, i64 %.05053
  %41 = sub nsw i32 %34, %19
  tail call void @InitCommand(ptr noundef %40, i64 noundef %.051, i64 noundef %20, i32 noundef %41, i64 noundef %39)
  %42 = icmp ne i32 %38, 0
  %or.cond = and i1 %37, %42
  br i1 %or.cond, label %43, label %46

43:                                               ; preds = %31
  %44 = load i32, ptr %12, align 4, !tbaa !13
  store i32 %44, ptr %13, align 4, !tbaa !13
  %45 = load <2 x i32>, ptr %4, align 4, !tbaa !13
  store <2 x i32> %45, ptr %14, align 4, !tbaa !13
  store i32 %32, ptr %4, align 4, !tbaa !13
  br label %46

46:                                               ; preds = %43, %31
  %47 = load i64, ptr %8, align 8, !tbaa !11
  %48 = add i64 %47, %.051
  store i64 %48, ptr %8, align 8, !tbaa !11
  %49 = add i64 %24, %20
  %50 = add i64 %.05053, 1
  %.not = icmp eq i32 %26, -1
  br i1 %.not, label %._crit_edge, label %15, !llvm.loop !14

._crit_edge:                                      ; preds = %46, %9
  %.0.lcssa = phi i64 [ 0, %9 ], [ %49, %46 ]
  %51 = sub i64 %0, %.0.lcssa
  %52 = load i64, ptr %5, align 8, !tbaa !11
  %53 = add i64 %51, %52
  store i64 %53, ptr %5, align 8, !tbaa !11
  ret void
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeCopyLength(ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeCopyDistance(ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeLengthCode(ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i64 @brotli_min_size_t(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden i32 @ZopfliNodeDistanceCode(ptr noundef) local_unnamed_addr #1

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @InitCommand(ptr noundef, i64 noundef, i64 noundef, i32 noundef, i64 noundef) 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 = { alwaysinline 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 8}
!9 = !{!"ZopfliNode", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 12}
!10 = !{!"int", !6, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !6, i64 0}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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