; LLVM IR from codemistic/Data-Structures-and-Algorithms :: Arrays/17_nsertion_and_Deletion.ll (ZN5Array6InsertEii)

; 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"

%class.Array = type { ptr, i32, i32 }

; 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 @_ZN5Array6InsertEii(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp sge i32 %9, 0
  br i1 %10, label %11, label %51

11:                                               ; preds = %3
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 2
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = icmp sle i32 %12, %14
  br i1 %15, label %16, label %51

16:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %17 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 2
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = sub nsw i32 %18, 1
  store i32 %19, ptr %7, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %38, %16
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp sgt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %41

25:                                               ; preds = %20
  %26 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %33, i64 %36
  store i32 %31, ptr %37, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %25
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr %7, align 4, !tbaa !9
  br label %20, !llvm.loop !14

41:                                               ; preds = %24
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  store i32 %42, ptr %47, align 4, !tbaa !9
  %48 = getelementptr inbounds %class.Array, ptr %8, i32 0, i32 2
  %49 = load i32, ptr %48, align 4, !tbaa !11
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %48, align 4, !tbaa !11
  br label %51

51:                                               ; preds = %41, %11, %3
  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 = !{!"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 = !{!12, !10, i64 12}
!12 = !{!"_ZTS5Array", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN5Array6InsertEii(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  %4 = icmp sgt i32 %1, -1
  br i1 %4, label %5, label %51

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not = icmp slt i32 %7, %1
  br i1 %.not, label %51, label %.preheader

.preheader:                                       ; preds = %5
  %.011 = add nsw i32 %7, -1
  %8 = icmp sgt i32 %.011, %1
  %.pre = load ptr, ptr %0, align 8, !tbaa !11
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %9 = xor i32 %1, -1
  %10 = add i32 %7, %9
  %min.iters.check = icmp ult i32 %10, 80
  br i1 %min.iters.check, label %.lr.ph.preheader31, label %vector.scevcheck

.lr.ph.preheader31:                               ; preds = %middle.block, %vector.memcheck, %vector.scevcheck, %.lr.ph.preheader
  %.013.ph = phi i32 [ %.011, %vector.memcheck ], [ %.011, %vector.scevcheck ], [ %.011, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.0.in12.ph = phi i32 [ %7, %vector.memcheck ], [ %7, %vector.scevcheck ], [ %7, %.lr.ph.preheader ], [ %ind.end24, %middle.block ]
  br label %.lr.ph

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %11 = add i32 %7, -2
  %12 = sub i32 %11, %1
  %13 = icmp ugt i32 %12, %7
  %14 = icmp ugt i32 %12, %.011
  %15 = zext i32 %7 to i64
  %16 = shl nuw nsw i64 %15, 2
  %scevgep = getelementptr i8, ptr %.pre, i64 %16
  %17 = zext i32 %12 to i64
  %mul17.neg = mul nsw i64 %17, -4
  %18 = getelementptr i8, ptr %scevgep, i64 %mul17.neg
  %19 = icmp ugt ptr %18, %scevgep
  %20 = zext i32 %.011 to i64
  %21 = shl nuw nsw i64 %20, 2
  %scevgep20 = getelementptr i8, ptr %.pre, i64 %21
  %22 = zext i32 %12 to i64
  %mul21.neg = mul nsw i64 %22, -4
  %23 = getelementptr i8, ptr %scevgep20, i64 %mul21.neg
  %24 = icmp ugt ptr %23, %scevgep20
  %25 = or i1 %13, %14
  %26 = or i1 %25, %19
  %27 = or i1 %26, %24
  br i1 %27, label %.lr.ph.preheader31, label %vector.memcheck

vector.memcheck:                                  ; preds = %vector.scevcheck
  %28 = zext i32 %.011 to i64
  %29 = zext i32 %7 to i64
  %30 = sub nsw i64 %28, %29
  %31 = and i64 %30, 4611686018427387896
  %diff.check = icmp eq i64 %31, 0
  br i1 %diff.check, label %.lr.ph.preheader31, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i32 %10, -8
  %ind.end = sub i32 %.011, %n.vec
  %ind.end24 = sub i32 %7, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i32 %.011, %index
  %offset.idx26 = sub i32 %7, %index
  %32 = zext nneg i32 %offset.idx to i64
  %33 = getelementptr inbounds i32, ptr %.pre, i64 %32
  %34 = getelementptr inbounds i8, ptr %33, i64 -12
  %35 = getelementptr inbounds i8, ptr %33, i64 -28
  %wide.load = load <4 x i32>, ptr %34, align 4, !tbaa !12
  %wide.load27 = load <4 x i32>, ptr %35, align 4, !tbaa !12
  %36 = zext nneg i32 %offset.idx26 to i64
  %37 = getelementptr inbounds i32, ptr %.pre, i64 %36
  %38 = getelementptr inbounds i8, ptr %37, i64 -12
  %39 = getelementptr inbounds i8, ptr %37, i64 -28
  store <4 x i32> %wide.load, ptr %38, align 4, !tbaa !12
  store <4 x i32> %wide.load27, ptr %39, align 4, !tbaa !12
  %index.next = add nuw i32 %index, 8
  %40 = icmp eq i32 %index.next, %n.vec
  br i1 %40, label %middle.block, label %vector.body, !llvm.loop !13

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

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %.preheader
  %41 = zext nneg i32 %1 to i64
  %42 = getelementptr inbounds i32, ptr %.pre, i64 %41
  store i32 %2, ptr %42, align 4, !tbaa !12
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %6, align 4, !tbaa !5
  br label %51

.lr.ph:                                           ; preds = %.lr.ph.preheader31, %.lr.ph
  %.013 = phi i32 [ %.0, %.lr.ph ], [ %.013.ph, %.lr.ph.preheader31 ]
  %.0.in12 = phi i32 [ %.013, %.lr.ph ], [ %.0.in12.ph, %.lr.ph.preheader31 ]
  %45 = zext nneg i32 %.013 to i64
  %46 = getelementptr inbounds i32, ptr %.pre, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !12
  %48 = zext nneg i32 %.0.in12 to i64
  %49 = getelementptr inbounds i32, ptr %.pre, i64 %48
  store i32 %47, ptr %49, align 4, !tbaa !12
  %.0 = add nsw i32 %.013, -1
  %50 = icmp sgt i32 %.0, %1
  br i1 %50, label %.lr.ph, label %._crit_edge, !llvm.loop !17

51:                                               ; preds = %._crit_edge, %5, %3
  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, !10, i64 12}
!6 = !{!"_ZTS5Array", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !15}


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