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

; 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.HuffmanTree = type { i32, i16, i16 }

; Function Attrs: nounwind uwtable
define hidden i32 @BrotliSetDepth(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca [16 x i32], align 16
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 64, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %14 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %14, ptr %12, align 4, !tbaa !5
  %15 = getelementptr inbounds [16 x i32], ptr %10, i64 0, i64 0
  store i32 -1, ptr %15, align 16, !tbaa !5
  br label %16

16:                                               ; preds = %82, %33, %4
  br label %17

17:                                               ; preds = %16
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = load i32, ptr %12, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds %struct.HuffmanTree, ptr %18, i64 %20
  %22 = getelementptr inbounds %struct.HuffmanTree, ptr %21, i32 0, i32 1
  %23 = load i16, ptr %22, align 4, !tbaa !11
  %24 = sext i16 %23 to i32
  %25 = icmp sge i32 %24, 0
  br i1 %25, label %26, label %51

26:                                               ; preds = %17
  %27 = load i32, ptr %11, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %11, align 4, !tbaa !5
  %29 = load i32, ptr %11, align 4, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !5
  %31 = icmp sgt i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %26
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %90

33:                                               ; preds = %26
  %34 = load ptr, ptr %7, align 8, !tbaa !9
  %35 = load i32, ptr %12, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds %struct.HuffmanTree, ptr %34, i64 %36
  %38 = getelementptr inbounds %struct.HuffmanTree, ptr %37, i32 0, i32 2
  %39 = load i16, ptr %38, align 2, !tbaa !14
  %40 = sext i16 %39 to i32
  %41 = load i32, ptr %11, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [16 x i32], ptr %10, i64 0, i64 %42
  store i32 %40, ptr %43, align 4, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !9
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.HuffmanTree, ptr %44, i64 %46
  %48 = getelementptr inbounds %struct.HuffmanTree, ptr %47, i32 0, i32 1
  %49 = load i16, ptr %48, align 4, !tbaa !11
  %50 = sext i16 %49 to i32
  store i32 %50, ptr %12, align 4, !tbaa !5
  br label %16

51:                                               ; preds = %17
  %52 = load i32, ptr %11, align 4, !tbaa !5
  %53 = trunc i32 %52 to i8
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = load ptr, ptr %7, align 8, !tbaa !9
  %56 = load i32, ptr %12, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds %struct.HuffmanTree, ptr %55, i64 %57
  %59 = getelementptr inbounds %struct.HuffmanTree, ptr %58, i32 0, i32 2
  %60 = load i16, ptr %59, align 2, !tbaa !14
  %61 = sext i16 %60 to i64
  %62 = getelementptr inbounds i8, ptr %54, i64 %61
  store i8 %53, ptr %62, align 1, !tbaa !15
  br label %63

63:                                               ; preds = %51
  br label %64

64:                                               ; preds = %75, %63
  %65 = load i32, ptr %11, align 4, !tbaa !5
  %66 = icmp sge i32 %65, 0
  br i1 %66, label %67, label %73

67:                                               ; preds = %64
  %68 = load i32, ptr %11, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [16 x i32], ptr %10, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !5
  %72 = icmp eq i32 %71, -1
  br label %73

73:                                               ; preds = %67, %64
  %74 = phi i1 [ false, %64 ], [ %72, %67 ]
  br i1 %74, label %75, label %78

75:                                               ; preds = %73
  %76 = load i32, ptr %11, align 4, !tbaa !5
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %11, align 4, !tbaa !5
  br label %64, !llvm.loop !16

78:                                               ; preds = %73
  %79 = load i32, ptr %11, align 4, !tbaa !5
  %80 = icmp slt i32 %79, 0
  br i1 %80, label %81, label %82

81:                                               ; preds = %78
  store i32 1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %90

82:                                               ; preds = %78
  %83 = load i32, ptr %11, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [16 x i32], ptr %10, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !5
  store i32 %86, ptr %12, align 4, !tbaa !5
  %87 = load i32, ptr %11, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [16 x i32], ptr %10, i64 0, i64 %88
  store i32 -1, ptr %89, align 4, !tbaa !5
  br label %16

90:                                               ; preds = %81, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr %10) #2
  %91 = load i32, ptr %5, align 4
  ret i32 %91
}

