; LLVM IR from Ayon-SSP/The-SDE-Prep :: 2]_Algorithms/2]_Sorting/Sorting in C++/Radixsort.ll (Z9countsortPiii)

; 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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3maxPii(ptr 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: mustprogress nounwind uwtable
define dso_local void @_Z9countsortPiii(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 ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca [10 x i32], align 16
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = 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
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = call ptr @llvm.stacksave.p0()
  store ptr %17, ptr %7, align 8
  %18 = alloca i32, i64 %16, align 16
  store i64 %16, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %28, %3
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %31

24:                                               ; preds = %19
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10 x i32], ptr %9, i64 0, i64 %26
  store i32 0, ptr %27, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %24
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %10, align 4, !tbaa !9
  br label %19, !llvm.loop !11

31:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %50, %31
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %53

37:                                               ; preds = %32
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load i32, ptr %11, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %38, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = sdiv i32 %42, %43
  %45 = srem i32 %44, 10
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [10 x i32], ptr %9, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %47, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %37
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %11, align 4, !tbaa !9
  br label %32, !llvm.loop !13

53:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %72, %53
  %55 = load i32, ptr %12, align 4, !tbaa !9
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = call noundef i32 @_Z3maxPii(ptr noundef %56, i32 noundef %57)
  %59 = icmp slt i32 %55, %58
  br i1 %59, label %61, label %60

60:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %75

61:                                               ; preds = %54
  %62 = load i32, ptr %12, align 4, !tbaa !9
  %63 = sub nsw i32 %62, 1
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [10 x i32], ptr %9, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !9
  %67 = load i32, ptr %12, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [10 x i32], ptr %9, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !9
  %71 = add nsw i32 %70, %66
  store i32 %71, ptr %69, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %61
  %73 = load i32, ptr %12, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %12, align 4, !tbaa !9
  br label %54, !llvm.loop !14

75:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %76 = load i32, ptr %5, align 4, !tbaa !9
  %77 = sub nsw i32 %76, 1
  store i32 %77, ptr %13, align 4, !tbaa !9
  br label %78

78:                                               ; preds = %102, %75
  %79 = load i32, ptr %13, align 4, !tbaa !9
  %80 = icmp sge i32 %79, 0
  br i1 %80, label %82, label %81

81:                                               ; preds = %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %105

82:                                               ; preds = %78
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = load i32, ptr %13, align 4, !tbaa !9
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i32, ptr %83, i64 %85
  %87 = load i32, ptr %86, align 4, !tbaa !9
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = load i32, ptr %13, align 4, !tbaa !9
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds i32, ptr %88, i64 %90
  %92 = load i32, ptr %91, align 4, !tbaa !9
  %93 = load i32, ptr %6, align 4, !tbaa !9
  %94 = sdiv i32 %92, %93
  %95 = srem i32 %94, 10
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds i32, ptr %18, i64 %96
  %98 = load i32, ptr %97, align 4, !tbaa !9
  %99 = add nsw i32 %98, -1
  store i32 %99, ptr %97, align 4, !tbaa !9
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %18, i64 %100
  store i32 %87, ptr %101, align 4, !tbaa !9
  br label %102

102:                                              ; preds = %82
  %103 = load i32, ptr %13, align 4, !tbaa !9
  %104 = add nsw i32 %103, -1
  store i32 %104, ptr %13, align 4, !tbaa !9
  br label %78, !llvm.loop !15

105:                                              ; preds = %81
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %106

106:                                              ; preds = %120, %105
  %107 = load i32, ptr %14, align 4, !tbaa !9
  %108 = load i32, ptr %5, align 4, !tbaa !9
  %109 = icmp slt i32 %107, %108
  br i1 %109, label %111, label %110

110:                                              ; preds = %106
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %123

111:                                              ; preds = %106
  %112 = load i32, ptr %14, align 4, !tbaa !9
  %113 = sext i32 %112 to i64
  %114 = getelementptr inbounds i32, ptr %18, i64 %113
  %115 = load i32, ptr %114, align 4, !tbaa !9
  %116 = load ptr, ptr %4, align 8, !tbaa !5
  %117 = load i32, ptr %14, align 4, !tbaa !9
  %118 = sext i32 %117 to i64
  %119 = getelementptr inbounds i32, ptr %116, i64 %118
  store i32 %115, ptr %119, align 4, !tbaa !9
  br label %120

120:                                              ; preds = %111
  %121 = load i32, ptr %14, align 4, !tbaa !9
  %122 = add nsw i32 %121, 1
  store i32 %122, ptr %14, align 4, !tbaa !9
  br label %106, !llvm.loop !16

123:                                              ; preds = %110
  call void @llvm.lifetime.end.p0(i64 40, ptr %9) #3
  %124 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %124)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z3maxPii(ptr noundef, i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9countsortPiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca [10 x i32], align 16
  %5 = zext i32 %1 to i64
  %6 = alloca i32, i64 %5, align 16
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %4) #4
  %7 = icmp sgt i32 %1, 0
  br i1 %7, label %.lr.ph41.preheader, label %.preheader37

