; LLVM IR from tangjz/acm-icpc :: lydsy (1800)

; 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
@r = external dso_local global [21 x i32], align 16
@cnt = external dso_local global i32, align 4
@res = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 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 @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %23, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr @r, i64 %14
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %15)
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [21 x i32], ptr @r, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr @cnt, align 4, !tbaa !5
  %22 = add nsw i32 %21, %20
  store i32 %22, ptr @cnt, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %12
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !9

26:                                               ; preds = %11
  %27 = load i32, ptr @cnt, align 4, !tbaa !5
  %28 = and i32 %27, 1
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %32

30:                                               ; preds = %26
  %31 = call i32 @puts(ptr noundef @.str.1)
  store i32 0, ptr %1, align 4
  br label %78

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

33:                                               ; preds = %68, %32
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = sub nsw i32 %35, 1
  %37 = icmp slt i32 %34, %36
  br i1 %37, label %39, label %38

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

39:                                               ; preds = %33
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [21 x i32], ptr @r, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr %5, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %51, %39
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = load i32, ptr @cnt, align 4, !tbaa !5
  %49 = ashr i32 %48, 1
  %50 = icmp sgt i32 %47, %49
  br i1 %50, label %51, label %59

51:                                               ; preds = %46
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %4, align 4, !tbaa !5
  %54 = sext i32 %52 to i64
  %55 = getelementptr inbounds [21 x i32], ptr @r, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sub nsw i32 %57, %56
  store i32 %58, ptr %5, align 4, !tbaa !5
  br label %46, !llvm.loop !11

59:                                               ; preds = %46
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = load i32, ptr @cnt, align 4, !tbaa !5
  %62 = ashr i32 %61, 1
  %63 = icmp eq i32 %60, %62
  br i1 %63, label %64, label %67

64:                                               ; preds = %59
  %65 = load i32, ptr @res, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr @res, align 4, !tbaa !5
  br label %67

67:                                               ; preds = %64, %59
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %3, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %3, align 4, !tbaa !5
  br label %33, !llvm.loop !12

71:                                               ; preds = %38
  %72 = load i32, ptr @res, align 4, !tbaa !5
  %73 = load i32, ptr @res, align 4, !tbaa !5
  %74 = sub nsw i32 %73, 1
  %75 = mul nsw i32 %72, %74
  %76 = ashr i32 %75, 1
  %77 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %76)
  store i32 0, ptr %1, align 4
  br label %78

78:                                               ; preds = %71, %30
  %79 = load i32, ptr %1, align 4
  ret i32 %79
}

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 @puts(ptr noundef) #1

declare i32 @printf(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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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 global i32, align 4
@r = external dso_local global [21 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4
@res = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 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 @n)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = load i32, ptr @cnt, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %4 = phi i32 [ %.pre, %.._crit_edge_crit_edge ], [ %13, %.lr.ph ]
  %.lcssa19 = phi i32 [ %2, %.._crit_edge_crit_edge ], [ %14, %.lr.ph ]
  %5 = and i32 %4, 1
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %.preheader, label %17

.preheader:                                       ; preds = %._crit_edge
  %6 = icmp sgt i32 %.lcssa19, 1
  br i1 %6, label %.lr.ph32, label %.preheader.._crit_edge33_crit_edge

.preheader.._crit_edge33_crit_edge:               ; preds = %.preheader
  %.pre43 = load i32, ptr @res, align 4, !tbaa !5
  br label %._crit_edge33

.lr.ph32:                                         ; preds = %.preheader
  %7 = add nsw i32 %.lcssa19, -1
  %res.promoted = load i32, ptr @res, align 4, !tbaa !5
  %8 = ashr exact i32 %4, 1
  %wide.trip.count = zext nneg i32 %7 to i64
  br label %24

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %9 = getelementptr inbounds i32, ptr @r, i64 %indvars.iv
  %10 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %9)
  %11 = load i32, ptr %9, align 4, !tbaa !5
  %12 = load i32, ptr @cnt, align 4, !tbaa !5
  %13 = add nsw i32 %12, %11
  store i32 %13, ptr @cnt, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !9

17:                                               ; preds = %._crit_edge
  %18 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %41

._crit_edge33:                                    ; preds = %39, %.preheader.._crit_edge33_crit_edge
  %19 = phi i32 [ %.pre43, %.preheader.._crit_edge33_crit_edge ], [ %40, %39 ]
  %20 = add nsw i32 %19, -1
  %21 = mul nsw i32 %20, %19
  %22 = ashr i32 %21, 1
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %22)
  br label %41

24:                                               ; preds = %.lr.ph32, %39
  %indvars.iv40 = phi i64 [ 0, %.lr.ph32 ], [ %indvars.iv.next41, %39 ]
  %.031 = phi i32 [ 0, %.lr.ph32 ], [ %.1.lcssa, %39 ]
  %.01330 = phi i32 [ 0, %.lr.ph32 ], [ %.114.lcssa, %39 ]
  %25 = phi i32 [ %res.promoted, %.lr.ph32 ], [ %40, %39 ]
  %26 = getelementptr inbounds [21 x i32], ptr @r, i64 0, i64 %indvars.iv40
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = add nsw i32 %27, %.031
  %29 = icmp sgt i32 %28, %8
  br i1 %29, label %.lr.ph24.preheader, label %._crit_edge25

.lr.ph24.preheader:                               ; preds = %24
  %30 = sext i32 %.01330 to i64
  br label %.lr.ph24

.lr.ph24:                                         ; preds = %.lr.ph24.preheader, %.lr.ph24
  %indvars.iv37 = phi i64 [ %30, %.lr.ph24.preheader ], [ %indvars.iv.next38, %.lr.ph24 ]
  %.122 = phi i32 [ %28, %.lr.ph24.preheader ], [ %33, %.lr.ph24 ]
  %indvars.iv.next38 = add nsw i64 %indvars.iv37, 1
  %31 = getelementptr inbounds [21 x i32], ptr @r, i64 0, i64 %indvars.iv37
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sub nsw i32 %.122, %32
  %34 = icmp sgt i32 %33, %8
  br i1 %34, label %.lr.ph24, label %._crit_edge25.loopexit, !llvm.loop !11

._crit_edge25.loopexit:                           ; preds = %.lr.ph24
  %35 = trunc nsw i64 %indvars.iv.next38 to i32
  br label %._crit_edge25

._crit_edge25:                                    ; preds = %._crit_edge25.loopexit, %24
  %.114.lcssa = phi i32 [ %.01330, %24 ], [ %35, %._crit_edge25.loopexit ]
  %.1.lcssa = phi i32 [ %28, %24 ], [ %33, %._crit_edge25.loopexit ]
  %36 = icmp eq i32 %.1.lcssa, %8
  br i1 %36, label %37, label %39

37:                                               ; preds = %._crit_edge25
  %38 = add nsw i32 %25, 1
  store i32 %38, ptr @res, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %._crit_edge25, %37
  %40 = phi i32 [ %25, %._crit_edge25 ], [ %38, %37 ]
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge33, label %24, !llvm.loop !12

41:                                               ; preds = %._crit_edge33, %17
  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 @puts(ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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