; LLVM IR from hydrabus/hydrafw :: src/build-scripts (hex2dfu)

; 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_tab = external dso_local 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 @crc32(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 ptr, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %8 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %8, ptr %7, align 8, !tbaa !9
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = xor i32 %9, -1
  store i32 %10, ptr %4, align 4, !tbaa !5
  br label %11

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

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

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

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_tab = external dso_local local_unnamed_addr constant [256 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @crc32(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 = getelementptr inbounds i8, ptr %1, i64 1
  %7 = load i8, ptr %1, align 1, !tbaa !5
  %.07.tr.prol = trunc i32 %4 to i8
  %.narrow.prol = xor i8 %7, %.07.tr.prol
  %8 = zext i8 %.narrow.prol to i64
  %9 = getelementptr inbounds [256 x i32], ptr @crc32_tab, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !8
  %11 = lshr i32 %4, 8
  %12 = xor i32 %10, %11
  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 ], [ %12, %.lr.ph.prol ]
  %.011.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %6, %.lr.ph.prol ]
  %.0610.unr = phi i64 [ %2, %.lr.ph.preheader ], [ %5, %.lr.ph.prol ]
  %.079.unr = phi i32 [ %4, %.lr.ph.preheader ], [ %12, %.lr.ph.prol ]
  %13 = icmp eq i64 %2, 1
  br i1 %13, label %._crit_edge.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.011 = phi ptr [ %22, %.lr.ph ], [ %.011.unr, %.lr.ph.prol.loopexit ]
  %.0610 = phi i64 [ %21, %.lr.ph ], [ %.0610.unr, %.lr.ph.prol.loopexit ]
  %.079 = phi i32 [ %28, %.lr.ph ], [ %.079.unr, %.lr.ph.prol.loopexit ]
  %14 = getelementptr inbounds i8, ptr %.011, i64 1
  %15 = load i8, ptr %.011, align 1, !tbaa !5
  %.07.tr = trunc i32 %.079 to i8
  %.narrow = xor i8 %15, %.07.tr
  %16 = zext i8 %.narrow to i64
  %17 = getelementptr inbounds [256 x i32], ptr @crc32_tab, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !8
  %19 = lshr i32 %.079, 8
  %20 = xor i32 %18, %19
  %21 = add i64 %.0610, -2
  %22 = getelementptr inbounds i8, ptr %.011, i64 2
  %23 = load i8, ptr %14, align 1, !tbaa !5
  %.07.tr.1 = trunc i32 %20 to i8
  %.narrow.1 = xor i8 %23, %.07.tr.1
  %24 = zext i8 %.narrow.1 to i64
  %25 = getelementptr inbounds [256 x i32], ptr @crc32_tab, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !8
  %27 = lshr i32 %20, 8
  %28 = xor i32 %26, %27
  %.not.1 = icmp eq i64 %21, 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 ], [ %28, %.lr.ph ]
  %29 = xor i32 %.lcssa, -1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.07.lcssa = phi i32 [ %0, %3 ], [ %29, %._crit_edge.loopexit ]
  ret i32 %.07.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,SROAPass,SimplifyCFGPass