; LLVM IR from mncoppola/rpef :: utilities_src/squashfs2.1/lzma/C/Common/CRC.ll (ZN4CCRC6UpdateEPKvm)

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

%class.CCRC = type { i32 }

@_ZN4CCRC5TableE = external dso_local global [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: mustprogress nounwind uwtable
define dso_local void @_ZN4CCRC6UpdateEPKvm(ptr noundef nonnull align 4 dereferenceable(4) %0, ptr noundef %1, i64 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %10 = getelementptr inbounds %class.CCRC, ptr %9, i32 0, i32 0
  %11 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %11, ptr %7, align 4, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %30, %3
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = icmp ugt i64 %14, 0
  br i1 %15, label %16, label %35

16:                                               ; preds = %13
  %17 = load i32, ptr %7, align 4, !tbaa !14
  %18 = trunc i32 %17 to i8
  %19 = zext i8 %18 to i32
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = load i8, ptr %20, align 1, !tbaa !15
  %22 = zext i8 %21 to i32
  %23 = xor i32 %19, %22
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [256 x i32], ptr @_ZN4CCRC5TableE, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = load i32, ptr %7, align 4, !tbaa !14
  %28 = lshr i32 %27, 8
  %29 = xor i32 %26, %28
  store i32 %29, ptr %7, align 4, !tbaa !14
  br label %30

30:                                               ; preds = %16
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = add i64 %31, -1
  store i64 %32, ptr %6, align 8, !tbaa !9
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i32 1
  store ptr %34, ptr %8, align 8, !tbaa !5
  br label %13, !llvm.loop !16

35:                                               ; preds = %13
  %36 = load i32, ptr %7, align 4, !tbaa !14
  %37 = getelementptr inbounds %class.CCRC, ptr %9, i32 0, i32 0
  store i32 %36, ptr %37, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS4CCRC", !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@_ZN4CCRC5TableE = external dso_local local_unnamed_addr global [256 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN4CCRC6UpdateEPKvm(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = load i32, ptr %0, align 4, !tbaa !5
  %.not10 = icmp eq i64 %2, 0
  br i1 %.not10, 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
  %5 = and i32 %4, 255
  %6 = load i8, ptr %1, align 1, !tbaa !10
  %7 = zext i8 %6 to i32
  %8 = xor i32 %5, %7
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds [256 x i32], ptr @_ZN4CCRC5TableE, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = lshr i32 %4, 8
  %13 = xor i32 %11, %12
  %14 = add nsw i64 %2, -1
  %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 ], [ %13, %.lr.ph.prol ]
  %.013.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %15, %.lr.ph.prol ]
  %.0812.unr = phi i32 [ %4, %.lr.ph.preheader ], [ %13, %.lr.ph.prol ]
  %.0911.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %14, %.lr.ph.prol ]
  %16 = icmp eq i64 %2, 1
  br i1 %16, label %._crit_edge, label %.lr.ph

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

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  %.08.lcssa = phi i32 [ %4, %3 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %35, %.lr.ph ]
  store i32 %.08.lcssa, ptr %0, align 4, !tbaa !5
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: 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, !7, i64 0}
!6 = !{!"_ZTS4CCRC", !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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