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

; 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.MetaBlockSplit = type { %struct.BlockSplit, %struct.BlockSplit, %struct.BlockSplit, ptr, i64, ptr, i64, ptr, i64, ptr, i64, ptr, i64 }
%struct.BlockSplit = type { i64, i64, ptr, ptr, i64, i64 }
%struct.HistogramLiteral = type { [256 x i32], i64, double }
%struct.HistogramCommand = type { [704 x i32], i64, double }
%struct.HistogramDistance = type { [520 x i32], i64, double }

; 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 @BrotliOptimizeHistograms(i64 noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [704 x i8], align 16
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 704, 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
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %25, %3
  %11 = load i64, ptr %9, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.MetaBlockSplit, ptr %12, i32 0, i32 8
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = icmp ult i64 %11, %14
  br i1 %15, label %16, label %28

16:                                               ; preds = %10
  %17 = load ptr, ptr %6, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.MetaBlockSplit, ptr %17, i32 0, i32 7
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  %20 = load i64, ptr %9, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.HistogramLiteral, ptr %19, i64 %20
  %22 = getelementptr inbounds %struct.HistogramLiteral, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds [256 x i32], ptr %22, i64 0, i64 0
  %24 = getelementptr inbounds [704 x i8], ptr %7, i64 0, i64 0
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef 256, ptr noundef %23, ptr noundef %24)
  br label %25

25:                                               ; preds = %16
  %26 = load i64, ptr %9, align 8, !tbaa !5
  %27 = add i64 %26, 1
  store i64 %27, ptr %9, align 8, !tbaa !5
  br label %10, !llvm.loop !15

28:                                               ; preds = %10
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %44, %28
  %30 = load i64, ptr %9, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.MetaBlockSplit, ptr %31, i32 0, i32 10
  %33 = load i64, ptr %32, align 8, !tbaa !17
  %34 = icmp ult i64 %30, %33
  br i1 %34, label %35, label %47

35:                                               ; preds = %29
  %36 = load ptr, ptr %6, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.MetaBlockSplit, ptr %36, i32 0, i32 9
  %38 = load ptr, ptr %37, align 8, !tbaa !18
  %39 = load i64, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.HistogramCommand, ptr %38, i64 %39
  %41 = getelementptr inbounds %struct.HistogramCommand, ptr %40, i32 0, i32 0
  %42 = getelementptr inbounds [704 x i32], ptr %41, i64 0, i64 0
  %43 = getelementptr inbounds [704 x i8], ptr %7, i64 0, i64 0
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef 704, ptr noundef %42, ptr noundef %43)
  br label %44

44:                                               ; preds = %35
  %45 = load i64, ptr %9, align 8, !tbaa !5
  %46 = add i64 %45, 1
  store i64 %46, ptr %9, align 8, !tbaa !5
  br label %29, !llvm.loop !19

47:                                               ; preds = %29
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = add i64 16, %48
  %50 = load i64, ptr %5, align 8, !tbaa !5
  %51 = trunc i64 %50 to i32
  %52 = shl i32 48, %51
  %53 = zext i32 %52 to i64
  %54 = add i64 %49, %53
  store i64 %54, ptr %8, align 8, !tbaa !5
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %71, %47
  %56 = load i64, ptr %9, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.MetaBlockSplit, ptr %57, i32 0, i32 12
  %59 = load i64, ptr %58, align 8, !tbaa !20
  %60 = icmp ult i64 %56, %59
  br i1 %60, label %61, label %74

61:                                               ; preds = %55
  %62 = load i64, ptr %8, align 8, !tbaa !5
  %63 = load ptr, ptr %6, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.MetaBlockSplit, ptr %63, i32 0, i32 11
  %65 = load ptr, ptr %64, align 8, !tbaa !21
  %66 = load i64, ptr %9, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.HistogramDistance, ptr %65, i64 %66
  %68 = getelementptr inbounds %struct.HistogramDistance, ptr %67, i32 0, i32 0
  %69 = getelementptr inbounds [520 x i32], ptr %68, i64 0, i64 0
  %70 = getelementptr inbounds [704 x i8], ptr %7, i64 0, i64 0
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef %62, ptr noundef %69, ptr noundef %70)
  br label %71

71:                                               ; preds = %61
  %72 = load i64, ptr %9, align 8, !tbaa !5
  %73 = add i64 %72, 1
  store i64 %73, ptr %9, align 8, !tbaa !5
  br label %55, !llvm.loop !22

74:                                               ; preds = %55
  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 704, ptr %7) #3
  ret void
}

