; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/noncomparisonDistribution (radixLSDSort)

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

19:                                               ; preds = %44, %3
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i64, ptr %21, i64 %23
  %25 = icmp ult ptr %20, %24
  br i1 %25, label %26, label %47

26:                                               ; preds = %19
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = load i64, ptr %7, align 8, !tbaa !11
  %30 = icmp slt i64 %28, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %26
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = load i64, ptr %32, align 8, !tbaa !11
  store i64 %33, ptr %7, align 8, !tbaa !11
  br label %43

34:                                               ; preds = %26
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = load i64, ptr %8, align 8, !tbaa !11
  %38 = icmp sgt i64 %36, %37
  br i1 %38, label %39, label %42

39:                                               ; preds = %34
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = load i64, ptr %40, align 8, !tbaa !11
  store i64 %41, ptr %8, align 8, !tbaa !11
  br label %42

42:                                               ; preds = %39, %34
  br label %43

43:                                               ; preds = %42, %31
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds i64, ptr %45, i32 1
  store ptr %46, ptr %9, align 8, !tbaa !5
  br label %19, !llvm.loop !13

47:                                               ; preds = %19
  br label %48

48:                                               ; preds = %56, %47
  %49 = load i64, ptr %8, align 8, !tbaa !11
  %50 = load i64, ptr %7, align 8, !tbaa !11
  %51 = sub nsw i64 %49, %50
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = sdiv i64 %51, %53
  %55 = icmp sge i64 %54, 1
  br i1 %55, label %56, label %65

56:                                               ; preds = %48
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = load i32, ptr %10, align 4, !tbaa !9
  %60 = load i64, ptr %7, align 8, !tbaa !11
  %61 = load i32, ptr %5, align 4, !tbaa !9
  call void @countingByDigit(ptr noundef %57, i32 noundef %58, i32 noundef %59, i64 noundef %60, i32 noundef %61)
  %62 = load i32, ptr %6, align 4, !tbaa !9
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = mul nsw i32 %63, %62
  store i32 %64, ptr %10, align 4, !tbaa !9
  br label %48, !llvm.loop !15

65:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  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
  ret void
}

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: nounwind uwtable
declare dso_local void @countingByDigit(ptr noundef, i32 noundef, i32 noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @radixLSDSort(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = or i32 %2, 10
  %5 = load i64, ptr %0, align 8, !tbaa !5
  %6 = sext i32 %1 to i64
  %.idx = shl nsw i64 %6, 3
  %7 = getelementptr inbounds i8, ptr %0, i64 %.idx
  %8 = icmp sgt i32 %1, 1
  br i1 %8, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %3
  %.02329 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.lr.ph

.preheader:                                       ; preds = %.lr.ph, %3
  %.025.lcssa = phi i64 [ %5, %3 ], [ %.126, %.lr.ph ]
  %.024.lcssa = phi i64 [ %5, %3 ], [ %.1, %.lr.ph ]
  %9 = sub nsw i64 %.024.lcssa, %.025.lcssa
  %10 = icmp sgt i64 %9, 0
  br i1 %10, label %.lr.ph35, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.02332 = phi ptr [ %.023, %.lr.ph ], [ %.02329, %.lr.ph.preheader ]
  %.02431 = phi i64 [ %.1, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %.02530 = phi i64 [ %.126, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %11 = load i64, ptr %.02332, align 8, !tbaa !5
  %12 = icmp slt i64 %11, %.02530
  %spec.select = tail call i64 @llvm.smax.i64(i64 %11, i64 %.02431)
  %.126 = tail call i64 @llvm.smin.i64(i64 %11, i64 %.02530)
  %.1 = select i1 %12, i64 %.02431, i64 %spec.select
  %.023 = getelementptr inbounds i8, ptr %.02332, i64 8
  %13 = icmp ult ptr %.023, %7
  br i1 %13, label %.lr.ph, label %.preheader, !llvm.loop !9

.lr.ph35:                                         ; preds = %.preheader, %.lr.ph35
  %.034 = phi i32 [ %14, %.lr.ph35 ], [ 1, %.preheader ]
  tail call void @countingByDigit(ptr noundef nonnull %0, i32 noundef %4, i32 noundef %.034, i64 noundef %.025.lcssa, i32 noundef %1)
  %14 = mul nsw i32 %.034, %4
  %15 = sext i32 %14 to i64
  %16 = sdiv i64 %9, %15
  %17 = icmp sgt i64 %16, 0
  br i1 %17, label %.lr.ph35, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader
  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 = { 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 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,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass