; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/WF/Medium Hard/FNCS/r_64.ll (Z2giRi)

; 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 void @_Z2giRi(ptr noundef nonnull align 4 dereferenceable(4) %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  %4 = call i32 @getchar()
  %5 = trunc i32 %4 to i8
  store i8 %5, ptr %3, align 1, !tbaa !9
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !10
  br label %7

7:                                                ; preds = %17, %1
  %8 = load i8, ptr %3, align 1, !tbaa !9
  %9 = sext i8 %8 to i32
  %10 = icmp slt i32 %9, 48
  br i1 %10, label %15, label %11

11:                                               ; preds = %7
  %12 = load i8, ptr %3, align 1, !tbaa !9
  %13 = sext i8 %12 to i32
  %14 = icmp sgt i32 %13, 57
  br label %15

15:                                               ; preds = %11, %7
  %16 = phi i1 [ true, %7 ], [ %14, %11 ]
  br i1 %16, label %17, label %20

17:                                               ; preds = %15
  %18 = call i32 @getchar()
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %3, align 1, !tbaa !9
  br label %7, !llvm.loop !12

20:                                               ; preds = %15
  br label %21

21:                                               ; preds = %31, %20
  %22 = load i8, ptr %3, align 1, !tbaa !9
  %23 = sext i8 %22 to i32
  %24 = icmp sge i32 %23, 48
  br i1 %24, label %25, label %29

25:                                               ; preds = %21
  %26 = load i8, ptr %3, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp sle i32 %27, 57
  br label %29

29:                                               ; preds = %25, %21
  %30 = phi i1 [ false, %21 ], [ %28, %25 ]
  br i1 %30, label %31, label %42

31:                                               ; preds = %29
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = load i32, ptr %32, align 4, !tbaa !10
  %34 = mul nsw i32 %33, 10
  %35 = load i8, ptr %3, align 1, !tbaa !9
  %36 = sext i8 %35 to i32
  %37 = add nsw i32 %34, %36
  %38 = sub nsw i32 %37, 48
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %38, ptr %39, align 4, !tbaa !10
  %40 = call i32 @getchar()
  %41 = trunc i32 %40 to i8
  store i8 %41, ptr %3, align 1, !tbaa !9
  br label %21, !llvm.loop !14

42:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  ret void
}

; 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() #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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 void @_Z2giRi(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0) local_unnamed_addr #0 {
  %2 = tail call i32 @getchar()
  store i32 0, ptr %0, align 4, !tbaa !5
  %sext9 = shl i32 %2, 24
  %3 = ashr exact i32 %sext9, 24
  %4 = add nsw i32 %3, -58
  %5 = icmp ult i32 %4, -10
  br i1 %5, label %.lr.ph, label %.lr.ph11.preheader

.lr.ph:                                           ; preds = %1, %.lr.ph
  %6 = tail call i32 @getchar()
  %sext = shl i32 %6, 24
  %7 = ashr exact i32 %sext, 24
  %8 = add nsw i32 %7, -58
  %9 = icmp ult i32 %8, -10
  br i1 %9, label %.lr.ph, label %.lr.ph11.preheader, !llvm.loop !9

.lr.ph11.preheader:                               ; preds = %.lr.ph, %1
  %.ph = phi i32 [ %3, %1 ], [ %7, %.lr.ph ]
  br label %.lr.ph11

.lr.ph11:                                         ; preds = %.lr.ph11.preheader, %.lr.ph11
  %10 = phi i32 [ %16, %.lr.ph11 ], [ %.ph, %.lr.ph11.preheader ]
  %11 = load i32, ptr %0, align 4, !tbaa !5
  %12 = mul nsw i32 %11, 10
  %13 = add nsw i32 %10, -48
  %14 = add i32 %13, %12
  store i32 %14, ptr %0, align 4, !tbaa !5
  %15 = tail call i32 @getchar()
  %sext8 = shl i32 %15, 24
  %16 = ashr exact i32 %sext8, 24
  %17 = add nsw i32 %16, -48
  %18 = icmp ult i32 %17, 10
  br i1 %18, label %.lr.ph11, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph11
  ret void
}

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @getchar() 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 = !{!6, !6, i64 0}
!6 = !{!"int", !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}


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