; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-27/H - Machine - dby.ll (Z4readRi)

; 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 @_Z4readRi(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
  br label %6

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

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

14:                                               ; preds = %10, %6
  %15 = phi i1 [ true, %6 ], [ %13, %10 ]
  br i1 %15, label %16, label %19

16:                                               ; preds = %14
  %17 = call i32 @getchar()
  %18 = trunc i32 %17 to i8
  store i8 %18, ptr %3, align 1, !tbaa !9
  br label %6, !llvm.loop !10

19:                                               ; preds = %14
  %20 = load i8, ptr %3, align 1, !tbaa !9
  %21 = sext i8 %20 to i32
  %22 = sub nsw i32 %21, 48
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %22, ptr %23, align 4, !tbaa !12
  br label %24

24:                                               ; preds = %35, %19
  %25 = call i32 @getchar()
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %3, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp sge i32 %27, 48
  br i1 %28, label %29, label %33

29:                                               ; preds = %24
  %30 = load i8, ptr %3, align 1, !tbaa !9
  %31 = sext i8 %30 to i32
  %32 = icmp sle i32 %31, 57
  br label %33

33:                                               ; preds = %29, %24
  %34 = phi i1 [ false, %24 ], [ %32, %29 ]
  br i1 %34, label %35, label %44

35:                                               ; preds = %33
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = load i32, ptr %36, align 4, !tbaa !12
  %38 = mul nsw i32 %37, 10
  %39 = load i8, ptr %3, align 1, !tbaa !9
  %40 = sext i8 %39 to i32
  %41 = add nsw i32 %38, %40
  %42 = sub nsw i32 %41, 48
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %42, ptr %43, align 4, !tbaa !12
  br label %24, !llvm.loop !14

44:                                               ; preds = %33
  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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !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 void @_Z4readRi(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0) local_unnamed_addr #0 {
  %2 = tail call i32 @getchar()
  %sext10 = shl i32 %2, 24
  %3 = ashr exact i32 %sext10, 24
  %4 = add nsw i32 %3, -58
  %5 = icmp ult i32 %4, -10
  br i1 %5, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %1
  %.lcssa = phi i32 [ %3, %1 ], [ %11, %.lr.ph ]
  %storemerge11 = add nsw i32 %.lcssa, -48
  store i32 %storemerge11, ptr %0, align 4, !tbaa !5
  %6 = tail call i32 @getchar()
  %sext912 = shl i32 %6, 24
  %7 = ashr exact i32 %sext912, 24
  %8 = add nsw i32 %7, -48
  %9 = icmp ult i32 %8, 10
  br i1 %9, label %.lr.ph13, label %._crit_edge

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

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

._crit_edge:                                      ; preds = %.lr.ph13, %.preheader
  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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass