; LLVM IR from manishbisht/Competitive-Programming :: Algorithms/Sorting/counting_sort.ll (Z9countSortPc)

; 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: mustprogress nounwind uwtable
define dso_local void @_Z9countSortPc(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca [256 x i32], align 16
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = call i64 @strlen(ptr noundef %7) #5
  %9 = call ptr @llvm.stacksave.p0()
  store ptr %9, ptr %3, align 8
  %10 = alloca i8, i64 %8, align 16
  store i64 %8, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 1024, ptr %5) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %11 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 0, i64 1024, i1 false)
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %29, %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %13, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %19, label %32

19:                                               ; preds = %12
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = sext i8 %24 to i64
  %26 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %26, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %19
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %6, align 4, !tbaa !9
  br label %12, !llvm.loop !12

32:                                               ; preds = %12
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %47, %32
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = icmp sle i32 %34, 255
  br i1 %35, label %36, label %50

36:                                               ; preds = %33
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = add nsw i32 %45, %41
  store i32 %46, ptr %44, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %36
  %48 = load i32, ptr %6, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %6, align 4, !tbaa !9
  br label %33, !llvm.loop !14

50:                                               ; preds = %33
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %84, %50
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %52, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = icmp ne i8 %56, 0
  br i1 %57, label %58, label %87

58:                                               ; preds = %51
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = load i32, ptr %6, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %59, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !11
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %64, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !11
  %69 = sext i8 %68 to i64
  %70 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !9
  %72 = sub nsw i32 %71, 1
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i8, ptr %10, i64 %73
  store i8 %63, ptr %74, align 1, !tbaa !11
  %75 = load ptr, ptr %2, align 8, !tbaa !5
  %76 = load i32, ptr %6, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %75, i64 %77
  %79 = load i8, ptr %78, align 1, !tbaa !11
  %80 = sext i8 %79 to i64
  %81 = getelementptr inbounds [256 x i32], ptr %5, i64 0, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !9
  %83 = add nsw i32 %82, -1
  store i32 %83, ptr %81, align 4, !tbaa !9
  br label %84

84:                                               ; preds = %58
  %85 = load i32, ptr %6, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !9
  br label %51, !llvm.loop !15

87:                                               ; preds = %51
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %88

88:                                               ; preds = %104, %87
  %89 = load ptr, ptr %2, align 8, !tbaa !5
  %90 = load i32, ptr %6, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i8, ptr %89, i64 %91
  %93 = load i8, ptr %92, align 1, !tbaa !11
  %94 = icmp ne i8 %93, 0
  br i1 %94, label %95, label %107

95:                                               ; preds = %88
  %96 = load i32, ptr %6, align 4, !tbaa !9
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds i8, ptr %10, i64 %97
  %99 = load i8, ptr %98, align 1, !tbaa !11
  %100 = load ptr, ptr %2, align 8, !tbaa !5
  %101 = load i32, ptr %6, align 4, !tbaa !9
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds i8, ptr %100, i64 %102
  store i8 %99, ptr %103, align 1, !tbaa !11
  br label %104

104:                                              ; preds = %95
  %105 = load i32, ptr %6, align 4, !tbaa !9
  %106 = add nsw i32 %105, 1
  store i32 %106, ptr %6, align 4, !tbaa !9
  br label %88, !llvm.loop !16

107:                                              ; preds = %88
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 1024, ptr %5) #6
  %108 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %108)
  ret void
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nounwind willreturn memory(read) "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 nosync nounwind willreturn }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !13}


; 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 nofree nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z9countSortPc(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = alloca [256 x i32], align 16
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %4 = alloca i8, i64 %3, align 16
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1024) %2, i8 0, i64 1024, i1 false)
  %5 = load i8, ptr %0, align 1, !tbaa !5
  %.not31 = icmp eq i8 %5, 0
  br i1 %.not31, label %.preheader30.preheader, label %.lr.ph

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

.lr.ph:                                           ; preds = %1, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %1 ]
  %6 = phi i8 [ %12, %.lr.ph ], [ %5, %1 ]
  %7 = sext i8 %6 to i64
  %8 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !8
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %8, align 4, !tbaa !8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %.preheader30.preheader, label %.lr.ph, !llvm.loop !10

.preheader29:                                     ; preds = %.preheader30
  %invariant.gep = getelementptr i8, ptr %4, i64 -1
  br i1 %.not31, label %._crit_edge, label %.lr.ph36

.preheader30:                                     ; preds = %.preheader30, %.preheader30.preheader
  %13 = phi i32 [ 0, %.preheader30.preheader ], [ %28, %.preheader30 ]
  %indvars.iv40 = phi i64 [ 1, %.preheader30.preheader ], [ %indvars.iv.next41.4, %.preheader30 ]
  %14 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv40
  %15 = load i32, ptr %14, align 4, !tbaa !8
  %16 = add nsw i32 %15, %13
  store i32 %16, ptr %14, align 4, !tbaa !8
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %17 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv.next41
  %18 = load i32, ptr %17, align 4, !tbaa !8
  %19 = add nsw i32 %18, %16
  store i32 %19, ptr %17, align 4, !tbaa !8
  %indvars.iv.next41.1 = add nuw nsw i64 %indvars.iv40, 2
  %20 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv.next41.1
  %21 = load i32, ptr %20, align 4, !tbaa !8
  %22 = add nsw i32 %21, %19
  store i32 %22, ptr %20, align 4, !tbaa !8
  %indvars.iv.next41.2 = add nuw nsw i64 %indvars.iv40, 3
  %23 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv.next41.2
  %24 = load i32, ptr %23, align 4, !tbaa !8
  %25 = add nsw i32 %24, %22
  store i32 %25, ptr %23, align 4, !tbaa !8
  %indvars.iv.next41.3 = add nuw nsw i64 %indvars.iv40, 4
  %26 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %indvars.iv.next41.3
  %27 = load i32, ptr %26, align 4, !tbaa !8
  %28 = add nsw i32 %27, %25
  store i32 %28, ptr %26, align 4, !tbaa !8
  %indvars.iv.next41.4 = add nuw nsw i64 %indvars.iv40, 5
  %exitcond.not.4 = icmp eq i64 %indvars.iv.next41.4, 256
  br i1 %exitcond.not.4, label %.preheader29, label %.preheader30, !llvm.loop !12

.preheader:                                       ; preds = %.lr.ph36
  br i1 %.not31, label %._crit_edge, label %.lr.ph39

.lr.ph36:                                         ; preds = %.preheader29, %.lr.ph36
  %indvars.iv43 = phi i64 [ %indvars.iv.next44, %.lr.ph36 ], [ 0, %.preheader29 ]
  %29 = phi i8 [ %36, %.lr.ph36 ], [ %5, %.preheader29 ]
  %30 = sext i8 %29 to i64
  %31 = getelementptr inbounds [256 x i32], ptr %2, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !8
  %33 = sext i32 %32 to i64
  %gep = getelementptr i8, ptr %invariant.gep, i64 %33
  store i8 %29, ptr %gep, align 1, !tbaa !5
  %34 = add nsw i32 %32, -1
  store i32 %34, ptr %31, align 4, !tbaa !8
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %35 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next44
  %36 = load i8, ptr %35, align 1, !tbaa !5
  %.not27 = icmp eq i8 %36, 0
  br i1 %.not27, label %.preheader, label %.lr.ph36, !llvm.loop !13

.lr.ph39:                                         ; preds = %.preheader, %.lr.ph39
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %.lr.ph39 ], [ 0, %.preheader ]
  %37 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv45
  %38 = getelementptr inbounds i8, ptr %4, i64 %indvars.iv45
  %39 = load i8, ptr %38, align 1, !tbaa !5
  store i8 %39, ptr %37, align 1, !tbaa !5
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %40 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next46
  %41 = load i8, ptr %40, align 1, !tbaa !5
  %.not28 = icmp eq i8 %41, 0
  br i1 %.not28, label %._crit_edge, label %.lr.ph39, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph39, %.preheader29, %.preheader
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #5
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

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

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

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

attributes #0 = { mustprogress nofree 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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind willreturn memory(read) }
attributes #5 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !11}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass