; LLVM IR from Nigh/Basic-Compression-Library :: src (huffman)

; 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.huff_sym_t = type { i32, i32, i32, i32 }
%struct.huff_bitstream_t = type { ptr, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @Huffman_Compress(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca [256 x %struct.huff_sym_t], align 16
  %9 = alloca %struct.huff_sym_t, align 4
  %10 = alloca %struct.huff_bitstream_t, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4096, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp ult i32 %16, 1
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %106

19:                                               ; preds = %3
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  call void @_Huffman_InitBitstream(ptr noundef %10, ptr noundef %20)
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 0
  %23 = load i32, ptr %7, align 4, !tbaa !9
  call void @_Huffman_Hist(ptr noundef %21, ptr noundef %22, i32 noundef %23)
  %24 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 0
  call void @_Huffman_MakeTree(ptr noundef %24, ptr noundef %10)
  br label %25

25:                                               ; preds = %62, %19
  store i32 0, ptr %13, align 4, !tbaa !9
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %58, %25
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = icmp ult i32 %27, 255
  br i1 %28, label %29, label %61

29:                                               ; preds = %26
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.huff_sym_t, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 16, !tbaa !11
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = add i32 %35, 1
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.huff_sym_t, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 16, !tbaa !11
  %41 = icmp sgt i32 %34, %40
  br i1 %41, label %42, label %57

42:                                               ; preds = %29
  %43 = load i32, ptr %11, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %44
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 16 %45, i64 16, i1 false), !tbaa.struct !13
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %47
  %49 = load i32, ptr %11, align 4, !tbaa !9
  %50 = add i32 %49, 1
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %51
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %48, ptr align 16 %52, i64 16, i1 false), !tbaa.struct !13
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = add i32 %53, 1
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %55
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %56, ptr align 4 %9, i64 16, i1 false), !tbaa.struct !13
  store i32 1, ptr %13, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %42, %29
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = add i32 %59, 1
  store i32 %60, ptr %11, align 4, !tbaa !9
  br label %26, !llvm.loop !14

61:                                               ; preds = %26
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %13, align 4, !tbaa !9
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %25, label %65, !llvm.loop !16

65:                                               ; preds = %62
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %87, %65
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = icmp ult i32 %67, %68
  br i1 %69, label %70, label %90

70:                                               ; preds = %66
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = load i32, ptr %11, align 4, !tbaa !9
  %73 = zext i32 %72 to i64
  %74 = getelementptr inbounds i8, ptr %71, i64 %73
  %75 = load i8, ptr %74, align 1, !tbaa !17
  %76 = zext i8 %75 to i32
  store i32 %76, ptr %14, align 4, !tbaa !9
  %77 = load i32, ptr %14, align 4, !tbaa !9
  %78 = zext i32 %77 to i64
  %79 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %78
  %80 = getelementptr inbounds %struct.huff_sym_t, ptr %79, i32 0, i32 2
  %81 = load i32, ptr %80, align 8, !tbaa !18
  %82 = load i32, ptr %14, align 4, !tbaa !9
  %83 = zext i32 %82 to i64
  %84 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %8, i64 0, i64 %83
  %85 = getelementptr inbounds %struct.huff_sym_t, ptr %84, i32 0, i32 3
  %86 = load i32, ptr %85, align 4, !tbaa !19
  call void @_Huffman_WriteBits(ptr noundef %10, i32 noundef %81, i32 noundef %86)
  br label %87

87:                                               ; preds = %70
  %88 = load i32, ptr %11, align 4, !tbaa !9
  %89 = add i32 %88, 1
  store i32 %89, ptr %11, align 4, !tbaa !9
  br label %66, !llvm.loop !20

90:                                               ; preds = %66
  %91 = getelementptr inbounds %struct.huff_bitstream_t, ptr %10, i32 0, i32 0
  %92 = load ptr, ptr %91, align 8, !tbaa !21
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = ptrtoint ptr %92 to i64
  %95 = ptrtoint ptr %93 to i64
  %96 = sub i64 %94, %95
  %97 = trunc i64 %96 to i32
  store i32 %97, ptr %12, align 4, !tbaa !9
  %98 = getelementptr inbounds %struct.huff_bitstream_t, ptr %10, i32 0, i32 1
  %99 = load i32, ptr %98, align 8, !tbaa !23
  %100 = icmp ugt i32 %99, 0
  br i1 %100, label %101, label %104

101:                                              ; preds = %90
  %102 = load i32, ptr %12, align 4, !tbaa !9
  %103 = add i32 %102, 1
  store i32 %103, ptr %12, align 4, !tbaa !9
  br label %104

104:                                              ; preds = %101, %90
  %105 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %105, ptr %4, align 4
  store i32 1, ptr %15, align 4
  br label %106

