; LLVM IR from HarshCasper/NeoAlgo :: C/sort (pigeon_hole_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"

%struct.MaxAndMin = type { i32, i32 }

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

26:                                               ; preds = %35, %2
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = load i32, ptr %6, 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) #3
  br label %38

31:                                               ; preds = %26
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %25, i64 %33
  store i32 0, ptr %34, align 4, !tbaa !9
  br label %35

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

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

39:                                               ; preds = %57, %38
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %60

44:                                               ; preds = %39
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i32, ptr %10, 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
  %50 = getelementptr inbounds %struct.MaxAndMin, ptr %5, i32 0, i32 1
  %51 = load i32, ptr %50, align 4, !tbaa !13
  %52 = sub nsw i32 %49, %51
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %25, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %54, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %44
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %10, align 4, !tbaa !9
  br label %39, !llvm.loop !16

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

61:                                               ; preds = %86, %60
  %62 = load i32, ptr %12, align 4, !tbaa !9
  %63 = load i32, ptr %6, align 4, !tbaa !9
  %64 = icmp slt i32 %62, %63
  br i1 %64, label %66, label %65

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

66:                                               ; preds = %61
  br label %67

67:                                               ; preds = %74, %66
  %68 = load i32, ptr %12, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds i32, ptr %25, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !9
  %72 = add nsw i32 %71, -1
  store i32 %72, ptr %70, align 4, !tbaa !9
  %73 = icmp sgt i32 %71, 0
  br i1 %73, label %74, label %85

74:                                               ; preds = %67
  %75 = load i32, ptr %12, align 4, !tbaa !9
  %76 = getelementptr inbounds %struct.MaxAndMin, ptr %5, i32 0, i32 1
  %77 = load i32, ptr %76, align 4, !tbaa !13
  %78 = add nsw i32 %75, %77
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds i32, ptr %79, i64 %81
  store i32 %78, ptr %82, align 4, !tbaa !9
  %83 = load i32, ptr %11, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %11, align 4, !tbaa !9
  br label %67, !llvm.loop !17

85:                                               ; preds = %67
  br label %86

86:                                               ; preds = %85
  %87 = load i32, ptr %12, align 4, !tbaa !9
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %12, align 4, !tbaa !9
  br label %61, !llvm.loop !18

89:                                               ; preds = %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %90 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %90)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; 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 = { 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 = !{!12, !10, i64 0}
!12 = !{!"MaxAndMin", !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}
!18 = distinct !{!18, !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: nounwind uwtable
declare dso_local i64 @find_max_min_elements(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @pigeon_hole_sort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call i64 @find_max_min_elements(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
  %.not29 = icmp slt i32 %4, 0
  br i1 %.not29, label %.preheader28, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %8 = add i64 %3, 1
  %9 = sub i64 %8, %.sroa.4.0.extract.shift
  %10 = shl i64 %9, 2
  %11 = and i64 %10, 17179869180
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %7, i8 0, i64 %11, i1 false), !tbaa !5
  br label %.preheader28

.preheader28:                                     ; preds = %.lr.ph.preheader, %2
  %12 = icmp sgt i32 %1, 0
  br i1 %12, 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
  %13 = icmp eq i32 %1, 1
  br i1 %13, 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
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = sub nsw i32 %15, %.sroa.4.0.extract.trunc
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr %7, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %18, 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
  %21 = add i64 %3, 1
  %22 = sub i64 %21, %.sroa.4.0.extract.shift
  %wide.trip.count50 = and i64 %22, 4294967295
  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 ]
  %23 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = sub nsw i32 %24, %.sroa.4.0.extract.trunc
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %7, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %27, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %30 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sub nsw i32 %31, %.sroa.4.0.extract.trunc
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %7, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %34, 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 ]
  %.02237 = phi i32 [ 0, %.preheader.preheader ], [ %.1.lcssa, %._crit_edge ]
  %37 = getelementptr inbounds i32, ptr %7, i64 %indvars.iv47
  %.promoted = load i32, ptr %37, align 4, !tbaa !5
  %38 = add nsw i32 %.promoted, -1
  %39 = icmp sgt i32 %.promoted, 0
  br i1 %39, label %.lr.ph34, label %._crit_edge

.lr.ph34:                                         ; preds = %.preheader
  %40 = add nuw i64 %indvars.iv47, %.sroa.4.0.extract.shift
  %41 = sext i32 %.02237 to i64
  %42 = trunc i64 %40 to i32
  %43 = 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.ph34
  %indvars.iv44.ph = phi i64 [ %41, %.lr.ph34 ], [ %ind.end, %middle.block ]
  %.ph = phi i32 [ %38, %.lr.ph34 ], [ %ind.end52, %middle.block ]
  br label %scalar.ph

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

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

._crit_edge39:                                    ; preds = %._crit_edge, %.preheader27
  ret void

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv44 = phi i64 [ %indvars.iv.next45, %scalar.ph ], [ %indvars.iv44.ph, %scalar.ph.preheader ]
  %46 = phi i32 [ %48, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %47 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv44
  store i32 %42, ptr %47, align 4, !tbaa !5
  %indvars.iv.next45 = add nsw i64 %indvars.iv44, 1
  %48 = add nsw i32 %46, -1
  %.not = icmp eq i32 %46, 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 ]
  %49 = trunc nsw i64 %indvars.iv.next45.lcssa to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.lcssa = phi i32 [ %38, %.preheader ], [ -1, %._crit_edge.loopexit ]
  %.1.lcssa = phi i32 [ %.02237, %.preheader ], [ %49, %._crit_edge.loopexit ]
  store i32 %.lcssa, ptr %37, 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
}

; 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 = { 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 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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass