; LLVM IR from ismdeep/sort-algos-c :: include/sort-algos-int (insertion-sort-int)

; 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: nounwind uwtable
define dso_local void @insertion_sort(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
  %9 = alloca i32, align 4
  %10 = 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
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %76, %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %79

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

20:                                               ; preds = %40, %16
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %43

25:                                               ; preds = %20
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = icmp slt i32 %30, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %25
  %38 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %38, ptr %7, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %37, %25
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %8, align 4, !tbaa !9
  br label %20, !llvm.loop !11

43:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  store i32 %48, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sub nsw i32 %49, 1
  store i32 %50, ptr %10, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %67, %43
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = icmp sge i32 %52, %53
  br i1 %54, label %56, label %55

55:                                               ; preds = %51
  store i32 8, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %70

56:                                               ; preds = %51
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i32, ptr %57, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i32, ptr %62, i64 %65
  store i32 %61, ptr %66, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %56
  %68 = load i32, ptr %10, align 4, !tbaa !9
  %69 = add nsw i32 %68, -1
  store i32 %69, ptr %10, align 4, !tbaa !9
  br label %51, !llvm.loop !13

70:                                               ; preds = %55
  %71 = load i32, ptr %9, align 4, !tbaa !9
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = load i32, ptr %5, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds i32, ptr %72, i64 %74
  store i32 %71, ptr %75, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %76

76:                                               ; preds = %70
  %77 = load i32, ptr %5, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !14

79:                                               ; preds = %15
  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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @insertion_sort(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph38.preheader, label %._crit_edge39

.lr.ph38.preheader:                               ; preds = %2
  %4 = zext nneg i32 %1 to i64
  %wide.trip.count51 = zext nneg i32 %1 to i64
  br label %.lr.ph38

._crit_edge39:                                    ; preds = %._crit_edge35, %2
  ret void

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %._crit_edge35
  %indvars.iv48 = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next49, %._crit_edge35 ]
  %indvars.iv = phi i64 [ 1, %.lr.ph38.preheader ], [ %indvars.iv.next, %._crit_edge35 ]
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %5 = icmp ult i64 %indvars.iv.next49, %4
  %6 = trunc nuw nsw i64 %indvars.iv48 to i32
  br i1 %5, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph38
  %.027.lcssa = phi i32 [ %6, %.lr.ph38 ], [ %spec.select, %.lr.ph ]
  %7 = sext i32 %.027.lcssa to i64
  %8 = getelementptr inbounds i32, ptr %0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not.not31 = icmp slt i64 %indvars.iv48, %7
  br i1 %.not.not31, label %.lr.ph34.preheader, label %._crit_edge35

.lr.ph34.preheader:                               ; preds = %._crit_edge
  %10 = sub i64 %7, %indvars.iv48
  %min.iters.check = icmp ult i64 %10, 8
  br i1 %min.iters.check, label %.lr.ph34.preheader57, label %vector.ph

.lr.ph34.preheader57:                             ; preds = %middle.block, %.lr.ph34.preheader
  %indvars.iv45.ph = phi i64 [ %7, %.lr.ph34.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph34

vector.ph:                                        ; preds = %.lr.ph34.preheader
  %n.vec = and i64 %10, -8
  %ind.end = sub i64 %7, %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 %7, %index
  %11 = getelementptr i32, ptr %0, i64 %offset.idx
  %12 = getelementptr i8, ptr %11, i64 -16
  %13 = getelementptr i8, ptr %11, i64 -32
  %wide.load = load <4 x i32>, ptr %12, align 4, !tbaa !5
  %wide.load53 = load <4 x i32>, ptr %13, align 4, !tbaa !5
  %14 = getelementptr inbounds i32, ptr %0, i64 %offset.idx
  %15 = getelementptr inbounds i8, ptr %14, i64 -12
  %16 = getelementptr inbounds i8, ptr %14, i64 -28
  store <4 x i32> %wide.load, ptr %15, align 4, !tbaa !5
  store <4 x i32> %wide.load53, ptr %16, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %10, %n.vec
  br i1 %cmp.n, label %._crit_edge35, label %.lr.ph34.preheader57

.lr.ph:                                           ; preds = %.lr.ph38, %.lr.ph
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %.lr.ph ], [ %indvars.iv, %.lr.ph38 ]
  %.02729 = phi i32 [ %spec.select, %.lr.ph ], [ %6, %.lr.ph38 ]
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv40
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = sext i32 %.02729 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp slt i32 %19, %22
  %24 = trunc nuw nsw i64 %indvars.iv40 to i32
  %spec.select = select i1 %23, i32 %24, i32 %.02729
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count51
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge35:                                    ; preds = %.lr.ph34, %middle.block, %._crit_edge
  %25 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv48
  store i32 %9, ptr %25, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond52.not = icmp eq i64 %indvars.iv.next49, %wide.trip.count51
  br i1 %exitcond52.not, label %._crit_edge39, label %.lr.ph38, !llvm.loop !14

.lr.ph34:                                         ; preds = %.lr.ph34.preheader57, %.lr.ph34
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %.lr.ph34 ], [ %indvars.iv45.ph, %.lr.ph34.preheader57 ]
  %indvars.iv.next46 = add nsw i64 %indvars.iv45, -1
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next46
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv45
  store i32 %27, ptr %28, align 4, !tbaa !5
  %.not.not = icmp sgt i64 %indvars.iv.next46, %indvars.iv48
  br i1 %.not.not, label %.lr.ph34, label %._crit_edge35, !llvm.loop !15
}

attributes #0 = { 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" }

!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/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}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10, !12, !11}


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