; LLVM IR from tangjz/acm-icpc :: self_training/2015-05-01/K.ll (Z4scanRi)

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

@_ZZ4scanRiE2ch = external hidden global i32, align 4
@_ZZ4scanRiE4flag = external hidden global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4scanRi(ptr noundef nonnull align 4 dereferenceable(4) %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  br label %3

3:                                                ; preds = %14, %1
  %4 = call i32 @getchar()
  store i32 %4, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %5 = icmp slt i32 %4, 48
  br i1 %5, label %9, label %6

6:                                                ; preds = %3
  %7 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %8 = icmp sgt i32 %7, 57
  br i1 %8, label %9, label %12

9:                                                ; preds = %6, %3
  %10 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 45
  br label %12

12:                                               ; preds = %9, %6
  %13 = phi i1 [ false, %6 ], [ %11, %9 ]
  br i1 %13, label %14, label %15

14:                                               ; preds = %12
  br label %3, !llvm.loop !11

15:                                               ; preds = %12
  %16 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %17 = icmp eq i32 %16, 45
  %18 = zext i1 %17 to i32
  store i32 %18, ptr @_ZZ4scanRiE4flag, align 4, !tbaa !9
  br i1 %17, label %19, label %20

19:                                               ; preds = %15
  br label %23

20:                                               ; preds = %15
  %21 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %22 = sub nsw i32 %21, 48
  br label %23

23:                                               ; preds = %20, %19
  %24 = phi i32 [ 0, %19 ], [ %22, %20 ]
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %24, ptr %25, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %34, %23
  %27 = call i32 @getchar()
  store i32 %27, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %28 = icmp sge i32 %27, 48
  br i1 %28, label %29, label %32

29:                                               ; preds = %26
  %30 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %31 = icmp sle i32 %30, 57
  br label %32

32:                                               ; preds = %29, %26
  %33 = phi i1 [ false, %26 ], [ %31, %29 ]
  br i1 %33, label %34, label %46

34:                                               ; preds = %32
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = shl i32 %36, 3
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = shl i32 %39, 1
  %41 = add nsw i32 %37, %40
  %42 = load i32, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !9
  %43 = add nsw i32 %41, %42
  %44 = sub nsw i32 %43, 48
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %44, ptr %45, align 4, !tbaa !9
  br label %26, !llvm.loop !13

46:                                               ; preds = %32
  %47 = load i32, ptr @_ZZ4scanRiE4flag, align 4, !tbaa !9
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %54

49:                                               ; preds = %46
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = sub nsw i32 0, %51
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %52, ptr %53, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %49, %46
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @getchar() #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 = { 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" }

!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 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

@_ZZ4scanRiE2ch = external hidden local_unnamed_addr global i32, align 4
@_ZZ4scanRiE4flag = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z4scanRi(ptr nocapture noundef nonnull align 4 dereferenceable(4) %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %2, %1
  %3 = tail call i32 @getchar()
  store i32 %3, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !5
  %4 = add i32 %3, -58
  %or.cond = icmp ult i32 %4, -10
  %5 = icmp ne i32 %3, 45
  %or.cond3 = and i1 %5, %or.cond
  br i1 %or.cond3, label %2, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %2
  %6 = icmp eq i32 %3, 45
  %7 = zext i1 %6 to i32
  store i32 %7, ptr @_ZZ4scanRiE4flag, align 4, !tbaa !5
  %8 = add nsw i32 %3, -48
  %9 = select i1 %6, i32 0, i32 %8
  store i32 %9, ptr %0, align 4, !tbaa !5
  %10 = tail call i32 @getchar()
  store i32 %10, ptr @_ZZ4scanRiE2ch, align 4, !tbaa !5
  %11 = add i32 %10, -48
  %12 = icmp ult i32 %11, 10
  br i1 %12, label %.lr.ph, label %._crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph, %.critedge
  %21 = load i32, ptr @_ZZ4scanRiE4flag, align 4, !tbaa !5
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %25, label %22

22:                                               ; preds = %._crit_edge
  %23 = load i32, ptr %0, align 4, !tbaa !5
  %24 = sub nsw i32 0, %23
  store i32 %24, ptr %0, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %22, %._crit_edge
  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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass