; LLVM IR from intel/qpl :: sources/isal/crc (crc_base)

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

@crc16tab = external hidden constant [256 x i16], align 16

; 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 zeroext i16 @qpl_crc16_t10dif_copy_base(i16 noundef zeroext %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca i16, align 2
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i16, align 2
  %10 = alloca i64, align 8
  store i16 %0, ptr %5, align 2, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  %11 = load i16, ptr %5, align 2, !tbaa !5
  store i16 %11, ptr %9, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !11
  br label %12

12:                                               ; preds = %40, %4
  %13 = load i64, ptr %10, align 8, !tbaa !11
  %14 = load i64, ptr %8, align 8, !tbaa !11
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %43

17:                                               ; preds = %12
  %18 = load i16, ptr %9, align 2, !tbaa !5
  %19 = zext i16 %18 to i32
  %20 = shl i32 %19, 8
  %21 = load i16, ptr %9, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = ashr i32 %22, 8
  %24 = load ptr, ptr %7, align 8, !tbaa !9
  %25 = load i8, ptr %24, align 1, !tbaa !13
  %26 = zext i8 %25 to i32
  %27 = xor i32 %23, %26
  %28 = and i32 %27, 255
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [256 x i16], ptr @crc16tab, i64 0, i64 %29
  %31 = load i16, ptr %30, align 2, !tbaa !5
  %32 = zext i16 %31 to i32
  %33 = xor i32 %20, %32
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %9, align 2, !tbaa !5
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds i8, ptr %35, i32 1
  store ptr %36, ptr %7, align 8, !tbaa !9
  %37 = load i8, ptr %35, align 1, !tbaa !13
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %6, align 8, !tbaa !9
  store i8 %37, ptr %38, align 1, !tbaa !13
  br label %40

40:                                               ; preds = %17
  %41 = load i64, ptr %10, align 8, !tbaa !11
  %42 = add i64 %41, 1
  store i64 %42, ptr %10, align 8, !tbaa !11
  br label %12, !llvm.loop !14

43:                                               ; preds = %16
  %44 = load i16, ptr %9, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  ret i16 %44
}

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 = !{!"short", !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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !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"

@crc16tab = external hidden local_unnamed_addr constant [256 x i16], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @qpl_crc16_t10dif_copy_base(i16 noundef zeroext %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %xtraiter = and i64 %3, 1
  %5 = icmp eq i64 %3, 1
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %3, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i16 [ poison, %.lr.ph.preheader ], [ %34, %.lr.ph ]
  %.0914.unr = phi i16 [ %0, %.lr.ph.preheader ], [ %34, %.lr.ph ]
  %.01013.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %36, %.lr.ph ]
  %.01112.unr = phi ptr [ %2, %.lr.ph.preheader ], [ %35, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %6 = shl i16 %.0914.unr, 8
  %7 = lshr i16 %.0914.unr, 8
  %8 = load i8, ptr %.01112.unr, align 1, !tbaa !5
  %9 = zext i8 %8 to i16
  %10 = xor i16 %7, %9
  %11 = zext nneg i16 %10 to i64
  %12 = getelementptr inbounds [256 x i16], ptr @crc16tab, i64 0, i64 %11
  %13 = load i16, ptr %12, align 2, !tbaa !8
  %14 = xor i16 %13, %6
  store i8 %8, ptr %.01013.unr, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %4
  %.09.lcssa = phi i16 [ %0, %4 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %14, %.lr.ph.epil ]
  ret i16 %.09.lcssa

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.0914 = phi i16 [ %0, %.lr.ph.preheader.new ], [ %34, %.lr.ph ]
  %.01013 = phi ptr [ %1, %.lr.ph.preheader.new ], [ %36, %.lr.ph ]
  %.01112 = phi ptr [ %2, %.lr.ph.preheader.new ], [ %35, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %15 = shl i16 %.0914, 8
  %16 = lshr i16 %.0914, 8
  %17 = load i8, ptr %.01112, align 1, !tbaa !5
  %18 = zext i8 %17 to i16
  %19 = xor i16 %16, %18
  %20 = zext nneg i16 %19 to i64
  %21 = getelementptr inbounds [256 x i16], ptr @crc16tab, i64 0, i64 %20
  %22 = load i16, ptr %21, align 2, !tbaa !8
  %23 = xor i16 %22, %15
  %24 = getelementptr inbounds i8, ptr %.01112, i64 1
  %25 = getelementptr inbounds i8, ptr %.01013, i64 1
  store i8 %17, ptr %.01013, align 1, !tbaa !5
  %26 = shl i16 %22, 8
  %27 = lshr i16 %23, 8
  %28 = load i8, ptr %24, align 1, !tbaa !5
  %29 = zext i8 %28 to i16
  %30 = xor i16 %27, %29
  %31 = zext nneg i16 %30 to i64
  %32 = getelementptr inbounds [256 x i16], ptr @crc16tab, i64 0, i64 %31
  %33 = load i16, ptr %32, align 2, !tbaa !8
  %34 = xor i16 %33, %26
  %35 = getelementptr inbounds i8, ptr %.01112, i64 2
  %36 = getelementptr inbounds i8, ptr %.01013, i64 2
  store i8 %28, ptr %25, align 1, !tbaa !5
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = !{!9, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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