; LLVM IR from AlgoGenesis/C :: Searching Algorithms/Kth_Largest_Element_Of_The_Array (quickSelect)

; 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, 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 i32 @quickSelect(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 %7, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %4
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  store i32 %20, ptr %5, align 4
  br label %63

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

38:                                               ; preds = %21
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %39, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !9
  store i32 %43, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %62

44:                                               ; preds = %21
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %55

48:                                               ; preds = %44
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = sub nsw i32 %51, 1
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = call i32 @quickSelect(ptr noundef %49, i32 noundef %50, i32 noundef %52, i32 noundef %53)
  store i32 %54, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %62

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

62:                                               ; preds = %55, %48, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %63

63:                                               ; preds = %62, %15
  %64 = load i32, ptr %5, align 4
  ret i32 %64
}

; Function Attrs: nounwind
declare i32 @rand() #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 = { nounwind "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}


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

; Function Attrs: nounwind uwtable
define dso_local i32 @quickSelect(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq i32 %1, %2
  br i1 %5, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse.outer
  %.tr32.ph41 = phi i32 [ %.tr3238, %tailrecurse.outer ], [ %2, %4 ]
  %.tr31.ph40 = phi i32 [ %17, %tailrecurse.outer ], [ %1, %4 ]
  br label %6

6:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr3238 = phi i32 [ %.tr32.ph41, %.lr.ph ], [ %15, %tailrecurse ]
  %7 = tail call i32 @rand() #2
  %reass.sub = sub i32 %.tr3238, %.tr31.ph40
  %8 = add i32 %reass.sub, 1
  %9 = srem i32 %7, %8
  %10 = add nsw i32 %9, %.tr31.ph40
  %11 = tail call i32 @partition(ptr noundef %0, i32 noundef %.tr31.ph40, i32 noundef %.tr3238, i32 noundef %10)
  %12 = icmp eq i32 %11, %3
  br i1 %12, label %tailrecurse.outer._crit_edge, label %13

13:                                               ; preds = %6
  %14 = icmp sgt i32 %11, %3
  br i1 %14, label %tailrecurse, label %tailrecurse.outer

tailrecurse:                                      ; preds = %13
  %15 = add nsw i32 %11, -1
  %16 = icmp eq i32 %.tr31.ph40, %15
  br i1 %16, label %tailrecurse.outer._crit_edge, label %6

tailrecurse.outer:                                ; preds = %13
  %17 = add nsw i32 %11, 1
  %18 = icmp eq i32 %17, %.tr3238
  br i1 %18, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %6, %tailrecurse, %4
  %.pn.in = phi i32 [ %1, %4 ], [ %3, %6 ], [ %.tr31.ph40, %tailrecurse ], [ %.tr3238, %tailrecurse.outer ]
  %.pn = sext i32 %.pn.in to i64
  %.1.in = getelementptr inbounds i32, ptr %0, i64 %.pn
  %.1 = load i32, ptr %.1.in, align 4, !tbaa !5
  ret i32 %.1
}

; Function Attrs: nounwind
declare i32 @rand() 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 = { nounwind "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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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