; LLVM IR from alenic/realGA :: src/algorithms.ll (ZN4RALG12kthSmallest_EPfiii)

; 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: 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 @_ZN4RALG9partitionEPfii(ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef float @_ZN4RALG12kthSmallest_EPfiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #2 {
  %5 = alloca float, 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 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %65

14:                                               ; preds = %4
  %15 = load i32, ptr %9, align 4, !tbaa !9
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sub nsw i32 %16, %17
  %19 = add nsw i32 %18, 1
  %20 = icmp sle i32 %15, %19
  br i1 %20, label %21, label %65

21:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = call noundef i32 @_ZN4RALG9partitionEPfii(ptr noundef %22, i32 noundef %23, i32 noundef %24)
  store i32 %25, ptr %10, align 4, !tbaa !9
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = sub nsw i32 %26, %27
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = sub nsw i32 %29, 1
  %31 = icmp eq i32 %28, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %21
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds float, ptr %33, i64 %35
  %37 = load float, ptr %36, align 4, !tbaa !11
  store float %37, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %64

38:                                               ; preds = %21
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = sub nsw i32 %39, %40
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = sub nsw i32 %42, 1
  %44 = icmp sgt i32 %41, %43
  br i1 %44, label %45, label %52

45:                                               ; preds = %38
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = sub nsw i32 %48, 1
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = call noundef float @_ZN4RALG12kthSmallest_EPfiii(ptr noundef %46, i32 noundef %47, i32 noundef %49, i32 noundef %50)
  store float %51, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %64

52:                                               ; preds = %38
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = sub nsw i32 %57, %58
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = add nsw i32 %59, %60
  %62 = sub nsw i32 %61, 1
  %63 = call noundef float @_ZN4RALG12kthSmallest_EPfiii(ptr noundef %53, i32 noundef %55, i32 noundef %56, i32 noundef %62)
  store float %63, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %64

64:                                               ; preds = %52, %45, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %66

65:                                               ; preds = %14, %4
  store float -1.000000e+00, ptr %5, align 4
  br label %66

66:                                               ; preds = %65, %64
  %67 = load float, ptr %5, align 4
  ret float %67
}

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}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !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 i32 @_ZN4RALG9partitionEPfii(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef float @_ZN4RALG12kthSmallest_EPfiii(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %3, 0
  br i1 %5, label %.lr.ph.split.us, label %.loopexit

.lr.ph.split.us:                                  ; preds = %4, %tailrecurse.outer
  %.tr31.ph47 = phi i32 [ %20, %tailrecurse.outer ], [ %3, %4 ]
  %.tr30.ph46 = phi i32 [ %.tr3035.us4361, %tailrecurse.outer ], [ %2, %4 ]
  %.tr29.ph45 = phi i32 [ %17, %tailrecurse.outer ], [ %1, %4 ]
  %6 = add nsw i32 %.tr31.ph47, -1
  %reass.sub = sub i32 %.tr30.ph46, %.tr29.ph45
  %7 = add i32 %reass.sub, 1
  %.not.us42 = icmp slt i32 %7, %.tr31.ph47
  br i1 %.not.us42, label %.loopexit, label %.lr.ph44.preheader

.lr.ph44.preheader:                               ; preds = %.lr.ph.split.us
  %8 = tail call noundef i32 @_ZN4RALG9partitionEPfii(ptr noundef %0, i32 noundef %.tr29.ph45, i32 noundef %.tr30.ph46)
  %9 = sub nsw i32 %8, %.tr29.ph45
  %10 = icmp eq i32 %9, %6
  br i1 %10, label %.split.us, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph44.preheader, %tailrecurse.us
  %11 = phi i32 [ %15, %tailrecurse.us ], [ %9, %.lr.ph44.preheader ]
  %12 = phi i32 [ %14, %tailrecurse.us ], [ %8, %.lr.ph44.preheader ]
  %.tr3035.us4361 = phi i32 [ %13, %tailrecurse.us ], [ %.tr30.ph46, %.lr.ph44.preheader ]
  %.not28.us = icmp slt i32 %11, %.tr31.ph47
  br i1 %.not28.us, label %tailrecurse.outer, label %tailrecurse.us

tailrecurse.us:                                   ; preds = %.lr.ph
  %13 = add nsw i32 %12, -1
  %14 = tail call noundef i32 @_ZN4RALG9partitionEPfii(ptr noundef %0, i32 noundef %.tr29.ph45, i32 noundef %13)
  %15 = sub nsw i32 %14, %.tr29.ph45
  %16 = icmp eq i32 %15, %6
  br i1 %16, label %.split.us, label %.lr.ph

.split.us:                                        ; preds = %.lr.ph44.preheader, %tailrecurse.us
  %.lcssa = phi i32 [ %14, %tailrecurse.us ], [ %8, %.lr.ph44.preheader ]
  %.phi.trans.insert = sext i32 %.lcssa to i64
  %.phi.trans.insert56 = getelementptr inbounds float, ptr %0, i64 %.phi.trans.insert
  %.pre = load float, ptr %.phi.trans.insert56, align 4, !tbaa !5
  br label %.loopexit

tailrecurse.outer:                                ; preds = %.lr.ph
  %17 = add nsw i32 %12, 1
  %18 = add i32 %.tr29.ph45, -1
  %19 = add i32 %18, %.tr31.ph47
  %20 = sub i32 %19, %12
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %.lr.ph.split.us, label %.loopexit

.loopexit:                                        ; preds = %tailrecurse.outer, %.lr.ph.split.us, %4, %.split.us
  %.1 = phi float [ %.pre, %.split.us ], [ -1.000000e+00, %4 ], [ -1.000000e+00, %.lr.ph.split.us ], [ -1.000000e+00, %tailrecurse.outer ]
  ret float %.1
}

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

!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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass