; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/selection (selectionSort)

; 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
define dso_local void @selectionSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %9, ptr %7, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %54, %2
  %11 = load ptr, ptr %7, align 8, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = sub nsw i32 %12, 1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = sext i32 %13 to i64
  %16 = getelementptr inbounds i64, ptr %14, i64 %15
  %17 = icmp ult ptr %11, %16
  br i1 %17, label %18, label %57

18:                                               ; preds = %10
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds i64, ptr %20, i64 1
  store ptr %21, ptr %8, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %38, %18
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = sext i32 %24 to i64
  %27 = getelementptr inbounds i64, ptr %25, i64 %26
  %28 = icmp ult ptr %23, %27
  br i1 %28, label %29, label %41

29:                                               ; preds = %22
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = load i64, ptr %32, align 8, !tbaa !11
  %34 = icmp sgt i64 %31, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %29
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %36, ptr %6, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %35, %29
  br label %38

38:                                               ; preds = %37
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds i64, ptr %39, i32 1
  store ptr %40, ptr %8, align 8, !tbaa !5
  br label %22, !llvm.loop !13

41:                                               ; preds = %22
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = icmp ne ptr %42, %43
  br i1 %44, label %45, label %53

45:                                               ; preds = %41
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i64, ptr %46, align 8, !tbaa !11
  store i64 %47, ptr %5, align 8, !tbaa !11
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %49, ptr %50, align 8, !tbaa !11
  %51 = load i64, ptr %5, align 8, !tbaa !11
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %51, ptr %52, align 8, !tbaa !11
  br label %53

53:                                               ; preds = %45, %41
  br label %54

54:                                               ; preds = %53
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds i64, ptr %55, i32 1
  store ptr %56, ptr %7, align 8, !tbaa !5
  br label %10, !llvm.loop !15

57:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

; 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

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 }

!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}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @selectionSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr i64, ptr %0, i64 %3
  %5 = getelementptr i8, ptr %4, i64 -8
  %6 = icmp ugt ptr %5, %0
  br i1 %6, label %.lr.ph26, label %._crit_edge27

.lr.ph26:                                         ; preds = %2, %18
  %.02024 = phi ptr [ %7, %18 ], [ %0, %2 ]
  %7 = getelementptr inbounds i8, ptr %.02024, i64 8
  %8 = icmp ult ptr %7, %4
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.lr.ph26
  %.pre = load i64, ptr %.02024, align 8, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %9 = phi i64 [ %12, %.lr.ph ], [ %.pre, %.lr.ph.preheader ]
  %.023 = phi ptr [ %13, %.lr.ph ], [ %7, %.lr.ph.preheader ]
  %.02122 = phi ptr [ %spec.select, %.lr.ph ], [ %.02024, %.lr.ph.preheader ]
  %10 = load i64, ptr %.023, align 8, !tbaa !5
  %11 = icmp sgt i64 %9, %10
  %12 = tail call i64 @llvm.smin.i64(i64 %9, i64 %10)
  %spec.select = select i1 %11, ptr %.023, ptr %.02122
  %13 = getelementptr inbounds i8, ptr %.023, i64 8
  %14 = icmp ult ptr %13, %4
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph26
  %.021.lcssa = phi ptr [ %.02024, %.lr.ph26 ], [ %spec.select, %.lr.ph ]
  %.not = icmp eq ptr %.02024, %.021.lcssa
  br i1 %.not, label %18, label %15

15:                                               ; preds = %._crit_edge
  %16 = load i64, ptr %.02024, align 8, !tbaa !5
  %17 = load i64, ptr %.021.lcssa, align 8, !tbaa !5
  store i64 %17, ptr %.02024, align 8, !tbaa !5
  store i64 %16, ptr %.021.lcssa, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %15
  %19 = icmp ult ptr %7, %5
  br i1 %19, label %.lr.ph26, label %._crit_edge27, !llvm.loop !11

._crit_edge27:                                    ; preds = %18, %2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 speculatable willreturn memory(none) }

!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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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