; LLVM IR from priyankchheda/algorithms :: _other_languages/c_language/sorts/generic (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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @selection_sort(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  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) #2
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %15

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

19:                                               ; preds = %15
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %70

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %11, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = add i64 %23, 1
  store i64 %24, ptr %12, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %51, %20
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %54

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %12, align 8, !tbaa !9
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = mul i64 %32, %33
  %35 = getelementptr inbounds i8, ptr %31, i64 %34
  store ptr %35, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr %11, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = mul i64 %38, %39
  %41 = getelementptr inbounds i8, ptr %36, i64 %40
  store ptr %41, ptr %14, align 8, !tbaa !5
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = load ptr, ptr %13, align 8, !tbaa !5
  %44 = load ptr, ptr %14, align 8, !tbaa !5
  %45 = call i32 %42(ptr noundef %43, ptr noundef %44)
  %46 = icmp slt i32 %45, 0
  br i1 %46, label %47, label %50

47:                                               ; preds = %30
  %48 = load i64, ptr %12, align 8, !tbaa !9
  %49 = trunc i64 %48 to i32
  store i32 %49, ptr %11, align 4, !tbaa !11
  br label %50

50:                                               ; preds = %47, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %12, align 8, !tbaa !9
  %53 = add i64 %52, 1
  store i64 %53, ptr %12, align 8, !tbaa !9
  br label %25, !llvm.loop !13

54:                                               ; preds = %29
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load i64, ptr %9, align 8, !tbaa !9
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = mul i64 %56, %57
  %59 = getelementptr inbounds i8, ptr %55, i64 %58
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr %11, align 4, !tbaa !11
  %62 = sext i32 %61 to i64
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = mul i64 %62, %63
  %65 = getelementptr inbounds i8, ptr %60, i64 %64
  %66 = load i64, ptr %7, align 8, !tbaa !9
  call void @swap(ptr noundef %59, ptr noundef %65, i64 noundef %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %67

67:                                               ; preds = %54
  %68 = load i64, ptr %9, align 8, !tbaa !9
  %69 = add i64 %68, 1
  store i64 %69, ptr %9, align 8, !tbaa !9
  br label %15, !llvm.loop !15

70:                                               ; preds = %19
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"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 hidden void @swap(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @selection_sort(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge32, label %.lr.ph31

._crit_edge32:                                    ; preds = %._crit_edge, %4
  ret void

.lr.ph31:                                         ; preds = %4, %._crit_edge
  %.029 = phi i64 [ %6, %._crit_edge ], [ 0, %4 ]
  %5 = trunc i64 %.029 to i32
  %6 = add nuw i64 %.029, 1
  %7 = icmp ult i64 %6, %1
  br i1 %7, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph31
  %.025.lcssa = phi i32 [ %5, %.lr.ph31 ], [ %spec.select, %.lr.ph ]
  %8 = mul i64 %.029, %2
  %9 = getelementptr inbounds i8, ptr %0, i64 %8
  %10 = sext i32 %.025.lcssa to i64
  %11 = mul i64 %10, %2
  %12 = getelementptr inbounds i8, ptr %0, i64 %11
  tail call void @swap(ptr noundef %9, ptr noundef %12, i64 noundef %2)
  %exitcond.not = icmp eq i64 %6, %1
  br i1 %exitcond.not, label %._crit_edge32, label %.lr.ph31, !llvm.loop !5

.lr.ph:                                           ; preds = %.lr.ph31, %.lr.ph
  %.02428 = phi i64 [ %21, %.lr.ph ], [ %6, %.lr.ph31 ]
  %.02527 = phi i32 [ %spec.select, %.lr.ph ], [ %5, %.lr.ph31 ]
  %13 = mul i64 %.02428, %2
  %14 = getelementptr inbounds i8, ptr %0, i64 %13
  %15 = sext i32 %.02527 to i64
  %16 = mul i64 %15, %2
  %17 = getelementptr inbounds i8, ptr %0, i64 %16
  %18 = tail call i32 %3(ptr noundef %14, ptr noundef %17) #1
  %19 = icmp slt i32 %18, 0
  %20 = trunc i64 %.02428 to i32
  %spec.select = select i1 %19, i32 %20, i32 %.02527
  %21 = add nuw i64 %.02428, 1
  %22 = icmp ult i64 %21, %1
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !7
}

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


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