; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/insertion (binaryInsertionSort)

; 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
declare dso_local i32 @binarySearch(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local void @binaryInsertionSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = 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 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %50, %2
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %53

13:                                               ; preds = %9
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = sub nsw i32 %14, 1
  store i32 %15, ptr %7, align 4, !tbaa !9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i64, ptr %16, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !11
  store i64 %20, ptr %5, align 8, !tbaa !11
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !11
  %23 = trunc i64 %22 to i32
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = call i32 @binarySearch(ptr noundef %21, i32 noundef %23, i32 noundef 0, i32 noundef %24)
  store i32 %25, ptr %8, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %30, %13
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp sle i32 %27, %28
  br i1 %29, label %30, label %43

30:                                               ; preds = %26
  %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 i64, ptr %31, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !11
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i64, ptr %36, i64 %38
  %40 = getelementptr inbounds i64, ptr %39, i64 1
  store i64 %35, ptr %40, align 8, !tbaa !11
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, -1
  store i32 %42, ptr %7, align 4, !tbaa !9
  br label %26, !llvm.loop !13

43:                                               ; preds = %26
  %44 = load i64, ptr %5, align 8, !tbaa !11
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i64, ptr %45, i64 %47
  %49 = getelementptr inbounds i64, ptr %48, i64 1
  store i64 %44, ptr %49, align 8, !tbaa !11
  br label %50

50:                                               ; preds = %43
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !9
  br label %9, !llvm.loop !15

53:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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
declare dso_local i32 @binarySearch(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @binaryInsertionSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %invariant.gep = getelementptr i8, ptr %0, i64 8
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %.lr.ph23.preheader, label %._crit_edge24

.lr.ph23.preheader:                               ; preds = %2
  %4 = add nsw i32 %1, -1
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph23

.lr.ph23:                                         ; preds = %.lr.ph23.preheader, %._crit_edge
  %indvars.iv31 = phi i64 [ 1, %.lr.ph23.preheader ], [ %indvars.iv.next32, %._crit_edge ]
  %indvars.iv = phi i64 [ 0, %.lr.ph23.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %5 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv31
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = trunc i64 %6 to i32
  %8 = trunc i64 %indvars.iv31 to i32
  %9 = add i32 %8, -1
  %10 = tail call i32 @binarySearch(ptr noundef %0, i32 noundef %7, i32 noundef 0, i32 noundef %9)
  %11 = sext i32 %10 to i64
  %.not19.not = icmp sgt i64 %indvars.iv31, %11
  br i1 %.not19.not, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph23
  %12 = sub i64 %indvars.iv31, %11
  %min.iters.check = icmp ult i64 %12, 4
  br i1 %min.iters.check, label %.lr.ph.preheader38, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %12, -4
  %ind.end = sub i64 %indvars.iv, %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 %indvars.iv, %index
  %13 = getelementptr inbounds i64, ptr %0, i64 %offset.idx
  %14 = getelementptr inbounds i8, ptr %13, i64 -8
  %15 = getelementptr inbounds i8, ptr %13, i64 -24
  %wide.load = load <2 x i64>, ptr %14, align 8, !tbaa !5
  %wide.load34 = load <2 x i64>, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %13, i64 -16
  store <2 x i64> %wide.load, ptr %13, align 8, !tbaa !5
  store <2 x i64> %wide.load34, ptr %16, align 8, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %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 %12, %n.vec
  br i1 %cmp.n, label %._crit_edge.loopexit, label %.lr.ph.preheader38

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

.lr.ph:                                           ; preds = %.lr.ph.preheader38, %.lr.ph
  %indvars.iv28 = phi i64 [ %indvars.iv.next29, %.lr.ph ], [ %indvars.iv28.ph, %.lr.ph.preheader38 ]
  %18 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv28
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %18, i64 8
  store i64 %19, ptr %20, align 8, !tbaa !5
  %indvars.iv.next29 = add nsw i64 %indvars.iv28, -1
  %.not.not = icmp sgt i64 %indvars.iv28, %11
  br i1 %.not.not, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !13

._crit_edge.loopexit:                             ; preds = %.lr.ph, %middle.block
  %indvars.iv.next29.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next29, %.lr.ph ]
  %21 = trunc nsw i64 %indvars.iv.next29.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph23
  %.018.lcssa = phi i32 [ %9, %.lr.ph23 ], [ %21, %._crit_edge.loopexit ]
  %22 = sext i32 %.018.lcssa to i64
  %gep = getelementptr i64, ptr %invariant.gep, i64 %22
  store i64 %6, ptr %gep, align 8, !tbaa !5
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge24, label %.lr.ph23, !llvm.loop !14

._crit_edge24:                                    ; preds = %._crit_edge, %2
  ret void
}

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

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


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