; LLVM IR from eyangch/competitive-programming :: CSES/Introductory_Problems (digit_queries)

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

@Q = external dso_local global i32, align 4
@K = external dso_local global i64, align 8

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4getNv() #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: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  %6 = call noundef i64 @_Z4getNv()
  %7 = trunc i64 %6 to i32
  store i32 %7, ptr @Q, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %33, %0
  %9 = load i32, ptr @Q, align 4, !tbaa !5
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @Q, align 4, !tbaa !5
  %11 = icmp ne i32 %9, 0
  br i1 %11, label %12, label %52

12:                                               ; preds = %8
  %13 = call noundef i64 @_Z4getNv()
  store i64 %13, ptr @K, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  store i64 1, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %14 = load i64, ptr @K, align 8, !tbaa !9
  store i64 %14, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  store i64 1, ptr %5, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %19, %12
  %16 = load i64, ptr %3, align 8, !tbaa !9
  %17 = load i64, ptr @K, align 8, !tbaa !9
  %18 = icmp sle i64 %16, %17
  br i1 %18, label %19, label %33

19:                                               ; preds = %15
  %20 = load i64, ptr @K, align 8, !tbaa !9
  %21 = load i64, ptr %3, align 8, !tbaa !9
  %22 = sub nsw i64 %20, %21
  store i64 %22, ptr %4, align 8, !tbaa !9
  %23 = load i64, ptr %5, align 8, !tbaa !9
  %24 = mul nsw i64 9, %23
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = mul nsw i64 %24, %27
  %29 = load i64, ptr %3, align 8, !tbaa !9
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr %3, align 8, !tbaa !9
  %31 = load i64, ptr %5, align 8, !tbaa !9
  %32 = mul nsw i64 %31, 10
  store i64 %32, ptr %5, align 8, !tbaa !9
  br label %15, !llvm.loop !11

33:                                               ; preds = %15
  %34 = load i64, ptr %5, align 8, !tbaa !9
  %35 = sdiv i64 %34, 10
  store i64 %35, ptr %5, align 8, !tbaa !9
  %36 = load i64, ptr %5, align 8, !tbaa !9
  %37 = load i64, ptr %4, align 8, !tbaa !9
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = sdiv i64 %37, %39
  %41 = add nsw i64 %36, %40
  %42 = load i64, ptr %4, align 8, !tbaa !9
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = srem i64 %42, %44
  %46 = trunc i64 %45 to i32
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = call noundef i32 @_Z11getNthDigitxii(i64 noundef %41, i32 noundef %46, i32 noundef %47)
  %49 = add nsw i32 %48, 48
  %50 = call i32 @putchar_unlocked(i32 noundef %49)
  %51 = call i32 @putchar_unlocked(i32 noundef 10)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %8, !llvm.loop !13

52:                                               ; preds = %8
  %53 = load i32, ptr %1, align 4
  ret i32 %53
}

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar_unlocked(i32 noundef) #4

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


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

@Q = external dso_local local_unnamed_addr global i32, align 4
@K = external dso_local local_unnamed_addr global i64, align 8

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4getNv() local_unnamed_addr #1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = tail call noundef i64 @_Z4getNv()
  %2 = trunc i64 %1 to i32
  %3 = add nsw i32 %2, -1
  store i32 %3, ptr @Q, align 4, !tbaa !5
  %.not25 = icmp eq i32 %2, 0
  br i1 %.not25, label %._crit_edge28, label %.lr.ph27

.lr.ph27:                                         ; preds = %0, %11
  %4 = tail call noundef i64 @_Z4getNv()
  store i64 %4, ptr @K, align 8, !tbaa !9
  %.not1819 = icmp slt i64 %4, 1
  br i1 %.not1819, label %11, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph27, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.lr.ph27 ]
  %.022 = phi i64 [ %8, %.lr.ph ], [ 1, %.lr.ph27 ]
  %.01521 = phi i64 [ %7, %.lr.ph ], [ 1, %.lr.ph27 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %5 = mul nuw nsw i64 %indvars.iv.next, 9
  %6 = mul nuw nsw i64 %5, %.022
  %7 = add nuw nsw i64 %6, %.01521
  %8 = mul nuw nsw i64 %.022, 10
  %.not18 = icmp sgt i64 %7, %4
  br i1 %.not18, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph
  %9 = trunc nuw nsw i64 %indvars.iv.next to i32
  %10 = sub nsw i64 %4, %.01521
  br label %11

11:                                               ; preds = %._crit_edge, %.lr.ph27
  %.016.lcssa = phi i32 [ %9, %._crit_edge ], [ 0, %.lr.ph27 ]
  %.014.lcssa = phi i64 [ %10, %._crit_edge ], [ %4, %.lr.ph27 ]
  %.0.lcssa = phi i64 [ %.022, %._crit_edge ], [ 0, %.lr.ph27 ]
  %12 = zext nneg i32 %.016.lcssa to i64
  %13 = sdiv i64 %.014.lcssa, %12
  %14 = add nsw i64 %13, %.0.lcssa
  %15 = srem i64 %.014.lcssa, %12
  %16 = trunc nsw i64 %15 to i32
  %17 = tail call noundef i32 @_Z11getNthDigitxii(i64 noundef %14, i32 noundef %16, i32 noundef %.016.lcssa)
  %18 = add nsw i32 %17, 48
  %19 = tail call i32 @putchar_unlocked(i32 noundef %18)
  %20 = tail call i32 @putchar_unlocked(i32 noundef 10)
  %.pr = load i32, ptr @Q, align 4, !tbaa !5
  %21 = add nsw i32 %.pr, -1
  store i32 %21, ptr @Q, align 4, !tbaa !5
  %.not = icmp eq i32 %.pr, 0
  br i1 %.not, label %._crit_edge28, label %.lr.ph27, !llvm.loop !13

._crit_edge28:                                    ; preds = %11, %0
  ret i32 0
}

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar_unlocked(i32 noundef) local_unnamed_addr #3

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 = { 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 #2 = { mustprogress norecurse 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 #3 = { inlinehint mustprogress 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" }

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


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