; LLVM IR from cvra/robot-software :: lib/crc (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"

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @crc16(i16 noundef zeroext %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i32, align 4
  store i16 %0, ptr %4, align 2, !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
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %10, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  %11 = load i16, ptr %4, align 2, !tbaa !5
  store i16 %11, ptr %8, align 2, !tbaa !5
  br label %12

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

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %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 = load i16, ptr %8, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = xor i32 %22, %20
  %24 = trunc i32 %23 to i16
  store i16 %24, ptr %8, align 2, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !14
  br label %25

25:                                               ; preds = %45, %16
  %26 = load i32, ptr %9, align 4, !tbaa !14
  %27 = icmp slt i32 %26, 8
  br i1 %27, label %28, label %48

28:                                               ; preds = %25
  %29 = load i16, ptr %8, align 2, !tbaa !5
  %30 = zext i16 %29 to i32
  %31 = and i32 %30, 1
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %39

33:                                               ; preds = %28
  %34 = load i16, ptr %8, align 2, !tbaa !5
  %35 = zext i16 %34 to i32
  %36 = ashr i32 %35, 1
  %37 = xor i32 %36, 40961
  %38 = trunc i32 %37 to i16
  store i16 %38, ptr %8, align 2, !tbaa !5
  br label %44

39:                                               ; preds = %28
  %40 = load i16, ptr %8, align 2, !tbaa !5
  %41 = zext i16 %40 to i32
  %42 = ashr i32 %41, 1
  %43 = trunc i32 %42 to i16
  store i16 %43, ptr %8, align 2, !tbaa !5
  br label %44

44:                                               ; preds = %39, %33
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %9, align 4, !tbaa !14
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %9, align 4, !tbaa !14
  br label %25, !llvm.loop !16

48:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %12, !llvm.loop !18

49:                                               ; preds = %12
  %50 = load i16, ptr %8, align 2, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i16 %50
}

; 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 = !{!"short", !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 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

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

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.in = phi i64 [ %4, %.lr.ph ], [ %2, %3 ]
  %.01018 = phi i16 [ %.2.7, %.lr.ph ], [ %0, %3 ]
  %.01117 = phi ptr [ %32, %.lr.ph ], [ %1, %3 ]
  %4 = add i64 %.in, -1
  %5 = load i8, ptr %.01117, align 1, !tbaa !5
  %6 = zext i8 %5 to i16
  %7 = xor i16 %.01018, %6
  %8 = and i16 %7, 1
  %.not13 = icmp eq i16 %8, 0
  %9 = lshr i16 %7, 1
  %10 = xor i16 %9, -24575
  %.2 = select i1 %.not13, i16 %9, i16 %10
  %11 = and i16 %.2, 1
  %.not13.1 = icmp eq i16 %11, 0
  %12 = lshr i16 %.2, 1
  %13 = xor i16 %12, -24575
  %.2.1 = select i1 %.not13.1, i16 %12, i16 %13
  %14 = and i16 %.2.1, 1
  %.not13.2 = icmp eq i16 %14, 0
  %15 = lshr i16 %.2.1, 1
  %16 = xor i16 %15, -24575
  %.2.2 = select i1 %.not13.2, i16 %15, i16 %16
  %17 = and i16 %.2.2, 1
  %.not13.3 = icmp eq i16 %17, 0
  %18 = lshr i16 %.2.2, 1
  %19 = xor i16 %18, -24575
  %.2.3 = select i1 %.not13.3, i16 %18, i16 %19
  %20 = and i16 %.2.3, 1
  %.not13.4 = icmp eq i16 %20, 0
  %21 = lshr i16 %.2.3, 1
  %22 = xor i16 %21, -24575
  %.2.4 = select i1 %.not13.4, i16 %21, i16 %22
  %23 = and i16 %.2.4, 1
  %.not13.5 = icmp eq i16 %23, 0
  %24 = lshr i16 %.2.4, 1
  %25 = xor i16 %24, -24575
  %.2.5 = select i1 %.not13.5, i16 %24, i16 %25
  %26 = and i16 %.2.5, 1
  %.not13.6 = icmp eq i16 %26, 0
  %27 = lshr i16 %.2.5, 1
  %28 = xor i16 %27, -24575
  %.2.6 = select i1 %.not13.6, i16 %27, i16 %28
  %29 = and i16 %.2.6, 1
  %.not13.7 = icmp eq i16 %29, 0
  %30 = lshr i16 %.2.6, 1
  %31 = xor i16 %30, -24575
  %.2.7 = select i1 %.not13.7, i16 %30, i16 %31
  %32 = getelementptr inbounds i8, ptr %.01117, i64 1
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.010.lcssa = phi i16 [ %0, %3 ], [ %.2.7, %.lr.ph ]
  ret i16 %.010.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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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