; LLVM IR from xxks-kkk/algo :: sorting (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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare void @swap(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @insertionSort(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @median3(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @Qsort(ptr noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = add nsw i32 %10, 3
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %14, label %77

14:                                               ; preds = %3
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = call i32 @median3(ptr noundef %15, i32 noundef %16, i32 noundef %17)
  store i32 %18, ptr %9, align 4, !tbaa !9
  %19 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %19, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 1
  store i32 %21, ptr %8, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %58, %14
  br label %23

23:                                               ; preds = %32, %22
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %7, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %24, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %23
  br label %23, !llvm.loop !11

33:                                               ; preds = %23
  br label %34

34:                                               ; preds = %43, %33
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %8, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %35, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = icmp sgt i32 %40, %41
  br i1 %42, label %43, label %44

43:                                               ; preds = %34
  br label %34, !llvm.loop !13

44:                                               ; preds = %34
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %57

48:                                               ; preds = %44
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %49, i64 %51
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %53, i64 %55
  call void @swap(ptr noundef %52, ptr noundef %56)
  br label %58

57:                                               ; preds = %44
  br label %59

58:                                               ; preds = %48
  br label %22

59:                                               ; preds = %57
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds i32, ptr %60, i64 %62
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = sub nsw i32 %65, 1
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i32, ptr %64, i64 %67
  call void @swap(ptr noundef %63, ptr noundef %68)
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = load i32, ptr %5, align 4, !tbaa !9
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = sub nsw i32 %71, 1
  call void @Qsort(ptr noundef %69, i32 noundef %70, i32 noundef %72)
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  %76 = load i32, ptr %6, align 4, !tbaa !9
  call void @Qsort(ptr noundef %73, i32 noundef %75, i32 noundef %76)
  br label %86

77:                                               ; preds = %3
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = load i32, ptr %5, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds i32, ptr %78, i64 %80
  %82 = load i32, ptr %6, align 4, !tbaa !9
  %83 = load i32, ptr %5, align 4, !tbaa !9
  %84 = sub nsw i32 %82, %83
  %85 = add nsw i32 %84, 1
  call void @insertionSort(ptr noundef %81, i32 noundef %85)
  br label %86

86:                                               ; preds = %77, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = !{!"int", !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"

declare void @swap(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @insertionSort(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @median3(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @Qsort(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = add nsw i32 %1, 3
  %.not45 = icmp sgt i32 %4, %2
  br i1 %.not45, label %tailrecurse._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = add nsw i32 %2, -1
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds i32, ptr %0, i64 %6
  br label %8

8:                                                ; preds = %.lr.ph, %tailrecurse
  %.tr3946 = phi i32 [ %1, %.lr.ph ], [ %29, %tailrecurse ]
  %9 = tail call i32 @median3(ptr noundef %0, i32 noundef %.tr3946, i32 noundef %2)
  br label %10

10:                                               ; preds = %25, %8
  %.032 = phi i32 [ %5, %8 ], [ %27, %25 ]
  %.0 = phi i32 [ %.tr3946, %8 ], [ %17, %25 ]
  %11 = sext i32 %.0 to i64
  br label %12

12:                                               ; preds = %12, %10
  %indvars.iv = phi i64 [ %indvars.iv.next, %12 ], [ %11, %10 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %13 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp slt i32 %14, %9
  br i1 %15, label %12, label %.preheader, !llvm.loop !9

.preheader:                                       ; preds = %12
  %16 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %17 = trunc nsw i64 %indvars.iv.next to i32
  %18 = sext i32 %.032 to i64
  br label %19

19:                                               ; preds = %.preheader, %19
  %indvars.iv53 = phi i64 [ %18, %.preheader ], [ %indvars.iv.next54, %19 ]
  %indvars.iv.next54 = add nsw i64 %indvars.iv53, -1
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next54
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp sgt i32 %21, %9
  br i1 %22, label %19, label %23, !llvm.loop !11

23:                                               ; preds = %19
  %24 = icmp slt i64 %indvars.iv.next, %indvars.iv.next54
  br i1 %24, label %25, label %tailrecurse

25:                                               ; preds = %23
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next54
  %27 = trunc nsw i64 %indvars.iv.next54 to i32
  tail call void @swap(ptr noundef nonnull %16, ptr noundef nonnull %26) #2
  br label %10

tailrecurse:                                      ; preds = %23
  %28 = trunc nsw i64 %indvars.iv to i32
  tail call void @swap(ptr noundef nonnull %16, ptr noundef nonnull %7) #2
  tail call void @Qsort(ptr noundef nonnull %0, i32 noundef %.tr3946, i32 noundef %28)
  %29 = add nsw i32 %28, 2
  %30 = add nsw i32 %28, 5
  %.not = icmp sgt i32 %30, %2
  br i1 %.not, label %tailrecurse._crit_edge, label %8

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  %.tr39.lcssa = phi i32 [ %1, %3 ], [ %29, %tailrecurse ]
  %31 = sext i32 %.tr39.lcssa to i64
  %32 = getelementptr inbounds i32, ptr %0, i64 %31
  %33 = add nsw i32 %2, 1
  %34 = sub i32 %33, %.tr39.lcssa
  tail call void @insertionSort(ptr noundef %32, i32 noundef %34)
  ret void
}

attributes #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 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 = !{!"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,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass