; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/int/sc_nbutils.ll (ZN5sc_dt13vec_sub_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_sub_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 = zext i32 %17 to i64
  %19 = add i64 %18, 1073741824
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = sub i64 %19, %21
  %23 = trunc i64 %22 to i32
  store i32 %23, ptr %10, align 4, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !5
  %25 = zext i32 %24 to i64
  %26 = and i64 %25, 1073741823
  %27 = trunc i64 %26 to i32
  %28 = load ptr, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds i32, ptr %28, i32 1
  store ptr %29, ptr %8, align 8, !tbaa !9
  store i32 %27, ptr %28, align 4, !tbaa !5
  %30 = load i32, ptr %10, align 4, !tbaa !5
  %31 = lshr i32 %30, 30
  %32 = sub i32 1, %31
  store i32 %32, ptr %10, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %42, %4
  %34 = load i32, ptr %10, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %33
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  %38 = load ptr, ptr %9, align 8, !tbaa !9
  %39 = icmp ult ptr %37, %38
  br label %40

40:                                               ; preds = %36, %33
  %41 = phi i1 [ false, %33 ], [ %39, %36 ]
  br i1 %41, label %42, label %59

42:                                               ; preds = %40
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds i32, ptr %43, i32 1
  store ptr %44, ptr %6, align 8, !tbaa !9
  %45 = load i32, ptr %43, align 4, !tbaa !5
  %46 = zext i32 %45 to i64
  %47 = add i64 %46, 1073741824
  %48 = sub i64 %47, 1
  %49 = trunc i64 %48 to i32
  store i32 %49, ptr %10, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = zext i32 %50 to i64
  %52 = and i64 %51, 1073741823
  %53 = trunc i64 %52 to i32
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = getelementptr inbounds i32, ptr %54, i32 1
  store ptr %55, ptr %8, align 8, !tbaa !9
  store i32 %53, ptr %54, align 4, !tbaa !5
  %56 = load i32, ptr %10, align 4, !tbaa !5
  %57 = lshr i32 %56, 30
  %58 = sub i32 1, %57
  store i32 %58, ptr %10, align 4, !tbaa !5
  br label %33, !llvm.loop !11

59:                                               ; preds = %40
  br label %60

60:                                               ; preds = %64, %59
  %61 = load ptr, ptr %6, align 8, !tbaa !9
  %62 = load ptr, ptr %9, align 8, !tbaa !9
  %63 = icmp ult ptr %61, %62
  br i1 %63, label %64, label %70

64:                                               ; preds = %60
  %65 = load ptr, ptr %6, align 8, !tbaa !9
  %66 = getelementptr inbounds i32, ptr %65, i32 1
  store ptr %66, ptr %6, align 8, !tbaa !9
  %67 = load i32, ptr %65, align 4, !tbaa !5
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  %69 = getelementptr inbounds i32, ptr %68, i32 1
  store ptr %69, ptr %8, align 8, !tbaa !9
  store i32 %67, ptr %68, align 4, !tbaa !5
  br label %60, !llvm.loop !13

70:                                               ; preds = %60
  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_sub_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
  %reass.sub = sub i32 %8, %2
  %9 = and i32 %reass.sub, 1073741823
  store i32 %9, ptr %3, align 4, !tbaa !5
  %.01821 = getelementptr inbounds i8, ptr %3, i64 4
  %.01722 = getelementptr inbounds i8, ptr %1, i64 4
  %10 = icmp ugt i32 %reass.sub, 1073741823
  %11 = icmp sgt i32 %0, 1
  %12 = and i1 %10, %11
  br i1 %12, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %4
  %.018.lcssa = phi ptr [ %.01821, %4 ], [ %.018, %.lr.ph ]
  %.017.lcssa = phi ptr [ %.01722, %4 ], [ %.017, %.lr.ph ]
  %13 = icmp ult ptr %.017.lcssa, %7
  br i1 %13, label %.lr.ph29.preheader, label %._crit_edge

.lr.ph29.preheader:                               ; preds = %.preheader
  %.017.lcssa37 = ptrtoint ptr %.017.lcssa to i64
  %.018.lcssa36 = ptrtoint ptr %.018.lcssa to i64
  %.017.lcssa38 = ptrtoint ptr %.017.lcssa to i64
  %14 = add i64 %.idx, %5
  %15 = add i64 %.017.lcssa38, 4
  %umax = tail call i64 @llvm.umax.i64(i64 %14, i64 %15)
  %16 = xor i64 %.017.lcssa38, -1
  %17 = add i64 %umax, %16
  %18 = lshr i64 %17, 2
  %19 = add nuw nsw i64 %18, 1
  %min.iters.check = icmp ult i64 %17, 28
  %20 = sub i64 %.018.lcssa36, %.017.lcssa37
  %diff.check = icmp ult i64 %20, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph29.preheader46, label %vector.ph

.lr.ph29.preheader46:                             ; preds = %middle.block, %.lr.ph29.preheader
  %.128.ph = phi ptr [ %.017.lcssa, %.lr.ph29.preheader ], [ %ind.end, %middle.block ]
  %.11927.ph = phi ptr [ %.018.lcssa, %.lr.ph29.preheader ], [ %ind.end39, %middle.block ]
  br label %.lr.ph29

vector.ph:                                        ; preds = %.lr.ph29.preheader
  %n.vec = and i64 %19, 9223372036854775800
  %21 = shl i64 %n.vec, 2
  %ind.end = getelementptr i8, ptr %.017.lcssa, i64 %21
  %22 = shl i64 %n.vec, 2
  %ind.end39 = getelementptr i8, ptr %.018.lcssa, i64 %22
  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 %.017.lcssa, i64 %offset.idx
  %offset.idx42 = shl i64 %index, 2
  %next.gep43 = getelementptr i8, ptr %.018.lcssa, i64 %offset.idx42
  %23 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <4 x i32>, ptr %next.gep, align 4, !tbaa !5
  %wide.load45 = load <4 x i32>, ptr %23, align 4, !tbaa !5
  %24 = getelementptr i8, ptr %next.gep43, i64 16
  store <4 x i32> %wide.load, ptr %next.gep43, align 4, !tbaa !5
  store <4 x i32> %wide.load45, ptr %24, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %19, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph29.preheader46

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.01725 = phi ptr [ %.017, %.lr.ph ], [ %.01722, %4 ]
  %.01824 = phi ptr [ %.018, %.lr.ph ], [ %.01821, %4 ]
  %26 = load i32, ptr %.01725, align 4, !tbaa !5
  %27 = add i32 %26, 1073741823
  %28 = and i32 %27, 1073741823
  store i32 %28, ptr %.01824, align 4, !tbaa !5
  %.018 = getelementptr inbounds i8, ptr %.01824, i64 4
  %.017 = getelementptr inbounds i8, ptr %.01725, i64 4
  %.pn.mask = and i32 %27, -1073741824
  %29 = icmp ne i32 %.pn.mask, 1073741824
  %30 = icmp ult ptr %.017, %7
  %31 = select i1 %29, i1 %30, i1 false
  br i1 %31, label %.lr.ph, label %.preheader, !llvm.loop !13

.lr.ph29:                                         ; preds = %.lr.ph29.preheader46, %.lr.ph29
  %.128 = phi ptr [ %32, %.lr.ph29 ], [ %.128.ph, %.lr.ph29.preheader46 ]
  %.11927 = phi ptr [ %34, %.lr.ph29 ], [ %.11927.ph, %.lr.ph29.preheader46 ]
  %32 = getelementptr inbounds i8, ptr %.128, i64 4
  %33 = load i32, ptr %.128, align 4, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %.11927, i64 4
  store i32 %33, ptr %.11927, align 4, !tbaa !5
  %35 = icmp ult ptr %32, %7
  br i1 %35, label %.lr.ph29, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph29, %middle.block, %.preheader
  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: GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass