; LLVM IR from UtkarshPathrabe/Competitive-Coding :: HackerRank Solutions/C/Arrays and Strings (Digit Frequency)

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

@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [1001 x i8], align 16
  %3 = alloca [10 x i32], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 1001, ptr %2) #5
  %6 = getelementptr inbounds [1001 x i8], ptr %2, i64 0, i64 0
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 40, ptr %3) #5
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %41, %0
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [1001 x i8], ptr %2, i64 0, i64 0
  %12 = call i64 @strlen(ptr noundef %11) #6
  %13 = icmp ult i64 %10, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %44

15:                                               ; preds = %8
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [1001 x i8], ptr %2, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = sext i8 %19 to i32
  %21 = icmp sge i32 %20, 48
  br i1 %21, label %22, label %40

22:                                               ; preds = %15
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [1001 x i8], ptr %2, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp sle i32 %27, 57
  br i1 %28, label %29, label %40

29:                                               ; preds = %22
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1001 x i8], ptr %2, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = sub nsw i32 %34, 48
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %37, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %29, %22, %15
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %4, align 4, !tbaa !5
  br label %8, !llvm.loop !10

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

45:                                               ; preds = %55, %44
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = icmp slt i32 %46, 10
  br i1 %47, label %49, label %48

48:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %58

49:                                               ; preds = %45
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10 x i32], ptr %3, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %53)
  br label %55

55:                                               ; preds = %49
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %5, align 4, !tbaa !5
  br label %45, !llvm.loop !12

58:                                               ; preds = %48
  %59 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.end.p0(i64 40, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 1001, ptr %2) #5
  ret i32 0
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

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

declare i32 @printf(ptr noundef, ...) #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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca [1001 x i8], align 16
  %2 = alloca [10 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 1001, ptr nonnull %1) #5
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %2, i8 0, i64 40, i1 false)
  %4 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #6
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %xtraiter = and i64 %4, 1
  %5 = icmp eq i64 %4, 1
  br i1 %5, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, -2
  br label %.lr.ph

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

.lr.ph.epil:                                      ; preds = %.preheader.loopexit.unr-lcssa
  %6 = getelementptr inbounds [1001 x i8], ptr %1, i64 0, i64 %indvars.iv.unr
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = add i8 %7, -48
  %or.cond.epil = icmp ult i8 %8, 10
  br i1 %or.cond.epil, label %9, label %.preheader.loopexit

9:                                                ; preds = %.lr.ph.epil
  %10 = zext nneg i8 %8 to i64
  %11 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !8
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %11, align 4, !tbaa !8
  br label %.preheader.loopexit

.preheader.loopexit:                              ; preds = %.lr.ph.epil, %9, %.preheader.loopexit.unr-lcssa
  %.pre = load i32, ptr %2, align 16, !tbaa !8
  %.phi.trans.insert = getelementptr inbounds i8, ptr %2, i64 4
  %.pre18 = load i32, ptr %.phi.trans.insert, align 4, !tbaa !8
  %.phi.trans.insert19 = getelementptr inbounds i8, ptr %2, i64 8
  %.pre20 = load i32, ptr %.phi.trans.insert19, align 8, !tbaa !8
  %.phi.trans.insert21 = getelementptr inbounds i8, ptr %2, i64 12
  %.pre22 = load i32, ptr %.phi.trans.insert21, align 4, !tbaa !8
  %.phi.trans.insert23 = getelementptr inbounds i8, ptr %2, i64 16
  %.pre24 = load i32, ptr %.phi.trans.insert23, align 16, !tbaa !8
  %.phi.trans.insert25 = getelementptr inbounds i8, ptr %2, i64 20
  %.pre26 = load i32, ptr %.phi.trans.insert25, align 4, !tbaa !8
  %.phi.trans.insert27 = getelementptr inbounds i8, ptr %2, i64 24
  %.pre28 = load i32, ptr %.phi.trans.insert27, align 8, !tbaa !8
  %.phi.trans.insert29 = getelementptr inbounds i8, ptr %2, i64 28
  %.pre30 = load i32, ptr %.phi.trans.insert29, align 4, !tbaa !8
  %.phi.trans.insert31 = getelementptr inbounds i8, ptr %2, i64 32
  %.pre32 = load i32, ptr %.phi.trans.insert31, align 16, !tbaa !8
  %.phi.trans.insert33 = getelementptr inbounds i8, ptr %2, i64 36
  %.pre34 = load i32, ptr %.phi.trans.insert33, align 4, !tbaa !8
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %0
  %14 = phi i32 [ %.pre34, %.preheader.loopexit ], [ 0, %0 ]
  %15 = phi i32 [ %.pre32, %.preheader.loopexit ], [ 0, %0 ]
  %16 = phi i32 [ %.pre30, %.preheader.loopexit ], [ 0, %0 ]
  %17 = phi i32 [ %.pre28, %.preheader.loopexit ], [ 0, %0 ]
  %18 = phi i32 [ %.pre26, %.preheader.loopexit ], [ 0, %0 ]
  %19 = phi i32 [ %.pre24, %.preheader.loopexit ], [ 0, %0 ]
  %20 = phi i32 [ %.pre22, %.preheader.loopexit ], [ 0, %0 ]
  %21 = phi i32 [ %.pre20, %.preheader.loopexit ], [ 0, %0 ]
  %22 = phi i32 [ %.pre18, %.preheader.loopexit ], [ 0, %0 ]
  %23 = phi i32 [ %.pre, %.preheader.loopexit ], [ 0, %0 ]
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %23)
  %25 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %22)
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %21)
  %27 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %20)
  %28 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %19)
  %29 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %18)
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %17)
  %31 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %16)
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %15)
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %14)
  %34 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 1001, ptr nonnull %1) #5
  ret i32 0

.lr.ph:                                           ; preds = %51, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %51 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %51 ]
  %35 = getelementptr inbounds [1001 x i8], ptr %1, i64 0, i64 %indvars.iv
  %36 = load i8, ptr %35, align 2, !tbaa !5
  %37 = add i8 %36, -48
  %or.cond = icmp ult i8 %37, 10
  br i1 %or.cond, label %38, label %.lr.ph.1

38:                                               ; preds = %.lr.ph
  %39 = zext nneg i8 %37 to i64
  %40 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !8
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %40, align 4, !tbaa !8
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %38
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %43 = getelementptr inbounds [1001 x i8], ptr %1, i64 0, i64 %indvars.iv.next
  %44 = load i8, ptr %43, align 1, !tbaa !5
  %45 = add i8 %44, -48
  %or.cond.1 = icmp ult i8 %45, 10
  br i1 %or.cond.1, label %46, label %51

46:                                               ; preds = %.lr.ph.1
  %47 = zext nneg i8 %45 to i64
  %48 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !8
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %48, align 4, !tbaa !8
  br label %51

51:                                               ; preds = %46, %.lr.ph.1
  %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 %.preheader.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10
}

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

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

attributes #0 = { nofree 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass