; LLVM IR from Izaron/ACM-ICPC :: Timus Online Judge/Volume 7/1658. Sum of Digits.ll (Z2reii)

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

@calced = external dso_local global [905 x [8105 x i32]], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1

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

; 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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z2reii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  br label %64

15:                                               ; preds = %11, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [905 x [8105 x i32]], ptr @calced, i64 0, i64 %17
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [8105 x i32], ptr %18, i64 0, i64 %20
  store ptr %21, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %56, %15
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = icmp slt i32 %23, 10
  br i1 %24, label %26, label %25

25:                                               ; preds = %22
  store i32 2, ptr %7, align 4
  br label %59

26:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = sub nsw i32 %27, %28
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = mul nsw i32 %31, %32
  %34 = sub nsw i32 %30, %33
  %35 = call noundef i32 @_Z2dpii(i32 noundef %29, i32 noundef %34)
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load ptr, ptr %5, align 8, !tbaa !9
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  %40 = icmp eq i32 %37, %39
  br i1 %40, label %41, label %52

41:                                               ; preds = %26
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %42)
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = sub nsw i32 %44, %45
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = mul nsw i32 %48, %49
  %51 = sub nsw i32 %47, %50
  call void @_Z2reii(i32 noundef %46, i32 noundef %51)
  store i32 1, ptr %7, align 4
  br label %53

52:                                               ; preds = %26
  store i32 0, ptr %7, align 4
  br label %53

53:                                               ; preds = %52, %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %54 = load i32, ptr %7, align 4
  switch i32 %54, label %59 [
    i32 0, label %55
  ]

55:                                               ; preds = %53
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %6, align 4, !tbaa !5
  br label %22, !llvm.loop !11

59:                                               ; preds = %53, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %60 = load i32, ptr %7, align 4
  switch i32 %60, label %62 [
    i32 2, label %61
  ]

61:                                               ; preds = %59
  store i32 0, ptr %7, align 4
  br label %62

62:                                               ; preds = %61, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %63 = load i32, ptr %7, align 4
  switch i32 %63, label %65 [
    i32 0, label %64
    i32 1, label %64
  ]

64:                                               ; preds = %62, %62, %14
  ret void

65:                                               ; preds = %62
  unreachable
}

declare i32 @printf(ptr noundef, ...) #2

attributes #0 = { mustprogress 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 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

@calced = external dso_local local_unnamed_addr global [905 x [8105 x i32]], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z2reii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = or i32 %1, %0
  %or.cond28 = icmp eq i32 %3, 0
  br i1 %or.cond28, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2, %tailrecurse
  %.tr2330 = phi i32 [ %.lcssa, %tailrecurse ], [ %1, %2 ]
  %.tr29 = phi i32 [ %.lcssa33, %tailrecurse ], [ %0, %2 ]
  %4 = sext i32 %.tr29 to i64
  %5 = sext i32 %.tr2330 to i64
  %6 = getelementptr inbounds [905 x [8105 x i32]], ptr @calced, i64 0, i64 %4, i64 %5
  %7 = add nsw i32 %.tr29, -1
  %8 = add nsw i32 %.tr2330, -1
  %9 = tail call noundef i32 @_Z2dpii(i32 noundef %7, i32 noundef %8)
  %10 = load i32, ptr %6, align 4, !tbaa !5
  %11 = add nsw i32 %9, 1
  %.not = icmp eq i32 %10, %11
  br i1 %.not, label %tailrecurse, label %.critedge

tailrecurse:                                      ; preds = %.critedge.7, %.critedge.6, %.critedge.5, %.critedge.4, %.critedge.3, %.critedge.2, %.critedge.1, %.critedge, %.lr.ph
  %.027.lcssa = phi i32 [ 1, %.lr.ph ], [ 2, %.critedge ], [ 3, %.critedge.1 ], [ 4, %.critedge.2 ], [ 5, %.critedge.3 ], [ 6, %.critedge.4 ], [ 7, %.critedge.5 ], [ 8, %.critedge.6 ], [ 9, %.critedge.7 ]
  %.lcssa33 = phi i32 [ %7, %.lr.ph ], [ %14, %.critedge ], [ %19, %.critedge.1 ], [ %24, %.critedge.2 ], [ %29, %.critedge.3 ], [ %34, %.critedge.4 ], [ %39, %.critedge.5 ], [ %44, %.critedge.6 ], [ %49, %.critedge.7 ]
  %.lcssa = phi i32 [ %8, %.lr.ph ], [ %15, %.critedge ], [ %20, %.critedge.1 ], [ %25, %.critedge.2 ], [ %30, %.critedge.3 ], [ %35, %.critedge.4 ], [ %40, %.critedge.5 ], [ %45, %.critedge.6 ], [ %50, %.critedge.7 ]
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.027.lcssa)
  %13 = or i32 %.lcssa, %.lcssa33
  %or.cond = icmp eq i32 %13, 0
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.critedge:                                        ; preds = %.lr.ph
  %14 = add nsw i32 %.tr29, -2
  %15 = add nsw i32 %.tr2330, -4
  %16 = tail call noundef i32 @_Z2dpii(i32 noundef %14, i32 noundef %15)
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = add nsw i32 %16, 1
  %.not.1 = icmp eq i32 %17, %18
  br i1 %.not.1, label %tailrecurse, label %.critedge.1

.critedge.1:                                      ; preds = %.critedge
  %19 = add nsw i32 %.tr29, -3
  %20 = add nsw i32 %.tr2330, -9
  %21 = tail call noundef i32 @_Z2dpii(i32 noundef %19, i32 noundef %20)
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = add nsw i32 %21, 1
  %.not.2 = icmp eq i32 %22, %23
  br i1 %.not.2, label %tailrecurse, label %.critedge.2

.critedge.2:                                      ; preds = %.critedge.1
  %24 = add nsw i32 %.tr29, -4
  %25 = add nsw i32 %.tr2330, -16
  %26 = tail call noundef i32 @_Z2dpii(i32 noundef %24, i32 noundef %25)
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = add nsw i32 %26, 1
  %.not.3 = icmp eq i32 %27, %28
  br i1 %.not.3, label %tailrecurse, label %.critedge.3

.critedge.3:                                      ; preds = %.critedge.2
  %29 = add nsw i32 %.tr29, -5
  %30 = add nsw i32 %.tr2330, -25
  %31 = tail call noundef i32 @_Z2dpii(i32 noundef %29, i32 noundef %30)
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = add nsw i32 %31, 1
  %.not.4 = icmp eq i32 %32, %33
  br i1 %.not.4, label %tailrecurse, label %.critedge.4

.critedge.4:                                      ; preds = %.critedge.3
  %34 = add nsw i32 %.tr29, -6
  %35 = add nsw i32 %.tr2330, -36
  %36 = tail call noundef i32 @_Z2dpii(i32 noundef %34, i32 noundef %35)
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = add nsw i32 %36, 1
  %.not.5 = icmp eq i32 %37, %38
  br i1 %.not.5, label %tailrecurse, label %.critedge.5

.critedge.5:                                      ; preds = %.critedge.4
  %39 = add nsw i32 %.tr29, -7
  %40 = add nsw i32 %.tr2330, -49
  %41 = tail call noundef i32 @_Z2dpii(i32 noundef %39, i32 noundef %40)
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add nsw i32 %41, 1
  %.not.6 = icmp eq i32 %42, %43
  br i1 %.not.6, label %tailrecurse, label %.critedge.6

.critedge.6:                                      ; preds = %.critedge.5
  %44 = add nsw i32 %.tr29, -8
  %45 = add nsw i32 %.tr2330, -64
  %46 = tail call noundef i32 @_Z2dpii(i32 noundef %44, i32 noundef %45)
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = add nsw i32 %46, 1
  %.not.7 = icmp eq i32 %47, %48
  br i1 %.not.7, label %tailrecurse, label %.critedge.7

.critedge.7:                                      ; preds = %.critedge.6
  %49 = add nsw i32 %.tr29, -9
  %50 = add nsw i32 %.tr2330, -81
  %51 = tail call noundef i32 @_Z2dpii(i32 noundef %49, i32 noundef %50)
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = add nsw i32 %51, 1
  %.not.8 = icmp eq i32 %52, %53
  br i1 %.not.8, label %tailrecurse, label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %.critedge.7, %2
  ret void
}

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

attributes #0 = { mustprogress 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 = { 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" }

!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"}


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