; LLVM IR from vladpolisuk/algorithms :: Sorting/Counting Sort/code.ll (Z13counting_sortPii)

; 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: mustprogress nounwind uwtable
define dso_local void @_Z13counting_sortPii(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  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
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds i32, ptr %17, i64 0
  %19 = load i32, ptr %18, align 4, !tbaa !9
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds i32, ptr %20, i64 0
  %22 = load i32, ptr %21, align 4, !tbaa !9
  store i32 %22, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = sub nsw i32 %23, %24
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %8, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = call ptr @llvm.stacksave.p0()
  store ptr %29, ptr %9, align 8
  %30 = alloca i32, i64 %28, align 16
  store i64 %28, ptr %10, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 1, ptr %11, align 4, !tbaa !9
  br label %31

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

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

36:                                               ; preds = %31
  %37 = load i32, ptr %7, align 4, !tbaa !9
  %38 = load ptr, ptr %3, 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 = icmp slt i32 %37, %42
  br i1 %43, label %44, label %50

44:                                               ; preds = %36
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr %11, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %65

50:                                               ; preds = %36
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i32, ptr %52, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = icmp sgt i32 %51, %56
  br i1 %57, label %58, label %64

58:                                               ; preds = %50
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !9
  store i32 %63, ptr %6, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %58, %50
  br label %65

65:                                               ; preds = %64, %44
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %11, align 4, !tbaa !9
  br label %31, !llvm.loop !11

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

70:                                               ; preds = %79, %69
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = icmp slt i32 %71, %72
  br i1 %73, label %75, label %74

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

75:                                               ; preds = %70
  %76 = load i32, ptr %12, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %30, i64 %77
  store i32 0, ptr %78, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %75
  %80 = load i32, ptr %12, align 4, !tbaa !9
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %12, align 4, !tbaa !9
  br label %70, !llvm.loop !13

82:                                               ; preds = %74
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %83

83:                                               ; preds = %100, %82
  %84 = load i32, ptr %13, align 4, !tbaa !9
  %85 = load i32, ptr %4, align 4, !tbaa !9
  %86 = icmp slt i32 %84, %85
  br i1 %86, label %88, label %87

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

88:                                               ; preds = %83
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = load i32, ptr %13, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i32, ptr %89, i64 %91
  %93 = load i32, ptr %92, align 4, !tbaa !9
  %94 = load i32, ptr %6, align 4, !tbaa !9
  %95 = sub nsw i32 %93, %94
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds i32, ptr %30, 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
  br label %100

100:                                              ; preds = %88
  %101 = load i32, ptr %13, align 4, !tbaa !9
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %13, align 4, !tbaa !9
  br label %83, !llvm.loop !14

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

104:                                              ; preds = %132, %103
  %105 = load i32, ptr %14, align 4, !tbaa !9
  %106 = load i32, ptr %8, align 4, !tbaa !9
  %107 = icmp slt i32 %105, %106
  br i1 %107, label %109, label %108

108:                                              ; preds = %104
  store i32 11, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %135

109:                                              ; preds = %104
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %110

110:                                              ; preds = %128, %109
  %111 = load i32, ptr %16, align 4, !tbaa !9
  %112 = load i32, ptr %14, align 4, !tbaa !9
  %113 = sext i32 %112 to i64
  %114 = getelementptr inbounds i32, ptr %30, i64 %113
  %115 = load i32, ptr %114, align 4, !tbaa !9
  %116 = icmp slt i32 %111, %115
  br i1 %116, label %118, label %117

117:                                              ; preds = %110
  store i32 14, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  br label %131

118:                                              ; preds = %110
  %119 = load i32, ptr %14, align 4, !tbaa !9
  %120 = load i32, ptr %6, align 4, !tbaa !9
  %121 = add nsw i32 %119, %120
  %122 = load ptr, ptr %3, align 8, !tbaa !5
  %123 = load i32, ptr %5, align 4, !tbaa !9
  %124 = sext i32 %123 to i64
  %125 = getelementptr inbounds i32, ptr %122, i64 %124
  store i32 %121, ptr %125, align 4, !tbaa !9
  %126 = load i32, ptr %5, align 4, !tbaa !9
  %127 = add nsw i32 %126, 1
  store i32 %127, ptr %5, align 4, !tbaa !9
  br label %128

128:                                              ; preds = %118
  %129 = load i32, ptr %16, align 4, !tbaa !9
  %130 = add nsw i32 %129, 1
  store i32 %130, ptr %16, align 4, !tbaa !9
  br label %110, !llvm.loop !15

131:                                              ; preds = %117
  br label %132

132:                                              ; preds = %131
  %133 = load i32, ptr %14, align 4, !tbaa !9
  %134 = add nsw i32 %133, 1
  store i32 %134, ptr %14, align 4, !tbaa !9
  br label %104, !llvm.loop !16

135:                                              ; preds = %108
  %136 = load ptr, ptr %9, align 8
  call void @llvm.stackrestore.p0(ptr %136)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  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.end.p0(i64 immarg, ptr nocapture) #0

; 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z13counting_sortPii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 4, !tbaa !5
  %4 = icmp sgt i32 %1, 1
  br i1 %4, label %.lr.ph.preheader, label %.critedge.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  %5 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %5, 1
  %6 = icmp eq i32 %1, 2
  br i1 %6, label %.critedge.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %5, -2
  %invariant.gep = getelementptr inbounds i8, ptr %0, i64 4
  br label %.lr.ph

.critedge.preheader.loopexit.unr-lcssa:           ; preds = %.lr.ph, %.lr.ph.preheader
  %.145.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.145.1, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %.04455.unr = phi i32 [ %3, %.lr.ph.preheader ], [ %.145.1, %.lr.ph ]
  %.04654.unr = phi i32 [ %3, %.lr.ph.preheader ], [ %.147.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.critedge.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.critedge.preheader.loopexit.unr-lcssa
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp slt i32 %.04654.unr, %8
  %spec.select.epil = tail call i32 @llvm.smin.i32(i32 %.04455.unr, i32 %8)
  %.145.epil = select i1 %9, i32 %.04455.unr, i32 %spec.select.epil
  br label %.critedge.preheader

.critedge.preheader:                              ; preds = %.lr.ph.epil, %.critedge.preheader.loopexit.unr-lcssa, %2
  %.044.lcssa = phi i32 [ %3, %2 ], [ %.145.lcssa.ph, %.critedge.preheader.loopexit.unr-lcssa ], [ %.145.epil, %.lr.ph.epil ]
  %10 = icmp sgt i32 %1, 0
  br i1 %10, label %.preheader.us.preheader, label %.split

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.04455 = phi i32 [ %3, %.lr.ph.preheader.new ], [ %.145.1, %.lr.ph ]
  %.04654 = phi i32 [ %3, %.lr.ph.preheader.new ], [ %.147.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %11 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp slt i32 %.04654, %12
  %spec.select = tail call i32 @llvm.smin.i32(i32 %.04455, i32 %12)
  %.147 = tail call i32 @llvm.smax.i32(i32 %.04654, i32 %12)
  %.145 = select i1 %13, i32 %.04455, i32 %spec.select
  %gep = getelementptr inbounds i32, ptr %invariant.gep, i64 %indvars.iv
  %14 = load i32, ptr %gep, align 4, !tbaa !5
  %15 = icmp slt i32 %.147, %14
  %spec.select.1 = tail call i32 @llvm.smin.i32(i32 %.145, i32 %14)
  %.147.1 = tail call i32 @llvm.smax.i32(i32 %.147, i32 %14)
  %.145.1 = select i1 %15, i32 %.145, i32 %spec.select.1
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.critedge.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

.preheader.us.preheader:                          ; preds = %.critedge.preheader
  %wide.trip.count73 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.preheader.us.preheader77, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us.preheader
  %n.vec = and i64 %wide.trip.count73, 2147483640
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %.044.lcssa, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

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

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count73
  br i1 %cmp.n, label %.split, label %.preheader.us.preheader77

.preheader.us.preheader77:                        ; preds = %middle.block, %.preheader.us.preheader
  %indvars.iv68.ph = phi i64 [ 0, %.preheader.us.preheader ], [ %n.vec, %middle.block ]
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader77, %.preheader.us
  %indvars.iv68 = phi i64 [ %indvars.iv.next69, %.preheader.us ], [ %indvars.iv68.ph, %.preheader.us.preheader77 ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv68
  store i32 %.044.lcssa, ptr %19, align 4, !tbaa !5
  %indvars.iv.next69 = add nuw nsw i64 %indvars.iv68, 1
  %exitcond74.not = icmp eq i64 %indvars.iv.next69, %wide.trip.count73
  br i1 %exitcond74.not, label %.split, label %.preheader.us, !llvm.loop !14

.split:                                           ; preds = %.preheader.us, %middle.block, %.critedge.preheader
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { mustprogress 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 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 = !{!"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, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !10, !13, !12}


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