; LLVM IR from Dev-XYS/Algorithms :: Insertion-Sort.ll (Z14insertion_sortv)

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

@n = external dso_local global i32, align 4
@d = external dso_local global [1000 x i32], align 16

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

5:                                                ; preds = %52, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %55

10:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %11 = load i32, ptr %1, align 4, !tbaa !5
  store i32 %11, ptr %3, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %47, %10
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store i32 5, ptr %2, align 4
  br label %50

16:                                               ; preds = %12
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = icmp slt i32 %20, %25
  br i1 %26, label %27, label %45

27:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  store i32 %31, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sub nsw i32 %32, 1
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %38
  store i32 %36, ptr %39, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %43
  store i32 %40, ptr %44, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %46

45:                                               ; preds = %16
  store i32 5, ptr %2, align 4
  br label %50

46:                                               ; preds = %27
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !9

50:                                               ; preds = %45, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %51

51:                                               ; preds = %50
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %1, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !11

55:                                               ; preds = %9
  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 = !{!"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}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [1000 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z14insertion_sortv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 1
  br i1 %2, label %.preheader.preheader, label %._crit_edge

.preheader.preheader:                             ; preds = %0
  %3 = add nsw i32 %1, -1
  %xtraiter = and i32 %3, 1
  %4 = icmp eq i32 %1, 2
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i32 %3, -2
  br label %.preheader

.preheader:                                       ; preds = %36, %.preheader.preheader.new
  %.016 = phi i32 [ 1, %.preheader.preheader.new ], [ %37, %36 ]
  %niter = phi i32 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %36 ]
  %.phi.trans.insert = zext nneg i32 %.016 to i64
  %.phi.trans.insert17 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %.phi.trans.insert
  %.pre = load i32, ptr %.phi.trans.insert17, align 4, !tbaa !5
  br label %15

._crit_edge.loopexit.unr-lcssa:                   ; preds = %36, %.preheader.preheader
  %.016.unr = phi i32 [ 1, %.preheader.preheader ], [ %37, %36 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.preheader.epil

.preheader.epil:                                  ; preds = %._crit_edge.loopexit.unr-lcssa
  %.phi.trans.insert.epil = zext nneg i32 %.016.unr to i64
  %.phi.trans.insert17.epil = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %.phi.trans.insert.epil
  %.pre.epil = load i32, ptr %.phi.trans.insert17.epil, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %11, %.preheader.epil
  %.01215.epil = phi i32 [ %.016.unr, %.preheader.epil ], [ %6, %11 ]
  %6 = add nsw i32 %.01215.epil, -1
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp slt i32 %.pre.epil, %9
  br i1 %10, label %11, label %._crit_edge

11:                                               ; preds = %5
  %12 = zext nneg i32 %.01215.epil to i64
  %13 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %12
  store i32 %9, ptr %13, align 4, !tbaa !5
  store i32 %.pre.epil, ptr %8, align 4, !tbaa !5
  %14 = icmp sgt i32 %.01215.epil, 1
  br i1 %14, label %5, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %11, %5, %0
  ret void

15:                                               ; preds = %.preheader, %21
  %.01215 = phi i32 [ %.016, %.preheader ], [ %16, %21 ]
  %16 = add nsw i32 %.01215, -1
  %17 = zext nneg i32 %16 to i64
  %18 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp slt i32 %.pre, %19
  br i1 %20, label %21, label %.preheader.1

21:                                               ; preds = %15
  %22 = zext nneg i32 %.01215 to i64
  %23 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %22
  store i32 %19, ptr %23, align 4, !tbaa !5
  store i32 %.pre, ptr %18, align 4, !tbaa !5
  %24 = icmp sgt i32 %.01215, 1
  br i1 %24, label %15, label %.preheader.1, !llvm.loop !9

.preheader.1:                                     ; preds = %15, %21
  %25 = add nuw nsw i32 %.016, 1
  %.phi.trans.insert.1 = zext nneg i32 %25 to i64
  %.phi.trans.insert17.1 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %.phi.trans.insert.1
  %.pre.1 = load i32, ptr %.phi.trans.insert17.1, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %32, %.preheader.1
  %.01215.1 = phi i32 [ %25, %.preheader.1 ], [ %27, %32 ]
  %27 = add nsw i32 %.01215.1, -1
  %28 = zext nneg i32 %27 to i64
  %29 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = icmp slt i32 %.pre.1, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %26
  %33 = zext nneg i32 %.01215.1 to i64
  %34 = getelementptr inbounds [1000 x i32], ptr @d, i64 0, i64 %33
  store i32 %30, ptr %34, align 4, !tbaa !5
  store i32 %.pre.1, ptr %29, align 4, !tbaa !5
  %35 = icmp sgt i32 %.01215.1, 1
  br i1 %35, label %26, label %36, !llvm.loop !9

36:                                               ; preds = %32, %26
  %37 = add nuw nsw i32 %.016, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.preheader, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, 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, !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}


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