; LLVM IR from ismdeep/sort-algos-c :: include/sort-algos (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
define dso_local void @selection_sort(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %9, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %38, %4
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ult ptr %15, %16
  br i1 %17, label %18, label %54

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %19 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %19, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %20 = load ptr, ptr %10, align 8, !tbaa !5
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %20, i64 %21
  store ptr %22, ptr %11, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %34, %18
  %24 = load ptr, ptr %11, align 8, !tbaa !5
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = icmp ult ptr %24, %25
  br i1 %26, label %27, label %38

27:                                               ; preds = %23
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = load ptr, ptr %11, align 8, !tbaa !5
  %31 = call zeroext i1 %28(ptr noundef %29, ptr noundef %30)
  br i1 %31, label %34, label %32

32:                                               ; preds = %27
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %33, ptr %10, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %32, %27
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i64 %35
  store ptr %37, ptr %11, align 8, !tbaa !5
  br label %23, !llvm.loop !11

38:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = call noalias ptr @malloc(i64 noundef %39) #6
  store ptr %40, ptr %12, align 8, !tbaa !5
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %41, ptr align 1 %42, i64 %43, i1 false)
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %44, ptr align 1 %45, i64 %46, i1 false)
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = load ptr, ptr %12, align 8, !tbaa !5
  %49 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 1 %48, i64 %49, i1 false)
  %50 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %50) #5
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = load i64, ptr %7, align 8, !tbaa !9
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  store ptr %53, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %14, !llvm.loop !13

54:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  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: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

; 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 allocsize(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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

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


; 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
define dso_local void @selection_sort(ptr noundef %0, ptr noundef readnone %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = icmp ult ptr %0, %1
  br i1 %5, label %.lr.ph33, label %._crit_edge34

.lr.ph33:                                         ; preds = %4, %._crit_edge
  %.031 = phi ptr [ %6, %._crit_edge ], [ %0, %4 ]
  %6 = getelementptr inbounds i8, ptr %.031, i64 %2
  %7 = icmp ult ptr %6, %1
  br i1 %7, label %.lr.ph, label %._crit_edge34.loopexit.critedge

.lr.ph:                                           ; preds = %.lr.ph33, %.lr.ph
  %.02630 = phi ptr [ %9, %.lr.ph ], [ %6, %.lr.ph33 ]
  %.02729 = phi ptr [ %spec.select, %.lr.ph ], [ %.031, %.lr.ph33 ]
  %8 = tail call zeroext i1 %3(ptr noundef %.02729, ptr noundef %.02630) #4
  %spec.select = select i1 %8, ptr %.02729, ptr %.02630
  %9 = getelementptr inbounds i8, ptr %.02630, i64 %2
  %10 = icmp ult ptr %9, %1
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph
  %11 = tail call noalias ptr @malloc(i64 noundef %2) #5
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %11, ptr align 1 %.031, i64 %2, i1 false)
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.031, ptr align 1 %spec.select, i64 %2, i1 false)
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %spec.select, ptr align 1 %11, i64 %2, i1 false)
  tail call void @free(ptr noundef %11) #4
  br i1 %7, label %.lr.ph33, label %._crit_edge34, !llvm.loop !7

._crit_edge34.loopexit.critedge:                  ; preds = %.lr.ph33
  %12 = tail call noalias ptr @malloc(i64 noundef %2) #5
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %12, ptr align 1 %.031, i64 %2, i1 false)
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.031, ptr align 1 %12, i64 %2, i1 false)
  tail call void @free(ptr noundef %12) #4
  br label %._crit_edge34

._crit_edge34:                                    ; preds = %._crit_edge, %._crit_edge34.loopexit.critedge, %4
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

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


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