; LLVM IR from intel/qpl :: sources/core-sw/src/filtering (qplc_pack_be_idx)

; 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 @px_qplc_pack_index_be_8u16u(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = 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 i32 %3, ptr %10, align 4, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = load i32, ptr %18, align 4, !tbaa !9
  store i32 %19, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  store ptr %21, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %22 = load ptr, ptr %14, align 8, !tbaa !5
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %22, i64 %24
  store ptr %25, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %60, %5
  %27 = load i32, ptr %16, align 4, !tbaa !9
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = icmp ult i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 2, ptr %17, align 4
  br label %63

31:                                               ; preds = %26
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load i32, ptr %16, align 4, !tbaa !9
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %32, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = zext i8 %36 to i32
  %38 = icmp ult i32 0, %37
  br i1 %38, label %39, label %57

39:                                               ; preds = %31
  %40 = load i32, ptr %12, align 4, !tbaa !9
  %41 = icmp ult i32 65535, %40
  br i1 %41, label %42, label %44

42:                                               ; preds = %39
  store i32 221, ptr %13, align 4, !tbaa !9
  %43 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %43, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %63

44:                                               ; preds = %39
  %45 = load ptr, ptr %14, align 8, !tbaa !5
  %46 = load ptr, ptr %15, align 8, !tbaa !5
  %47 = icmp uge ptr %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %44
  store i32 225, ptr %13, align 4, !tbaa !9
  %49 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %49, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %63

50:                                               ; preds = %44
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = trunc i32 %51 to i16
  %53 = call zeroext i16 @qplc_swap_bytes_16u(i16 noundef zeroext %52)
  %54 = load ptr, ptr %14, align 8, !tbaa !5
  store i16 %53, ptr %54, align 2, !tbaa !12
  %55 = load ptr, ptr %14, align 8, !tbaa !5
  %56 = getelementptr inbounds i16, ptr %55, i32 1
  store ptr %56, ptr %14, align 8, !tbaa !5
  br label %57

57:                                               ; preds = %50, %31
  %58 = load i32, ptr %12, align 4, !tbaa !9
  %59 = add i32 %58, 1
  store i32 %59, ptr %12, align 4, !tbaa !9
  br label %60

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

63:                                               ; preds = %48, %42, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  %64 = load i32, ptr %17, align 4
  switch i32 %64, label %71 [
    i32 2, label %65
  ]

65:                                               ; preds = %63
  %66 = load ptr, ptr %14, align 8, !tbaa !5
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %66, ptr %67, align 8, !tbaa !5
  %68 = load i32, ptr %12, align 4, !tbaa !9
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  store i32 %68, ptr %69, align 4, !tbaa !9
  %70 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %70, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %71

71:                                               ; preds = %65, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, 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
  %72 = load i32, ptr %6, align 4
  ret i32 %72
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i16 @qplc_swap_bytes_16u(i16 noundef zeroext) #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 = { 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 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 noundef i32 @px_qplc_pack_index_be_8u16u(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef %2, i32 noundef %3, ptr nocapture noundef %4) local_unnamed_addr #0 {
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !9
  %8 = zext i32 %3 to i64
  %9 = getelementptr inbounds i8, ptr %7, i64 %8
  %.not3136.not = icmp eq i32 %1, 0
  br i1 %.not3136.not, label %._crit_edge, label %.lr.ph.preheader

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %19 ]
  %.02638 = phi ptr [ %7, %.lr.ph.preheader ], [ %.127, %19 ]
  %.02937 = phi i32 [ %6, %.lr.ph.preheader ], [ %20, %19 ]
  %10 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %11 = load i8, ptr %10, align 1, !tbaa !11
  %.not = icmp eq i8 %11, 0
  br i1 %.not, label %19, label %12

12:                                               ; preds = %.lr.ph
  %13 = icmp ugt i32 %.02937, 65535
  br i1 %13, label %.thread, label %14

14:                                               ; preds = %12
  %.not30 = icmp ult ptr %.02638, %9
  br i1 %.not30, label %15, label %.thread

15:                                               ; preds = %14
  %16 = trunc nuw i32 %.02937 to i16
  %17 = tail call zeroext i16 @qplc_swap_bytes_16u(i16 noundef zeroext %16)
  store i16 %17, ptr %.02638, align 2, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %.02638, i64 2
  br label %19

19:                                               ; preds = %15, %.lr.ph
  %.127 = phi ptr [ %18, %15 ], [ %.02638, %.lr.ph ]
  %20 = add i32 %.02937, 1
  %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 !14

._crit_edge:                                      ; preds = %19, %5
  %.029.lcssa = phi i32 [ %6, %5 ], [ %20, %19 ]
  %.026.lcssa = phi ptr [ %7, %5 ], [ %.127, %19 ]
  store ptr %.026.lcssa, ptr %2, align 8, !tbaa !9
  store i32 %.029.lcssa, ptr %4, align 4, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %12, %14, %._crit_edge
  %.1 = phi i32 [ 0, %._crit_edge ], [ 221, %12 ], [ 225, %14 ]
  ret i32 %.1
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i16 @qplc_swap_bytes_16u(i16 noundef zeroext) 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 = !{!"int", !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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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