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

; 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_dt13vec_add_smallEiPKjjPj(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 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
  %11 = load ptr, ptr %6, align 8, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr %11, i64 %13
  store ptr %14, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = getelementptr inbounds i32, ptr %15, i32 1
  store ptr %16, ptr %6, align 8, !tbaa !9
  %17 = load i32, ptr %15, align 4, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !5
  %19 = add i32 %17, %18
  store i32 %19, ptr %10, align 4, !tbaa !5
  %20 = load i32, ptr %10, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = and i64 %21, 1073741823
  %23 = trunc i64 %22 to i32
  %24 = load ptr, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds i32, ptr %24, i32 1
  store ptr %25, ptr %8, align 8, !tbaa !9
  store i32 %23, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %10, align 4, !tbaa !5
  %27 = lshr i32 %26, 30
  store i32 %27, ptr %10, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %37, %4
  %29 = load i32, ptr %10, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %28
  %32 = load ptr, ptr %6, align 8, !tbaa !9
  %33 = load ptr, ptr %9, align 8, !tbaa !9
  %34 = icmp ult ptr %32, %33
  br label %35

35:                                               ; preds = %31, %28
  %36 = phi i1 [ false, %28 ], [ %34, %31 ]
  br i1 %36, label %37, label %50

37:                                               ; preds = %35
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds i32, ptr %38, i32 1
  store ptr %39, ptr %6, align 8, !tbaa !9
  %40 = load i32, ptr %38, align 4, !tbaa !5
  %41 = add i32 %40, 1
  store i32 %41, ptr %10, align 4, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !5
  %43 = zext i32 %42 to i64
  %44 = and i64 %43, 1073741823
  %45 = trunc i64 %44 to i32
  %46 = load ptr, ptr %8, align 8, !tbaa !9
  %47 = getelementptr inbounds i32, ptr %46, i32 1
  store ptr %47, ptr %8, align 8, !tbaa !9
  store i32 %45, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = lshr i32 %48, 30
  store i32 %49, ptr %10, align 4, !tbaa !5
  br label %28, !llvm.loop !11

50:                                               ; preds = %35
  br label %51

51:                                               ; preds = %55, %50
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = load ptr, ptr %9, align 8, !tbaa !9
  %54 = icmp ult ptr %52, %53
  br i1 %54, label %55, label %61

55:                                               ; preds = %51
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = getelementptr inbounds i32, ptr %56, i32 1
  store ptr %57, ptr %6, align 8, !tbaa !9
  %58 = load i32, ptr %56, align 4, !tbaa !5
  %59 = load ptr, ptr %8, align 8, !tbaa !9
  %60 = getelementptr inbounds i32, ptr %59, i32 1
  store ptr %60, ptr %8, align 8, !tbaa !9
  store i32 %58, ptr %59, align 4, !tbaa !5
  br label %51, !llvm.loop !13

61:                                               ; preds = %51
  %62 = load i32, ptr %10, align 4, !tbaa !5
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %66

64:                                               ; preds = %61
  %65 = load ptr, ptr %8, align 8, !tbaa !9
  store i32 1, ptr %65, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %64, %61
  call void @llvm.lifetime.end.p0(i64 4, 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_dt13vec_add_smallEiPKjjPj(i32 noundef %0, ptr noundef readonly %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = ptrtoint ptr %1 to i64
  %6 = sext i32 %0 to i64
  %.idx = shl nsw i64 %6, 2
  %7 = getelementptr inbounds i8, ptr %1, i64 %.idx
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = add i32 %8, %2
  %10 = and i32 %9, 1073741823
  store i32 %10, ptr %3, align 4, !tbaa !5
  %.02023 = getelementptr inbounds i8, ptr %3, i64 4
  %.01924 = getelementptr inbounds i8, ptr %1, i64 4
  %11 = icmp ugt i32 %9, 1073741823
  %12 = icmp sgt i32 %0, 1
  %13 = and i1 %11, %12
  br i1 %13, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %4
  %.020.lcssa = phi ptr [ %.02023, %4 ], [ %.020, %.lr.ph ]
  %.019.lcssa = phi ptr [ %.01924, %4 ], [ %.019, %.lr.ph ]
  %.lcssa = phi i1 [ %11, %4 ], [ %30, %.lr.ph ]
  %14 = icmp ult ptr %.019.lcssa, %7
  br i1 %14, label %.lr.ph31.preheader, label %._crit_edge

.lr.ph31.preheader:                               ; preds = %.preheader
  %.019.lcssa46 = ptrtoint ptr %.019.lcssa to i64
  %.020.lcssa45 = ptrtoint ptr %.020.lcssa to i64
  %.019.lcssa47 = ptrtoint ptr %.019.lcssa to i64
  %15 = add i64 %.idx, %5
  %16 = add i64 %.019.lcssa47, 4
  %umax = tail call i64 @llvm.umax.i64(i64 %15, i64 %16)
  %17 = xor i64 %.019.lcssa47, -1
  %18 = add i64 %umax, %17
  %19 = lshr i64 %18, 2
  %20 = add nuw nsw i64 %19, 1
  %min.iters.check = icmp ult i64 %18, 28
  %21 = sub i64 %.020.lcssa45, %.019.lcssa46
  %diff.check = icmp ult i64 %21, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph31.preheader55, label %vector.ph

.lr.ph31.preheader55:                             ; preds = %middle.block, %.lr.ph31.preheader
  %.130.ph = phi ptr [ %.019.lcssa, %.lr.ph31.preheader ], [ %ind.end, %middle.block ]
  %.12129.ph = phi ptr [ %.020.lcssa, %.lr.ph31.preheader ], [ %ind.end48, %middle.block ]
  br label %.lr.ph31

vector.ph:                                        ; preds = %.lr.ph31.preheader
  %n.vec = and i64 %20, 9223372036854775800
  %22 = shl i64 %n.vec, 2
  %ind.end = getelementptr i8, ptr %.019.lcssa, i64 %22
  %23 = shl i64 %n.vec, 2
  %ind.end48 = getelementptr i8, ptr %.020.lcssa, i64 %23
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %.019.lcssa, i64 %offset.idx
  %offset.idx51 = shl i64 %index, 2
  %next.gep52 = getelementptr i8, ptr %.020.lcssa, i64 %offset.idx51
  %24 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <4 x i32>, ptr %next.gep, align 4, !tbaa !5
  %wide.load54 = load <4 x i32>, ptr %24, align 4, !tbaa !5
  %25 = getelementptr i8, ptr %next.gep52, i64 16
  store <4 x i32> %wide.load, ptr %next.gep52, align 4, !tbaa !5
  store <4 x i32> %wide.load54, ptr %25, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %20, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph31.preheader55

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.01926 = phi ptr [ %.019, %.lr.ph ], [ %.01924, %4 ]
  %.02025 = phi ptr [ %.020, %.lr.ph ], [ %.02023, %4 ]
  %27 = load i32, ptr %.01926, align 4, !tbaa !5
  %28 = add i32 %27, 1
  %29 = and i32 %28, 1073741823
  store i32 %29, ptr %.02025, align 4, !tbaa !5
  %.020 = getelementptr inbounds i8, ptr %.02025, i64 4
  %.019 = getelementptr inbounds i8, ptr %.01926, i64 4
  %30 = icmp ugt i32 %28, 1073741823
  %31 = icmp ult ptr %.019, %7
  %32 = select i1 %30, i1 %31, i1 false
  br i1 %32, label %.lr.ph, label %.preheader, !llvm.loop !13

.lr.ph31:                                         ; preds = %.lr.ph31.preheader55, %.lr.ph31
  %.130 = phi ptr [ %33, %.lr.ph31 ], [ %.130.ph, %.lr.ph31.preheader55 ]
  %.12129 = phi ptr [ %35, %.lr.ph31 ], [ %.12129.ph, %.lr.ph31.preheader55 ]
  %33 = getelementptr inbounds i8, ptr %.130, i64 4
  %34 = load i32, ptr %.130, align 4, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %.12129, i64 4
  store i32 %34, ptr %.12129, align 4, !tbaa !5
  %36 = icmp ult ptr %33, %7
  br i1 %36, label %.lr.ph31, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph31, %middle.block, %.preheader
  %.121.lcssa = phi ptr [ %.020.lcssa, %.preheader ], [ %ind.end48, %middle.block ], [ %35, %.lr.ph31 ]
  br i1 %.lcssa, label %37, label %38

37:                                               ; preds = %._crit_edge
  store i32 1, ptr %.121.lcssa, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %37, %._crit_edge
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #1

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" }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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}
!14 = distinct !{!14, !10, !11}


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