; LLVM IR from intel/qpl :: sources/middle-layer/compression/deflate/utils (huffman_table_utils)

; 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.heap_tree = type { %union.anon }
%union.anon = type { [859 x i64] }
%struct.tree_node = type { i32, i32 }
%struct.huff_code = type { %union.anon.0 }
%union.anon.0 = type { %struct.anon }
%struct.anon = type { i32 }
%struct.anon.1 = type { i16, i8, i8 }

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) #2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3qpl2ml11compression22generate_huffman_codesEP9heap_treejPjP9huff_codejj(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4, i32 noundef %5) #3 personality ptr @__gxx_personality_v0 {
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.heap_tree, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [859 x %struct.tree_node], ptr %18, i64 0, i64 0
  store ptr %19, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #6
  store i32 858, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #6
  store i32 0, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #6
  store i32 0, ptr %16, align 4, !tbaa !9
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = load i32, ptr %14, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = invoke i32 @qpl_build_huff_tree(ptr noundef %20, i64 noundef %22, i64 noundef %24)
          to label %26 unwind label %64

26:                                               ; preds = %6
  store i32 %25, ptr %14, align 4, !tbaa !9
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = load i32, ptr %14, align 4, !tbaa !9
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load i32, ptr %12, align 4, !tbaa !9
  %31 = call noundef i32 @_ZN3qpl2ml11compressionL13fix_code_lensEP9heap_treejPjj(ptr noundef %27, i32 noundef %28, ptr noundef %29, i32 noundef %30) #6
  store i32 %31, ptr %16, align 4, !tbaa !9
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = zext i32 %33 to i64
  %35 = mul i64 %34, 4
  call void @llvm.memset.p0.i64(ptr align 4 %32, i8 0, i64 %35, i1 false)
  %36 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %36, ptr %15, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %60, %26
  %38 = load i32, ptr %15, align 4, !tbaa !9
  %39 = load i32, ptr %16, align 4, !tbaa !9
  %40 = icmp ult i32 %38, %39
  br i1 %40, label %41, label %63

41:                                               ; preds = %37
  %42 = load ptr, ptr %13, align 8, !tbaa !5
  %43 = load i32, ptr %15, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds %struct.tree_node, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.tree_node, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !11
  %48 = trunc i32 %47 to i8
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = load ptr, ptr %13, align 8, !tbaa !5
  %51 = load i32, ptr %15, align 4, !tbaa !9
  %52 = zext i32 %51 to i64
  %53 = getelementptr inbounds %struct.tree_node, ptr %50, i64 %52
  %54 = getelementptr inbounds %struct.tree_node, ptr %53, i32 0, i32 0
  %55 = load i32, ptr %54, align 4, !tbaa !13
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds %struct.huff_code, ptr %49, i64 %56
  %58 = getelementptr inbounds %struct.huff_code, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds %struct.anon.1, ptr %58, i32 0, i32 2
  store i8 %48, ptr %59, align 1, !tbaa !14
  br label %60

60:                                               ; preds = %41
  %61 = load i32, ptr %15, align 4, !tbaa !9
  %62 = add i32 %61, 1
  store i32 %62, ptr %15, align 4, !tbaa !9
  br label %37, !llvm.loop !15

63:                                               ; preds = %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  ret void

64:                                               ; preds = %6
  %65 = landingpad { ptr, i32 }
          catch ptr null
  %66 = extractvalue { ptr, i32 } %65, 0
  call void @__clang_call_terminate(ptr %66) #7
  unreachable
}

