; LLVM IR from gzc/CLRS :: C09-Medians-and-Order-Statistics/randomized-select.ll (Z11kthSmallestRSt6vectorIiSaIiEEiii)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z11kthSmallestRSt6vectorIiSaIiEEiii(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #2 {
  %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
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = load i32, ptr %8, align 4, !tbaa !9
  %15 = call noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24) %12, i32 noundef %13, i32 noundef %14)
  store i32 %15, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sub nsw i32 %16, %17
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = sub nsw i32 %19, 1
  %21 = icmp eq i32 %18, %20
  br i1 %21, label %22, label %28

22:                                               ; preds = %4
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %23, i64 noundef %25) #3
  %27 = load i32, ptr %26, align 4, !tbaa !9
  store i32 %27, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %54

28:                                               ; preds = %4
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = sub nsw i32 %29, %30
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sub nsw i32 %32, 1
  %34 = icmp sgt i32 %31, %33
  br i1 %34, label %35, label %42

35:                                               ; preds = %28
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = call noundef i32 @_Z11kthSmallestRSt6vectorIiSaIiEEiii(ptr noundef nonnull align 8 dereferenceable(24) %36, i32 noundef %37, i32 noundef %39, i32 noundef %40)
  store i32 %41, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %54

42:                                               ; preds = %28
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = sub nsw i32 %47, %48
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %49, %50
  %52 = sub nsw i32 %51, 1
  %53 = call noundef i32 @_Z11kthSmallestRSt6vectorIiSaIiEEiii(ptr noundef nonnull align 8 dereferenceable(24) %43, i32 noundef %45, i32 noundef %46, i32 noundef %52)
  store i32 %53, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %54

54:                                               ; preds = %42, %35, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %55 = load i32, ptr %5, align 4
  ret i32 %55
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 #2 = { mustprogress 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z11kthSmallestRSt6vectorIiSaIiEEiii(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = tail call noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2)
  %6 = sub nsw i32 %5, %1
  %7 = add nsw i32 %3, -1
  %8 = icmp eq i32 %6, %7
  br i1 %8, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %9 = phi i32 [ %27, %tailrecurse.outer ], [ %7, %4 ]
  %10 = phi i32 [ %26, %tailrecurse.outer ], [ %6, %4 ]
  %11 = phi i32 [ %25, %tailrecurse.outer ], [ %5, %4 ]
  %.tr25.ph32 = phi i32 [ %24, %tailrecurse.outer ], [ %3, %4 ]
  %.tr24.ph31 = phi i32 [ %.tr2428.lcssa, %tailrecurse.outer ], [ %2, %4 ]
  %.tr23.ph30 = phi i32 [ %21, %tailrecurse.outer ], [ %1, %4 ]
  %.not52 = icmp slt i32 %10, %.tr25.ph32
  br i1 %.not52, label %tailrecurse.outer, label %tailrecurse

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse, %4
  %.lcssa = phi i32 [ %5, %4 ], [ %18, %tailrecurse ], [ %25, %tailrecurse.outer ]
  %12 = sext i32 %.lcssa to i64
  %13 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %12) #1
  %14 = load i32, ptr %13, align 4, !tbaa !5
  ret i32 %14

15:                                               ; preds = %tailrecurse
  %.not = icmp slt i32 %19, %.tr25.ph32
  br i1 %.not, label %tailrecurse.outer, label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph, %15
  %16 = phi i32 [ %18, %15 ], [ %11, %.lr.ph ]
  %17 = add nsw i32 %16, -1
  %18 = tail call noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %.tr23.ph30, i32 noundef %17)
  %19 = sub nsw i32 %18, %.tr23.ph30
  %20 = icmp eq i32 %19, %9
  br i1 %20, label %tailrecurse.outer._crit_edge, label %15

tailrecurse.outer:                                ; preds = %15, %.lr.ph
  %.lcssa48 = phi i32 [ %11, %.lr.ph ], [ %18, %15 ]
  %.tr2428.lcssa = phi i32 [ %.tr24.ph31, %.lr.ph ], [ %17, %15 ]
  %21 = add nsw i32 %.lcssa48, 1
  %22 = add i32 %.tr23.ph30, -1
  %23 = add i32 %22, %.tr25.ph32
  %24 = sub i32 %23, %.lcssa48
  %25 = tail call noundef i32 @_Z15randomPartitionRSt6vectorIiSaIiEEii(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %21, i32 noundef %.tr2428.lcssa)
  %26 = sub nsw i32 %25, %21
  %27 = add nsw i32 %24, -1
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %tailrecurse.outer._crit_edge, label %.lr.ph
}

attributes #0 = { mustprogress 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++ TBAA"}


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