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

; 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, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %77, %3
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %80

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

22:                                               ; preds = %41, %18
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %22
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %44

27:                                               ; preds = %22
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  %37 = call zeroext i1 %28(ptr noundef %32, ptr noundef %36)
  br i1 %37, label %38, label %40

38:                                               ; preds = %27
  %39 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %39, ptr %9, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %38, %27
  br label %41

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

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

52:                                               ; preds = %68, %44
  %53 = load i32, ptr %12, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = icmp sge i32 %53, %54
  br i1 %55, label %57, label %56

56:                                               ; preds = %52
  store i32 8, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %71

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

68:                                               ; preds = %57
  %69 = load i32, ptr %12, align 4, !tbaa !9
  %70 = add nsw i32 %69, -1
  store i32 %70, ptr %12, align 4, !tbaa !9
  br label %52, !llvm.loop !13

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

77:                                               ; preds = %71
  %78 = load i32, ptr %7, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %7, align 4, !tbaa !9
  br label %13, !llvm.loop !14

80:                                               ; preds = %17
  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: nounwind uwtable
define dso_local void @insertion_sort(ptr noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph39.preheader, label %._crit_edge40

.lr.ph39.preheader:                               ; preds = %3
  %5 = zext nneg i32 %1 to i64
  %wide.trip.count52 = zext nneg i32 %1 to i64
  br label %.lr.ph39

._crit_edge40:                                    ; preds = %._crit_edge36, %3
  ret void

.lr.ph39:                                         ; preds = %.lr.ph39.preheader, %._crit_edge36
  %indvars.iv49 = phi i64 [ 0, %.lr.ph39.preheader ], [ %indvars.iv.next50, %._crit_edge36 ]
  %indvars.iv = phi i64 [ 1, %.lr.ph39.preheader ], [ %indvars.iv.next, %._crit_edge36 ]
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %6 = icmp ult i64 %indvars.iv.next50, %5
  %7 = trunc nuw nsw i64 %indvars.iv49 to i32
  br i1 %6, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph39
  %.028.lcssa = phi i32 [ %7, %.lr.ph39 ], [ %spec.select, %.lr.ph ]
  %8 = sext i32 %.028.lcssa to i64
  %9 = getelementptr inbounds i32, ptr %0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.not.not32 = icmp slt i64 %indvars.iv49, %8
  br i1 %.not.not32, label %.lr.ph35.preheader, label %._crit_edge36

.lr.ph35.preheader:                               ; preds = %._crit_edge
  %11 = sub i64 %8, %indvars.iv49
  %min.iters.check = icmp ult i64 %11, 8
  br i1 %min.iters.check, label %.lr.ph35.preheader58, label %vector.ph

.lr.ph35.preheader58:                             ; preds = %middle.block, %.lr.ph35.preheader
  %indvars.iv46.ph = phi i64 [ %8, %.lr.ph35.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph35

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

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %11, %n.vec
  br i1 %cmp.n, label %._crit_edge36, label %.lr.ph35.preheader58

.lr.ph:                                           ; preds = %.lr.ph39, %.lr.ph
  %indvars.iv41 = phi i64 [ %indvars.iv.next42, %.lr.ph ], [ %indvars.iv, %.lr.ph39 ]
  %.02830 = phi i32 [ %spec.select, %.lr.ph ], [ %7, %.lr.ph39 ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv41
  %20 = sext i32 %.02830 to i64
  %21 = getelementptr inbounds i32, ptr %0, i64 %20
  %22 = tail call zeroext i1 %2(ptr noundef nonnull %19, ptr noundef %21) #1
  %23 = trunc nuw nsw i64 %indvars.iv41 to i32
  %spec.select = select i1 %22, i32 %23, i32 %.02830
  %indvars.iv.next42 = add nuw nsw i64 %indvars.iv41, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next42, %wide.trip.count52
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge36:                                    ; preds = %.lr.ph35, %middle.block, %._crit_edge
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv49
  store i32 %10, ptr %24, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond53.not = icmp eq i64 %indvars.iv.next50, %wide.trip.count52
  br i1 %exitcond53.not, label %._crit_edge40, label %.lr.ph39, !llvm.loop !14

.lr.ph35:                                         ; preds = %.lr.ph35.preheader58, %.lr.ph35
  %indvars.iv46 = phi i64 [ %indvars.iv.next47, %.lr.ph35 ], [ %indvars.iv46.ph, %.lr.ph35.preheader58 ]
  %indvars.iv.next47 = add nsw i64 %indvars.iv46, -1
  %25 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next47
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv46
  store i32 %26, ptr %27, align 4, !tbaa !5
  %.not.not = icmp sgt i64 %indvars.iv.next47, %indvars.iv49
  br i1 %.not.not, label %.lr.ph35, label %._crit_edge36, !llvm.loop !15
}

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 = { 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/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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass