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

; 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 void @NetblockTrim(ptr noundef, i32 noundef, i8 noundef zeroext) #2

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

10:                                               ; preds = %3
  %11 = call zeroext i8 @DebugAssertEnabled()
  %12 = icmp ne i8 %11, 0
  br i1 %12, label %13, label %23

13:                                               ; preds = %10
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %21

16:                                               ; preds = %13
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.NET_BUF, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !12
  %20 = icmp eq i32 %19, 1718968942
  br i1 %20, label %22, label %21

21:                                               ; preds = %16, %13
  call void @DebugAssert(ptr noundef @.str, i64 noundef 1176, ptr noundef @.str.7)
  br label %22

22:                                               ; preds = %21, %16
  br label %23

23:                                               ; preds = %22, %10
  br label %24

24:                                               ; preds = %23
  br label %25

25:                                               ; preds = %24
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.NET_BUF, ptr %27, i32 0, i32 9
  %29 = load i32, ptr %28, align 4, !tbaa !16
  %30 = icmp ugt i32 %26, %29
  br i1 %30, label %31, label %35

31:                                               ; preds = %25
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.NET_BUF, ptr %32, i32 0, i32 9
  %34 = load i32, ptr %33, align 4, !tbaa !16
  store i32 %34, ptr %5, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %31, %25
  store i32 0, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.NET_BUF, ptr %37, i32 0, i32 9
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = sub i32 %39, %36
  store i32 %40, ptr %38, align 4, !tbaa !16
  %41 = load i8, ptr %6, align 1, !tbaa !11
  %42 = zext i8 %41 to i32
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %35
  br label %50

45:                                               ; preds = %35
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.NET_BUF, ptr %46, i32 0, i32 8
  %48 = load i32, ptr %47, align 8, !tbaa !17
  %49 = sub i32 %48, 1
  br label %50

50:                                               ; preds = %45, %44
  %51 = phi i32 [ 0, %44 ], [ %49, %45 ]
  store i32 %51, ptr %8, align 4, !tbaa !9
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.NET_BUF, ptr %52, i32 0, i32 10
  %54 = getelementptr inbounds [1 x %struct.NET_BLOCK_OP], ptr %53, i64 0, i64 0
  store ptr %54, ptr %7, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %118, %63, %50
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %56, i64 %58
  %60 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %59, i32 0, i32 4
  %61 = load i32, ptr %60, align 8, !tbaa !18
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %71

63:                                               ; preds = %55
  %64 = load i8, ptr %6, align 1, !tbaa !11
  %65 = zext i8 %64 to i32
  %66 = icmp ne i32 %65, 0
  %67 = zext i1 %66 to i64
  %68 = select i1 %66, i32 1, i32 -1
  %69 = load i32, ptr %8, align 4, !tbaa !9
  %70 = add i32 %69, %68
  store i32 %70, ptr %8, align 4, !tbaa !9
  br label %55

71:                                               ; preds = %55
  %72 = load i32, ptr %5, align 4, !tbaa !9
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = load i32, ptr %8, align 4, !tbaa !9
  %75 = zext i32 %74 to i64
  %76 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %73, i64 %75
  %77 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %76, i32 0, i32 4
  %78 = load i32, ptr %77, align 8, !tbaa !18
  %79 = icmp ugt i32 %72, %78
  br i1 %79, label %80, label %108

80:                                               ; preds = %71
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = load i32, ptr %8, align 4, !tbaa !9
  %83 = zext i32 %82 to i64
  %84 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %81, i64 %83
  %85 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %84, i32 0, i32 4
  %86 = load i32, ptr %85, align 8, !tbaa !18
  %87 = load i32, ptr %5, align 4, !tbaa !9
  %88 = sub i32 %87, %86
  store i32 %88, ptr %5, align 4, !tbaa !9
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = load i32, ptr %8, align 4, !tbaa !9
  %91 = zext i32 %90 to i64
  %92 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %89, i64 %91
  %93 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %92, i32 0, i32 4
  %94 = load i32, ptr %93, align 8, !tbaa !18
  %95 = load i32, ptr %9, align 4, !tbaa !9
  %96 = add i32 %95, %94
  store i32 %96, ptr %9, align 4, !tbaa !9
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = load i32, ptr %8, align 4, !tbaa !9
  %99 = zext i32 %98 to i64
  %100 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %97, i64 %99
  %101 = load ptr, ptr %7, align 8, !tbaa !5
  %102 = load i32, ptr %8, align 4, !tbaa !9
  %103 = zext i32 %102 to i64
  %104 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %101, i64 %103
  %105 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %104, i32 0, i32 4
  %106 = load i32, ptr %105, align 8, !tbaa !18
  %107 = load i8, ptr %6, align 1, !tbaa !11
  call void @NetblockTrim(ptr noundef %100, i32 noundef %106, i8 noundef zeroext %107)
  br label %118

