; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 04. Arrays/insert and delete an element of array.ll (ZN8arrayadt11insertvalueEii)

; 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.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%class.arrayadt = type { i32, i32, ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8arrayadt11insertvalueEii(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
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 0
  br i1 %10, label %16, label %11

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

16:                                               ; preds = %11, %3
  %17 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @.str)
  br label %18

18:                                               ; preds = %16, %11
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = icmp slt i32 %19, %21
  br i1 %22, label %23, label %58

23:                                               ; preds = %18
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %26, label %58

26:                                               ; preds = %23
  %27 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %27, align 4, !tbaa !13
  %30 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !13
  store i32 %31, ptr %7, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %36, %26
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp ne i32 %33, %34
  br i1 %35, label %36, label %51

36:                                               ; preds = %32
  %37 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 2
  %38 = load ptr, ptr %37, align 8, !tbaa !14
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %38, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 2
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  store i32 %43, ptr %48, align 4, !tbaa !9
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = add nsw i32 %49, -1
  store i32 %50, ptr %7, align 4, !tbaa !9
  br label %32, !llvm.loop !15

51:                                               ; preds = %32
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = getelementptr inbounds %class.arrayadt, ptr %8, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %54, i64 %56
  store i32 %52, ptr %57, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %51, %23, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

attributes #0 = { inlinehint mustprogress 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 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 0}
!12 = !{!"_ZTS8arrayadt", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [14 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8arrayadt11insertvalueEii(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = icmp slt i32 %1, 0
  %5 = load i32, ptr %0, align 8
  %6 = icmp slt i32 %5, %1
  %or.cond14 = select i1 %4, i1 true, i1 %6
  br i1 %or.cond14, label %7, label %9

7:                                                ; preds = %3
  %8 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull @.str)
  %.pre = load i32, ptr %0, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %3, %7
  %10 = phi i32 [ %5, %3 ], [ %.pre, %7 ]
  %11 = icmp sgt i32 %10, %1
  %12 = icmp sgt i32 %1, 0
  %or.cond = and i1 %12, %11
  br i1 %or.cond, label %13, label %33

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %0, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %14, align 4, !tbaa !11
  %.not15 = icmp eq i32 %16, %1
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 8
  %.pre19 = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !12
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %13
  %17 = sext i32 %15 to i64
  %18 = add nsw i64 %17, 1
  %sext = zext nneg i32 %1 to i64
  %19 = add nsw i64 %17, 1
  %20 = sub nsw i64 %19, %sext
  %min.iters.check = icmp ult i64 %20, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %20, -8
  %ind.end = sub nsw i64 %18, %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 = sub i64 %18, %index
  %21 = getelementptr i32, ptr %.pre19, i64 %offset.idx
  %22 = getelementptr i8, ptr %21, i64 -16
  %23 = getelementptr i8, ptr %21, i64 -32
  %wide.load = load <4 x i32>, ptr %22, align 4, !tbaa !13
  %wide.load20 = load <4 x i32>, ptr %23, align 4, !tbaa !13
  %24 = getelementptr i8, ptr %21, i64 -12
  %25 = getelementptr i8, ptr %21, i64 -28
  store <4 x i32> %wide.load, ptr %24, align 4, !tbaa !13
  store <4 x i32> %wide.load20, ptr %25, align 4, !tbaa !13
  %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 !14

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %20, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

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

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %27 = getelementptr i32, ptr %.pre19, i64 %indvars.iv
  %28 = getelementptr i8, ptr %27, i64 -4
  %29 = load i32, ptr %28, align 4, !tbaa !13
  store i32 %29, ptr %27, align 4, !tbaa !13
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %30 = icmp eq i64 %indvars.iv.next, %sext
  br i1 %30, label %._crit_edge, label %scalar.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %13
  %31 = zext nneg i32 %1 to i64
  %32 = getelementptr inbounds i32, ptr %.pre19, i64 %31
  store i32 %2, ptr %32, align 4, !tbaa !13
  br label %33

33:                                               ; preds = %._crit_edge, %9
  ret void
}

attributes #0 = { inlinehint mustprogress 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 = { mustprogress 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, !7, i64 0}
!6 = !{!"_ZTS8arrayadt", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15, !16, !17}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!"llvm.loop.isvectorized", i32 1}
!17 = !{!"llvm.loop.unroll.runtime.disable"}
!18 = distinct !{!18, !15, !17, !16}


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