; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Library (NetblockChecksum)

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

6:                                                ; preds = %9, %2
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp ugt i32 %7, 1
  br i1 %8, label %9, label %19

9:                                                ; preds = %6
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = load i16, ptr %10, align 2, !tbaa !11
  %12 = zext i16 %11 to i32
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = add i32 %13, %12
  store i32 %14, ptr %5, align 4, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 2
  store ptr %16, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = sub i32 %17, 2
  store i32 %18, ptr %4, align 4, !tbaa !9
  br label %6, !llvm.loop !13

19:                                               ; preds = %6
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = icmp ugt i32 %20, 0
  br i1 %21, label %22, label %28

22:                                               ; preds = %19
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !15
  %25 = zext i8 %24 to i32
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = add i32 %26, %25
  store i32 %27, ptr %5, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %22, %19
  br label %29

29:                                               ; preds = %33, %28
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = lshr i32 %30, 16
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %39

33:                                               ; preds = %29
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = and i32 %34, 65535
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = lshr i32 %36, 16
  %38 = add i32 %35, %37
  store i32 %38, ptr %5, align 4, !tbaa !9
  br label %29, !llvm.loop !16

39:                                               ; preds = %29
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = trunc i32 %40 to i16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i16 %41
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !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 @NetblockChecksum(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ugt i32 %1, 1
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

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

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, 4294967288
  %.cast = trunc nuw i64 %n.vec to i32
  %7 = shl i32 %.cast, 1
  %ind.end = sub i32 %1, %7
  %8 = shl nuw nsw i64 %n.vec, 1
  %ind.end35 = getelementptr i8, ptr %0, i64 %8
  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> [ zeroinitializer, %vector.ph ], [ %12, %vector.body ]
  %vec.phi37 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %offset.idx39 = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx39
  %9 = getelementptr i8, ptr %next.gep, i64 8
  %wide.load = load <4 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load41 = load <4 x i16>, ptr %9, align 2, !tbaa !5
  %10 = zext <4 x i16> %wide.load to <4 x i32>
  %11 = zext <4 x i16> %wide.load41 to <4 x i32>
  %12 = add <4 x i32> %vec.phi, %10
  %13 = add <4 x i32> %vec.phi37, %11
  %index.next = add nuw i64 %index, 8
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !9

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

.lr.ph.preheader42:                               ; preds = %middle.block, %.lr.ph.preheader
  %.016.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %15, %middle.block ]
  %.01115.ph = phi i32 [ %1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01214.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end35, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader42, %.lr.ph
  %.016 = phi i32 [ %18, %.lr.ph ], [ %.016.ph, %.lr.ph.preheader42 ]
  %.01115 = phi i32 [ %20, %.lr.ph ], [ %.01115.ph, %.lr.ph.preheader42 ]
  %.01214 = phi ptr [ %19, %.lr.ph ], [ %.01214.ph, %.lr.ph.preheader42 ]
  %16 = load i16, ptr %.01214, align 2, !tbaa !5
  %17 = zext i16 %16 to i32
  %18 = add i32 %.016, %17
  %19 = getelementptr inbounds i8, ptr %.01214, i64 2
  %20 = add i32 %.01115, -2
  %21 = icmp ugt i32 %20, 1
  br i1 %21, label %.lr.ph, label %._crit_edge, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  %.012.lcssa = phi ptr [ %0, %2 ], [ %ind.end35, %middle.block ], [ %19, %.lr.ph ]
  %.011.lcssa = phi i32 [ %1, %2 ], [ %ind.end, %middle.block ], [ %20, %.lr.ph ]
  %.0.lcssa = phi i32 [ 0, %2 ], [ %15, %middle.block ], [ %18, %.lr.ph ]
  %.not25 = icmp eq i32 %.011.lcssa, 0
  br i1 %.not25, label %26, label %22

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

26:                                               ; preds = %22, %._crit_edge
  %.1 = phi i32 [ %25, %22 ], [ %.0.lcssa, %._crit_edge ]
  %.not19 = icmp ult i32 %.1, 65536
  br i1 %.not19, label %._crit_edge23, label %.lr.ph22

.lr.ph22:                                         ; preds = %26, %.lr.ph22
  %.220 = phi i32 [ %29, %.lr.ph22 ], [ %.1, %26 ]
  %27 = lshr i32 %.220, 16
  %28 = and i32 %.220, 65535
  %29 = add nuw nsw i32 %28, %27
  %.not = icmp ult i32 %29, 65536
  br i1 %.not, label %._crit_edge23, label %.lr.ph22, !llvm.loop !15

._crit_edge23:                                    ; preds = %.lr.ph22, %26
  %.2.lcssa = phi i32 [ %.1, %26 ], [ %29, %.lr.ph22 ]
  %30 = trunc nuw i32 %.2.lcssa to i16
  ret i16 %30
}

; 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