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

; 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 uwtable
define dso_local noundef i64 @_Z4getNv() #0 {
  %1 = alloca i8, align 1
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %3

3:                                                ; preds = %8, %0
  %4 = call i32 @getchar_unlocked()
  %5 = trunc i32 %4 to i8
  store i8 %5, ptr %1, align 1, !tbaa !9
  %6 = sext i8 %5 to i32
  %7 = icmp slt i32 %6, 48
  br i1 %7, label %8, label %9

8:                                                ; preds = %3
  br label %3, !llvm.loop !10

9:                                                ; preds = %3
  br label %10

10:                                               ; preds = %14, %9
  %11 = load i8, ptr %1, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  %13 = icmp sge i32 %12, 48
  br i1 %13, label %14, label %25

14:                                               ; preds = %10
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = mul nsw i64 %15, 10
  store i64 %16, ptr %2, align 8, !tbaa !5
  %17 = load i8, ptr %1, align 1, !tbaa !9
  %18 = sext i8 %17 to i32
  %19 = sub nsw i32 %18, 48
  %20 = sext i32 %19 to i64
  %21 = load i64, ptr %2, align 8, !tbaa !5
  %22 = add nsw i64 %21, %20
  store i64 %22, ptr %2, align 8, !tbaa !5
  %23 = call i32 @getchar_unlocked()
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %1, align 1, !tbaa !9
  br label %10, !llvm.loop !12

25:                                               ; preds = %10
  %26 = load i64, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #3
  ret i64 %26
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @getchar_unlocked() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = !{!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"

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local noundef i64 @_Z4getNv() local_unnamed_addr #0 {
  br label %1

1:                                                ; preds = %1, %0
  %2 = tail call i32 @getchar_unlocked()
  %3 = trunc i32 %2 to i8
  %4 = icmp slt i8 %3, 48
  br i1 %4, label %1, label %.preheader, !llvm.loop !5

.preheader:                                       ; preds = %1
  %sext7 = and i32 %2, 127
  %5 = icmp ugt i32 %sext7, 47
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %6 = phi i32 [ %12, %.lr.ph ], [ %sext7, %.preheader ]
  %.08 = phi i64 [ %10, %.lr.ph ], [ 0, %.preheader ]
  %7 = mul nsw i64 %.08, 10
  %8 = add nsw i32 %6, -48
  %9 = zext nneg i32 %8 to i64
  %10 = add nsw i64 %7, %9
  %11 = tail call i32 @getchar_unlocked()
  %sext = shl i32 %11, 24
  %12 = ashr exact i32 %sext, 24
  %13 = icmp sgt i32 %12, 47
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !7

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

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

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


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