; 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 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 = !{!"int", !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 = !{!12, !13, i64 4}
!12 = !{!"HuffmanTree", !6, i64 0, !13, i64 4, !13, i64 6}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 6}
!15 = !{!7, !7, i64 0}
!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.HuffmanTree = type { i32, i16, i16 }

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define hidden range(i32 0, 2) i32 @BrotliSetDepth(i32 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef writeonly %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca [16 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #2
  store i32 -1, ptr %5, align 16, !tbaa !5
  br label %.backedge

.backedge:                                        ; preds = %.backedge.backedge, %4
  %.021 = phi i32 [ 0, %4 ], [ %.021.be, %.backedge.backedge ]
  %.0 = phi i32 [ %0, %4 ], [ %.0.be, %.backedge.backedge ]
  %6 = sext i32 %.0 to i64
  %7 = getelementptr inbounds %struct.HuffmanTree, ptr %1, i64 %6
  %8 = getelementptr inbounds i8, ptr %7, i64 4
  %9 = load i16, ptr %8, align 4, !tbaa !9
  %10 = icmp sgt i16 %9, -1
  br i1 %10, label %11, label %20

11:                                               ; preds = %.backedge
  %.not = icmp slt i32 %.021, %3
  br i1 %.not, label %12, label %.critedge.thread

12:                                               ; preds = %11
  %13 = add nsw i32 %.021, 1
  %14 = getelementptr inbounds i8, ptr %7, i64 6
  %15 = load i16, ptr %14, align 2, !tbaa !12
  %16 = sext i16 %15 to i32
  %17 = sext i32 %13 to i64
  %18 = getelementptr inbounds [16 x i32], ptr %5, i64 0, i64 %17
  store i32 %16, ptr %18, align 4, !tbaa !5
  %19 = zext nneg i16 %9 to i32
  br label %.backedge.backedge

.backedge.backedge:                               ; preds = %12, %.critedge
  %.021.be = phi i32 [ %13, %12 ], [ %.126, %.critedge ]
  %.0.be = phi i32 [ %19, %12 ], [ %29, %.critedge ]
  br label %.backedge

20:                                               ; preds = %.backedge
  %21 = trunc i32 %.021 to i8
  %22 = getelementptr inbounds i8, ptr %7, i64 6
  %23 = load i16, ptr %22, align 2, !tbaa !12
  %24 = sext i16 %23 to i64
  %25 = getelementptr inbounds i8, ptr %2, i64 %24
  store i8 %21, ptr %25, align 1, !tbaa !13
  %26 = icmp sgt i32 %.021, -1
  br i1 %26, label %.lr.ph, label %.critedge.thread

.lr.ph:                                           ; preds = %20, %31
  %.126 = phi i32 [ %32, %31 ], [ %.021, %20 ]
  %27 = zext nneg i32 %.126 to i64
  %28 = getelementptr inbounds [16 x i32], ptr %5, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp eq i32 %29, -1
  br i1 %30, label %31, label %.critedge

31:                                               ; preds = %.lr.ph
  %32 = add nsw i32 %.126, -1
  %33 = icmp sgt i32 %.126, 0
  br i1 %33, label %.lr.ph, label %.critedge.thread, !llvm.loop !14

.critedge:                                        ; preds = %.lr.ph
  %34 = getelementptr inbounds [16 x i32], ptr %5, i64 0, i64 %27
  store i32 -1, ptr %34, align 4, !tbaa !5
  br label %.backedge.backedge

.critedge.thread:                                 ; preds = %11, %20, %31
  %.022 = phi i32 [ 1, %31 ], [ 1, %20 ], [ 0, %11 ]
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #2
  ret i32 %.022
}

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

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

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


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