; LLVM IR from Dev-XYS/Algorithms :: Radix-Sort.ll (Z10radix_sortv)

; 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"

@n = external dso_local global i32, align 4
@d = external dso_local global [100000 x i32], align 16
@pow10 = external dso_local global [8 x i32], align 16
@_ZZ10radix_sortvE1c = external hidden global [10 x i32], align 16
@_ZZ10radix_sortvE2_d = external hidden global [100000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z10radix_sortv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %116, %0
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 8
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %119

12:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %21, %12
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 10
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %24

17:                                               ; preds = %13
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %19
  store i32 0, ptr %20, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %17
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %3, align 4, !tbaa !5
  br label %13, !llvm.loop !9

24:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %45, %24
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = load i32, ptr @n, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

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

30:                                               ; preds = %25
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [8 x i32], ptr @pow10, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = sdiv i32 %34, %38
  %40 = srem i32 %39, 10
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %42, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %30
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %4, align 4, !tbaa !5
  br label %25, !llvm.loop !11

48:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %64, %48
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = icmp slt i32 %50, 10
  br i1 %51, label %53, label %52

52:                                               ; preds = %49
  store i32 11, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %67

53:                                               ; preds = %49
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = sub nsw i32 %54, 1
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = add nsw i32 %62, %58
  store i32 %63, ptr %61, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %53
  %65 = load i32, ptr %5, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %5, align 4, !tbaa !5
  br label %49, !llvm.loop !12

67:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %68 = load i32, ptr @n, align 4, !tbaa !5
  %69 = sub nsw i32 %68, 1
  store i32 %69, ptr %6, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %95, %67
  %71 = load i32, ptr %6, align 4, !tbaa !5
  %72 = icmp sge i32 %71, 0
  br i1 %72, label %74, label %73

73:                                               ; preds = %70
  store i32 14, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %98

74:                                               ; preds = %70
  %75 = load i32, ptr %6, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %76
  %78 = load i32, ptr %77, align 4, !tbaa !5
  %79 = load i32, ptr %6, align 4, !tbaa !5
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !5
  %83 = load i32, ptr %1, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [8 x i32], ptr @pow10, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !5
  %87 = sdiv i32 %82, %86
  %88 = srem i32 %87, 10
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !5
  %92 = add nsw i32 %91, -1
  store i32 %92, ptr %90, align 4, !tbaa !5
  %93 = sext i32 %92 to i64
  %94 = getelementptr inbounds [100000 x i32], ptr @_ZZ10radix_sortvE2_d, i64 0, i64 %93
  store i32 %78, ptr %94, align 4, !tbaa !5
  br label %95

95:                                               ; preds = %74
  %96 = load i32, ptr %6, align 4, !tbaa !5
  %97 = add nsw i32 %96, -1
  store i32 %97, ptr %6, align 4, !tbaa !5
  br label %70, !llvm.loop !13

98:                                               ; preds = %73
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %99

99:                                               ; preds = %112, %98
  %100 = load i32, ptr %7, align 4, !tbaa !5
  %101 = load i32, ptr @n, align 4, !tbaa !5
  %102 = icmp slt i32 %100, %101
  br i1 %102, label %104, label %103

103:                                              ; preds = %99
  store i32 17, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %115

104:                                              ; preds = %99
  %105 = load i32, ptr %7, align 4, !tbaa !5
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds [100000 x i32], ptr @_ZZ10radix_sortvE2_d, i64 0, i64 %106
  %108 = load i32, ptr %107, align 4, !tbaa !5
  %109 = load i32, ptr %7, align 4, !tbaa !5
  %110 = sext i32 %109 to i64
  %111 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %110
  store i32 %108, ptr %111, align 4, !tbaa !5
  br label %112

112:                                              ; preds = %104
  %113 = load i32, ptr %7, align 4, !tbaa !5
  %114 = add nsw i32 %113, 1
  store i32 %114, ptr %7, align 4, !tbaa !5
  br label %99, !llvm.loop !14

115:                                              ; preds = %103
  br label %116

116:                                              ; preds = %115
  %117 = load i32, ptr %1, align 4, !tbaa !5
  %118 = add nsw i32 %117, 1
  store i32 %118, ptr %1, align 4, !tbaa !5
  br label %8, !llvm.loop !15

119:                                              ; preds = %11
  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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { 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}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


; 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"

@n = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [100000 x i32], align 16
@pow10 = external dso_local local_unnamed_addr global [8 x i32], align 16
@_ZZ10radix_sortvE1c = external hidden local_unnamed_addr global [10 x i32], align 16
@_ZZ10radix_sortvE2_d = external hidden local_unnamed_addr global [100000 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z10radix_sortv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  %3 = zext i32 %1 to i64
  %4 = shl nuw nsw i64 %3, 2
  br label %.preheader30

.preheader30:                                     ; preds = %0, %._crit_edge
  %indvars.iv51 = phi i64 [ 0, %0 ], [ %indvars.iv.next52, %._crit_edge ]
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) @_ZZ10radix_sortvE1c, i8 0, i64 40, i1 false), !tbaa !5
  br i1 %2, label %.lr.ph, label %.preheader28

5:                                                ; preds = %._crit_edge
  ret void

.lr.ph:                                           ; preds = %.preheader30
  %6 = getelementptr inbounds [8 x i32], ptr @pow10, i64 0, i64 %indvars.iv51
  %7 = load i32, ptr %6, align 4, !tbaa !5
  br label %26

.preheader28.loopexit:                            ; preds = %26
  %.pre = load i32, ptr @_ZZ10radix_sortvE1c, align 16, !tbaa !5
  %.pre55 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 4), align 4, !tbaa !5
  %.pre56 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 8), align 8, !tbaa !5
  %.pre57 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 12), align 4, !tbaa !5
  %.pre58 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 16), align 16, !tbaa !5
  %.pre59 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 20), align 4, !tbaa !5
  %.pre60 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 24), align 8, !tbaa !5
  %.pre61 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 28), align 4, !tbaa !5
  %.pre62 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 32), align 16, !tbaa !5
  %.pre63 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 36), align 4, !tbaa !5
  %8 = add nsw i32 %.pre55, %.pre
  br label %.preheader28

.preheader28:                                     ; preds = %.preheader28.loopexit, %.preheader30
  %9 = phi i32 [ %.pre63, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %10 = phi i32 [ %.pre62, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %11 = phi i32 [ %.pre61, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %12 = phi i32 [ %.pre60, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %13 = phi i32 [ %.pre59, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %14 = phi i32 [ %.pre58, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %15 = phi i32 [ %.pre57, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %16 = phi i32 [ %.pre56, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  %17 = phi i32 [ %8, %.preheader28.loopexit ], [ 0, %.preheader30 ]
  store i32 %17, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 4), align 4, !tbaa !5
  %18 = add nsw i32 %16, %17
  store i32 %18, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 8), align 8, !tbaa !5
  %19 = add nsw i32 %15, %18
  store i32 %19, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 12), align 4, !tbaa !5
  %20 = add nsw i32 %14, %19
  store i32 %20, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 16), align 16, !tbaa !5
  %21 = add nsw i32 %13, %20
  store i32 %21, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 20), align 4, !tbaa !5
  %22 = add nsw i32 %12, %21
  store i32 %22, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 24), align 8, !tbaa !5
  %23 = add nsw i32 %11, %22
  store i32 %23, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 28), align 4, !tbaa !5
  %24 = add nsw i32 %10, %23
  store i32 %24, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 32), align 16, !tbaa !5
  %25 = add nsw i32 %9, %24
  store i32 %25, ptr getelementptr inbounds (i8, ptr @_ZZ10radix_sortvE1c, i64 36), align 4, !tbaa !5
  br i1 %2, label %.lr.ph35, label %._crit_edge

26:                                               ; preds = %.lr.ph, %26
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %26 ]
  %27 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sdiv i32 %28, %7
  %30 = srem i32 %29, 10
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, 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
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %3
  br i1 %exitcond.not, label %.preheader28.loopexit, label %26, !llvm.loop !9

.lr.ph35:                                         ; preds = %.preheader28
  %35 = getelementptr inbounds [8 x i32], ptr @pow10, i64 0, i64 %indvars.iv51
  %36 = load i32, ptr %35, align 4, !tbaa !5
  br label %37

.preheader:                                       ; preds = %37
  br i1 %2, label %.lr.ph37.preheader, label %._crit_edge

.lr.ph37.preheader:                               ; preds = %.preheader
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @d, ptr nonnull align 16 @_ZZ10radix_sortvE2_d, i64 %4, i1 false), !tbaa !5
  br label %._crit_edge

37:                                               ; preds = %.lr.ph35, %37
  %indvars.iv45 = phi i64 [ %3, %.lr.ph35 ], [ %indvars.iv.next46, %37 ]
  %indvars.iv.next46 = add nsw i64 %indvars.iv45, -1
  %38 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv.next46
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = sdiv i32 %39, %36
  %41 = srem i32 %40, 10
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [10 x i32], ptr @_ZZ10radix_sortvE1c, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %43, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100000 x i32], ptr @_ZZ10radix_sortvE2_d, i64 0, i64 %46
  store i32 %39, ptr %47, align 4, !tbaa !5
  %48 = icmp sgt i64 %indvars.iv45, 1
  br i1 %48, label %37, label %.preheader, !llvm.loop !11

._crit_edge:                                      ; preds = %.preheader28, %.lr.ph37.preheader, %.preheader
  %indvars.iv.next52 = add nuw nsw i64 %indvars.iv51, 1
  %exitcond54.not = icmp eq i64 %indvars.iv.next52, 8
  br i1 %exitcond54.not, label %5, label %.preheader30, !llvm.loop !12
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; 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) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass