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

@crc32_table_ieee_norm = external hidden constant [256 x i32], 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 i32 @qpl_crc32_ieee_base(i32 noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = xor i32 %8, -1
  store i32 %9, ptr %7, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %14, %3
  %11 = load i64, ptr %6, align 8, !tbaa !11
  %12 = add i64 %11, -1
  store i64 %12, ptr %6, align 8, !tbaa !11
  %13 = icmp ne i64 %11, 0
  br i1 %13, label %14, label %30

14:                                               ; preds = %10
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = shl i32 %15, 8
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = lshr i32 %17, 24
  %19 = load ptr, ptr %5, align 8, !tbaa !9
  %20 = load i8, ptr %19, align 1, !tbaa !13
  %21 = zext i8 %20 to i32
  %22 = xor i32 %18, %21
  %23 = and i32 %22, 255
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds [256 x i32], ptr @crc32_table_ieee_norm, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = xor i32 %16, %26
  store i32 %27, ptr %7, align 4, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %5, align 8, !tbaa !9
  br label %10, !llvm.loop !14

30:                                               ; preds = %10
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = xor i32 %31, -1
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %32
}

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 = !{!"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 = !{!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"

@crc32_table_ieee_norm = external hidden local_unnamed_addr constant [256 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @qpl_crc32_ieee_base(i32 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not8 = icmp eq i64 %2, 0
  br i1 %.not8, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = xor i32 %0, -1
  %xtraiter = and i64 %2, 1
  %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
  %5 = add nsw i64 %2, -1
  %6 = shl i32 %4, 8
  %7 = lshr i32 %4, 24
  %8 = load i8, ptr %1, align 1, !tbaa !5
  %9 = zext i8 %8 to i32
  %10 = xor i32 %7, %9
  %11 = zext nneg i32 %10 to i64
  %12 = getelementptr inbounds [256 x i32], ptr @crc32_table_ieee_norm, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !8
  %14 = xor i32 %13, %6
  %15 = getelementptr inbounds i8, ptr %1, i64 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi i32 [ poison, %.lr.ph.preheader ], [ %14, %.lr.ph.prol ]
  %.011.unr = phi i32 [ %4, %.lr.ph.preheader ], [ %14, %.lr.ph.prol ]
  %.0610.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %5, %.lr.ph.prol ]
  %.079.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %15, %.lr.ph.prol ]
  %16 = icmp eq i64 %2, 1
  br i1 %16, label %._crit_edge.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.011 = phi i32 [ %36, %.lr.ph ], [ %.011.unr, %.lr.ph.prol.loopexit ]
  %.0610 = phi i64 [ %27, %.lr.ph ], [ %.0610.unr, %.lr.ph.prol.loopexit ]
  %.079 = phi ptr [ %37, %.lr.ph ], [ %.079.unr, %.lr.ph.prol.loopexit ]
  %17 = shl i32 %.011, 8
  %18 = lshr i32 %.011, 24
  %19 = load i8, ptr %.079, align 1, !tbaa !5
  %20 = zext i8 %19 to i32
  %21 = xor i32 %18, %20
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds [256 x i32], ptr @crc32_table_ieee_norm, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !8
  %25 = xor i32 %24, %17
  %26 = getelementptr inbounds i8, ptr %.079, i64 1
  %27 = add i64 %.0610, -2
  %28 = shl i32 %25, 8
  %29 = lshr i32 %25, 24
  %30 = load i8, ptr %26, align 1, !tbaa !5
  %31 = zext i8 %30 to i32
  %32 = xor i32 %29, %31
  %33 = zext nneg i32 %32 to i64
  %34 = getelementptr inbounds [256 x i32], ptr @crc32_table_ieee_norm, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !8
  %36 = xor i32 %35, %28
  %37 = getelementptr inbounds i8, ptr %.079, i64 2
  %.not.1 = icmp eq i64 %27, 0
  br i1 %.not.1, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !10

._crit_edge.loopexit:                             ; preds = %.lr.ph, %.lr.ph.prol.loopexit
  %.lcssa = phi i32 [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %36, %.lr.ph ]
  %38 = xor i32 %.lcssa, -1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.0.lcssa = phi i32 [ %0, %3 ], [ %38, %._crit_edge.loopexit ]
  ret i32 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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