108:                                              ; preds = %71
  %109 = load i32, ptr %5, align 4, !tbaa !9
  %110 = load i32, ptr %9, align 4, !tbaa !9
  %111 = add i32 %110, %109
  store i32 %111, ptr %9, align 4, !tbaa !9
  %112 = load ptr, ptr %7, align 8, !tbaa !5
  %113 = load i32, ptr %8, align 4, !tbaa !9
  %114 = zext i32 %113 to i64
  %115 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %112, i64 %114
  %116 = load i32, ptr %5, align 4, !tbaa !9
  %117 = load i8, ptr %6, align 1, !tbaa !11
  call void @NetblockTrim(ptr noundef %115, i32 noundef %116, i8 noundef zeroext %117)
  br label %126

118:                                              ; preds = %80
  %119 = load i8, ptr %6, align 1, !tbaa !11
  %120 = zext i8 %119 to i32
  %121 = icmp ne i32 %120, 0
  %122 = zext i1 %121 to i64
  %123 = select i1 %121, i32 1, i32 -1
  %124 = load i32, ptr %8, align 4, !tbaa !9
  %125 = add i32 %124, %123
  store i32 %125, ptr %8, align 4, !tbaa !9
  br label %55

126:                                              ; preds = %108
  %127 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 %127
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"", !10, i64 0, !14, i64 8, !15, i64 16, !7, i64 32, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 120, !10, i64 128, !10, i64 132, !7, i64 136}
!14 = !{!"long long", !7, i64 0}
!15 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!16 = !{!13, !10, i64 132}
!17 = !{!13, !10, i64 128}
!18 = !{!19, !10, i64 32}
!19 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}


; 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 void @NetblockTrim(ptr noundef, i32 noundef, i8 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @NetbufTrim(ptr noundef %0, i32 noundef %1, i8 noundef zeroext %2) local_unnamed_addr #1 {
  %4 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %10, label %5

5:                                                ; preds = %3
  %.not42 = icmp eq ptr %0, null
  br i1 %.not42, label %9, label %6

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

9:                                                ; preds = %6, %5
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 1176, ptr noundef nonnull @.str.7) #3
  br label %10

10:                                               ; preds = %6, %9, %3
  %11 = getelementptr inbounds i8, ptr %0, i64 132
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %spec.select = tail call i32 @llvm.umin.i32(i32 %12, i32 %1)
  %13 = sub i32 %12, %spec.select
  store i32 %13, ptr %11, align 4, !tbaa !13
  %.not43 = icmp eq i8 %2, 0
  br i1 %.not43, label %14, label %18

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %0, i64 128
  %16 = load i32, ptr %15, align 8, !tbaa !14
  %17 = add i32 %16, -1
  br label %18

18:                                               ; preds = %10, %14
  %19 = phi i32 [ -1, %14 ], [ 1, %10 ]
  %20 = phi i32 [ %17, %14 ], [ 0, %10 ]
  %21 = getelementptr inbounds i8, ptr %0, i64 136
  br label %.outer

.outer:                                           ; preds = %34, %18
  %.1.ph = phi i32 [ %35, %34 ], [ %spec.select, %18 ]
  %.036.ph = phi i32 [ %37, %34 ], [ %20, %18 ]
  %.0.ph = phi i32 [ %36, %34 ], [ 0, %18 ]
  %22 = zext i32 %.036.ph to i64
  %23 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %21, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 32
  %25 = load i32, ptr %24, align 8, !tbaa !15
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.outer, %.lr.ph
  %.03645 = phi i32 [ %27, %.lr.ph ], [ %.036.ph, %.outer ]
  %27 = add i32 %.03645, %19
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds %struct.NET_BLOCK_OP, ptr %21, i64 %28
  %30 = getelementptr inbounds i8, ptr %29, i64 32
  %31 = load i32, ptr %30, align 8, !tbaa !15
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.outer
  %.036.lcssa = phi i32 [ %.036.ph, %.outer ], [ %27, %.lr.ph ]
  %.lcssa44 = phi ptr [ %23, %.outer ], [ %29, %.lr.ph ]
  %.lcssa = phi i32 [ %25, %.outer ], [ %31, %.lr.ph ]
  %33 = icmp ugt i32 %.1.ph, %.lcssa
  br i1 %33, label %34, label %38

34:                                               ; preds = %._crit_edge
  %35 = sub i32 %.1.ph, %.lcssa
  %36 = add i32 %.lcssa, %.0.ph
  tail call void @NetblockTrim(ptr noundef nonnull %.lcssa44, i32 noundef %.lcssa, i8 noundef zeroext %2)
  %37 = add i32 %.036.lcssa, %19
  br label %.outer

38:                                               ; preds = %._crit_edge
  %39 = add i32 %.0.ph, %.1.ph
  tail call void @NetblockTrim(ptr noundef nonnull %.lcssa44, i32 noundef %.1.ph, i8 noundef zeroext %2)
  ret i32 %39
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !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 132}
!14 = !{!6, !7, i64 128}
!15 = !{!16, !7, i64 32}
!16 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !7, i64 32}


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