; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2018-19/Open 2019/Gold/walk (12:15).ll (Z11bucket_sorti)

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

@ed = external dso_local global [28128750 x i32], align 16
@tmp = external dso_local global [28128750 x i32], align 16
@cnt = external dso_local global [50000 x i32], align 16
@st = external dso_local global [50000 x i32], align 16
@co = external dso_local global i32, align 4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6getValii(i32 noundef, i32 noundef) #1

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

8:                                                ; preds = %16, %1
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 50000
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %8
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %14
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %12
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %3, align 4, !tbaa !5
  br label %8, !llvm.loop !9

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

20:                                               ; preds = %36, %19
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = load i32, ptr @co, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = call noundef i32 @_Z6getValii(i32 noundef %29, i32 noundef %30)
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %33, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %25
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %4, align 4, !tbaa !5
  br label %20, !llvm.loop !11

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

40:                                               ; preds = %63, %39
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = icmp slt i32 %41, 50000
  br i1 %42, label %44, label %43

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

44:                                               ; preds = %40
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %62

47:                                               ; preds = %44
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = add nsw i32 %52, %57
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %60
  store i32 %58, ptr %61, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %47, %44
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %5, align 4, !tbaa !5
  br label %40, !llvm.loop !12

66:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %89, %66
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = load i32, ptr @co, align 4, !tbaa !5
  %70 = icmp slt i32 %68, %69
  br i1 %70, label %72, label %71

71:                                               ; preds = %67
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %92

72:                                               ; preds = %67
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !5
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = load i32, ptr %2, align 4, !tbaa !5
  %82 = call noundef i32 @_Z6getValii(i32 noundef %80, i32 noundef %81)
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %83
  %85 = load i32, ptr %84, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %84, align 4, !tbaa !5
  %87 = sext i32 %85 to i64
  %88 = getelementptr inbounds [28128750 x i32], ptr @tmp, i64 0, i64 %87
  store i32 %76, ptr %88, align 4, !tbaa !5
  br label %89

89:                                               ; preds = %72
  %90 = load i32, ptr %6, align 4, !tbaa !5
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %6, align 4, !tbaa !5
  br label %67, !llvm.loop !13

92:                                               ; preds = %71
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %93

93:                                               ; preds = %105, %92
  %94 = load i32, ptr %7, align 4, !tbaa !5
  %95 = load i32, ptr @co, align 4, !tbaa !5
  %96 = icmp slt i32 %94, %95
  br i1 %96, label %98, label %97

97:                                               ; preds = %93
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %108

98:                                               ; preds = %93
  %99 = load i32, ptr %7, align 4, !tbaa !5
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %100
  %102 = load i32, ptr %7, align 4, !tbaa !5
  %103 = sext i32 %102 to i64
  %104 = getelementptr inbounds [28128750 x i32], ptr @tmp, i64 0, i64 %103
  call void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4) %101, ptr noundef nonnull align 4 dereferenceable(4) %104) #3
  br label %105

105:                                              ; preds = %98
  %106 = load i32, ptr %7, align 4, !tbaa !5
  %107 = add nsw i32 %106, 1
  store i32 %107, ptr %7, align 4, !tbaa !5
  br label %93, !llvm.loop !14

108:                                              ; preds = %97
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #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 = { inlinehint 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 #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 = !{!"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 = distinct !{!12, !10}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !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"

@ed = external dso_local global [28128750 x i32], align 16
@tmp = external dso_local global [28128750 x i32], align 16
@cnt = external dso_local local_unnamed_addr global [50000 x i32], align 16
@st = external dso_local local_unnamed_addr global [50000 x i32], align 16
@co = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z11bucket_sorti(i32 noundef %0) local_unnamed_addr #0 {
.preheader28:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(200000) @cnt, i8 0, i64 200000, i1 false), !tbaa !5
  %1 = load i32, ptr @co, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %.peel.next

.preheader27.loopexit:                            ; preds = %.lr.ph
  %3 = icmp sgt i32 %12, 0
  br label %.peel.next

.peel.next:                                       ; preds = %.preheader27.loopexit, %.preheader28
  %4 = phi i1 [ %3, %.preheader27.loopexit ], [ false, %.preheader28 ]
  %load_initial = load i32, ptr @st, align 16
  br label %15

.lr.ph:                                           ; preds = %.preheader28, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader28 ]
  %5 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = tail call noundef i32 @_Z6getValii(i32 noundef %6, i32 noundef %0)
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr %9, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = load i32, ptr @co, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = icmp slt i64 %indvars.iv.next, %13
  br i1 %14, label %.lr.ph, label %.preheader27.loopexit, !llvm.loop !9

.preheader26:                                     ; preds = %15
  br i1 %4, label %.lr.ph33, label %._crit_edge

15:                                               ; preds = %30, %.peel.next
  %store_forwarded = phi i32 [ %load_initial, %.peel.next ], [ %34, %30 ]
  %indvars.iv38 = phi i64 [ 1, %.peel.next ], [ %indvars.iv.next39.3, %30 ]
  %16 = add nsw i64 %indvars.iv38, -1
  %17 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = add nsw i32 %18, %store_forwarded
  %20 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %indvars.iv38
  store i32 %19, ptr %20, align 4, !tbaa !5
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %21 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %indvars.iv38
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, %19
  %24 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %indvars.iv.next39
  store i32 %23, ptr %24, align 4, !tbaa !5
  %indvars.iv.next39.1 = add nuw nsw i64 %indvars.iv38, 2
  %25 = add nuw nsw i64 %indvars.iv38, 1
  %26 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, %23
  %29 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %indvars.iv.next39.1
  store i32 %28, ptr %29, align 4, !tbaa !5
  %indvars.iv.next39.2 = add nuw nsw i64 %indvars.iv38, 3
  %exitcond.not.2 = icmp eq i64 %indvars.iv.next39.2, 50000
  br i1 %exitcond.not.2, label %.preheader26, label %30, !llvm.loop !11

30:                                               ; preds = %15
  %31 = add nuw nsw i64 %indvars.iv38, 2
  %32 = getelementptr inbounds [50000 x i32], ptr @cnt, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %33, %28
  %35 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %indvars.iv.next39.2
  store i32 %34, ptr %35, align 4, !tbaa !5
  %indvars.iv.next39.3 = add nuw nsw i64 %indvars.iv38, 4
  br label %15

.preheader:                                       ; preds = %.lr.ph33
  %36 = icmp sgt i32 %46, 0
  br i1 %36, label %.lr.ph35, label %._crit_edge

.lr.ph33:                                         ; preds = %.preheader26, %.lr.ph33
  %indvars.iv42 = phi i64 [ %indvars.iv.next43, %.lr.ph33 ], [ 0, %.preheader26 ]
  %37 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %indvars.iv42
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = tail call noundef i32 @_Z6getValii(i32 noundef %38, i32 noundef %0)
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [50000 x i32], ptr @st, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %41, align 4, !tbaa !5
  %44 = sext i32 %42 to i64
  %45 = getelementptr inbounds [28128750 x i32], ptr @tmp, i64 0, i64 %44
  store i32 %38, ptr %45, align 4, !tbaa !5
  %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1
  %46 = load i32, ptr @co, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = icmp slt i64 %indvars.iv.next43, %47
  br i1 %48, label %.lr.ph33, label %.preheader, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader26, %.preheader
  ret void

.lr.ph35:                                         ; preds = %.preheader, %.lr.ph35
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %.lr.ph35 ], [ 0, %.preheader ]
  %49 = getelementptr inbounds [28128750 x i32], ptr @ed, i64 0, i64 %indvars.iv45
  %50 = getelementptr inbounds [28128750 x i32], ptr @tmp, i64 0, i64 %indvars.iv45
  tail call void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4) %49, ptr noundef nonnull align 4 dereferenceable(4) %50) #3
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %51 = load i32, ptr @co, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = icmp slt i64 %indvars.iv.next46, %52
  br i1 %53, label %.lr.ph35, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_ZSt4swapIiENSt9enable_ifIXsr6__and_ISt6__not_ISt15__is_tuple_likeIT_EESt21is_move_constructibleIS3_ESt18is_move_assignableIS3_EEE5valueEvE4typeERS3_SC_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { inlinehint 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 nounwind willreturn memory(argmem: write) }
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 = !{!"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}
!12 = !{!"llvm.loop.peeled.count", i32 1}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopFullUnrollPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass