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

; 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.EFI_TCP4_FRAGMENT_DATA = type { i32, ptr }

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

11:                                               ; preds = %24, %3
  %12 = load i32, ptr %8, align 4, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = icmp ult i32 %12, %13
  br i1 %14, label %15, label %27

15:                                               ; preds = %11
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = load ptr, ptr %7, align 8, !tbaa !9
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %17, i64 %19
  %21 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = add i32 %16, %22
  store i32 %23, ptr %9, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %15
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = add i32 %25, 1
  store i32 %26, ptr %8, align 4, !tbaa !5
  br label %11, !llvm.loop !13

27:                                               ; preds = %11
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = icmp ne i32 %28, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  store i64 -9223372036854775806, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %33

32:                                               ; preds = %27
  store i64 0, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %33

33:                                               ; preds = %32, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %34 = load i64, ptr %4, align 8
  ret i64 %34
}

; 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 = !{!"int", !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, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"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.EFI_TCP4_FRAGMENT_DATA = type { i32, ptr }

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local range(i64 -9223372036854775806, 1) i64 @Tcp4ChkDataBuf(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %.not12 = icmp eq i32 %1, 0
  br i1 %.not12, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 9
  br i1 %min.iters.check, label %.lr.ph.preheader15, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.mod.vf = and i64 %wide.trip.count, 7
  %4 = icmp eq i64 %n.mod.vf, 0
  %5 = select i1 %4, i64 8, i64 %n.mod.vf
  %n.vec = sub nsw i64 %wide.trip.count, %5
  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 ], [ %37, %vector.body ]
  %vec.phi14 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %38, %vector.body ]
  %6 = or disjoint i64 %index, 1
  %7 = or disjoint i64 %index, 2
  %8 = or disjoint i64 %index, 3
  %9 = or disjoint i64 %index, 4
  %10 = or disjoint i64 %index, 5
  %11 = or disjoint i64 %index, 6
  %12 = or disjoint i64 %index, 7
  %13 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %index
  %14 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %6
  %15 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %7
  %16 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %8
  %17 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %9
  %18 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %10
  %19 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %11
  %20 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %12
  %21 = load i32, ptr %13, align 8, !tbaa !5
  %22 = load i32, ptr %14, align 8, !tbaa !5
  %23 = load i32, ptr %15, align 8, !tbaa !5
  %24 = load i32, ptr %16, align 8, !tbaa !5
  %25 = insertelement <4 x i32> poison, i32 %21, i64 0
  %26 = insertelement <4 x i32> %25, i32 %22, i64 1
  %27 = insertelement <4 x i32> %26, i32 %23, i64 2
  %28 = insertelement <4 x i32> %27, i32 %24, i64 3
  %29 = load i32, ptr %17, align 8, !tbaa !5
  %30 = load i32, ptr %18, align 8, !tbaa !5
  %31 = load i32, ptr %19, align 8, !tbaa !5
  %32 = load i32, ptr %20, align 8, !tbaa !5
  %33 = insertelement <4 x i32> poison, i32 %29, i64 0
  %34 = insertelement <4 x i32> %33, i32 %30, i64 1
  %35 = insertelement <4 x i32> %34, i32 %31, i64 2
  %36 = insertelement <4 x i32> %35, i32 %32, i64 3
  %37 = add <4 x i32> %28, %vec.phi
  %38 = add <4 x i32> %36, %vec.phi14
  %index.next = add nuw i64 %index, 8
  %39 = icmp eq i64 %index.next, %n.vec
  br i1 %39, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %38, %37
  %40 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br label %.lr.ph.preheader15

.lr.ph.preheader15:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.011.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %40, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader15, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader15 ]
  %.011 = phi i32 [ %43, %.lr.ph ], [ %.011.ph, %.lr.ph.preheader15 ]
  %41 = getelementptr inbounds %struct.EFI_TCP4_FRAGMENT_DATA, ptr %2, i64 %indvars.iv
  %42 = load i32, ptr %41, align 8, !tbaa !5
  %43 = add i32 %42, %.011
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.0.lcssa = phi i32 [ 0, %3 ], [ %43, %.lr.ph ]
  %.not = icmp eq i32 %.0.lcssa, %0
  %. = select i1 %.not, i64 0, i64 -9223372036854775806
  ret i64 %.
}

; 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(argmem: read) 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}


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