; LLVM IR from Dev-XYS/Algorithms :: Bucket-Sort.ll (Z11bucket_sortv)

; 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.node = type { i32, i32 }

@n = external dso_local global i32, align 4
@d = external dso_local global [100000 x i32], align 16
@A = external dso_local global [100001 x %struct.node], align 16
@head = external dso_local global [32768 x i32], align 16
@cnt = external dso_local global [32768 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z6inserti(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 @_Z11bucket_sortv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %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
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %19, %0
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %22

14:                                               ; preds = %9
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  call void @_Z6inserti(i32 noundef %18)
  br label %19

19:                                               ; preds = %14
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %1, align 4, !tbaa !5
  br label %9, !llvm.loop !9

22:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %81, %22
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = icmp slt i32 %24, 32768
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %84

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %77, %27
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [32768 x i32], ptr @cnt, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp slt i32 %29, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %28
  store i32 8, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %80

36:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 2147483647, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [32768 x i32], ptr @head, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  store i32 %40, ptr %8, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %61, %36
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %45, label %44

44:                                               ; preds = %41
  store i32 11, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %67

45:                                               ; preds = %41
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.node, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !11
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %53, label %60

53:                                               ; preds = %45
  %54 = load i32, ptr %8, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.node, ptr %56, i32 0, i32 0
  %58 = load i32, ptr %57, align 8, !tbaa !11
  store i32 %58, ptr %6, align 4, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %59, ptr %7, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %53, %45
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %63
  %65 = getelementptr inbounds %struct.node, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !13
  store i32 %66, ptr %8, align 4, !tbaa !5
  br label %41, !llvm.loop !14

67:                                               ; preds = %44
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %2, align 4, !tbaa !5
  %71 = sext i32 %69 to i64
  %72 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %71
  store i32 %68, ptr %72, align 4, !tbaa !5
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %74
  %76 = getelementptr inbounds %struct.node, ptr %75, i32 0, i32 0
  store i32 2147483647, ptr %76, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %77

77:                                               ; preds = %67
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %5, align 4, !tbaa !5
  br label %28, !llvm.loop !15

80:                                               ; preds = %35
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %3, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %3, align 4, !tbaa !5
  br label %23, !llvm.loop !16

84:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  ret void
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS4node", !6, i64 0, !6, i64 4}
!13 = !{!12, !6, i64 4}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !10}


; 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"

%struct.node = type { i32, i32 }

@n = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [100000 x i32], align 16
@A = external dso_local local_unnamed_addr global [100001 x %struct.node], align 16
@head = external dso_local local_unnamed_addr global [32768 x i32], align 16
@cnt = external dso_local local_unnamed_addr global [32768 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z6inserti(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z11bucket_sortv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %.preheader.preheader

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %3 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv
  %4 = load i32, ptr %3, align 4, !tbaa !5
  tail call void @_Z6inserti(i32 noundef %4)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = icmp slt i64 %indvars.iv.next, %6
  br i1 %7, label %.lr.ph, label %.preheader.preheader, !llvm.loop !9

.preheader.preheader:                             ; preds = %.lr.ph, %0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge41
  %indvars.iv57 = phi i64 [ %indvars.iv.next58, %._crit_edge41 ], [ 0, %.preheader.preheader ]
  %.01847 = phi i32 [ %.1.lcssa, %._crit_edge41 ], [ undef, %.preheader.preheader ]
  %.02345 = phi i32 [ %.124.lcssa, %._crit_edge41 ], [ 0, %.preheader.preheader ]
  %8 = getelementptr inbounds [32768 x i32], ptr @cnt, i64 0, i64 %indvars.iv57
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph40, label %._crit_edge41

.lr.ph40:                                         ; preds = %.preheader
  %11 = getelementptr inbounds [32768 x i32], ptr @head, i64 0, i64 %indvars.iv57
  %.030 = load i32, ptr %11, align 4, !tbaa !5
  %.not31 = icmp eq i32 %.030, 0
  br i1 %.not31, label %.lr.ph40.split.us, label %.lr.ph35.preheader

.lr.ph35.preheader:                               ; preds = %.lr.ph40
  %12 = sext i32 %.02345 to i64
  br label %.lr.ph35

.lr.ph40.split.us:                                ; preds = %.lr.ph40
  %13 = sext i32 %.01847 to i64
  %14 = sext i32 %.02345 to i64
  %15 = zext nneg i32 %9 to i64
  %min.iters.check = icmp ult i32 %9, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph40.split.us
  %n.vec = and i64 %15, 2147483640
  %ind.end = add nsw i64 %n.vec, %14
  %ind.end61 = trunc nuw nsw i64 %n.vec to i32
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = add i64 %index, %14
  %16 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %offset.idx
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  store <4 x i32> <i32 2147483647, i32 2147483647, i32 2147483647, i32 2147483647>, ptr %16, align 4, !tbaa !5
  store <4 x i32> <i32 2147483647, i32 2147483647, i32 2147483647, i32 2147483647>, 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, %15
  br i1 %cmp.n, label %._crit_edge41.split.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph40.split.us
  %indvars.iv53.ph = phi i64 [ %14, %.lr.ph40.split.us ], [ %ind.end, %middle.block ]
  %.02138.us.ph = phi i32 [ 0, %.lr.ph40.split.us ], [ %ind.end61, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv53 = phi i64 [ %indvars.iv.next54, %scalar.ph ], [ %indvars.iv53.ph, %scalar.ph.preheader ]
  %.02138.us = phi i32 [ %20, %scalar.ph ], [ %.02138.us.ph, %scalar.ph.preheader ]
  %indvars.iv.next54 = add nsw i64 %indvars.iv53, 1
  %19 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv53
  store i32 2147483647, ptr %19, align 4, !tbaa !5
  %20 = add nuw nsw i32 %.02138.us, 1
  %exitcond56.not = icmp eq i32 %20, %9
  br i1 %exitcond56.not, label %._crit_edge41.split.us, label %scalar.ph, !llvm.loop !14

._crit_edge41.split.us:                           ; preds = %scalar.ph, %middle.block
  %indvars.iv.next54.lcssa = phi i64 [ %ind.end, %middle.block ], [ %indvars.iv.next54, %scalar.ph ]
  %21 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %13
  %22 = trunc nsw i64 %indvars.iv.next54.lcssa to i32
  store i32 2147483647, ptr %21, align 8, !tbaa !15
  br label %._crit_edge41

23:                                               ; preds = %._crit_edge41
  ret void

._crit_edge41.loopexit:                           ; preds = %._crit_edge
  %24 = trunc nsw i64 %indvars.iv.next51 to i32
  br label %._crit_edge41

._crit_edge41:                                    ; preds = %._crit_edge41.loopexit, %._crit_edge41.split.us, %.preheader
  %.124.lcssa = phi i32 [ %.02345, %.preheader ], [ %22, %._crit_edge41.split.us ], [ %24, %._crit_edge41.loopexit ]
  %.1.lcssa = phi i32 [ %.01847, %.preheader ], [ %.01847, %._crit_edge41.split.us ], [ %spec.select27, %._crit_edge41.loopexit ]
  %indvars.iv.next58 = add nuw nsw i64 %indvars.iv57, 1
  %exitcond60.not = icmp eq i64 %indvars.iv.next58, 32768
  br i1 %exitcond60.not, label %23, label %.preheader, !llvm.loop !17

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %._crit_edge
  %indvars.iv50 = phi i64 [ %12, %.lr.ph35.preheader ], [ %indvars.iv.next51, %._crit_edge ]
  %.139 = phi i32 [ %.01847, %.lr.ph35.preheader ], [ %spec.select27, %._crit_edge ]
  %.02138 = phi i32 [ 0, %.lr.ph35.preheader ], [ %28, %._crit_edge ]
  br label %29

._crit_edge:                                      ; preds = %29
  %indvars.iv.next51 = add nsw i64 %indvars.iv50, 1
  %25 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv50
  store i32 %spec.select, ptr %25, align 4, !tbaa !5
  %26 = sext i32 %spec.select27 to i64
  %27 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %26
  store i32 2147483647, ptr %27, align 8, !tbaa !15
  %28 = add nuw nsw i32 %.02138, 1
  %exitcond.not = icmp eq i32 %28, %9
  br i1 %exitcond.not, label %._crit_edge41.loopexit, label %.lr.ph35, !llvm.loop !18

29:                                               ; preds = %.lr.ph35, %29
  %.034 = phi i32 [ %.030, %.lr.ph35 ], [ %.0, %29 ]
  %.233 = phi i32 [ %.139, %.lr.ph35 ], [ %spec.select27, %29 ]
  %.01932 = phi i32 [ 2147483647, %.lr.ph35 ], [ %spec.select, %29 ]
  %30 = sext i32 %.034 to i64
  %31 = getelementptr inbounds [100001 x %struct.node], ptr @A, i64 0, i64 %30
  %32 = load i32, ptr %31, align 8, !tbaa !15
  %33 = icmp slt i32 %32, %.01932
  %spec.select = tail call i32 @llvm.smin.i32(i32 %32, i32 %.01932)
  %spec.select27 = select i1 %33, i32 %.034, i32 %.233
  %34 = getelementptr inbounds i8, ptr %31, i64 4
  %.0 = load i32, ptr %34, align 4, !tbaa !5
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %._crit_edge, label %29, !llvm.loop !19
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #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 = { 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}
!15 = !{!16, !6, i64 0}
!16 = !{!"_ZTS4node", !6, i64 0, !6, i64 4}
!17 = distinct !{!17, !10}
!18 = distinct !{!18, !10}
!19 = distinct !{!19, !10}


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