; LLVM IR from kkan2020/evacc :: firmware/main (crc16)

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

@CRC16Tab = external dso_local constant [256 x i16], align 16

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @CalcCrc16(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  store i16 %2, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %9, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %14, %3
  %11 = load i16, ptr %5, align 2, !tbaa !9
  %12 = add i16 %11, -1
  store i16 %12, ptr %5, align 2, !tbaa !9
  %13 = icmp ne i16 %11, 0
  br i1 %13, label %14, label %37

14:                                               ; preds = %10
  %15 = load i16, ptr %6, align 2, !tbaa !9
  %16 = zext i16 %15 to i32
  %17 = ashr i32 %16, 8
  %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 !11
  %22 = zext i8 %21 to i32
  %23 = xor i32 %19, %22
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %7, align 1, !tbaa !11
  %25 = load i8, ptr %7, align 1, !tbaa !11
  %26 = zext i8 %25 to i64
  %27 = getelementptr inbounds [256 x i16], ptr @CRC16Tab, i64 0, i64 %26
  %28 = load i16, ptr %27, align 2, !tbaa !9
  %29 = zext i16 %28 to i32
  %30 = load i16, ptr %6, align 2, !tbaa !9
  %31 = zext i16 %30 to i32
  %32 = shl i32 %31, 8
  %33 = xor i32 %29, %32
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %6, align 2, !tbaa !9
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i32 1
  store ptr %36, ptr %8, align 8, !tbaa !5
  br label %10, !llvm.loop !12

37:                                               ; preds = %10
  %38 = load i16, ptr %6, align 2, !tbaa !9
  %39 = zext i16 %38 to i32
  %40 = shl i32 %39, 8
  %41 = load i16, ptr %6, align 2, !tbaa !9
  %42 = zext i16 %41 to i32
  %43 = ashr i32 %42, 8
  %44 = or i32 %40, %43
  %45 = trunc i32 %44 to i16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  ret i16 %45
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@CRC16Tab = external dso_local local_unnamed_addr constant [256 x i16], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @CalcCrc16(ptr nocapture noundef readonly %0, i16 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %.not10 = icmp eq i16 %1, 0
  br i1 %.not10, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i16 %1, 1
  %lcmp.mod.not = icmp eq i16 %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 i16 %1, -1
  %5 = lshr i16 %2, 8
  %6 = load i8, ptr %0, align 1, !tbaa !5
  %7 = trunc nuw i16 %5 to i8
  %8 = xor i8 %6, %7
  %9 = zext i8 %8 to i64
  %10 = getelementptr inbounds [256 x i16], ptr @CRC16Tab, i64 0, i64 %9
  %11 = load i16, ptr %10, align 2, !tbaa !8
  %12 = shl i16 %2, 8
  %13 = xor i16 %11, %12
  %14 = getelementptr inbounds i8, ptr %0, i64 1
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi i16 [ poison, %.lr.ph.preheader ], [ %13, %.lr.ph.prol ]
  %.013.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %14, %.lr.ph.prol ]
  %.0812.unr = phi i16 [ %1, %.lr.ph.preheader ], [ %4, %.lr.ph.prol ]
  %.0911.unr = phi i16 [ %2, %.lr.ph.preheader ], [ %13, %.lr.ph.prol ]
  %15 = icmp eq i16 %1, 1
  br i1 %15, label %._crit_edge, label %.lr.ph

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

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  %.09.lcssa = phi i16 [ %2, %3 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %35, %.lr.ph ]
  %37 = tail call i16 @llvm.bswap.i16(i16 %.09.lcssa)
  ret i16 %37
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.bswap.i16(i16) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

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


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