; LLVM IR from quixdb/squash :: plugins/snappy (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"

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

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

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

32:                                               ; preds = %10
  %33 = load i64, ptr %4, align 8, !tbaa !5
  %34 = and i64 %33, 4294967295
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %34
}

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

@crc_table = external hidden local_unnamed_addr constant [256 x i64], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local range(i64 0, 4294967296) i64 @crc_update(i64 noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not9 = icmp eq i64 %2, 0
  br i1 %.not9, 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 = load i8, ptr %1, align 1, !tbaa !5
  %.0.tr.prol = trunc i64 %0 to i8
  %.narrow.prol = xor i8 %5, %.0.tr.prol
  %6 = zext i8 %.narrow.prol to i64
  %7 = getelementptr inbounds [256 x i64], ptr @crc_table, i64 0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !8
  %9 = lshr i64 %0, 8
  %10 = xor i64 %8, %9
  %11 = and i64 %10, 4294967295
  %12 = 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 i64 [ poison, %.lr.ph.preheader ], [ %11, %.lr.ph.prol ]
  %.012.unr = phi i64 [ %0, %.lr.ph.preheader ], [ %11, %.lr.ph.prol ]
  %.0711.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %12, %.lr.ph.prol ]
  %.0810.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %4, %.lr.ph.prol ]
  %13 = icmp eq i64 %2, 1
  br i1 %13, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.012 = phi i64 [ %28, %.lr.ph ], [ %.012.unr, %.lr.ph.prol.loopexit ]
  %.0711 = phi ptr [ %29, %.lr.ph ], [ %.0711.unr, %.lr.ph.prol.loopexit ]
  %.0810 = phi i64 [ %21, %.lr.ph ], [ %.0810.unr, %.lr.ph.prol.loopexit ]
  %14 = load i8, ptr %.0711, align 1, !tbaa !5
  %.0.tr = trunc i64 %.012 to i8
  %.narrow = xor i8 %14, %.0.tr
  %15 = zext i8 %.narrow to i64
  %16 = getelementptr inbounds [256 x i64], ptr @crc_table, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !8
  %18 = lshr i64 %.012, 8
  %19 = xor i64 %17, %18
  %20 = getelementptr inbounds i8, ptr %.0711, i64 1
  %21 = add i64 %.0810, -2
  %22 = load i8, ptr %20, align 1, !tbaa !5
  %.0.tr.1 = trunc i64 %19 to i8
  %.narrow.1 = xor i8 %22, %.0.tr.1
  %23 = zext i8 %.narrow.1 to i64
  %24 = getelementptr inbounds [256 x i64], ptr @crc_table, i64 0, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !8
  %26 = lshr i64 %19, 8
  %27 = and i64 %26, 16777215
  %.masked = and i64 %25, 4294967295
  %28 = xor i64 %.masked, %27
  %29 = getelementptr inbounds i8, ptr %.0711, i64 2
  %.not.1 = icmp eq i64 %21, 0
  br i1 %.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  %.0.lcssa = phi i64 [ %0, %3 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %28, %.lr.ph ]
  %30 = and i64 %.0.lcssa, 4294967295
  ret i64 %30
}

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 = !{!"long", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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