; LLVM IR from JessonYue/LeetCodeLearning :: Array/linrun/arithmetic/July_array_2.ll (Z10moveZeroesPii)

; 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: mustprogress nounwind uwtable
define dso_local void @_Z10moveZeroesPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = sub nsw i32 %9, 1
  store i32 %10, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sub nsw i32 %11, 1
  store i32 %12, ptr %6, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %53, %2
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = icmp sge i32 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %56

17:                                               ; preds = %13
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %18, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %52

24:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %25 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %25, ptr %8, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %42, %24
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %45

31:                                               ; preds = %26
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %32, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %38, i64 %40
  store i32 %37, ptr %41, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %31
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %8, align 4, !tbaa !9
  br label %26, !llvm.loop !11

45:                                               ; preds = %30
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  store i32 0, ptr %49, align 4, !tbaa !9
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %5, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %45, %17
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = add nsw i32 %54, -1
  store i32 %55, ptr %6, align 4, !tbaa !9
  br label %13, !llvm.loop !13

56:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

; 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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(argmem: readwrite) uwtable
define dso_local void @_Z10moveZeroesPii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph23.preheader, label %._crit_edge24

.lr.ph23.preheader:                               ; preds = %2
  %4 = add nsw i32 %1, -1
  %5 = zext nneg i32 %4 to i64
  br label %.lr.ph23

._crit_edge24:                                    ; preds = %29, %2
  ret void

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %29
  %indvar = phi i32 [ 0, %.lr.ph23.preheader ], [ %indvar.next, %29 ]
  %indvars.iv = phi i64 [ %5, %.lr.ph23.preheader ], [ %indvars.iv.next, %29 ]
  %.01719 = phi i32 [ %4, %.lr.ph23.preheader ], [ %.1, %29 ]
  %6 = sub i32 %1, %indvar
  %7 = sub i32 %indvar, %1
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %.preheader, label %29

.preheader:                                       ; preds = %.lr.ph23
  %11 = sext i32 %.01719 to i64
  %12 = icmp slt i64 %indvars.iv, %11
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %smax = tail call i32 @llvm.smax.i32(i32 %.01719, i32 %6)
  %13 = add i32 %smax, %7
  %14 = zext i32 %13 to i64
  %15 = add nuw nsw i64 %14, 1
  %min.iters.check = icmp ult i32 %13, 7
  br i1 %min.iters.check, label %.lr.ph.preheader31, label %vector.ph

.lr.ph.preheader31:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv26.ph = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %15, 8589934584
  %ind.end = add i64 %indvars.iv, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %indvars.iv, %index
  %16 = getelementptr i32, ptr %0, i64 %offset.idx
  %17 = getelementptr i8, ptr %16, i64 4
  %18 = getelementptr i8, ptr %16, i64 20
  %wide.load = load <4 x i32>, ptr %17, align 4, !tbaa !5
  %wide.load30 = load <4 x i32>, ptr %18, align 4, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %0, i64 %offset.idx
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store <4 x i32> %wide.load, ptr %19, align 4, !tbaa !5
  store <4 x i32> %wide.load30, ptr %20, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %21 = icmp eq i64 %index.next, %n.vec
  br i1 %21, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %15, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader31

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %.preheader
  %22 = getelementptr inbounds i32, ptr %0, i64 %11
  store i32 0, ptr %22, align 4, !tbaa !5
  %23 = add nsw i32 %.01719, -1
  br label %29

.lr.ph:                                           ; preds = %.lr.ph.preheader31, %.lr.ph
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %.lr.ph ], [ %indvars.iv26.ph, %.lr.ph.preheader31 ]
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next27
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv26
  store i32 %25, ptr %26, align 4, !tbaa !5
  %27 = trunc nuw i64 %indvars.iv.next27 to i32
  %28 = icmp sgt i32 %.01719, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !13

29:                                               ; preds = %.lr.ph23, %._crit_edge
  %.1 = phi i32 [ %23, %._crit_edge ], [ %.01719, %.lr.ph23 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %30 = icmp sgt i64 %indvars.iv, 0
  %indvar.next = add i32 %indvar, 1
  br i1 %30, label %.lr.ph23, label %._crit_edge24, !llvm.loop !14
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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, !12, !11}
!14 = distinct !{!14, !10}


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