; 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"

; 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 @BrotliWriteHuffmanTree(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i8, align 1
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca i8, align 1
  %17 = alloca i64, align 8
  %18 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  store i8 8, ptr %11, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 0, ptr %14, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %19 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %19, ptr %15, align 8, !tbaa !9
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %39, %5
  %21 = load i64, ptr %12, align 8, !tbaa !9
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %24, label %42

24:                                               ; preds = %20
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = load i64, ptr %12, align 8, !tbaa !9
  %28 = sub i64 %26, %27
  %29 = sub i64 %28, 1
  %30 = getelementptr inbounds i8, ptr %25, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %24
  %35 = load i64, ptr %15, align 8, !tbaa !9
  %36 = add i64 %35, -1
  store i64 %36, ptr %15, align 8, !tbaa !9
  br label %38

37:                                               ; preds = %24
  br label %42

38:                                               ; preds = %34
  br label %39

39:                                               ; preds = %38
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %12, align 8, !tbaa !9
  br label %20, !llvm.loop !14

42:                                               ; preds = %37, %20
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = icmp ugt i64 %43, 50
  br i1 %44, label %45, label %48

45:                                               ; preds = %42
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %15, align 8, !tbaa !9
  call void @DecideOverRleUse(ptr noundef %46, i64 noundef %47, ptr noundef %13, ptr noundef %14)
  br label %48

48:                                               ; preds = %45, %42
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %113, %48
  %50 = load i64, ptr %12, align 8, !tbaa !9
  %51 = load i64, ptr %15, align 8, !tbaa !9
  %52 = icmp ult i64 %50, %51
  br i1 %52, label %53, label %117

53:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #2
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr %12, align 8, !tbaa !9
  %56 = getelementptr inbounds i8, ptr %54, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !11
  store i8 %57, ptr %16, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #2
  store i64 1, ptr %17, align 8, !tbaa !9
  %58 = load i8, ptr %16, align 1, !tbaa !11
  %59 = zext i8 %58 to i32
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %61, label %64

61:                                               ; preds = %53
  %62 = load i32, ptr %13, align 4, !tbaa !12
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %71, label %64

64:                                               ; preds = %61, %53
  %65 = load i8, ptr %16, align 1, !tbaa !11
  %66 = zext i8 %65 to i32
  %67 = icmp eq i32 %66, 0
  br i1 %67, label %68, label %96

68:                                               ; preds = %64
  %69 = load i32, ptr %14, align 4, !tbaa !12
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %96

71:                                               ; preds = %68, %61
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #2
  %72 = load i64, ptr %12, align 8, !tbaa !9
  %73 = add i64 %72, 1
  store i64 %73, ptr %18, align 8, !tbaa !9
  br label %74

74:                                               ; preds = %92, %71
  %75 = load i64, ptr %18, align 8, !tbaa !9
  %76 = load i64, ptr %15, align 8, !tbaa !9
  %77 = icmp ult i64 %75, %76
  br i1 %77, label %78, label %87

78:                                               ; preds = %74
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = load i64, ptr %18, align 8, !tbaa !9
  %81 = getelementptr inbounds i8, ptr %79, i64 %80
  %82 = load i8, ptr %81, align 1, !tbaa !11
  %83 = zext i8 %82 to i32
  %84 = load i8, ptr %16, align 1, !tbaa !11
  %85 = zext i8 %84 to i32
  %86 = icmp eq i32 %83, %85
  br label %87

87:                                               ; preds = %78, %74
  %88 = phi i1 [ false, %74 ], [ %86, %78 ]
  br i1 %88, label %89, label %95

89:                                               ; preds = %87
  %90 = load i64, ptr %17, align 8, !tbaa !9
  %91 = add i64 %90, 1
  store i64 %91, ptr %17, align 8, !tbaa !9
  br label %92

92:                                               ; preds = %89
  %93 = load i64, ptr %18, align 8, !tbaa !9
  %94 = add i64 %93, 1
  store i64 %94, ptr %18, align 8, !tbaa !9
  br label %74, !llvm.loop !16

95:                                               ; preds = %87
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #2
  br label %96

96:                                               ; preds = %95, %68, %64
  %97 = load i8, ptr %16, align 1, !tbaa !11
  %98 = zext i8 %97 to i32
  %99 = icmp eq i32 %98, 0
  br i1 %99, label %100, label %105

100:                                              ; preds = %96
  %101 = load i64, ptr %17, align 8, !tbaa !9
  %102 = load ptr, ptr %8, align 8, !tbaa !5
  %103 = load ptr, ptr %9, align 8, !tbaa !5
  %104 = load ptr, ptr %10, align 8, !tbaa !5
  call void @BrotliWriteHuffmanTreeRepetitionsZeros(i64 noundef %101, ptr noundef %102, ptr noundef %103, ptr noundef %104)
  br label %113

105:                                              ; preds = %96
  %106 = load i8, ptr %11, align 1, !tbaa !11
  %107 = load i8, ptr %16, align 1, !tbaa !11
  %108 = load i64, ptr %17, align 8, !tbaa !9
  %109 = load ptr, ptr %8, align 8, !tbaa !5
  %110 = load ptr, ptr %9, align 8, !tbaa !5
  %111 = load ptr, ptr %10, align 8, !tbaa !5
  call void @BrotliWriteHuffmanTreeRepetitions(i8 noundef zeroext %106, i8 noundef zeroext %107, i64 noundef %108, ptr noundef %109, ptr noundef %110, ptr noundef %111)
  %112 = load i8, ptr %16, align 1, !tbaa !11
  store i8 %112, ptr %11, align 1, !tbaa !11
  br label %113

113:                                              ; preds = %105, %100
  %114 = load i64, ptr %17, align 8, !tbaa !9
  %115 = load i64, ptr %12, align 8, !tbaa !9
  %116 = add i64 %115, %114
  store i64 %116, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #2
  br label %49, !llvm.loop !17

117:                                              ; preds = %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @DecideOverRleUse(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @BrotliWriteHuffmanTreeRepetitionsZeros(i64 noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @BrotliWriteHuffmanTreeRepetitions(i8 noundef zeroext, i8 noundef zeroext, i64 noundef, ptr noundef, ptr noundef, ptr noundef) #1

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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 @BrotliWriteHuffmanTree(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  %invariant.gep = getelementptr i8, ptr %0, i64 %1
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge64, label %.lr.ph

.lr.ph:                                           ; preds = %5, %11
  %.04449 = phi i64 [ %12, %11 ], [ %1, %5 ]
  %.04548 = phi i64 [ %13, %11 ], [ 0, %5 ]
  %8 = xor i64 %.04548, -1
  %gep = getelementptr i8, ptr %invariant.gep, i64 %8
  %9 = load i8, ptr %gep, align 1, !tbaa !9
  %10 = icmp eq i8 %9, 0
  br i1 %10, label %11, label %._crit_edge

11:                                               ; preds = %.lr.ph
  %12 = add i64 %.04449, -1
  %13 = add nuw i64 %.04548, 1
  %exitcond.not = icmp eq i64 %13, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %11, %.lr.ph
  %.044.lcssa = phi i64 [ 0, %11 ], [ %.04449, %.lr.ph ]
  %14 = icmp ugt i64 %1, 50
  br i1 %14, label %15, label %16

15:                                               ; preds = %._crit_edge
  call void @DecideOverRleUse(ptr noundef nonnull %0, i64 noundef %.044.lcssa, ptr noundef nonnull %6, ptr noundef nonnull %7)
  br label %16

16:                                               ; preds = %15, %._crit_edge
  %.not66 = icmp eq i64 %.044.lcssa, 0
  br i1 %.not66, label %._crit_edge64, label %.lr.ph63

.lr.ph63:                                         ; preds = %16, %36
  %.04261 = phi i8 [ %.143, %36 ], [ 8, %16 ]
  %.14660 = phi i64 [ %37, %36 ], [ 0, %16 ]
  %17 = getelementptr inbounds i8, ptr %0, i64 %.14660
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = icmp ne i8 %18, 0
  %20 = load i32, ptr %6, align 4
  %21 = icmp ne i32 %20, 0
  %or.cond = select i1 %19, i1 %21, i1 false
  br i1 %or.cond, label %27, label %22

22:                                               ; preds = %.lr.ph63
  %23 = icmp eq i8 %18, 0
  %24 = load i32, ptr %7, align 4
  %25 = icmp ne i32 %24, 0
  %or.cond3 = select i1 %23, i1 %25, i1 false
  %.052 = add i64 %.14660, 1
  %26 = icmp ult i64 %.052, %.044.lcssa
  %or.cond65 = select i1 %or.cond3, i1 %26, i1 false
  br i1 %or.cond65, label %.lr.ph56.preheader, label %.critedge

27:                                               ; preds = %.lr.ph63
  %.052.old = add nuw i64 %.14660, 1
  %.old = icmp ult i64 %.052.old, %.044.lcssa
  br i1 %.old, label %.lr.ph56.preheader, label %.critedge.thread

.lr.ph56.preheader:                               ; preds = %27, %22
  %.054.ph = phi i64 [ %.052.old, %27 ], [ %.052, %22 ]
  br label %.lr.ph56

.lr.ph56:                                         ; preds = %.lr.ph56.preheader, %31
  %.054 = phi i64 [ %.0, %31 ], [ %.054.ph, %.lr.ph56.preheader ]
  %.04153 = phi i64 [ %32, %31 ], [ 1, %.lr.ph56.preheader ]
  %28 = getelementptr inbounds i8, ptr %0, i64 %.054
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = icmp eq i8 %29, %18
  br i1 %30, label %31, label %.critedge

31:                                               ; preds = %.lr.ph56
  %32 = add i64 %.04153, 1
  %.0 = add nuw i64 %.054, 1
  %33 = icmp ult i64 %.0, %.044.lcssa
  br i1 %33, label %.lr.ph56, label %.critedge, !llvm.loop !12

.critedge:                                        ; preds = %31, %.lr.ph56, %22
  %.1 = phi i64 [ 1, %22 ], [ %32, %31 ], [ %.04153, %.lr.ph56 ]
  %34 = icmp eq i8 %18, 0
  br i1 %34, label %35, label %.critedge.thread

35:                                               ; preds = %.critedge
  call void @BrotliWriteHuffmanTreeRepetitionsZeros(i64 noundef %.1, ptr noundef %2, ptr noundef %3, ptr noundef %4)
  br label %36

.critedge.thread:                                 ; preds = %27, %.critedge
  %.173 = phi i64 [ %.1, %.critedge ], [ 1, %27 ]
  call void @BrotliWriteHuffmanTreeRepetitions(i8 noundef zeroext %.04261, i8 noundef zeroext %18, i64 noundef %.173, ptr noundef %2, ptr noundef %3, ptr noundef %4)
  br label %36

36:                                               ; preds = %.critedge.thread, %35
  %.172 = phi i64 [ %.1, %35 ], [ %.173, %.critedge.thread ]
  %.143 = phi i8 [ %.04261, %35 ], [ %18, %.critedge.thread ]
  %37 = add i64 %.172, %.14660
  %38 = icmp ult i64 %37, %.044.lcssa
  br i1 %38, label %.lr.ph63, label %._crit_edge64, !llvm.loop !13

._crit_edge64:                                    ; preds = %36, %5, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @DecideOverRleUse(ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @BrotliWriteHuffmanTreeRepetitionsZeros(i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @BrotliWriteHuffmanTreeRepetitions(i8 noundef zeroext, i8 noundef zeroext, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!"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"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}


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