; LLVM IR from ahampriyanshu/algo-ds-101 :: Algorithms/Sorting/Bucket_Sort (bucketsortinC)

; 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
define dso_local void @Bucket_Sort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %7, align 8
  %12 = alloca i32, i64 %10, align 16
  store i64 %10, ptr %8, align 8
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %13

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

17:                                               ; preds = %13
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr %12, i64 %19
  store i32 0, ptr %20, align 4, !tbaa !9
  br label %21

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

24:                                               ; preds = %13
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %39, %24
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !9
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %29, label %42

29:                                               ; preds = %25
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %30, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %12, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %36, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %29
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %5, align 4, !tbaa !9
  br label %25, !llvm.loop !13

42:                                               ; preds = %25
  store i32 0, ptr %5, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %68, %42
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = load i32, ptr %4, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %71

47:                                               ; preds = %43
  br label %48

48:                                               ; preds = %61, %47
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr %12, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !9
  %53 = icmp sgt i32 %52, 0
  br i1 %53, label %54, label %67

54:                                               ; preds = %48
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %6, align 4, !tbaa !9
  %59 = sext i32 %57 to i64
  %60 = getelementptr inbounds i32, ptr %56, i64 %59
  store i32 %55, ptr %60, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %54
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i32, ptr %12, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !9
  %66 = add nsw i32 %65, -1
  store i32 %66, ptr %64, align 4, !tbaa !9
  br label %48, !llvm.loop !14

67:                                               ; preds = %48
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %5, align 4, !tbaa !9
  br label %43, !llvm.loop !15

71:                                               ; preds = %43
  %72 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %72)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  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
declare ptr @llvm.stacksave.p0() #2

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 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/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}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @Bucket_Sort(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i32 %1 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph25.preheader, label %._crit_edge31

.lr.ph25.preheader:                               ; preds = %2
  %6 = shl nuw nsw i64 %3, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %4, i8 0, i64 %6, i1 false), !tbaa !5
  %xtraiter = and i64 %3, 3
  %7 = icmp ult i32 %1, 4
  br i1 %7, label %.preheader21.unr-lcssa, label %.lr.ph25.preheader.new

.lr.ph25.preheader.new:                           ; preds = %.lr.ph25.preheader
  %unroll_iter = and i64 %3, 2147483644
  br label %.lr.ph25

.preheader21.unr-lcssa:                           ; preds = %.lr.ph25, %.lr.ph25.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph25.preheader ], [ %indvars.iv.next.3, %.lr.ph25 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader21, label %.lr.ph25.epil

.lr.ph25.epil:                                    ; preds = %.preheader21.unr-lcssa, %.lr.ph25.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph25.epil ], [ %indvars.iv.unr, %.preheader21.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph25.epil ], [ 0, %.preheader21.unr-lcssa ]
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.epil
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr %4, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %11, align 4, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader21, label %.lr.ph25.epil, !llvm.loop !9

.preheader21:                                     ; preds = %.lr.ph25.epil, %.preheader21.unr-lcssa
  br i1 %5, label %.preheader, label %._crit_edge31

.lr.ph25:                                         ; preds = %.lr.ph25, %.lr.ph25.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph25.preheader.new ], [ %indvars.iv.next.3, %.lr.ph25 ]
  %niter = phi i64 [ 0, %.lr.ph25.preheader.new ], [ %niter.next.3, %.lr.ph25 ]
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr %4, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %17, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %4, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %23, align 4, !tbaa !5
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %4, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %29, align 4, !tbaa !5
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %32 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.2
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %4, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %35, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.preheader21.unr-lcssa, label %.lr.ph25, !llvm.loop !11

.preheader:                                       ; preds = %.preheader21, %._crit_edge
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %._crit_edge ], [ 0, %.preheader21 ]
  %.01829 = phi i32 [ %.119.lcssa, %._crit_edge ], [ 0, %.preheader21 ]
  %38 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv39
  %.promoted = load i32, ptr %38, align 4, !tbaa !5
  %39 = icmp sgt i32 %.promoted, 0
  br i1 %39, label %.lr.ph27.preheader, label %._crit_edge

.lr.ph27.preheader:                               ; preds = %.preheader
  %40 = sext i32 %.01829 to i64
  %41 = trunc nuw nsw i64 %indvars.iv39 to i32
  %42 = zext nneg i32 %.promoted to i64
  %min.iters.check = icmp ult i32 %.promoted, 8
  br i1 %min.iters.check, label %.lr.ph27.preheader46, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph27.preheader
  %n.vec = and i64 %42, 2147483640
  %ind.end = add nsw i64 %n.vec, %40
  %.cast = trunc nuw nsw i64 %n.vec to i32
  %ind.end44 = sub nsw i32 %.promoted, %.cast
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %41, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %invariant.gep = getelementptr i32, ptr %0, i64 %40
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %index
  %43 = getelementptr inbounds i8, ptr %gep, i64 16
  store <4 x i32> %broadcast.splat, ptr %gep, align 4, !tbaa !5
  store <4 x i32> %broadcast.splat, ptr %43, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %44 = icmp eq i64 %index.next, %n.vec
  br i1 %44, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %42
  br i1 %cmp.n, label %._crit_edge.loopexit, label %.lr.ph27.preheader46

.lr.ph27.preheader46:                             ; preds = %middle.block, %.lr.ph27.preheader
  %indvars.iv36.ph = phi i64 [ %40, %.lr.ph27.preheader ], [ %ind.end, %middle.block ]
  %.ph = phi i32 [ %.promoted, %.lr.ph27.preheader ], [ %ind.end44, %middle.block ]
  br label %.lr.ph27

.lr.ph27:                                         ; preds = %.lr.ph27.preheader46, %.lr.ph27
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %.lr.ph27 ], [ %indvars.iv36.ph, %.lr.ph27.preheader46 ]
  %45 = phi i32 [ %47, %.lr.ph27 ], [ %.ph, %.lr.ph27.preheader46 ]
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, 1
  %46 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv36
  store i32 %41, ptr %46, align 4, !tbaa !5
  %47 = add nsw i32 %45, -1
  %48 = icmp ugt i32 %45, 1
  br i1 %48, label %.lr.ph27, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %.lr.ph27, %middle.block
  %indvars.iv.next37.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next37, %.lr.ph27 ]
  %49 = trunc nsw i64 %indvars.iv.next37.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.lcssa = phi i32 [ %.promoted, %.preheader ], [ 0, %._crit_edge.loopexit ]
  %.119.lcssa = phi i32 [ %.01829, %.preheader ], [ %49, %._crit_edge.loopexit ]
  store i32 %.lcssa, ptr %38, align 4, !tbaa !5
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next40, %3
  br i1 %exitcond43.not, label %._crit_edge31, label %.preheader, !llvm.loop !17

._crit_edge31:                                    ; preds = %._crit_edge, %2, %.preheader21
  ret void
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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) }

!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.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12, !14, !15}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !12, !15, !14}
!17 = distinct !{!17, !12}


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