; LLVM IR from tangjz/acm-icpc :: 51nod (1116)

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

@lim = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4
@str = external dso_local global [100001 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %69, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !9
  %11 = icmp ne i8 %10, 0
  br i1 %11, label %13, label %12

12:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %72

13:                                               ; preds = %6
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = sext i8 %17 to i32
  %19 = icmp sge i32 %18, 48
  br i1 %19, label %20, label %34

20:                                               ; preds = %13
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp sle i32 %25, 57
  br i1 %26, label %27, label %34

27:                                               ; preds = %20
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = sub nsw i32 %32, 48
  br label %42

34:                                               ; preds = %20, %13
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !9
  %39 = sext i8 %38 to i32
  %40 = sub nsw i32 %39, 65
  %41 = add nsw i32 %40, 10
  br label %42

42:                                               ; preds = %34, %27
  %43 = phi i32 [ %33, %27 ], [ %41, %34 ]
  %44 = trunc i32 %43 to i8
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %46
  store i8 %44, ptr %47, align 1, !tbaa !9
  %48 = load i32, ptr @lim, align 4, !tbaa !5
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !9
  %53 = sext i8 %52 to i32
  %54 = icmp slt i32 %48, %53
  br i1 %54, label %55, label %61

55:                                               ; preds = %42
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %57
  %59 = load i8, ptr %58, align 1, !tbaa !9
  %60 = sext i8 %59 to i32
  store i32 %60, ptr @lim, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %55, %42
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !9
  %66 = sext i8 %65 to i32
  %67 = load i32, ptr @sum, align 4, !tbaa !5
  %68 = add nsw i32 %67, %66
  store i32 %68, ptr @sum, align 4, !tbaa !5
  br label %69

69:                                               ; preds = %61
  %70 = load i32, ptr %2, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !10

72:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %73 = load i32, ptr @lim, align 4, !tbaa !5
  store i32 %73, ptr %3, align 4, !tbaa !5
  br label %74

74:                                               ; preds = %88, %72
  %75 = load i32, ptr %3, align 4, !tbaa !5
  %76 = icmp slt i32 %75, 36
  br i1 %76, label %78, label %77

77:                                               ; preds = %74
  store i32 5, ptr %4, align 4
  br label %91

78:                                               ; preds = %74
  %79 = load i32, ptr @sum, align 4, !tbaa !5
  %80 = load i32, ptr %3, align 4, !tbaa !5
  %81 = srem i32 %79, %80
  %82 = icmp eq i32 %81, 0
  br i1 %82, label %83, label %87

83:                                               ; preds = %78
  %84 = load i32, ptr %3, align 4, !tbaa !5
  %85 = add nsw i32 %84, 1
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %85)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %4, align 4
  br label %91

87:                                               ; preds = %78
  br label %88

88:                                               ; preds = %87
  %89 = load i32, ptr %3, align 4, !tbaa !5
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %3, align 4, !tbaa !5
  br label %74, !llvm.loop !12

91:                                               ; preds = %83, %77
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  %92 = load i32, ptr %4, align 4
  switch i32 %92, label %97 [
    i32 5, label %93
    i32 1, label %95
  ]

93:                                               ; preds = %91
  %94 = call i32 @puts(ptr noundef @.str.2)
  store i32 0, ptr %1, align 4
  br label %95

95:                                               ; preds = %93, %91
  %96 = load i32, ptr %1, align 4
  ret i32 %96

97:                                               ; preds = %91
  unreachable
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #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

declare i32 @printf(ptr noundef, ...) #1

declare i32 @puts(ptr noundef) #1

attributes #0 = { 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 #1 = { "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 = { 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 = !{!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"

@lim = external dso_local local_unnamed_addr global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4
@str = external dso_local global [100001 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %lim.promoted = load i32, ptr @lim, align 4, !tbaa !5
  %sum.promoted = load i32, ptr @sum, align 4, !tbaa !5
  %2 = load i8, ptr @str, align 16, !tbaa !9
  %.not26 = icmp eq i8 %2, 0
  br i1 %.not26, label %3, label %.lr.ph

._crit_edge:                                      ; preds = %16
  store i32 %18, ptr @sum, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %._crit_edge, %0
  %4 = phi i32 [ %18, %._crit_edge ], [ %sum.promoted, %0 ]
  %5 = phi i32 [ %17, %._crit_edge ], [ %lim.promoted, %0 ]
  %6 = icmp slt i32 %5, 36
  br i1 %6, label %.lr.ph30, label %._crit_edge31

.lr.ph:                                           ; preds = %0, %16
  %indvars.iv = phi i64 [ %indvars.iv.next, %16 ], [ 0, %0 ]
  %7 = phi i8 [ %20, %16 ], [ %2, %0 ]
  %8 = phi ptr [ %19, %16 ], [ @str, %0 ]
  %9 = phi i32 [ %17, %16 ], [ %lim.promoted, %0 ]
  %10 = phi i32 [ %18, %16 ], [ %sum.promoted, %0 ]
  %11 = add i8 %7, -48
  %or.cond = icmp ult i8 %11, 10
  %.v = select i1 %or.cond, i8 -48, i8 -55
  %12 = add i8 %.v, %7
  store i8 %12, ptr %8, align 1, !tbaa !9
  %13 = sext i8 %12 to i32
  %14 = icmp slt i32 %9, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %.lr.ph
  store i32 %13, ptr @lim, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %15, %.lr.ph
  %17 = phi i32 [ %13, %15 ], [ %9, %.lr.ph ]
  %18 = add nsw i32 %10, %13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds [100001 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %.not = icmp eq i8 %20, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

.lr.ph30:                                         ; preds = %3, %24
  %.01628 = phi i32 [ %23, %24 ], [ %5, %3 ]
  %21 = srem i32 %4, %.01628
  %22 = icmp eq i32 %21, 0
  %23 = add i32 %.01628, 1
  br i1 %22, label %25, label %24

24:                                               ; preds = %.lr.ph30
  %exitcond.not = icmp eq i32 %23, 36
  br i1 %exitcond.not, label %._crit_edge31, label %.lr.ph30, !llvm.loop !12

25:                                               ; preds = %.lr.ph30
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %23)
  br label %28

._crit_edge31:                                    ; preds = %24, %3
  %27 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %28

28:                                               ; preds = %25, %._crit_edge31
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #1

attributes #0 = { mustprogress nofree norecurse 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 = { nofree nounwind "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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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