106:                                              ; preds = %104, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4096, ptr %8) #3
  %107 = load i32, ptr %4, align 4
  ret i32 %107
}

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

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_InitBitstream(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_Hist(ptr noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_MakeTree(ptr noundef, ptr noundef) #0

; 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: nounwind uwtable
declare hidden void @_Huffman_WriteBits(ptr noundef, i32 noundef, i32 noundef) #0

; 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!13 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 4, !9, i64 12, i64 4, !9}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !10, i64 8}
!19 = !{!12, !10, i64 12}
!20 = distinct !{!20, !15}
!21 = !{!22, !6, i64 0}
!22 = !{!"", !6, i64 0, !10, i64 8}
!23 = !{!22, !10, i64 8}


; 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.huff_sym_t = type { i32, i32, i32, i32 }
%struct.huff_bitstream_t = type { ptr, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @Huffman_Compress(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca [256 x %struct.huff_sym_t], align 16
  %5 = alloca %struct.huff_sym_t, align 4
  %6 = alloca %struct.huff_bitstream_t, align 8
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %6) #3
  %7 = icmp eq i32 %2, 0
  br i1 %7, label %35, label %8

8:                                                ; preds = %3
  call void @_Huffman_InitBitstream(ptr noundef nonnull %6, ptr noundef %1)
  call void @_Huffman_Hist(ptr noundef %0, ptr noundef nonnull %4, i32 noundef %2)
  call void @_Huffman_MakeTree(ptr noundef nonnull %4, ptr noundef nonnull %6)
  br label %9

9:                                                ; preds = %.backedge, %8
  %indvars.iv = phi i64 [ 0, %8 ], [ %indvars.iv.be, %.backedge ]
  %.02330 = phi i32 [ 0, %8 ], [ %.02330.be, %.backedge ]
  %10 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %4, i64 0, i64 %indvars.iv
  %11 = load i32, ptr %10, align 16, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %4, i64 0, i64 %indvars.iv.next
  %13 = load i32, ptr %12, align 16, !tbaa !5
  %14 = icmp sgt i32 %11, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %9
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %5, ptr noundef nonnull align 16 dereferenceable(16) %10, i64 16, i1 false), !tbaa.struct !10
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %10, ptr noundef nonnull align 16 dereferenceable(16) %12, i64 16, i1 false), !tbaa.struct !10
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %12, ptr noundef nonnull align 4 dereferenceable(16) %5, i64 16, i1 false), !tbaa.struct !10
  br label %16

16:                                               ; preds = %9, %15
  %.1 = phi i32 [ 1, %15 ], [ %.02330, %9 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, 255
  br i1 %exitcond.not, label %17, label %.backedge

.backedge:                                        ; preds = %16, %17
  %indvars.iv.be = phi i64 [ %indvars.iv.next, %16 ], [ 0, %17 ]
  %.02330.be = phi i32 [ %.1, %16 ], [ 0, %17 ]
  br label %9, !llvm.loop !12

17:                                               ; preds = %16
  %.not = icmp eq i32 %.1, 0
  br i1 %.not, label %.preheader.preheader, label %.backedge

.preheader.preheader:                             ; preds = %17
  %wide.trip.count = zext i32 %2 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %indvars.iv33 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next34, %.preheader ]
  %18 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv33
  %19 = load i8, ptr %18, align 1, !tbaa !14
  %20 = zext i8 %19 to i64
  %21 = getelementptr inbounds [256 x %struct.huff_sym_t], ptr %4, i64 0, i64 %20
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = load i32, ptr %22, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %21, i64 12
  %25 = load i32, ptr %24, align 4, !tbaa !16
  call void @_Huffman_WriteBits(ptr noundef nonnull %6, i32 noundef %23, i32 noundef %25)
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next34, %wide.trip.count
  br i1 %exitcond36.not, label %26, label %.preheader, !llvm.loop !17

26:                                               ; preds = %.preheader
  %27 = load ptr, ptr %6, align 8, !tbaa !18
  %28 = ptrtoint ptr %27 to i64
  %29 = ptrtoint ptr %1 to i64
  %30 = sub i64 %28, %29
  %31 = trunc i64 %30 to i32
  %32 = getelementptr inbounds i8, ptr %6, i64 8
  %33 = load i32, ptr %32, align 8, !tbaa !21
  %.not28 = icmp ne i32 %33, 0
  %34 = zext i1 %.not28 to i32
  %spec.select = add i32 %31, %34
  br label %35

35:                                               ; preds = %3, %26
  %.0 = phi i32 [ %spec.select, %26 ], [ 0, %3 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5)
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %4) #3
  ret i32 %.0
}

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

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_InitBitstream(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_Hist(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @_Huffman_MakeTree(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress 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: nounwind uwtable
declare hidden void @_Huffman_WriteBits(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{i64 0, i64 4, !11, i64 4, i64 4, !11, i64 8, i64 4, !11, i64 12, i64 4, !11}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!8, !8, i64 0}
!15 = !{!6, !7, i64 8}
!16 = !{!6, !7, i64 12}
!17 = distinct !{!17, !13}
!18 = !{!19, !20, i64 0}
!19 = !{!"", !20, i64 0, !7, i64 8}
!20 = !{!"any pointer", !8, i64 0}
!21 = !{!19, !7, i64 8}


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