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

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

%struct.NET_BUF = type { i32, i64, %struct._LIST_ENTRY, %union.NET_IP_HEAD, ptr, ptr, [64 x i8], ptr, i32, i32, [1 x %struct.NET_BLOCK_OP] }
%struct._LIST_ENTRY = type { ptr, ptr }
%union.NET_IP_HEAD = type { ptr }
%struct.NET_BLOCK_OP = type { ptr, ptr, ptr, ptr, i32 }

@.str = external hidden unnamed_addr constant [144 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [107 x i8], align 1

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

declare zeroext i8 @DebugAssertEnabled() #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #1

; 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
declare dso_local zeroext i16 @NetblockChecksum(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @NetAddChecksum(i16 noundef zeroext, i16 noundef zeroext) #2

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

8:                                                ; preds = %1
  %9 = call zeroext i8 @DebugAssertEnabled()
  %10 = icmp ne i8 %9, 0
  br i1 %10, label %11, label %21

11:                                               ; preds = %8
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %19

14:                                               ; preds = %11
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.NET_BUF, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !9
  %18 = icmp eq i32 %17, 1718968942
  br i1 %18, label %20, label %19

19:                                               ; preds = %14, %11
  call void @DebugAssert(ptr noundef @.str, i64 noundef 1741, ptr noundef @.str.7)
  br label %20

20:                                               ; preds = %19, %14
  br label %21

21:                                               ; preds = %20, %8
  br label %22

22:                                               ; preds = %21
  br label %23

23:                                               ; preds = %22
  store i16 0, ptr %5, align 2, !tbaa !14
  store i32 0, ptr %4, align 4, !tbaa !16
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.NET_BUF, ptr %24, i32 0, i32 10
  %26 = getelementptr inbounds [1 x %struct.NET_BLOCK_OP], ptr %25, i64 0, i64 0
  store ptr %26, ptr %3, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !16
  br label %27

27:                                               ; preds = %74, %23
  %28 = load i32, ptr %7, align 4, !tbaa !16
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.NET_BUF, ptr %29, i32 0, i32 8
  %31 = load i32, ptr %30, align 8, !tbaa !17
  %32 = icmp ult i32 %28, %31
  br i1 %32, label %33, label %77

33:                                               ; preds = %27
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %7, align 4, !tbaa !16
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %34, i64 %36
  %38 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %37, i32 0, i32 4
  %39 = load i32, ptr %38, align 8, !tbaa !18
  %40 = icmp eq i32 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %33
  br label %74

42:                                               ; preds = %33
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !16
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %46, i32 0, i32 2
  %48 = load ptr, ptr %47, align 8, !tbaa !20
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load i32, ptr %7, align 4, !tbaa !16
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %49, i64 %51
  %53 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %52, i32 0, i32 4
  %54 = load i32, ptr %53, align 8, !tbaa !18
  %55 = call zeroext i16 @NetblockChecksum(ptr noundef %48, i32 noundef %54)
  store i16 %55, ptr %6, align 2, !tbaa !14
  %56 = load i32, ptr %4, align 4, !tbaa !16
  %57 = and i32 %56, 1
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %42
  %60 = load i16, ptr %6, align 2, !tbaa !14
  %61 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %60)
  store i16 %61, ptr %6, align 2, !tbaa !14
  br label %62

62:                                               ; preds = %59, %42
  %63 = load i16, ptr %6, align 2, !tbaa !14
  %64 = load i16, ptr %5, align 2, !tbaa !14
  %65 = call zeroext i16 @NetAddChecksum(i16 noundef zeroext %63, i16 noundef zeroext %64)
  store i16 %65, ptr %5, align 2, !tbaa !14
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %7, align 4, !tbaa !16
  %68 = zext i32 %67 to i64
  %69 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %66, i64 %68
  %70 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %69, i32 0, i32 4
  %71 = load i32, ptr %70, align 8, !tbaa !18
  %72 = load i32, ptr %4, align 4, !tbaa !16
  %73 = add i32 %72, %71
  store i32 %73, ptr %4, align 4, !tbaa !16
  br label %74

74:                                               ; preds = %62, %41
  %75 = load i32, ptr %7, align 4, !tbaa !16
  %76 = add i32 %75, 1
  store i32 %76, ptr %7, align 4, !tbaa !16
  br label %27, !llvm.loop !21

77:                                               ; preds = %27
  %78 = load i16, ptr %5, align 2, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i16 %78
}

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !12, i64 8, !13, i64 16, !7, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 120, !11, i64 128, !11, i64 132, !7, i64 136}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!11, !11, i64 0}
!17 = !{!10, !11, i64 128}
!18 = !{!19, !11, i64 32}
!19 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!20 = !{!19, !6, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"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"

%struct.NET_BLOCK_OP = type { ptr, ptr, ptr, ptr, i32 }

@.str = external hidden unnamed_addr constant [144 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [107 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @NetblockChecksum(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @NetAddChecksum(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @NetbufChecksum(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %8, label %3

3:                                                ; preds = %1
  %.not25 = icmp eq ptr %0, null
  br i1 %.not25, label %7, label %4

4:                                                ; preds = %3
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 1718968942
  br i1 %6, label %8, label %7

7:                                                ; preds = %4, %3
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 1741, ptr noundef nonnull @.str.7) #2
  br label %8

8:                                                ; preds = %4, %7, %1
  %9 = getelementptr inbounds i8, ptr %0, i64 136
  %10 = getelementptr inbounds i8, ptr %0, i64 128
  %11 = load i32, ptr %10, align 8, !tbaa !13
  %.not30 = icmp eq i32 %11, 0
  br i1 %.not30, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8, %28
  %12 = phi i32 [ %29, %28 ], [ %11, %8 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %28 ], [ 0, %8 ]
  %.02028 = phi i16 [ %.1, %28 ], [ 0, %8 ]
  %.02127 = phi i32 [ %.122, %28 ], [ 0, %8 ]
  %13 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %9, i64 %indvars.iv
  %14 = getelementptr inbounds i8, ptr %13, i64 32
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %28, label %17

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds i8, ptr %13, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = tail call zeroext i16 @NetblockChecksum(ptr noundef %19, i32 noundef %15)
  %21 = and i32 %.02127, 1
  %.not26 = icmp eq i32 %21, 0
  br i1 %.not26, label %24, label %22

22:                                               ; preds = %17
  %23 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext %20) #2
  br label %24

24:                                               ; preds = %22, %17
  %.019 = phi i16 [ %23, %22 ], [ %20, %17 ]
  %25 = tail call zeroext i16 @NetAddChecksum(i16 noundef zeroext %.019, i16 noundef zeroext %.02028)
  %26 = load i32, ptr %14, align 8, !tbaa !14
  %27 = add i32 %26, %.02127
  %.pre = load i32, ptr %10, align 8, !tbaa !13
  br label %28

28:                                               ; preds = %.lr.ph, %24
  %29 = phi i32 [ %12, %.lr.ph ], [ %.pre, %24 ]
  %.122 = phi i32 [ %.02127, %.lr.ph ], [ %27, %24 ]
  %.1 = phi i16 [ %.02028, %.lr.ph ], [ %25, %24 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = zext i32 %29 to i64
  %31 = icmp ult i64 %indvars.iv.next, %30
  br i1 %31, label %.lr.ph, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %28, %8
  %.020.lcssa = phi i16 [ 0, %8 ], [ %.1, %28 ]
  ret i16 %.020.lcssa
}

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) local_unnamed_addr #0

attributes #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 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16, !8, i64 32, !12, i64 40, !12, i64 48, !8, i64 56, !12, i64 120, !7, i64 128, !7, i64 132, !8, i64 136}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!"_LIST_ENTRY", !12, i64 0, !12, i64 8}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!6, !7, i64 128}
!14 = !{!15, !7, i64 32}
!15 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !7, i64 32}
!16 = !{!15, !12, i64 16}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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