; LLVM IR from Innocentsax/sorting_algorithms :: 2-selection_sort (selection_sort)

; 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 void @swap_ints(ptr noundef, ptr 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 void @selection_sort(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %14, label %11

11:                                               ; preds = %2
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = icmp ult i64 %12, 2
  br i1 %13, label %14, label %15

14:                                               ; preds = %11, %2
  store i32 1, ptr %8, align 4
  br label %68

15:                                               ; preds = %11
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %64, %15
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = sub i64 %18, 1
  %20 = icmp ult i64 %17, %19
  br i1 %20, label %21, label %67

21:                                               ; preds = %16
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = getelementptr inbounds i32, ptr %22, i64 %23
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = add i64 %25, 1
  store i64 %26, ptr %7, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %47, %21
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = load i64, ptr %4, align 8, !tbaa !9
  %30 = icmp ult i64 %28, %29
  br i1 %30, label %31, label %50

31:                                               ; preds = %27
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = getelementptr inbounds i32, ptr %32, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = icmp slt i32 %35, %37
  br i1 %38, label %39, label %43

39:                                               ; preds = %31
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load i64, ptr %7, align 8, !tbaa !9
  %42 = getelementptr inbounds i32, ptr %40, i64 %41
  br label %45

43:                                               ; preds = %31
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %43, %39
  %46 = phi ptr [ %42, %39 ], [ %44, %43 ]
  store ptr %46, ptr %5, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %45
  %48 = load i64, ptr %7, align 8, !tbaa !9
  %49 = add i64 %48, 1
  store i64 %49, ptr %7, align 8, !tbaa !9
  br label %27, !llvm.loop !13

50:                                               ; preds = %27
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = load i64, ptr %6, align 8, !tbaa !9
  %53 = getelementptr inbounds i32, ptr %51, i64 %52
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = icmp ne ptr %53, %54
  br i1 %55, label %56, label %63

56:                                               ; preds = %50
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = load i64, ptr %6, align 8, !tbaa !9
  %59 = getelementptr inbounds i32, ptr %57, i64 %58
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  call void @swap_ints(ptr noundef %59, ptr noundef %60)
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = load i64, ptr %4, align 8, !tbaa !9
  call void @print_array(ptr noundef %61, i64 noundef %62)
  br label %63

63:                                               ; preds = %56, %50
  br label %64

64:                                               ; preds = %63
  %65 = load i64, ptr %6, align 8, !tbaa !9
  %66 = add i64 %65, 1
  store i64 %66, ptr %6, align 8, !tbaa !9
  br label %16, !llvm.loop !15

67:                                               ; preds = %16
  store i32 0, ptr %8, align 4
  br label %68

68:                                               ; preds = %67, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %69 = load i32, ptr %8, align 4
  switch i32 %69, label %71 [
    i32 0, label %70
    i32 1, label %70
  ]

70:                                               ; preds = %68, %68
  ret void

71:                                               ; preds = %68
  unreachable
}

declare void @print_array(ptr noundef, i64 noundef) #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 = { "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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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: nounwind uwtable
declare dso_local void @swap_ints(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @selection_sort(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp ult i64 %1, 2
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit, label %.lr.ph.preheader.preheader

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

.lr.ph.preheader:                                 ; preds = %.lr.ph.preheader.preheader, %17
  %.02532 = phi i64 [ %7, %17 ], [ 0, %.lr.ph.preheader.preheader ]
  %6 = getelementptr inbounds i32, ptr %0, i64 %.02532
  %7 = add nuw i64 %.02532, 1
  %.pre = load i32, ptr %6, align 4, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %8 = phi i32 [ %12, %.lr.ph ], [ %.pre, %.lr.ph.preheader ]
  %.031 = phi i64 [ %14, %.lr.ph ], [ %7, %.lr.ph.preheader ]
  %.02630 = phi ptr [ %13, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %9 = getelementptr inbounds i32, ptr %0, i64 %.031
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp slt i32 %10, %8
  %12 = tail call i32 @llvm.smin.i32(i32 %10, i32 %8)
  %13 = select i1 %11, ptr %9, ptr %.02630
  %14 = add nuw i64 %.031, 1
  %15 = icmp ult i64 %14, %1
  br i1 %15, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph
  %.not = icmp eq ptr %6, %13
  br i1 %.not, label %17, label %16

16:                                               ; preds = %._crit_edge
  tail call void @swap_ints(ptr noundef nonnull %6, ptr noundef %13)
  tail call void @print_array(ptr noundef nonnull %0, i64 noundef %1) #3
  br label %17

17:                                               ; preds = %._crit_edge, %16
  %exitcond.not = icmp eq i64 %.02532, %5
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph.preheader, !llvm.loop !11

.loopexit:                                        ; preds = %17, %2
  ret void
}

declare void @print_array(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass