; LLVM IR from intel/qpl :: sources/core-iaa/sources/aecs (hw_huffman_table)

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

20:                                               ; preds = %47, %4
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = icmp ult i32 %21, %22
  br i1 %23, label %24, label %50

24:                                               ; preds = %20
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = lshr i32 %29, 15
  %31 = trunc i32 %30 to i16
  store i16 %31, ptr %13, align 2, !tbaa !11
  %32 = load i16, ptr %13, align 2, !tbaa !11
  %33 = zext i16 %32 to i32
  %34 = load i16, ptr %12, align 2, !tbaa !11
  %35 = zext i16 %34 to i32
  %36 = icmp eq i32 %33, %35
  br i1 %36, label %37, label %40

37:                                               ; preds = %24
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = add i32 %38, 1
  store i32 %39, ptr %10, align 4, !tbaa !9
  br label %47

40:                                               ; preds = %24
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = load i16, ptr %12, align 2, !tbaa !11
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = call ptr @hw_write_rl(ptr noundef %41, i16 noundef zeroext %42, i32 noundef %43, ptr noundef %44)
  store ptr %45, ptr %11, align 8, !tbaa !5
  %46 = load i16, ptr %13, align 2, !tbaa !11
  store i16 %46, ptr %12, align 2, !tbaa !11
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %40, %37
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = add i32 %48, 1
  store i32 %49, ptr %9, align 4, !tbaa !9
  br label %20, !llvm.loop !13

50:                                               ; preds = %20
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = load i16, ptr %12, align 2, !tbaa !11
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = call ptr @hw_write_rl(ptr noundef %51, i16 noundef zeroext %52, i32 noundef %53, ptr noundef %54)
  store ptr %55, ptr %11, align 8, !tbaa !5
  %56 = load ptr, ptr %11, align 8, !tbaa !5
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = ptrtoint ptr %56 to i64
  %59 = ptrtoint ptr %57 to i64
  %60 = sub i64 %58, %59
  %61 = sdiv exact i64 %60, 2
  %62 = trunc i64 %61 to i32
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %62
}

; Function Attrs: nounwind uwtable
declare hidden ptr @hw_write_rl(ptr noundef, i16 noundef zeroext, i32 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local i32 @hw_rl_encode(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = load i32, ptr %0, align 4, !tbaa !5
  %6 = lshr i32 %5, 15
  %7 = trunc i32 %6 to i16
  %8 = icmp ugt i32 %1, 1
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %wide.trip.count = zext i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %18
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %18 ]
  %.028 = phi i16 [ %7, %.lr.ph.preheader ], [ %.1, %18 ]
  %.02027 = phi ptr [ %3, %.lr.ph.preheader ], [ %.121, %18 ]
  %.02226 = phi i32 [ 1, %.lr.ph.preheader ], [ %.123, %18 ]
  %9 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = lshr i32 %10, 15
  %12 = trunc i32 %11 to i16
  %13 = icmp eq i16 %.028, %12
  br i1 %13, label %14, label %16

14:                                               ; preds = %.lr.ph
  %15 = add i32 %.02226, 1
  br label %18

16:                                               ; preds = %.lr.ph
  %17 = tail call ptr @hw_write_rl(ptr noundef %.02027, i16 noundef zeroext %.028, i32 noundef %.02226, ptr noundef %2)
  br label %18

18:                                               ; preds = %16, %14
  %.123 = phi i32 [ %15, %14 ], [ 1, %16 ]
  %.121 = phi ptr [ %.02027, %14 ], [ %17, %16 ]
  %.1 = phi i16 [ %.028, %14 ], [ %12, %16 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %18, %4
  %.022.lcssa = phi i32 [ 1, %4 ], [ %.123, %18 ]
  %.020.lcssa = phi ptr [ %3, %4 ], [ %.121, %18 ]
  %.0.lcssa = phi i16 [ %7, %4 ], [ %.1, %18 ]
  %19 = tail call ptr @hw_write_rl(ptr noundef %.020.lcssa, i16 noundef zeroext %.0.lcssa, i32 noundef %.022.lcssa, ptr noundef %2)
  %20 = ptrtoint ptr %19 to i64
  %21 = ptrtoint ptr %3 to i64
  %22 = sub i64 %20, %21
  %23 = lshr exact i64 %22, 1
  %24 = trunc i64 %23 to i32
  ret i32 %24
}

; Function Attrs: nounwind uwtable
declare hidden ptr @hw_write_rl(ptr noundef, i16 noundef zeroext, i32 noundef, ptr noundef) local_unnamed_addr #0

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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