; LLVM IR from sbshah97/DS-Algo-Handbook :: Algorithms/Sort_Algorithms/Quick_Sort (QuickSort)

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

@arr = external dso_local global [1000000 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @swap(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 @quickSort(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %11, label %56

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  store i32 %15, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sub nsw i32 %16, 1
  store i32 %17, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %18 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %18, ptr %7, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %44, %11
  br label %20

20:                                               ; preds = %28, %19
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %6, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %20
  br label %20, !llvm.loop !9

29:                                               ; preds = %20
  br label %30

30:                                               ; preds = %38, %29
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %7, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = icmp sgt i32 %35, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %30
  br label %30, !llvm.loop !11

39:                                               ; preds = %30
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = icmp sge i32 %40, %41
  br i1 %42, label %43, label %44

43:                                               ; preds = %39
  br label %47

44:                                               ; preds = %39
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !5
  call void @swap(i32 noundef %45, i32 noundef %46)
  br label %19

47:                                               ; preds = %43
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  call void @swap(i32 noundef %48, i32 noundef %49)
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = sub nsw i32 %51, 1
  call void @quickSort(i32 noundef %50, i32 noundef %52)
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  %55 = load i32, ptr %4, align 4, !tbaa !5
  call void @quickSort(i32 noundef %54, i32 noundef %55)
  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 4, ptr %5) #2
  br label %56

56:                                               ; preds = %47, %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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/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"

@arr = external dso_local local_unnamed_addr global [1000000 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @swap(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @quickSort(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %0, %1
  br i1 %3, label %.lr.ph, label %tailrecurse._crit_edge

.lr.ph:                                           ; preds = %2
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %4
  br label %6

6:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr28 = phi i32 [ %0, %.lr.ph ], [ %25, %tailrecurse ]
  %7 = load i32, ptr %5, align 4, !tbaa !5
  %8 = add nsw i32 %.tr28, -1
  br label %9

9:                                                ; preds = %22, %6
  %.019 = phi i32 [ %8, %6 ], [ %21, %22 ]
  %.0 = phi i32 [ %1, %6 ], [ %23, %22 ]
  %10 = sext i32 %.019 to i64
  br label %11

11:                                               ; preds = %11, %9
  %indvars.iv = phi i64 [ %indvars.iv.next, %11 ], [ %10, %9 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %12 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %indvars.iv.next
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp slt i32 %13, %7
  br i1 %14, label %11, label %.preheader, !llvm.loop !9

.preheader:                                       ; preds = %11
  %15 = sext i32 %.0 to i64
  br label %16

16:                                               ; preds = %.preheader, %16
  %indvars.iv31 = phi i64 [ %15, %.preheader ], [ %indvars.iv.next32, %16 ]
  %indvars.iv.next32 = add nsw i64 %indvars.iv31, -1
  %17 = getelementptr inbounds [1000000 x i32], ptr @arr, i64 0, i64 %indvars.iv.next32
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, %7
  br i1 %19, label %16, label %20, !llvm.loop !11

20:                                               ; preds = %16
  %21 = trunc nsw i64 %indvars.iv.next to i32
  %.not = icmp slt i64 %indvars.iv.next, %indvars.iv.next32
  br i1 %.not, label %22, label %tailrecurse

22:                                               ; preds = %20
  %23 = trunc nsw i64 %indvars.iv.next32 to i32
  tail call void @swap(i32 noundef %21, i32 noundef %23)
  br label %9

tailrecurse:                                      ; preds = %20
  %24 = trunc nsw i64 %indvars.iv to i32
  tail call void @swap(i32 noundef %21, i32 noundef %1)
  tail call void @quickSort(i32 noundef %.tr28, i32 noundef %24)
  %25 = add nsw i32 %24, 2
  %26 = icmp slt i32 %25, %1
  br i1 %26, label %6, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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