; LLVM IR from VAR-solutions/Algorithms :: Selection Algorithms/Quick_Select/cpp/quick_select.ll (Z12quick_selectPxxx)

; 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: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z10partition_Pxxxx(ptr noundef, i64 noundef, i64 noundef, i64 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z12quick_selectPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = sub nsw i64 %11, 1
  store i64 %12, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = sub nsw i64 %13, 1
  store i64 %14, ptr %8, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %54, %3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = icmp sle i64 %16, %17
  br i1 %18, label %19, label %55

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

39:                                               ; preds = %19
  store i32 3, ptr %10, align 4
  br label %52

40:                                               ; preds = %19
  %41 = load i64, ptr %9, align 8, !tbaa !9
  %42 = load i64, ptr %6, align 8, !tbaa !9
  %43 = icmp slt i64 %41, %42
  br i1 %43, label %44, label %47

44:                                               ; preds = %40
  %45 = load i64, ptr %9, align 8, !tbaa !9
  %46 = add nsw i64 %45, 1
  store i64 %46, ptr %7, align 8, !tbaa !9
  br label %50

47:                                               ; preds = %40
  %48 = load i64, ptr %9, align 8, !tbaa !9
  %49 = sub nsw i64 %48, 1
  store i64 %49, ptr %8, align 8, !tbaa !9
  br label %50

50:                                               ; preds = %47, %44
  br label %51

51:                                               ; preds = %50
  store i32 0, ptr %10, align 4
  br label %52

52:                                               ; preds = %51, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %53 = load i32, ptr %10, align 4
  switch i32 %53, label %60 [
    i32 0, label %54
    i32 3, label %55
  ]

54:                                               ; preds = %52
  br label %15, !llvm.loop !11

55:                                               ; preds = %52, %15
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load i64, ptr %6, align 8, !tbaa !9
  %58 = getelementptr inbounds i64, ptr %56, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i64 %59

60:                                               ; preds = %52
  unreachable
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) #2

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @rand() #2

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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !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: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z10partition_Pxxxx(ptr noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z12quick_selectPxxx(ptr noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = add nsw i64 %2, -1
  %.not29 = icmp slt i64 %1, 1
  br i1 %.not29, label %.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %5 = add nsw i64 %1, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %15
  %.01931 = phi i64 [ %.1, %15 ], [ %5, %.lr.ph.preheader ]
  %.02030 = phi i64 [ %.121, %15 ], [ 0, %.lr.ph.preheader ]
  %6 = tail call i64 @time(ptr noundef null) #2
  %7 = trunc i64 %6 to i32
  tail call void @srand(i32 noundef %7) #2
  %8 = tail call i32 @rand() #2
  %9 = sext i32 %8 to i64
  %reass.sub = sub i64 %.01931, %.02030
  %10 = add i64 %reass.sub, 1
  %11 = srem i64 %9, %10
  %12 = add nsw i64 %11, %.02030
  %13 = tail call noundef i64 @_Z10partition_Pxxxx(ptr noundef %0, i64 noundef %.02030, i64 noundef %.01931, i64 noundef %12)
  %14 = icmp eq i64 %13, %4
  br i1 %14, label %.thread, label %15

15:                                               ; preds = %.lr.ph
  %16 = icmp slt i64 %13, %4
  %17 = add nsw i64 %13, 1
  %18 = add nsw i64 %13, -1
  %.121 = select i1 %16, i64 %17, i64 %.02030
  %.1 = select i1 %16, i64 %.01931, i64 %18
  %.not = icmp sgt i64 %.121, %.1
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %15, %.lr.ph, %3
  %19 = getelementptr inbounds i64, ptr %0, i64 %4
  %20 = load i64, ptr %19, align 8, !tbaa !5
  ret i64 %20
}

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #1

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


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