declare i32 @qpl_build_huff_tree(ptr noundef, i64 noundef, i64 noundef) #4

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZN3qpl2ml11compressionL13fix_code_lensEP9heap_treejPjj(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { noinline noreturn nounwind uwtable "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 = { mustprogress 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 #4 = { "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 #5 = { inlinehint mustprogress 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 #6 = { nounwind }
attributes #7 = { noreturn 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS9tree_node", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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.tree_node = type { i32, i32 }
%struct.huff_code = type { %union.anon.0 }
%union.anon.0 = type { %struct.anon }
%struct.anon = type { i32 }

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3qpl2ml11compression22generate_huffman_codesEP9heap_treejPjP9huff_codejj(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #2 personality ptr @__gxx_personality_v0 {
  %7 = zext i32 %1 to i64
  %8 = invoke i32 @qpl_build_huff_tree(ptr noundef %0, i64 noundef %7, i64 noundef 858)
          to label %9 unwind label %51

9:                                                ; preds = %6
  %10 = tail call noundef i32 @_ZN3qpl2ml11compressionL13fix_code_lensEP9heap_treejPjj(ptr noundef %0, i32 noundef %8, ptr noundef %2, i32 noundef %5) #5
  %11 = zext i32 %4 to i64
  %12 = shl nuw nsw i64 %11, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %3, i8 0, i64 %12, i1 false)
  %invariant.gep = getelementptr inbounds i8, ptr %3, i64 3
  %13 = icmp ult i32 %8, %10
  br i1 %13, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %9
  %14 = zext i32 %8 to i64
  %wide.trip.count = zext i32 %10 to i64
  %15 = sub nsw i64 %wide.trip.count, %14
  %xtraiter = and i64 %15, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %14, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %16 = getelementptr inbounds %struct.tree_node, ptr %0, i64 %indvars.iv.prol
  %17 = getelementptr inbounds i8, ptr %16, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = trunc i32 %18 to i8
  %20 = load i32, ptr %16, align 4, !tbaa !10
  %21 = zext i32 %20 to i64
  %gep.prol = getelementptr inbounds %struct.huff_code, ptr %invariant.gep, i64 %21
  store i8 %19, ptr %gep.prol, align 1, !tbaa !11
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !12

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %14, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %22 = sub nsw i64 %14, %wide.trip.count
  %23 = icmp ugt i64 %22, -4
  br i1 %23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %24 = getelementptr inbounds %struct.tree_node, ptr %0, i64 %indvars.iv
  %25 = getelementptr inbounds i8, ptr %24, i64 4
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = trunc i32 %26 to i8
  %28 = load i32, ptr %24, align 4, !tbaa !10
  %29 = zext i32 %28 to i64
  %gep = getelementptr inbounds %struct.huff_code, ptr %invariant.gep, i64 %29
  store i8 %27, ptr %gep, align 1, !tbaa !11
  %30 = getelementptr %struct.tree_node, ptr %0, i64 %indvars.iv
  %31 = getelementptr i8, ptr %30, i64 8
  %32 = getelementptr i8, ptr %30, i64 12
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = trunc i32 %33 to i8
  %35 = load i32, ptr %31, align 4, !tbaa !10
  %36 = zext i32 %35 to i64
  %gep.1 = getelementptr inbounds %struct.huff_code, ptr %invariant.gep, i64 %36
  store i8 %34, ptr %gep.1, align 1, !tbaa !11
  %37 = getelementptr %struct.tree_node, ptr %0, i64 %indvars.iv
  %38 = getelementptr i8, ptr %37, i64 16
  %39 = getelementptr i8, ptr %37, i64 20
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = trunc i32 %40 to i8
  %42 = load i32, ptr %38, align 4, !tbaa !10
  %43 = zext i32 %42 to i64
  %gep.2 = getelementptr inbounds %struct.huff_code, ptr %invariant.gep, i64 %43
  store i8 %41, ptr %gep.2, align 1, !tbaa !11
  %44 = getelementptr %struct.tree_node, ptr %0, i64 %indvars.iv
  %45 = getelementptr i8, ptr %44, i64 24
  %46 = getelementptr i8, ptr %44, i64 28
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = trunc i32 %47 to i8
  %49 = load i32, ptr %45, align 4, !tbaa !10
  %50 = zext i32 %49 to i64
  %gep.3 = getelementptr inbounds %struct.huff_code, ptr %invariant.gep, i64 %50
  store i8 %48, ptr %gep.3, align 1, !tbaa !11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %9
  ret void

51:                                               ; preds = %6
  %52 = landingpad { ptr, i32 }
          catch ptr null
  %53 = extractvalue { ptr, i32 } %52, 0
  tail call void @__clang_call_terminate(ptr %53) #6
  unreachable
}

declare i32 @qpl_build_huff_tree(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZN3qpl2ml11compressionL13fix_code_lensEP9heap_treejPjj(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { noinline noreturn nounwind uwtable "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 = { mustprogress 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 = { "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 #4 = { inlinehint mustprogress 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 #5 = { nounwind }
attributes #6 = { noreturn 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 4}
!6 = !{!"_ZTS9tree_node", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!8, !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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