; 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
define dso_local i32 @binarySearch(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %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
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %12 = load i32, ptr %9, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %31

15:                                               ; preds = %4
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr %18, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp sgt i64 %17, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %15
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  br label %29

27:                                               ; preds = %15
  %28 = load i32, ptr %8, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %27, %24
  %30 = phi i32 [ %26, %24 ], [ %28, %27 ]
  store i32 %30, ptr %5, align 4
  br label %71

31:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = add nsw i32 %32, %33
  %35 = sdiv i32 %34, 2
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i64, ptr %38, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !11
  %43 = icmp eq i64 %37, %42
  br i1 %43, label %44, label %47

44:                                               ; preds = %31
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %70

47:                                               ; preds = %31
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i64, ptr %50, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !11
  %55 = icmp sgt i64 %49, %54
  br i1 %55, label %56, label %63

56:                                               ; preds = %47
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = load i32, ptr %10, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = call i32 @binarySearch(ptr noundef %57, i32 noundef %58, i32 noundef %60, i32 noundef %61)
  store i32 %62, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %70

63:                                               ; preds = %47
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = load i32, ptr %7, align 4, !tbaa !9
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = load i32, ptr %10, align 4, !tbaa !9
  %68 = sub nsw i32 %67, 1
  %69 = call i32 @binarySearch(ptr noundef %64, i32 noundef %65, i32 noundef %66, i32 noundef %68)
  store i32 %69, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %70

70:                                               ; preds = %63, %56, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %71

71:                                               ; preds = %70, %29
  %72 = load i32, ptr %5, align 4
  ret i32 %72
}

; 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}


; 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: read) uwtable
define dso_local i32 @binarySearch(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not3639 = icmp sgt i32 %3, %2
  %5 = sext i32 %1 to i64
  br i1 %.not3639, label %.lr.ph, label %tailrecurse.outer._crit_edge

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr31.ph41 = phi i32 [ %.tr3137, %tailrecurse.outer ], [ %3, %4 ]
  %.tr30.ph40 = phi i32 [ %23, %tailrecurse.outer ], [ %2, %4 ]
  br label %12

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  %.tr30.ph.lcssa35 = phi i32 [ %2, %4 ], [ %.tr30.ph40, %tailrecurse ], [ %23, %tailrecurse.outer ]
  %6 = sext i32 %.tr30.ph.lcssa35 to i64
  %7 = getelementptr inbounds i64, ptr %0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp slt i64 %8, %5
  %10 = zext i1 %9 to i32
  %11 = add nsw i32 %.tr30.ph.lcssa35, %10
  br label %25

12:                                               ; preds = %.lr.ph, %tailrecurse
  %.tr3137 = phi i32 [ %.tr31.ph41, %.lr.ph ], [ %24, %tailrecurse ]
  %13 = add nsw i32 %.tr3137, %.tr30.ph40
  %14 = sdiv i32 %13, 2
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i64, ptr %0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = icmp eq i64 %17, %5
  br i1 %18, label %19, label %21

19:                                               ; preds = %12
  %20 = add nsw i32 %14, 1
  br label %25

21:                                               ; preds = %12
  %22 = icmp slt i64 %17, %5
  br i1 %22, label %tailrecurse.outer, label %tailrecurse

tailrecurse.outer:                                ; preds = %21
  %23 = add nsw i32 %14, 1
  %.not36 = icmp sgt i32 %.tr3137, %23
  br i1 %.not36, label %.lr.ph, label %tailrecurse.outer._crit_edge

tailrecurse:                                      ; preds = %21
  %24 = add nsw i32 %14, -1
  %.not = icmp sgt i32 %24, %.tr30.ph40
  br i1 %.not, label %12, label %tailrecurse.outer._crit_edge

25:                                               ; preds = %19, %tailrecurse.outer._crit_edge
  %.1 = phi i32 [ %11, %tailrecurse.outer._crit_edge ], [ %20, %19 ]
  ret i32 %.1
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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"}


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