; LLVM IR from carlk3/no-OS-FatFS-SD-SPI-RPi-Pico :: FatFs_SPI/sd_driver (crc)

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

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

8:                                                ; preds = %36, %3
  %9 = load i64, ptr %7, align 8, !tbaa !9
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %39

13:                                               ; preds = %8
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = load i16, ptr %14, align 2, !tbaa !11
  %16 = zext i16 %15 to i32
  %17 = shl i32 %16, 8
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = load i16, ptr %18, align 2, !tbaa !11
  %20 = zext i16 %19 to i32
  %21 = ashr i32 %20, 8
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %22, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !13
  %26 = sext i8 %25 to i32
  %27 = xor i32 %21, %26
  %28 = and i32 %27, 255
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [256 x i16], ptr @m_Crc16Table, i64 0, i64 %29
  %31 = load i16, ptr %30, align 2, !tbaa !11
  %32 = zext i16 %31 to i32
  %33 = xor i32 %17, %32
  %34 = trunc i32 %33 to i16
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  store i16 %34, ptr %35, align 2, !tbaa !11
  br label %36

36:                                               ; preds = %13
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = add i64 %37, 1
  store i64 %38, ptr %7, align 8, !tbaa !9
  br label %8, !llvm.loop !14

39:                                               ; preds = %12
  ret void
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !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"

@m_Crc16Table = external hidden local_unnamed_addr constant [256 x i16], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @update_crc16(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %.promoted = load i16, ptr %0, align 2, !tbaa !5
  %xtraiter = and i64 %2, 1
  %4 = icmp eq i64 %2, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %2, -2
  br label %15

._crit_edge.loopexit.unr-lcssa:                   ; preds = %15, %.lr.ph
  %.unr = phi i16 [ %.promoted, %.lr.ph ], [ %37, %15 ]
  %.07.unr = phi i64 [ 0, %.lr.ph ], [ %38, %15 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %5 = shl i16 %.unr, 8
  %6 = lshr i16 %.unr, 8
  %7 = getelementptr inbounds i8, ptr %1, i64 %.07.unr
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = zext i8 %8 to i16
  %10 = xor i16 %6, %9
  %11 = zext nneg i16 %10 to i64
  %12 = getelementptr inbounds [256 x i16], ptr @m_Crc16Table, i64 0, i64 %11
  %13 = load i16, ptr %12, align 2, !tbaa !5
  %14 = xor i16 %13, %5
  store i16 %14, ptr %0, align 2, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %3
  ret void

15:                                               ; preds = %15, %.lr.ph.new
  %16 = phi i16 [ %.promoted, %.lr.ph.new ], [ %37, %15 ]
  %.07 = phi i64 [ 0, %.lr.ph.new ], [ %38, %15 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %15 ]
  %17 = shl i16 %16, 8
  %18 = lshr i16 %16, 8
  %19 = getelementptr inbounds i8, ptr %1, i64 %.07
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = zext i8 %20 to i16
  %22 = xor i16 %18, %21
  %23 = zext nneg i16 %22 to i64
  %24 = getelementptr inbounds [256 x i16], ptr @m_Crc16Table, i64 0, i64 %23
  %25 = load i16, ptr %24, align 2, !tbaa !5
  %26 = xor i16 %25, %17
  store i16 %26, ptr %0, align 2, !tbaa !5
  %27 = or disjoint i64 %.07, 1
  %28 = shl i16 %25, 8
  %29 = lshr i16 %26, 8
  %30 = getelementptr inbounds i8, ptr %1, i64 %27
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = zext i8 %31 to i16
  %33 = xor i16 %29, %32
  %34 = zext nneg i16 %33 to i64
  %35 = getelementptr inbounds [256 x i16], ptr @m_Crc16Table, i64 0, i64 %34
  %36 = load i16, ptr %35, align 2, !tbaa !5
  %37 = xor i16 %36, %28
  store i16 %37, ptr %0, align 2, !tbaa !5
  %38 = add nuw i64 %.07, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %15, !llvm.loop !10
}

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


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