declare hidden void @BrotliOptimizeHuffmanCountsForRle(i64 noundef, ptr noundef, ptr 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 = { "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 = !{!12, !6, i64 184}
!12 = !{!"MetaBlockSplit", !13, i64 0, !13, i64 48, !13, i64 96, !10, i64 144, !6, i64 152, !10, i64 160, !6, i64 168, !10, i64 176, !6, i64 184, !10, i64 192, !6, i64 200, !10, i64 208, !6, i64 216}
!13 = !{!"BlockSplit", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !6, i64 40}
!14 = !{!12, !10, i64 176}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !6, i64 200}
!18 = !{!12, !10, i64 192}
!19 = distinct !{!19, !16}
!20 = !{!12, !6, i64 216}
!21 = !{!12, !10, i64 208}
!22 = distinct !{!22, !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"

%struct.HistogramLiteral = type { [256 x i32], i64, double }
%struct.HistogramCommand = type { [704 x i32], i64, double }
%struct.HistogramDistance = type { [520 x i32], i64, double }

; 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 @BrotliOptimizeHistograms(i64 noundef %0, i64 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #1 {
  %4 = alloca [704 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 704, ptr nonnull %4) #3
  %5 = getelementptr inbounds i8, ptr %2, i64 184
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %.preheader, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %7 = getelementptr inbounds i8, ptr %2, i64 176
  br label %11

.preheader:                                       ; preds = %11, %3
  %8 = getelementptr inbounds i8, ptr %2, i64 200
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %.not24 = icmp eq i64 %9, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph19

.lr.ph19:                                         ; preds = %.preheader
  %10 = getelementptr inbounds i8, ptr %2, i64 192
  br label %17

11:                                               ; preds = %.lr.ph, %11
  %.017 = phi i64 [ 0, %.lr.ph ], [ %14, %11 ]
  %12 = load ptr, ptr %7, align 8, !tbaa !13
  %13 = getelementptr inbounds %struct.HistogramLiteral, ptr %12, i64 %.017
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef 256, ptr noundef %13, ptr noundef nonnull %4) #3
  %14 = add nuw i64 %.017, 1
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %11, label %.preheader, !llvm.loop !14

17:                                               ; preds = %.lr.ph19, %17
  %.118 = phi i64 [ 0, %.lr.ph19 ], [ %20, %17 ]
  %18 = load ptr, ptr %10, align 8, !tbaa !16
  %19 = getelementptr inbounds %struct.HistogramCommand, ptr %18, i64 %.118
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef 704, ptr noundef %19, ptr noundef nonnull %4) #3
  %20 = add nuw i64 %.118, 1
  %21 = load i64, ptr %8, align 8, !tbaa !12
  %22 = icmp ult i64 %20, %21
  br i1 %22, label %17, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %17, %.preheader
  %23 = add i64 %0, 16
  %24 = trunc i64 %1 to i32
  %25 = shl i32 48, %24
  %26 = zext i32 %25 to i64
  %27 = add i64 %23, %26
  %28 = getelementptr inbounds i8, ptr %2, i64 216
  %29 = load i64, ptr %28, align 8, !tbaa !18
  %.not25 = icmp eq i64 %29, 0
  br i1 %.not25, label %._crit_edge23, label %.lr.ph22

.lr.ph22:                                         ; preds = %._crit_edge
  %30 = getelementptr inbounds i8, ptr %2, i64 208
  br label %31

31:                                               ; preds = %.lr.ph22, %31
  %.220 = phi i64 [ 0, %.lr.ph22 ], [ %34, %31 ]
  %32 = load ptr, ptr %30, align 8, !tbaa !19
  %33 = getelementptr inbounds %struct.HistogramDistance, ptr %32, i64 %.220
  call void @BrotliOptimizeHuffmanCountsForRle(i64 noundef %27, ptr noundef %33, ptr noundef nonnull %4) #3
  %34 = add nuw i64 %.220, 1
  %35 = load i64, ptr %28, align 8, !tbaa !18
  %36 = icmp ult i64 %34, %35
  br i1 %36, label %31, label %._crit_edge23, !llvm.loop !20

._crit_edge23:                                    ; preds = %31, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 704, ptr nonnull %4) #3
  ret void
}

declare hidden void @BrotliOptimizeHuffmanCountsForRle(i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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 = { "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, !8, i64 184}
!6 = !{!"MetaBlockSplit", !7, i64 0, !7, i64 48, !7, i64 96, !11, i64 144, !8, i64 152, !11, i64 160, !8, i64 168, !11, i64 176, !8, i64 184, !11, i64 192, !8, i64 200, !11, i64 208, !8, i64 216}
!7 = !{!"BlockSplit", !8, i64 0, !8, i64 8, !11, i64 16, !11, i64 24, !8, i64 32, !8, i64 40}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!6, !8, i64 200}
!13 = !{!6, !11, i64 176}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !11, i64 192}
!17 = distinct !{!17, !15}
!18 = !{!6, !8, i64 216}
!19 = !{!6, !11, i64 208}
!20 = distinct !{!20, !15}


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