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

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

@n = external dso_local global i32, align 4
@pre = external dso_local global [1000002 x i32], align 16
@suf = external dso_local global [1000002 x i32], align 16
@str = external dso_local global [1000002 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 [3 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
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef getelementptr inbounds (i8, ptr @str, i64 1))
  %7 = call i64 @strlen(ptr noundef getelementptr inbounds (i8, ptr @str, i64 1)) #4
  %8 = trunc i64 %7 to i32
  store i32 %8, ptr @n, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %31, %0
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %34

14:                                               ; preds = %9
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sub nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = sext i8 %23 to i32
  %25 = icmp eq i32 %24, 48
  %26 = zext i1 %25 to i32
  %27 = add nsw i32 %19, %26
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %29
  store i32 %27, ptr %30, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %14
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %2, align 4, !tbaa !5
  br label %9, !llvm.loop !10

34:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %35 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %35, ptr %3, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %57, %34
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = icmp sge i32 %37, 1
  br i1 %38, label %40, label %39

39:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %60

40:                                               ; preds = %36
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1000002 x i32], ptr @suf, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !9
  %50 = sext i8 %49 to i32
  %51 = icmp eq i32 %50, 49
  %52 = zext i1 %51 to i32
  %53 = add nsw i32 %45, %52
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1000002 x i32], ptr @suf, i64 0, i64 %55
  store i32 %53, ptr %56, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %40
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %3, align 4, !tbaa !5
  br label %36, !llvm.loop !12

60:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %81, %60
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = load i32, ptr @n, align 4, !tbaa !5
  %64 = icmp sle i32 %62, %63
  br i1 %64, label %66, label %65

65:                                               ; preds = %61
  store i32 8, ptr %5, align 4
  br label %84

66:                                               ; preds = %61
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = add nsw i32 %71, 1
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [1000002 x i32], ptr @suf, i64 0, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !5
  %76 = icmp eq i32 %70, %75
  br i1 %76, label %77, label %80

77:                                               ; preds = %66
  %78 = load i32, ptr %4, align 4, !tbaa !5
  %79 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %78)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %84

80:                                               ; preds = %66
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %4, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %4, align 4, !tbaa !5
  br label %61, !llvm.loop !13

84:                                               ; preds = %77, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  %85 = load i32, ptr %5, align 4
  switch i32 %85, label %90 [
    i32 8, label %86
    i32 1, label %88
  ]

86:                                               ; preds = %84
  %87 = call i32 @puts(ptr noundef @.str.2)
  store i32 0, ptr %1, align 4
  br label %88

88:                                               ; preds = %86, %84
  %89 = load i32, ptr %1, align 4
  ret i32 %89

90:                                               ; preds = %84
  unreachable
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

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

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 = { nounwind willreturn memory(read) "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind willreturn memory(read) }
attributes #5 = { 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}
!13 = distinct !{!13, !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"

@n = external dso_local local_unnamed_addr global i32, align 4
@pre = external dso_local local_unnamed_addr global [1000002 x i32], align 16
@suf = external dso_local local_unnamed_addr global [1000002 x i32], align 16
@str = external dso_local global [1000002 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 [3 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 getelementptr inbounds (i8, ptr @str, i64 1))
  %2 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) getelementptr inbounds (i8, ptr @str, i64 1)) #4
  %3 = trunc i64 %2 to i32
  store i32 %3, ptr @n, align 4, !tbaa !5
  %.not28 = icmp slt i32 %3, 1
  br i1 %.not28, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %4 = add nuw nsw i64 %2, 1
  %wide.trip.count = and i64 %4, 4294967295
  %.pre = load i32, ptr @pre, align 16, !tbaa !5
  %5 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %5, 1
  %6 = icmp eq i64 %wide.trip.count, 2
  br i1 %6, label %.lr.ph31.preheader.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %5, -2
  br label %.lr.ph

.lr.ph31.preheader.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.unr = phi i32 [ %.pre, %.lr.ph.preheader ], [ %27, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph31.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.lr.ph31.preheader.unr-lcssa
  %7 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %indvars.iv.unr
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = icmp eq i8 %8, 48
  %10 = zext i1 %9 to i32
  %11 = add nsw i32 %.unr, %10
  %12 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %indvars.iv.unr
  store i32 %11, ptr %12, align 4, !tbaa !5
  br label %.lr.ph31.preheader

.lr.ph31.preheader:                               ; preds = %.lr.ph31.preheader.unr-lcssa, %.lr.ph.epil
  %13 = and i64 %2, 2147483647
  %14 = shl nuw nsw i64 %13, 2
  %15 = getelementptr i8, ptr @suf, i64 %14
  %scevgep = getelementptr i8, ptr %15, i64 4
  %load_initial = load i32, ptr %scevgep, align 4
  br label %.lr.ph31

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %16 = phi i32 [ %.pre, %.lr.ph.preheader.new ], [ %27, %.lr.ph ]
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %17 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %indvars.iv
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = icmp eq i8 %18, 48
  %20 = zext i1 %19 to i32
  %21 = add nsw i32 %16, %20
  %22 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %indvars.iv
  store i32 %21, ptr %22, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = icmp eq i8 %24, 48
  %26 = zext i1 %25 to i32
  %27 = add nsw i32 %21, %26
  %28 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %indvars.iv.next
  store i32 %27, ptr %28, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.lr.ph31.preheader.unr-lcssa, label %.lr.ph, !llvm.loop !10

.preheader:                                       ; preds = %.lr.ph31, %0
  %smax = tail call i32 @llvm.smax.i32(i32 %3, i32 -1)
  %29 = add i32 %smax, 1
  %wide.trip.count40 = zext i32 %29 to i64
  br label %37

.lr.ph31:                                         ; preds = %.lr.ph31.preheader, %.lr.ph31
  %store_forwarded = phi i32 [ %load_initial, %.lr.ph31.preheader ], [ %34, %.lr.ph31 ]
  %indvars.iv34 = phi i64 [ %13, %.lr.ph31.preheader ], [ %indvars.iv.next35, %.lr.ph31 ]
  %30 = getelementptr inbounds [1000002 x i8], ptr @str, i64 0, i64 %indvars.iv34
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = icmp eq i8 %31, 49
  %33 = zext i1 %32 to i32
  %34 = add nsw i32 %store_forwarded, %33
  %35 = getelementptr inbounds [1000002 x i32], ptr @suf, i64 0, i64 %indvars.iv34
  store i32 %34, ptr %35, align 4, !tbaa !5
  %indvars.iv.next35 = add nsw i64 %indvars.iv34, -1
  %36 = icmp ugt i64 %indvars.iv34, 1
  br i1 %36, label %.lr.ph31, label %.preheader, !llvm.loop !12

37:                                               ; preds = %.preheader, %38
  %indvars.iv37 = phi i64 [ 0, %.preheader ], [ %indvars.iv.next38, %38 ]
  %exitcond41 = icmp eq i64 %indvars.iv37, %wide.trip.count40
  br i1 %exitcond41, label %47, label %38

38:                                               ; preds = %37
  %39 = getelementptr inbounds [1000002 x i32], ptr @pre, i64 0, i64 %indvars.iv37
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %41 = getelementptr inbounds [1000002 x i32], ptr @suf, i64 0, i64 %indvars.iv.next38
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp eq i32 %40, %42
  br i1 %43, label %44, label %37, !llvm.loop !13

44:                                               ; preds = %38
  %45 = trunc nuw nsw i64 %indvars.iv37 to i32
  %46 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %45)
  br label %49

47:                                               ; preds = %37
  %48 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %49

49:                                               ; preds = %44, %47
  ret i32 0
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; 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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

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" }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind willreturn memory(read) }

!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}
!13 = distinct !{!13, !11}


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