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

; 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 @pigeonhole_sort(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sub nsw i32 %16, %17
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = call ptr @llvm.stacksave.p0()
  store ptr %22, ptr %14, align 8
  %23 = alloca i32, i64 %21, align 16
  store i64 %21, ptr %15, align 8
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %32, %4
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %28, label %35

28:                                               ; preds = %24
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %23, i64 %30
  store i32 0, ptr %31, align 4, !tbaa !9
  br label %32

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

35:                                               ; preds = %24
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %52, %35
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %40, label %55

40:                                               ; preds = %36
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sub nsw i32 %45, %46
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %23, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %49, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %40
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %11, align 4, !tbaa !9
  br label %36, !llvm.loop !13

55:                                               ; preds = %36
  store i32 0, ptr %12, align 4, !tbaa !9
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %79, %55
  %57 = load i32, ptr %13, align 4, !tbaa !9
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %60, label %82

60:                                               ; preds = %56
  br label %61

61:                                               ; preds = %68, %60
  %62 = load i32, ptr %13, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i32, ptr %23, 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
  %67 = icmp sgt i32 %65, 0
  br i1 %67, label %68, label %78

68:                                               ; preds = %61
  %69 = load i32, ptr %13, align 4, !tbaa !9
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = add nsw i32 %69, %70
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = load i32, ptr %12, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds i32, ptr %72, i64 %74
  store i32 %71, ptr %75, align 4, !tbaa !9
  %76 = load i32, ptr %12, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %12, align 4, !tbaa !9
  br label %61, !llvm.loop !14

78:                                               ; preds = %61
  br label %79

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

82:                                               ; preds = %56
  %83 = load ptr, ptr %14, align 8
  call void @llvm.stackrestore.p0(ptr %83)
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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 @pigeonhole_sort(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = sub nsw i32 %3, %2
  %6 = add nsw i32 %5, 1
  %7 = zext i32 %6 to i64
  %8 = alloca i32, i64 %7, align 16
  %.not29 = icmp slt i32 %5, 0
  br i1 %.not29, label %.preheader28, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %9 = add i32 %3, 1
  %10 = sub i32 %9, %2
  %11 = zext i32 %10 to i64
  %12 = shl nuw nsw i64 %11, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %8, i8 0, i64 %12, i1 false), !tbaa !5
  br label %.preheader28

.preheader28:                                     ; preds = %.lr.ph.preheader, %4
  %13 = icmp sgt i32 %1, 0
  br i1 %13, label %.lr.ph32.preheader, label %.preheader27

.lr.ph32.preheader:                               ; preds = %.preheader28
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %14 = icmp eq i32 %1, 1
  br i1 %14, label %.preheader27.loopexit.unr-lcssa, label %.lr.ph32.preheader.new

.lr.ph32.preheader.new:                           ; preds = %.lr.ph32.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph32

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

.lr.ph32.epil:                                    ; preds = %.preheader27.loopexit.unr-lcssa
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sub nsw i32 %16, %2
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %8, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %19, align 4, !tbaa !5
  br label %.preheader27

.preheader27:                                     ; preds = %.lr.ph32.epil, %.preheader27.loopexit.unr-lcssa, %.preheader28
  br i1 %.not29, label %._crit_edge39, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader27
  %22 = add i32 %3, 1
  %23 = sub i32 %22, %2
  %wide.trip.count50 = zext i32 %23 to i64
  br label %.preheader

.lr.ph32:                                         ; preds = %.lr.ph32, %.lr.ph32.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph32.preheader.new ], [ %indvars.iv.next.1, %.lr.ph32 ]
  %niter = phi i64 [ 0, %.lr.ph32.preheader.new ], [ %niter.next.1, %.lr.ph32 ]
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sub nsw i32 %25, %2
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %8, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %28, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %31 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sub nsw i32 %32, %2
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %8, 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.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 %.preheader27.loopexit.unr-lcssa, label %.lr.ph32, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv47 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next48, %._crit_edge ]
  %.02437 = phi i32 [ 0, %.preheader.preheader ], [ %.1.lcssa, %._crit_edge ]
  %38 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv47
  %.promoted = load i32, ptr %38, align 4, !tbaa !5
  %39 = add nsw i32 %.promoted, -1
  %40 = icmp sgt i32 %.promoted, 0
  br i1 %40, label %.lr.ph34, label %._crit_edge

.lr.ph34:                                         ; preds = %.preheader
  %41 = sext i32 %.02437 to i64
  %42 = trunc i64 %indvars.iv47 to i32
  %43 = add i32 %42, %2
  %44 = zext nneg i32 %.promoted to i64
  %min.iters.check = icmp ult i32 %.promoted, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph34
  %n.vec = and i64 %44, 2147483640
  %ind.end = add nsw i64 %n.vec, %41
  %.cast = trunc nuw nsw i64 %n.vec to i32
  %ind.end52 = sub i32 %39, %.cast
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %43, 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 %41
  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
  %45 = 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 %45, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %46 = icmp eq i64 %index.next, %n.vec
  br i1 %46, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %44
  br i1 %cmp.n, label %._crit_edge.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph34
  %indvars.iv44.ph = phi i64 [ %41, %.lr.ph34 ], [ %ind.end, %middle.block ]
  %.ph = phi i32 [ %39, %.lr.ph34 ], [ %ind.end52, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv44 = phi i64 [ %indvars.iv.next45, %scalar.ph ], [ %indvars.iv44.ph, %scalar.ph.preheader ]
  %47 = phi i32 [ %49, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %48 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv44
  store i32 %43, ptr %48, align 4, !tbaa !5
  %indvars.iv.next45 = add nsw i64 %indvars.iv44, 1
  %49 = add nsw i32 %47, -1
  %.not = icmp eq i32 %47, 0
  br i1 %.not, label %._crit_edge.loopexit, label %scalar.ph, !llvm.loop !14

._crit_edge.loopexit:                             ; preds = %scalar.ph, %middle.block
  %indvars.iv.next45.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next45, %scalar.ph ]
  %50 = trunc nsw i64 %indvars.iv.next45.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.lcssa = phi i32 [ %39, %.preheader ], [ -1, %._crit_edge.loopexit ]
  %.1.lcssa = phi i32 [ %.02437, %.preheader ], [ %50, %._crit_edge.loopexit ]
  store i32 %.lcssa, ptr %38, align 4, !tbaa !5
  %indvars.iv.next48 = add nuw nsw i64 %indvars.iv47, 1
  %exitcond51.not = icmp eq i64 %indvars.iv.next48, %wide.trip.count50
  br i1 %exitcond51.not, label %._crit_edge39, label %.preheader, !llvm.loop !15

._crit_edge39:                                    ; preds = %._crit_edge, %.preheader27
  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.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}
!15 = distinct !{!15, !10}


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