.lr.ph41.preheader:                               ; preds = %3
  %8 = shl nuw nsw i64 %5, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %4, i8 0, i64 %8, i1 false), !tbaa !5
  br label %.lr.ph41

.preheader37:                                     ; preds = %.lr.ph41, %3
  %9 = tail call noundef i32 @_Z3maxPii(ptr noundef %0, i32 noundef %1)
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph43.preheader, label %.preheader36

.lr.ph43.preheader:                               ; preds = %.preheader37
  %.phi.trans.insert = getelementptr inbounds i8, ptr %4, i64 -4
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !5
  br label %.lr.ph43

.lr.ph41:                                         ; preds = %.lr.ph41.preheader, %.lr.ph41
  %indvars.iv = phi i64 [ 0, %.lr.ph41.preheader ], [ %indvars.iv.next, %.lr.ph41 ]
  %11 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sdiv i32 %12, %2
  %14 = srem i32 %13, 10
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10 x i32], ptr %4, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %16, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %5
  br i1 %exitcond.not, label %.preheader37, label %.lr.ph41, !llvm.loop !9

.preheader36:                                     ; preds = %.lr.ph43, %.preheader37
  br i1 %7, label %.lr.ph45, label %._crit_edge

.lr.ph43:                                         ; preds = %.lr.ph43.preheader, %.lr.ph43
  %19 = phi i32 [ %.pre, %.lr.ph43.preheader ], [ %22, %.lr.ph43 ]
  %indvars.iv50 = phi i64 [ 0, %.lr.ph43.preheader ], [ %indvars.iv.next51, %.lr.ph43 ]
  %20 = getelementptr inbounds [10 x i32], ptr %4, i64 0, i64 %indvars.iv50
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %19
  store i32 %22, ptr %20, align 4, !tbaa !5
  %indvars.iv.next51 = add nuw nsw i64 %indvars.iv50, 1
  %23 = tail call noundef i32 @_Z3maxPii(ptr noundef %0, i32 noundef %1)
  %24 = sext i32 %23 to i64
  %25 = icmp slt i64 %indvars.iv.next51, %24
  br i1 %25, label %.lr.ph43, label %.preheader36, !llvm.loop !11

.preheader:                                       ; preds = %.lr.ph45
  br i1 %7, label %.lr.ph47.preheader, label %._crit_edge

.lr.ph47.preheader:                               ; preds = %.preheader
  %26 = shl nuw nsw i64 %5, 2
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %0, ptr nonnull align 16 %6, i64 %26, i1 false), !tbaa !5
  br label %._crit_edge

.lr.ph45:                                         ; preds = %.preheader36, %.lr.ph45
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %.lr.ph45 ], [ %5, %.preheader36 ]
  %indvars.iv.next54 = add nsw i64 %indvars.iv53, -1
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next54
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sdiv i32 %28, %2
  %30 = srem i32 %29, 10
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %6, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %33, -1
  store i32 %34, ptr %32, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %6, i64 %35
  store i32 %28, ptr %36, align 4, !tbaa !5
  %37 = icmp sgt i64 %indvars.iv53, 1
  br i1 %37, label %.lr.ph45, label %.preheader, !llvm.loop !12

._crit_edge:                                      ; preds = %.preheader36, %.lr.ph47.preheader, %.preheader
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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

attributes #0 = { mustprogress 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass