; LLVM IR from ravireddy07/cpp-book :: src/Problems-on-Strings/CountNumBinaryString.ll (Z12countStringsx)

; 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 noundef i64 @_Z12countStringsx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = call ptr @llvm.stacksave.p0()
  store ptr %8, ptr %3, align 8
  %9 = alloca i64, i64 %7, align 16
  store i64 %7, ptr %4, align 8
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = alloca i64, i64 %10, align 16
  store i64 %10, ptr %5, align 8
  %12 = getelementptr inbounds i64, ptr %11, i64 0
  store i64 1, ptr %12, align 16, !tbaa !5
  %13 = getelementptr inbounds i64, ptr %9, i64 0
  store i64 1, ptr %13, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %43, %1
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = icmp slt i64 %16, %17
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %14
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i64, ptr %9, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = sub nsw i32 %26, 1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i64, ptr %11, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = add nsw i64 %25, %30
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i64, ptr %9, i64 %33
  store i64 %31, ptr %34, align 8, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i64, ptr %9, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i64, ptr %11, i64 %41
  store i64 %39, ptr %42, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %20
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !9
  br label %14, !llvm.loop !11

46:                                               ; preds = %19
  %47 = load i64, ptr %2, align 8, !tbaa !5
  %48 = sub nsw i64 %47, 1
  %49 = getelementptr inbounds i64, ptr %9, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = load i64, ptr %2, align 8, !tbaa !5
  %52 = sub nsw i64 %51, 1
  %53 = getelementptr inbounds i64, ptr %11, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !5
  %55 = add nsw i64 %50, %54
  %56 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %56)
  ret i64 %55
}

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 willreturn }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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 norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z12countStringsx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, i64 %0, align 16
  %3 = alloca i64, i64 %0, align 16
  store i64 1, ptr %3, align 16, !tbaa !5
  store i64 1, ptr %2, align 16, !tbaa !5
  %4 = icmp sgt i64 %0, 1
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %5 = add nsw i64 %0, -1
  %6 = add nsw i64 %0, -2
  %xtraiter = and i64 %5, 3
  %7 = icmp ult i64 %6, 3
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

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

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %8 = phi i64 [ %9, %.lr.ph.epil ], [ %.unr, %._crit_edge.loopexit.unr-lcssa ]
  %9 = phi i64 [ %10, %.lr.ph.epil ], [ %.unr16, %._crit_edge.loopexit.unr-lcssa ]
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %10 = add nsw i64 %8, %9
  %11 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv.epil
  store i64 %10, ptr %11, align 8, !tbaa !5
  %12 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.epil
  store i64 %9, ptr %12, align 8, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %1
  %13 = add nsw i64 %0, -1
  %14 = getelementptr inbounds i64, ptr %2, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds i64, ptr %3, i64 %13
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add nsw i64 %17, %15
  ret i64 %18

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %19 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %27, %.lr.ph ]
  %20 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %30, %.lr.ph ]
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %21 = add nsw i64 %19, %20
  %22 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv
  store i64 %21, ptr %22, align 8, !tbaa !5
  %23 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv
  store i64 %20, ptr %23, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = add nsw i64 %20, %21
  %25 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv.next
  store i64 %24, ptr %25, align 8, !tbaa !5
  %26 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.next
  store i64 %21, ptr %26, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %27 = add nsw i64 %21, %24
  %28 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv.next.1
  store i64 %27, ptr %28, align 8, !tbaa !5
  %29 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.next.1
  store i64 %24, ptr %29, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %30 = add nsw i64 %24, %27
  %31 = getelementptr inbounds i64, ptr %2, i64 %indvars.iv.next.2
  store i64 %30, ptr %31, align 8, !tbaa !5
  %32 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.next.2
  store i64 %27, ptr %32, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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" }

!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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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