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

; 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 @doubleSelectionSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = 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
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i64, ptr %12, i64 %14
  %16 = getelementptr inbounds i64, ptr %15, i64 -1
  store ptr %16, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %17, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %18, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  br label %19

19:                                               ; preds = %71, %2
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = icmp ule ptr %20, %21
  br i1 %22, label %23, label %78

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %24, ptr %10, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %47, %23
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = icmp ule ptr %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %50

30:                                               ; preds = %25
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = load i64, ptr %31, align 8, !tbaa !11
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = icmp sgt i64 %32, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %30
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %37, ptr %8, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %36, %30
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i64, ptr %41, align 8, !tbaa !11
  %43 = icmp slt i64 %40, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %38
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %45, ptr %7, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %44, %38
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = getelementptr inbounds i64, ptr %48, i32 1
  store ptr %49, ptr %10, align 8, !tbaa !5
  br label %25, !llvm.loop !13

50:                                               ; preds = %29
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = icmp eq ptr %51, %52
  br i1 %53, label %54, label %56

54:                                               ; preds = %50
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %55, ptr %8, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %54, %50
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load i64, ptr %57, align 8, !tbaa !11
  store i64 %58, ptr %9, align 8, !tbaa !11
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = load i64, ptr %59, align 8, !tbaa !11
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %60, ptr %61, align 8, !tbaa !11
  %62 = load i64, ptr %9, align 8, !tbaa !11
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %62, ptr %63, align 8, !tbaa !11
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = load i64, ptr %64, align 8, !tbaa !11
  store i64 %65, ptr %9, align 8, !tbaa !11
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = load i64, ptr %66, align 8, !tbaa !11
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %67, ptr %68, align 8, !tbaa !11
  %69 = load i64, ptr %9, align 8, !tbaa !11
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %69, ptr %70, align 8, !tbaa !11
  br label %71

71:                                               ; preds = %56
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds i64, ptr %72, i32 -1
  store ptr %73, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds i64, ptr %74, i32 1
  store ptr %75, ptr %5, align 8, !tbaa !5
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %76, ptr %7, align 8, !tbaa !5
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %77, ptr %8, align 8, !tbaa !5
  br label %19, !llvm.loop !15

78:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  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 @doubleSelectionSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds i64, ptr %0, i64 %3
  %.03444 = getelementptr inbounds i8, ptr %4, i64 -8
  %.not45 = icmp ult ptr %.03444, %0
  br i1 %.not45, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %2, %5
  %.03447 = phi ptr [ %.034, %5 ], [ %.03444, %2 ]
  %.03346 = phi ptr [ %10, %5 ], [ %0, %2 ]
  %.pre = load i64, ptr %.03447, align 8, !tbaa !5
  %.pre48 = load i64, ptr %.03346, align 8, !tbaa !5
  br label %11

5:                                                ; preds = %11
  %6 = icmp eq ptr %spec.select40, %.03346
  %spec.select = select i1 %6, ptr %.237, ptr %spec.select40
  %7 = load i64, ptr %.237, align 8, !tbaa !5
  store i64 %7, ptr %.03346, align 8, !tbaa !5
  store i64 %.pre48, ptr %.237, align 8, !tbaa !5
  %8 = load i64, ptr %.03447, align 8, !tbaa !5
  %9 = load i64, ptr %spec.select, align 8, !tbaa !5
  store i64 %9, ptr %.03447, align 8, !tbaa !5
  store i64 %8, ptr %spec.select, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %.03346, i64 8
  %.034 = getelementptr inbounds i8, ptr %.03447, i64 -8
  %.not = icmp ugt ptr %10, %.034
  br i1 %.not, label %._crit_edge, label %.preheader, !llvm.loop !9

11:                                               ; preds = %.preheader, %11
  %12 = phi i64 [ %.pre48, %.preheader ], [ %18, %11 ]
  %13 = phi i64 [ %.pre, %.preheader ], [ %16, %11 ]
  %.043 = phi ptr [ %.03346, %.preheader ], [ %19, %11 ]
  %.142 = phi ptr [ %.03447, %.preheader ], [ %spec.select40, %11 ]
  %.13641 = phi ptr [ %.03346, %.preheader ], [ %.237, %11 ]
  %14 = load i64, ptr %.043, align 8, !tbaa !5
  %15 = icmp sgt i64 %14, %13
  %16 = tail call i64 @llvm.smax.i64(i64 %14, i64 %13)
  %spec.select40 = select i1 %15, ptr %.043, ptr %.142
  %17 = icmp slt i64 %14, %12
  %18 = tail call i64 @llvm.smin.i64(i64 %14, i64 %12)
  %.237 = select i1 %17, ptr %.043, ptr %.13641
  %19 = getelementptr inbounds i8, ptr %.043, i64 8
  %.not39 = icmp ugt ptr %19, %.03447
  br i1 %.not39, label %5, label %11, !llvm.loop !11

._crit_edge:                                      ; preds = %5, %2
  ret void
}

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

; 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,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass