; LLVM IR from intel/qpl :: sources/isal/igzip (encode_df)

; 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_code = type { %union.anon }
%union.anon = type { %struct.anon }
%struct.anon = type { i32 }
%struct.hufftables_icf = type { %union.anon.1 }
%union.anon.1 = type { %struct.anon.2 }
%struct.anon.2 = type { [31 x %struct.huff_code], [513 x %struct.huff_code] }
%struct.anon.3 = type { [288 x %struct.huff_code], [256 x %struct.huff_code] }
%struct.anon.0 = type { i16, i8, i8 }
%struct.deflate_icf = type { i32 }

; Function Attrs: nounwind uwtable
define dso_local ptr @qpl_encode_deflate_icf_base(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %struct.huff_code, align 4
  %10 = alloca %struct.huff_code, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  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) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  br label %11

11:                                               ; preds = %22, %4
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp ult ptr %12, %13
  br i1 %14, label %15, label %20

15:                                               ; preds = %11
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = call i32 @is_full(ptr noundef %16)
  %18 = icmp ne i32 %17, 0
  %19 = xor i1 %18, true
  br label %20

20:                                               ; preds = %15, %11
  %21 = phi i1 [ false, %11 ], [ %19, %15 ]
  br i1 %21, label %22, label %70

22:                                               ; preds = %20
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.hufftables_icf, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.anon.2, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load i32, ptr %26, align 4
  %28 = and i32 %27, 1023
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds [513 x %struct.huff_code], ptr %25, i64 0, i64 %29
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 4 %30, i64 4, i1 false), !tbaa.struct !9
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.hufftables_icf, ptr %31, i32 0, i32 0
  %33 = getelementptr inbounds %struct.anon.3, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i32, ptr %34, align 4
  %36 = lshr i32 %35, 10
  %37 = and i32 %36, 511
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds [288 x %struct.huff_code], ptr %33, i64 0, i64 %38
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %10, ptr align 4 %39, i64 4, i1 false), !tbaa.struct !9
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.huff_code, ptr %9, i32 0, i32 0
  %42 = load i32, ptr %41, align 4
  %43 = and i32 %42, 16777215
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds %struct.huff_code, ptr %9, i32 0, i32 0
  %46 = getelementptr inbounds %struct.anon.0, ptr %45, i32 0, i32 2
  %47 = load i8, ptr %46, align 1, !tbaa !10
  %48 = zext i8 %47 to i32
  call void @write_bits_unsafe(ptr noundef %40, i64 noundef %44, i32 noundef %48)
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.huff_code, ptr %10, i32 0, i32 0
  %51 = getelementptr inbounds %struct.anon.0, ptr %50, i32 0, i32 0
  %52 = load i16, ptr %51, align 4, !tbaa !10
  %53 = zext i16 %52 to i64
  %54 = getelementptr inbounds %struct.huff_code, ptr %10, i32 0, i32 0
  %55 = getelementptr inbounds %struct.anon.0, ptr %54, i32 0, i32 2
  %56 = load i8, ptr %55, align 1, !tbaa !10
  %57 = zext i8 %56 to i32
  call void @write_bits_unsafe(ptr noundef %49, i64 noundef %53, i32 noundef %57)
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = load i32, ptr %59, align 4
  %61 = lshr i32 %60, 19
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds %struct.huff_code, ptr %10, i32 0, i32 0
  %64 = getelementptr inbounds %struct.anon.0, ptr %63, i32 0, i32 1
  %65 = load i8, ptr %64, align 2, !tbaa !10
  %66 = zext i8 %65 to i32
  call void @write_bits_unsafe(ptr noundef %58, i64 noundef %62, i32 noundef %66)
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  call void @flush_bits(ptr noundef %67)
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.deflate_icf, ptr %68, i32 1
  store ptr %69, ptr %5, align 8, !tbaa !5
  br label %11, !llvm.loop !11

70:                                               ; preds = %20
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret ptr %71
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @is_full(ptr noundef) #2

; 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) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @write_bits_unsafe(ptr noundef, i64 noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @flush_bits(ptr noundef) #2

; 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 = { inlinehint 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{i64 0, i64 4, !10}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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.huff_code = type { %union.anon }
%union.anon = type { %struct.anon }
%struct.anon = type { i32 }

; Function Attrs: nounwind uwtable
define dso_local ptr @qpl_encode_deflate_icf_base(ptr noundef readonly %0, ptr noundef readnone %1, ptr noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = icmp ult ptr %0, %1
  br i1 %5, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %4
  %6 = getelementptr inbounds i8, ptr %3, i64 124
  br label %7

7:                                                ; preds = %.lr.ph, %9
  %.016 = phi ptr [ %0, %.lr.ph ], [ %25, %9 ]
  %8 = tail call i32 @is_full(ptr noundef %2)
  %.not = icmp eq i32 %8, 0
  br i1 %.not, label %9, label %.critedge

9:                                                ; preds = %7
  %10 = load i32, ptr %.016, align 4
  %11 = and i32 %10, 1023
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [513 x %struct.huff_code], ptr %6, i64 0, i64 %12
  %.sroa.01.sroa.0.0.copyload = load i24, ptr %13, align 4
  %.sroa.42.0..sroa_idx = getelementptr inbounds i8, ptr %13, i64 3
  %.sroa.42.0.copyload = load i8, ptr %.sroa.42.0..sroa_idx, align 1, !tbaa !5
  %14 = lshr i32 %10, 10
  %15 = and i32 %14, 511
  %16 = zext nneg i32 %15 to i64
  %17 = getelementptr inbounds [288 x %struct.huff_code], ptr %3, i64 0, i64 %16
  %.sroa.0.0.copyload = load i16, ptr %17, align 4
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %17, i64 2
  %.sroa.4.0.copyload = load i8, ptr %.sroa.4.0..sroa_idx, align 2
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %17, i64 3
  %.sroa.5.0.copyload = load i8, ptr %.sroa.5.0..sroa_idx, align 1, !tbaa !5
  %.sroa.42.0.insert.ext = zext i8 %.sroa.42.0.copyload to i32
  %18 = zext i24 %.sroa.01.sroa.0.0.copyload to i64
  tail call void @write_bits_unsafe(ptr noundef %2, i64 noundef %18, i32 noundef %.sroa.42.0.insert.ext)
  %19 = zext i16 %.sroa.0.0.copyload to i64
  %20 = zext i8 %.sroa.5.0.copyload to i32
  tail call void @write_bits_unsafe(ptr noundef %2, i64 noundef %19, i32 noundef %20)
  %21 = load i32, ptr %.016, align 4
  %22 = lshr i32 %21, 19
  %23 = zext nneg i32 %22 to i64
  %24 = zext i8 %.sroa.4.0.copyload to i32
  tail call void @write_bits_unsafe(ptr noundef %2, i64 noundef %23, i32 noundef %24)
  tail call void @flush_bits(ptr noundef %2)
  %25 = getelementptr inbounds i8, ptr %.016, i64 4
  %26 = icmp ult ptr %25, %1
  br i1 %26, label %7, label %.critedge, !llvm.loop !8

.critedge:                                        ; preds = %7, %9, %4
  %.0.lcssa = phi ptr [ %0, %4 ], [ %25, %9 ], [ %.016, %7 ]
  ret ptr %.0.lcssa
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @is_full(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @flush_bits(ptr noundef) local_unnamed_addr #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 = { inlinehint 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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