; LLVM IR from Mohammed-Shoaib/Coding-Problems :: HackerRank/Algorithms/Easy/E0094.ll (Z13insertionSortiPi)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z13insertionSortiPi(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %53, %2
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %12, label %56

12:                                               ; preds = %8
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i32, ptr %13, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  store i32 %19, ptr %6, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %33, %12
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = icmp sge i32 %21, 0
  br i1 %22, label %23, label %31

23:                                               ; preds = %20
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !9
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp slt i32 %24, %29
  br label %31

31:                                               ; preds = %23, %20
  %32 = phi i1 [ false, %20 ], [ %30, %23 ]
  br i1 %32, label %33, label %46

33:                                               ; preds = %31
  %34 = load ptr, ptr %4, align 8, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load ptr, ptr %4, align 8, !tbaa !9
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %39, i64 %42
  store i32 %38, ptr %43, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %20, !llvm.loop !11

46:                                               ; preds = %31
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %48, i64 %51
  store i32 %47, ptr %52, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %46
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !13

56:                                               ; preds = %8
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %69, %56
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = load i32, ptr %3, align 4, !tbaa !5
  %60 = icmp slt i32 %58, %59
  br i1 %60, label %61, label %72

61:                                               ; preds = %57
  %62 = load ptr, ptr %4, align 8, !tbaa !9
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds i32, ptr %62, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %66)
  %68 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %69

69:                                               ; preds = %61
  %70 = load i32, ptr %6, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %6, align 4, !tbaa !5
  br label %57, !llvm.loop !14

72:                                               ; preds = %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

declare i32 @printf(ptr noundef, ...) #2

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #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 = !{!"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}
!14 = distinct !{!14, !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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z13insertionSortiPi(i32 noundef %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %invariant.gep = getelementptr i8, ptr %1, i64 4
  %3 = icmp sgt i32 %0, 1
  br i1 %3, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %0 to i64
  %4 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %4, 1
  %5 = icmp eq i32 %0, 2
  br i1 %5, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, -2
  br label %.lr.ph

.preheader.loopexit.unr-lcssa:                    ; preds = %.critedge.1, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.critedge.1 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.loopexit.unr-lcssa
  %6 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.unr
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = trunc i64 %indvars.iv.unr to i32
  br label %9

9:                                                ; preds = %14, %.lr.ph.epil
  %.02226.in.epil = phi i32 [ %8, %.lr.ph.epil ], [ %.02226.epil, %14 ]
  %.02226.epil = add i32 %.02226.in.epil, -1
  %10 = zext nneg i32 %.02226.epil to i64
  %11 = getelementptr inbounds i32, ptr %1, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp slt i32 %7, %12
  br i1 %13, label %14, label %.critedge.epil

14:                                               ; preds = %9
  %15 = getelementptr inbounds i8, ptr %11, i64 4
  store i32 %12, ptr %15, align 4, !tbaa !5
  %16 = icmp sgt i32 %.02226.epil, 0
  br i1 %16, label %9, label %.critedge.epil, !llvm.loop !9

.critedge.epil:                                   ; preds = %14, %9
  %.022.lcssa.epil = phi i32 [ -1, %14 ], [ %.02226.epil, %9 ]
  %17 = sext i32 %.022.lcssa.epil to i64
  %gep.epil = getelementptr i32, ptr %invariant.gep, i64 %17
  store i32 %7, ptr %gep.epil, align 4, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.critedge.epil, %.preheader.loopexit.unr-lcssa, %2
  %18 = icmp sgt i32 %0, 0
  br i1 %18, label %.lr.ph29.preheader, label %._crit_edge

.lr.ph29.preheader:                               ; preds = %.preheader
  %wide.trip.count35 = zext nneg i32 %0 to i64
  br label %.lr.ph29

.lr.ph:                                           ; preds = %.critedge.1, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.critedge.1 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.critedge.1 ]
  %19 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = trunc i64 %indvars.iv to i32
  br label %22

22:                                               ; preds = %.lr.ph, %27
  %.02226.in = phi i32 [ %21, %.lr.ph ], [ %.02226, %27 ]
  %.02226 = add i32 %.02226.in, -1
  %23 = zext nneg i32 %.02226 to i64
  %24 = getelementptr inbounds i32, ptr %1, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp slt i32 %20, %25
  br i1 %26, label %27, label %.critedge

27:                                               ; preds = %22
  %28 = getelementptr inbounds i8, ptr %24, i64 4
  store i32 %25, ptr %28, align 4, !tbaa !5
  %29 = icmp sgt i32 %.02226, 0
  br i1 %29, label %22, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %27, %22
  %.022.lcssa = phi i32 [ -1, %27 ], [ %.02226, %22 ]
  %30 = sext i32 %.022.lcssa to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %30
  store i32 %20, ptr %gep, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %31 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv.next
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = trunc i64 %indvars.iv.next to i32
  br label %34

34:                                               ; preds = %39, %.critedge
  %.02226.in.1 = phi i32 [ %33, %.critedge ], [ %.02226.1, %39 ]
  %.02226.1 = add i32 %.02226.in.1, -1
  %35 = zext nneg i32 %.02226.1 to i64
  %36 = getelementptr inbounds i32, ptr %1, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp slt i32 %32, %37
  br i1 %38, label %39, label %.critedge.1

39:                                               ; preds = %34
  %40 = getelementptr inbounds i8, ptr %36, i64 4
  store i32 %37, ptr %40, align 4, !tbaa !5
  %41 = icmp sgt i32 %.02226.1, 0
  br i1 %41, label %34, label %.critedge.1, !llvm.loop !9

.critedge.1:                                      ; preds = %39, %34
  %.022.lcssa.1 = phi i32 [ -1, %39 ], [ %.02226.1, %34 ]
  %42 = sext i32 %.022.lcssa.1 to i64
  %gep.1 = getelementptr i32, ptr %invariant.gep, i64 %42
  store i32 %32, ptr %gep.1, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.lr.ph29
  %indvars.iv32 = phi i64 [ 0, %.lr.ph29.preheader ], [ %indvars.iv.next33, %.lr.ph29 ]
  %43 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv32
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %44)
  %46 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %._crit_edge, label %.lr.ph29, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph29, %.preheader
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree 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 = { nofree nounwind "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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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