; LLVM IR from Dev-XYS/Algorithms :: Selection.ll (Z6selecti)

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

@n = external dso_local global i32, align 4
@d = external dso_local global [10000 x i32], align 16

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

10:                                               ; preds = %40, %1
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp slt i32 %11, %12
  br i1 %13, label %14, label %41

14:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = call noundef i32 @_Z9partitionii(i32 noundef %15, i32 noundef %16)
  store i32 %17, ptr %6, align 4, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %14
  %22 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %22, ptr %4, align 4, !tbaa !5
  br label %37

23:                                               ; preds = %14
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = icmp sgt i32 %24, %25
  br i1 %26, label %27, label %30

27:                                               ; preds = %23
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 2
  store i32 %29, ptr %5, align 4, !tbaa !5
  br label %36

30:                                               ; preds = %23
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10000 x i32], ptr @d, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  store i32 %35, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %38

36:                                               ; preds = %27
  br label %37

37:                                               ; preds = %36, %21
  store i32 0, ptr %7, align 4
  br label %38

38:                                               ; preds = %37, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %39 = load i32, ptr %7, align 4
  switch i32 %39, label %46 [
    i32 0, label %40
  ]

40:                                               ; preds = %38
  br label %10, !llvm.loop !9

41:                                               ; preds = %10
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10000 x i32], ptr @d, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  store i32 %45, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %46

46:                                               ; preds = %41, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %47 = load i32, ptr %2, align 4
  ret i32 %47
}

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 }

!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"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@n = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [10000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z9partitionii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6selecti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  br i1 %3, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %1
  %4 = add nsw i32 %2, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %12
  %.01530 = phi i32 [ %.217, %12 ], [ %4, %.lr.ph.preheader ]
  %.01829 = phi i32 [ %.220, %12 ], [ 0, %.lr.ph.preheader ]
  %5 = tail call noundef i32 @_Z9partitionii(i32 noundef %.01829, i32 noundef %.01530)
  %6 = icmp slt i32 %5, %0
  br i1 %6, label %12, label %7

7:                                                ; preds = %.lr.ph
  %8 = icmp sgt i32 %5, %0
  br i1 %8, label %9, label %.thread

9:                                                ; preds = %7
  %10 = add nsw i32 %5, -2
  br label %12

.thread:                                          ; preds = %7
  %11 = add nsw i32 %5, -1
  br label %.loopexit

12:                                               ; preds = %9, %.lr.ph
  %.220 = phi i32 [ %.01829, %9 ], [ %5, %.lr.ph ]
  %.217 = phi i32 [ %10, %9 ], [ %.01530, %.lr.ph ]
  %13 = icmp slt i32 %.220, %.217
  br i1 %13, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %12, %1, %.thread
  %.pn.in = phi i32 [ %11, %.thread ], [ 0, %1 ], [ %.220, %12 ]
  %.pn = sext i32 %.pn.in to i64
  %.2.in = getelementptr inbounds [10000 x i32], ptr @d, i64 0, i64 %.pn
  %.2 = load i32, ptr %.2.in, align 4, !tbaa !5
  ret i32 %.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" }

!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"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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