; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/int/sc_nbutils.ll (ZN5sc_dt10vec_add_onEiPjiPKj)

; 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: mustprogress nounwind uwtable
define dso_local void @_ZN5sc_dt10vec_add_onEiPjiPKj(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %13 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %13, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %14 = load ptr, ptr %9, align 8, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %14, i64 %16
  store ptr %17, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %18 = load ptr, ptr %8, align 8, !tbaa !9
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %18, i64 %20
  store ptr %21, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %26, %4
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = load ptr, ptr %11, align 8, !tbaa !9
  %25 = icmp ult ptr %23, %24
  br i1 %25, label %26, label %43

26:                                               ; preds = %22
  %27 = load ptr, ptr %9, align 8, !tbaa !9
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load ptr, ptr %8, align 8, !tbaa !9
  %30 = getelementptr inbounds i32, ptr %29, i32 1
  store ptr %30, ptr %8, align 8, !tbaa !9
  %31 = load i32, ptr %29, align 4, !tbaa !5
  %32 = add i32 %28, %31
  %33 = load i32, ptr %12, align 4, !tbaa !5
  %34 = add i32 %33, %32
  store i32 %34, ptr %12, align 4, !tbaa !5
  %35 = load i32, ptr %12, align 4, !tbaa !5
  %36 = zext i32 %35 to i64
  %37 = and i64 %36, 1073741823
  %38 = trunc i64 %37 to i32
  %39 = load ptr, ptr %9, align 8, !tbaa !9
  %40 = getelementptr inbounds i32, ptr %39, i32 1
  store ptr %40, ptr %9, align 8, !tbaa !9
  store i32 %38, ptr %39, align 4, !tbaa !5
  %41 = load i32, ptr %12, align 4, !tbaa !5
  %42 = lshr i32 %41, 30
  store i32 %42, ptr %12, align 4, !tbaa !5
  br label %22, !llvm.loop !11

43:                                               ; preds = %22
  br label %44

44:                                               ; preds = %53, %43
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %51

47:                                               ; preds = %44
  %48 = load ptr, ptr %9, align 8, !tbaa !9
  %49 = load ptr, ptr %10, align 8, !tbaa !9
  %50 = icmp ult ptr %48, %49
  br label %51

51:                                               ; preds = %47, %44
  %52 = phi i1 [ false, %44 ], [ %50, %47 ]
  br i1 %52, label %53, label %65

53:                                               ; preds = %51
  %54 = load ptr, ptr %9, align 8, !tbaa !9
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = add i32 %55, 1
  store i32 %56, ptr %12, align 4, !tbaa !5
  %57 = load i32, ptr %12, align 4, !tbaa !5
  %58 = zext i32 %57 to i64
  %59 = and i64 %58, 1073741823
  %60 = trunc i64 %59 to i32
  %61 = load ptr, ptr %9, align 8, !tbaa !9
  %62 = getelementptr inbounds i32, ptr %61, i32 1
  store ptr %62, ptr %9, align 8, !tbaa !9
  store i32 %60, ptr %61, align 4, !tbaa !5
  %63 = load i32, ptr %12, align 4, !tbaa !5
  %64 = lshr i32 %63, 30
  store i32 %64, ptr %12, align 4, !tbaa !5
  br label %44, !llvm.loop !13

65:                                               ; preds = %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN5sc_dt10vec_add_onEiPjiPKj(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef readonly %3) local_unnamed_addr #0 {
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds i32, ptr %1, i64 %5
  %7 = sext i32 %2 to i64
  %8 = getelementptr inbounds i32, ptr %3, i64 %7
  %9 = icmp sgt i32 %2, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.preheader:                                       ; preds = %.lr.ph
  %10 = icmp ugt i32 %17, 1073741823
  %11 = icmp ult ptr %19, %6
  %12 = select i1 %10, i1 %11, i1 false
  br i1 %12, label %.lr.ph27, label %._crit_edge

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.024 = phi i32 [ %20, %.lr.ph ], [ 0, %4 ]
  %.01923 = phi ptr [ %14, %.lr.ph ], [ %3, %4 ]
  %.02022 = phi ptr [ %19, %.lr.ph ], [ %1, %4 ]
  %13 = load i32, ptr %.02022, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %.01923, i64 4
  %15 = load i32, ptr %.01923, align 4, !tbaa !5
  %16 = add i32 %13, %.024
  %17 = add i32 %16, %15
  %18 = and i32 %17, 1073741823
  %19 = getelementptr inbounds i8, ptr %.02022, i64 4
  store i32 %18, ptr %.02022, align 4, !tbaa !5
  %20 = lshr i32 %17, 30
  %21 = icmp ult ptr %14, %8
  br i1 %21, label %.lr.ph, label %.preheader, !llvm.loop !9

.lr.ph27:                                         ; preds = %.preheader, %.lr.ph27
  %.12126 = phi ptr [ %25, %.lr.ph27 ], [ %19, %.preheader ]
  %22 = load i32, ptr %.12126, align 4, !tbaa !5
  %23 = add i32 %22, 1
  %24 = and i32 %23, 1073741823
  %25 = getelementptr inbounds i8, ptr %.12126, i64 4
  store i32 %24, ptr %.12126, align 4, !tbaa !5
  %26 = icmp ugt i32 %23, 1073741823
  %27 = icmp ult ptr %25, %6
  %28 = select i1 %26, i1 %27, i1 false
  br i1 %28, label %.lr.ph27, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph27, %4, %.preheader
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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" }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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