; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/sort/pigeon_hole_sort.ll (Z16pigeon_hole_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"

%struct.LargestAndSmallest = type { i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i64 @_Z21find_max_min_elementsPii(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 @_Z16pigeon_hole_sortPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %struct.LargestAndSmallest, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = 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 8, ptr %5) #4
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = call i64 @_Z21find_max_min_elementsPii(ptr noundef %12, i32 noundef %13)
  store i64 %14, ptr %5, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %15 = getelementptr inbounds %struct.LargestAndSmallest, ptr %5, i32 0, i32 0
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = getelementptr inbounds %struct.LargestAndSmallest, ptr %5, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !13
  %19 = sub nsw i32 %16, %18
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %6, align 4, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = call ptr @llvm.stacksave.p0()
  store ptr %23, ptr %7, align 8
  %24 = alloca i32, i64 %22, align 16
  store i64 %22, ptr %8, align 8
  %25 = mul nuw i64 4, %22
  call void @llvm.memset.p0.i64(ptr align 16 %24, i8 0, i64 %25, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %44, %2
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %47

31:                                               ; preds = %26
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = getelementptr inbounds %struct.LargestAndSmallest, ptr %5, i32 0, i32 1
  %38 = load i32, ptr %37, align 4, !tbaa !13
  %39 = sub nsw i32 %36, %38
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %24, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %41, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %31
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %9, align 4, !tbaa !9
  br label %26, !llvm.loop !14

47:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %73, %47
  %49 = load i32, ptr %11, align 4, !tbaa !9
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %53, label %52

52:                                               ; preds = %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %76

53:                                               ; preds = %48
  br label %54

54:                                               ; preds = %61, %53
  %55 = load i32, ptr %11, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %24, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %57, align 4, !tbaa !9
  %60 = icmp sgt i32 %58, 0
  br i1 %60, label %61, label %72

61:                                               ; preds = %54
  %62 = load i32, ptr %11, align 4, !tbaa !9
  %63 = getelementptr inbounds %struct.LargestAndSmallest, ptr %5, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !13
  %65 = add nsw i32 %62, %64
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %10, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i32, ptr %66, i64 %68
  store i32 %65, ptr %69, align 4, !tbaa !9
  %70 = load i32, ptr %10, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %10, align 4, !tbaa !9
  br label %54, !llvm.loop !16

72:                                               ; preds = %54
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %11, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %11, align 4, !tbaa !9
  br label %48, !llvm.loop !17

76:                                               ; preds = %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %77 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %77)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

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

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

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS18LargestAndSmallest", !10, i64 0, !10, i64 4}
!13 = !{!12, !10, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


; 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 i64 @_Z21find_max_min_elementsPii(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z16pigeon_hole_sortPii(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i64 @_Z21find_max_min_elementsPii(ptr noundef %0, i32 noundef %1)
  %.sroa.0.0.extract.trunc = trunc i64 %3 to i32
  %.sroa.4.0.extract.shift = lshr i64 %3, 32
  %.sroa.4.0.extract.trunc = trunc nuw i64 %.sroa.4.0.extract.shift to i32
  %4 = sub nsw i32 %.sroa.0.0.extract.trunc, %.sroa.4.0.extract.trunc
  %5 = add nsw i32 %4, 1
  %6 = zext i32 %5 to i64
  %7 = alloca i32, i64 %6, align 16
  %8 = shl nuw nsw i64 %6, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %7, i8 0, i64 %8, i1 false)
  %9 = icmp sgt i32 %1, 0
  br i1 %9, label %.lr.ph.preheader, label %.preheader20

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

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

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

.lr.ph.epil:                                      ; preds = %.preheader20.loopexit.unr-lcssa
  %11 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sub nsw i32 %12, %.sroa.4.0.extract.trunc
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr %7, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %15, align 4, !tbaa !5
  br label %.preheader20

.preheader20:                                     ; preds = %.lr.ph.epil, %.preheader20.loopexit.unr-lcssa, %2
  %.not25 = icmp slt i32 %4, 0
  br i1 %.not25, label %._crit_edge28, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader20
  %18 = add i64 %3, 1
  %19 = sub i64 %18, %.sroa.4.0.extract.shift
  %wide.trip.count38 = and i64 %19, 4294967295
  br label %.preheader

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %20 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = sub nsw i32 %21, %.sroa.4.0.extract.trunc
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr %7, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %24, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sub nsw i32 %28, %.sroa.4.0.extract.trunc
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %7, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %31, 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 %.preheader20.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv35 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next36, %._crit_edge ]
  %.01826 = phi i32 [ 0, %.preheader.preheader ], [ %.1.lcssa, %._crit_edge ]
  %34 = getelementptr inbounds i32, ptr %7, i64 %indvars.iv35
  %.promoted = load i32, ptr %34, align 4, !tbaa !5
  %35 = add nsw i32 %.promoted, -1
  %36 = icmp sgt i32 %.promoted, 0
  br i1 %36, label %.lr.ph23, label %._crit_edge

.lr.ph23:                                         ; preds = %.preheader
  %37 = add nuw i64 %indvars.iv35, %.sroa.4.0.extract.shift
  %38 = sext i32 %.01826 to i64
  %39 = trunc i64 %37 to i32
  %40 = 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

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph23
  %indvars.iv32.ph = phi i64 [ %38, %.lr.ph23 ], [ %ind.end, %middle.block ]
  %.ph = phi i32 [ %35, %.lr.ph23 ], [ %ind.end40, %middle.block ]
  br label %scalar.ph

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

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

._crit_edge28:                                    ; preds = %._crit_edge, %.preheader20
  ret void

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

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

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.lcssa = phi i32 [ %35, %.preheader ], [ -1, %._crit_edge.loopexit ]
  %.1.lcssa = phi i32 [ %.01826, %.preheader ], [ %46, %._crit_edge.loopexit ]
  store i32 %.lcssa, ptr %34, align 4, !tbaa !5
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, 1
  %exitcond39.not = icmp eq i64 %indvars.iv.next36, %wide.trip.count38
  br i1 %exitcond39.not, label %._crit_edge28, label %.preheader, !llvm.loop !15
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { mustprogress 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++ 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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass