; LLVM IR from pandax381/microps :: util (cksum16)

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

9:                                                ; preds = %13, %3
  %10 = load i16, ptr %5, align 2, !tbaa !9
  %11 = zext i16 %10 to i32
  %12 = icmp sgt i32 %11, 1
  br i1 %12, label %13, label %24

13:                                               ; preds = %9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds i16, ptr %14, i32 1
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load i16, ptr %14, align 2, !tbaa !9
  %17 = zext i16 %16 to i32
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = add i32 %18, %17
  store i32 %19, ptr %7, align 4, !tbaa !11
  %20 = load i16, ptr %5, align 2, !tbaa !9
  %21 = zext i16 %20 to i32
  %22 = sub nsw i32 %21, 2
  %23 = trunc i32 %22 to i16
  store i16 %23, ptr %5, align 2, !tbaa !9
  br label %9, !llvm.loop !13

24:                                               ; preds = %9
  %25 = load i16, ptr %5, align 2, !tbaa !9
  %26 = zext i16 %25 to i32
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %28, label %34

28:                                               ; preds = %24
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i8, ptr %29, align 1, !tbaa !15
  %31 = zext i8 %30 to i32
  %32 = load i32, ptr %7, align 4, !tbaa !11
  %33 = add i32 %32, %31
  store i32 %33, ptr %7, align 4, !tbaa !11
  br label %34

34:                                               ; preds = %28, %24
  br label %35

35:                                               ; preds = %39, %34
  %36 = load i32, ptr %7, align 4, !tbaa !11
  %37 = lshr i32 %36, 16
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %45

39:                                               ; preds = %35
  %40 = load i32, ptr %7, align 4, !tbaa !11
  %41 = and i32 %40, 65535
  %42 = load i32, ptr %7, align 4, !tbaa !11
  %43 = lshr i32 %42, 16
  %44 = add i32 %41, %43
  store i32 %44, ptr %7, align 4, !tbaa !11
  br label %35, !llvm.loop !16

45:                                               ; preds = %35
  %46 = load i32, ptr %7, align 4, !tbaa !11
  %47 = trunc i32 %46 to i16
  %48 = zext i16 %47 to i32
  %49 = xor i32 %48, -1
  %50 = trunc i32 %49 to i16
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i16 %50
}

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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !14}


; 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 @cksum16(ptr nocapture noundef readonly %0, i16 noundef zeroext %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ugt i16 %1, 1
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %5 = add i16 %1, -2
  %6 = lshr i16 %5, 1
  %narrow = add nuw i16 %6, 1
  %7 = zext i16 %narrow to i64
  %min.iters.check = icmp ult i16 %5, 14
  br i1 %min.iters.check, label %.lr.ph.preheader44, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %7, 65528
  %8 = shl nuw nsw i64 %n.vec, 1
  %ind.end = getelementptr i8, ptr %0, i64 %8
  %.cast = trunc nuw i64 %n.vec to i16
  %9 = shl i16 %.cast, 1
  %ind.end37 = sub i16 %1, %9
  %10 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %2, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %10, %vector.ph ], [ %14, %vector.body ]
  %vec.phi39 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %15, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %11 = getelementptr i8, ptr %next.gep, i64 8
  %wide.load = load <4 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load43 = load <4 x i16>, ptr %11, align 2, !tbaa !5
  %12 = zext <4 x i16> %wide.load to <4 x i32>
  %13 = zext <4 x i16> %wide.load43 to <4 x i32>
  %14 = add <4 x i32> %vec.phi, %12
  %15 = add <4 x i32> %vec.phi39, %13
  %index.next = add nuw i64 %index, 8
  %16 = icmp eq i64 %index.next, %n.vec
  br i1 %16, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %15, %14
  %17 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.vec, %7
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader44

.lr.ph.preheader44:                               ; preds = %middle.block, %.lr.ph.preheader
  %.019.ph = phi i32 [ %2, %.lr.ph.preheader ], [ %17, %middle.block ]
  %.01118.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01217.ph = phi i16 [ %1, %.lr.ph.preheader ], [ %ind.end37, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader44, %.lr.ph
  %.019 = phi i32 [ %21, %.lr.ph ], [ %.019.ph, %.lr.ph.preheader44 ]
  %.01118 = phi ptr [ %18, %.lr.ph ], [ %.01118.ph, %.lr.ph.preheader44 ]
  %.01217 = phi i16 [ %22, %.lr.ph ], [ %.01217.ph, %.lr.ph.preheader44 ]
  %18 = getelementptr inbounds i8, ptr %.01118, i64 2
  %19 = load i16, ptr %.01118, align 2, !tbaa !5
  %20 = zext i16 %19 to i32
  %21 = add i32 %.019, %20
  %22 = add i16 %.01217, -2
  %23 = icmp ugt i16 %22, 1
  br i1 %23, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  %.012.lcssa = phi i16 [ %1, %3 ], [ %ind.end37, %middle.block ], [ %22, %.lr.ph ]
  %.011.lcssa = phi ptr [ %0, %3 ], [ %ind.end, %middle.block ], [ %18, %.lr.ph ]
  %.0.lcssa = phi i32 [ %2, %3 ], [ %17, %middle.block ], [ %21, %.lr.ph ]
  %.not = icmp eq i16 %.012.lcssa, 0
  br i1 %.not, label %28, label %24

24:                                               ; preds = %._crit_edge
  %25 = load i8, ptr %.011.lcssa, align 1, !tbaa !14
  %26 = zext i8 %25 to i32
  %27 = add i32 %.0.lcssa, %26
  br label %28

28:                                               ; preds = %24, %._crit_edge
  %.1 = phi i32 [ %27, %24 ], [ %.0.lcssa, %._crit_edge ]
  %.not1622 = icmp ult i32 %.1, 65536
  br i1 %.not1622, label %._crit_edge26, label %.lr.ph25

.lr.ph25:                                         ; preds = %28, %.lr.ph25
  %.223 = phi i32 [ %31, %.lr.ph25 ], [ %.1, %28 ]
  %29 = lshr i32 %.223, 16
  %30 = and i32 %.223, 65535
  %31 = add nuw nsw i32 %30, %29
  %.not16 = icmp ult i32 %31, 65536
  br i1 %.not16, label %._crit_edge26, label %.lr.ph25, !llvm.loop !15

._crit_edge26:                                    ; preds = %.lr.ph25, %28
  %.2.lcssa = phi i32 [ %.1, %28 ], [ %31, %.lr.ph25 ]
  %32 = trunc nuw i32 %.2.lcssa to i16
  %33 = xor i16 %32, -1
  ret i16 %33
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !10}


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