; LLVM IR from DionysiosB/CodeForces :: 0001-0099/49B-Sum.ll (Z12getSumLengthlll)

; 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 @_Z12getSumLengthlll(i64 noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 1, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %13, %3
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %13, label %25

13:                                               ; preds = %10
  %14 = load i64, ptr %8, align 8, !tbaa !5
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = srem i64 %15, 10
  %17 = mul nsw i64 %14, %16
  %18 = load i64, ptr %7, align 8, !tbaa !5
  %19 = add nsw i64 %18, %17
  store i64 %19, ptr %7, align 8, !tbaa !5
  %20 = load i64, ptr %6, align 8, !tbaa !5
  %21 = load i64, ptr %8, align 8, !tbaa !5
  %22 = mul nsw i64 %21, %20
  store i64 %22, ptr %8, align 8, !tbaa !5
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = sdiv i64 %23, 10
  store i64 %24, ptr %4, align 8, !tbaa !5
  br label %10, !llvm.loop !9

25:                                               ; preds = %10
  store i64 1, ptr %8, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %29, %25
  %27 = load i64, ptr %5, align 8, !tbaa !5
  %28 = icmp sgt i64 %27, 0
  br i1 %28, label %29, label %41

29:                                               ; preds = %26
  %30 = load i64, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = srem i64 %31, 10
  %33 = mul nsw i64 %30, %32
  %34 = load i64, ptr %7, align 8, !tbaa !5
  %35 = add nsw i64 %34, %33
  store i64 %35, ptr %7, align 8, !tbaa !5
  %36 = load i64, ptr %6, align 8, !tbaa !5
  %37 = load i64, ptr %8, align 8, !tbaa !5
  %38 = mul nsw i64 %37, %36
  store i64 %38, ptr %8, align 8, !tbaa !5
  %39 = load i64, ptr %5, align 8, !tbaa !5
  %40 = sdiv i64 %39, 10
  store i64 %40, ptr %5, align 8, !tbaa !5
  br label %26, !llvm.loop !11

41:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %45, %41
  %43 = load i64, ptr %7, align 8, !tbaa !5
  %44 = icmp sgt i64 %43, 0
  br i1 %44, label %45, label %51

45:                                               ; preds = %42
  %46 = load i64, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !5
  %48 = sdiv i64 %47, %46
  store i64 %48, ptr %7, align 8, !tbaa !5
  %49 = load i64, ptr %9, align 8, !tbaa !5
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %9, align 8, !tbaa !5
  br label %42, !llvm.loop !12

51:                                               ; preds = %42
  %52 = load i64, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %52
}

; 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

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 = !{!"long", !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}


; 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 @_Z12getSumLengthlll(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i64 %0, 0
  br i1 %4, label %.lr.ph, label %.preheader24

.preheader24:                                     ; preds = %.lr.ph, %3
  %.020.lcssa = phi i64 [ 0, %3 ], [ %8, %.lr.ph ]
  %5 = icmp sgt i64 %1, 0
  br i1 %5, label %.lr.ph31, label %.preheader

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.01927 = phi i64 [ %9, %.lr.ph ], [ 1, %3 ]
  %.02026 = phi i64 [ %8, %.lr.ph ], [ 0, %3 ]
  %.02225 = phi i64 [ %10, %.lr.ph ], [ %0, %3 ]
  %6 = urem i64 %.02225, 10
  %7 = mul nsw i64 %.01927, %6
  %8 = add nsw i64 %7, %.02026
  %9 = mul nsw i64 %.01927, %2
  %10 = udiv i64 %.02225, 10
  %.not = icmp ult i64 %.02225, 10
  br i1 %.not, label %.preheader24, label %.lr.ph, !llvm.loop !5

.preheader:                                       ; preds = %.lr.ph31, %.preheader24
  %.121.lcssa = phi i64 [ %.020.lcssa, %.preheader24 ], [ %14, %.lr.ph31 ]
  %11 = icmp sgt i64 %.121.lcssa, 0
  br i1 %11, label %.lr.ph35, label %._crit_edge

.lr.ph31:                                         ; preds = %.preheader24, %.lr.ph31
  %.130 = phi i64 [ %15, %.lr.ph31 ], [ 1, %.preheader24 ]
  %.12129 = phi i64 [ %14, %.lr.ph31 ], [ %.020.lcssa, %.preheader24 ]
  %.02328 = phi i64 [ %16, %.lr.ph31 ], [ %1, %.preheader24 ]
  %12 = urem i64 %.02328, 10
  %13 = mul nsw i64 %.130, %12
  %14 = add nsw i64 %13, %.12129
  %15 = mul nsw i64 %.130, %2
  %16 = udiv i64 %.02328, 10
  %.not37 = icmp ult i64 %.02328, 10
  br i1 %.not37, label %.preheader, label %.lr.ph31, !llvm.loop !7

.lr.ph35:                                         ; preds = %.preheader, %.lr.ph35
  %.034 = phi i64 [ %18, %.lr.ph35 ], [ 0, %.preheader ]
  %.233 = phi i64 [ %17, %.lr.ph35 ], [ %.121.lcssa, %.preheader ]
  %17 = sdiv i64 %.233, %2
  %18 = add nuw nsw i64 %.034, 1
  %19 = icmp sgt i64 %17, 0
  br i1 %19, label %.lr.ph35, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph35, %.preheader
  %.0.lcssa = phi i64 [ 0, %.preheader ], [ %18, %.lr.ph35 ]
  ret i64 %.0.lcssa
}

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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


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