; LLVM IR from bartobri/data-structures-c :: arrays/stack-based-quicksort (sort)

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

10:                                               ; preds = %46, %19, %3
  %11 = call i32 @STACKcount()
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %13, label %47

13:                                               ; preds = %10
  %14 = call i32 @STACKpop()
  store i32 %14, ptr %5, align 4, !tbaa !9
  %15 = call i32 @STACKpop()
  store i32 %15, ptr %6, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp sle i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  br label %10, !llvm.loop !11

20:                                               ; preds = %13
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = call i32 @partition(ptr noundef %21, i32 noundef %22, i32 noundef %23)
  store i32 %24, ptr %7, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = sub nsw i32 %25, %26
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = sub nsw i32 %28, %29
  %31 = icmp sgt i32 %27, %30
  br i1 %31, label %32, label %39

32:                                               ; preds = %20
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sub nsw i32 %33, 1
  call void @STACKpush(i32 noundef %34)
  %35 = load i32, ptr %5, align 4, !tbaa !9
  call void @STACKpush(i32 noundef %35)
  %36 = load i32, ptr %6, align 4, !tbaa !9
  call void @STACKpush(i32 noundef %36)
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  call void @STACKpush(i32 noundef %38)
  br label %46

39:                                               ; preds = %20
  %40 = load i32, ptr %6, align 4, !tbaa !9
  call void @STACKpush(i32 noundef %40)
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  call void @STACKpush(i32 noundef %42)
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = sub nsw i32 %43, 1
  call void @STACKpush(i32 noundef %44)
  %45 = load i32, ptr %5, align 4, !tbaa !9
  call void @STACKpush(i32 noundef %45)
  br label %46

46:                                               ; preds = %39, %32
  br label %10, !llvm.loop !11

47:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

declare void @STACKinit() #2

declare void @STACKpush(i32 noundef) #2

declare i32 @STACKcount() #2

declare i32 @STACKpop() #2

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


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

; Function Attrs: nounwind uwtable
define dso_local void @quicksort(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  tail call void @STACKinit() #2
  tail call void @STACKpush(i32 noundef %2) #2
  tail call void @STACKpush(i32 noundef %1) #2
  %4 = tail call i32 @STACKcount() #2
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %.backedge
  %6 = tail call i32 @STACKpop() #2
  %7 = tail call i32 @STACKpop() #2
  %.not = icmp sgt i32 %7, %6
  br i1 %.not, label %8, label %.backedge

8:                                                ; preds = %.lr.ph
  %9 = tail call i32 @partition(ptr noundef %0, i32 noundef %6, i32 noundef %7)
  %10 = sub nsw i32 %9, %6
  %11 = sub nsw i32 %7, %9
  %12 = icmp sgt i32 %10, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %8
  %14 = add nsw i32 %9, -1
  tail call void @STACKpush(i32 noundef %14) #2
  tail call void @STACKpush(i32 noundef %6) #2
  tail call void @STACKpush(i32 noundef %7) #2
  %15 = add nsw i32 %9, 1
  br label %.backedge.sink.split

16:                                               ; preds = %8
  tail call void @STACKpush(i32 noundef %7) #2
  %17 = add nsw i32 %9, 1
  tail call void @STACKpush(i32 noundef %17) #2
  %18 = add nsw i32 %9, -1
  tail call void @STACKpush(i32 noundef %18) #2
  br label %.backedge.sink.split

.backedge.sink.split:                             ; preds = %16, %13
  %.sink = phi i32 [ %15, %13 ], [ %6, %16 ]
  tail call void @STACKpush(i32 noundef %.sink) #2
  br label %.backedge

.backedge:                                        ; preds = %.backedge.sink.split, %.lr.ph
  %19 = tail call i32 @STACKcount() #2
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.backedge, %3
  ret void
}

declare void @STACKinit() local_unnamed_addr #1

declare void @STACKpush(i32 noundef) local_unnamed_addr #1

declare i32 @STACKcount() local_unnamed_addr #1

declare i32 @STACKpop() local_unnamed_addr #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 = { "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 #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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass