; LLVM IR from artagnon/phoenixfs :: crc32 (compute_crc32)

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @compute_crc32(i32 noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  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
  br label %7

7:                                                ; preds = %11, %3
  %8 = load i64, ptr %6, align 8, !tbaa !11
  %9 = add i64 %8, -1
  store i64 %9, ptr %6, align 8, !tbaa !11
  %10 = icmp ne i64 %8, 0
  br i1 %10, label %11, label %25

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

25:                                               ; preds = %7
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = xor i32 %26, -1
  ret i32 %27
}

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

@crc32c_table = 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 @compute_crc32(i32 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not6 = icmp eq i64 %2, 0
  br i1 %.not6, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %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
  %4 = add nsw i64 %2, -1
  %5 = getelementptr inbounds i8, ptr %1, i64 1
  %6 = load i8, ptr %1, align 1, !tbaa !5
  %.05.tr.prol = trunc i32 %0 to i8
  %.narrow.prol = xor i8 %6, %.05.tr.prol
  %7 = zext i8 %.narrow.prol to i64
  %8 = getelementptr inbounds [256 x i32], ptr @crc32c_table, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !8
  %10 = lshr i32 %0, 8
  %11 = xor i32 %9, %10
  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 ], [ %11, %.lr.ph.prol ]
  %.09.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %4, %.lr.ph.prol ]
  %.048.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %5, %.lr.ph.prol ]
  %.057.unr = phi i32 [ %0, %.lr.ph.preheader ], [ %11, %.lr.ph.prol ]
  %12 = icmp eq i64 %2, 1
  br i1 %12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.09 = phi i64 [ %20, %.lr.ph ], [ %.09.unr, %.lr.ph.prol.loopexit ]
  %.048 = phi ptr [ %21, %.lr.ph ], [ %.048.unr, %.lr.ph.prol.loopexit ]
  %.057 = phi i32 [ %27, %.lr.ph ], [ %.057.unr, %.lr.ph.prol.loopexit ]
  %13 = getelementptr inbounds i8, ptr %.048, i64 1
  %14 = load i8, ptr %.048, align 1, !tbaa !5
  %.05.tr = trunc i32 %.057 to i8
  %.narrow = xor i8 %14, %.05.tr
  %15 = zext i8 %.narrow to i64
  %16 = getelementptr inbounds [256 x i32], ptr @crc32c_table, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !8
  %18 = lshr i32 %.057, 8
  %19 = xor i32 %17, %18
  %20 = add i64 %.09, -2
  %21 = getelementptr inbounds i8, ptr %.048, i64 2
  %22 = load i8, ptr %13, align 1, !tbaa !5
  %.05.tr.1 = trunc i32 %19 to i8
  %.narrow.1 = xor i8 %22, %.05.tr.1
  %23 = zext i8 %.narrow.1 to i64
  %24 = getelementptr inbounds [256 x i32], ptr @crc32c_table, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !8
  %26 = lshr i32 %19, 8
  %27 = xor i32 %25, %26
  %.not.1 = icmp eq i64 %20, 0
  br i1 %.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  %.05.lcssa = phi i32 [ %0, %3 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %27, %.lr.ph ]
  %28 = xor i32 %.05.lcssa, -1
  ret i32 %28
}

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,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass