; LLVM IR from tangjz/acm-icpc :: codeforces/gym (100959J)

; 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
@cnt2 = external dso_local global i32, align 4
@sum = external dso_local global i32, align 4
@ans = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  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 %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %26

12:                                               ; preds = %7
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = sub nsw i32 %14, 1
  %16 = load i32, ptr @sum, align 4, !tbaa !5
  %17 = add nsw i32 %16, %15
  store i32 %17, ptr @sum, align 4, !tbaa !5
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 3
  %20 = zext i1 %19 to i32
  %21 = load i32, ptr @cnt2, align 4, !tbaa !5
  %22 = add nsw i32 %21, %20
  store i32 %22, ptr @cnt2, 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 @sum, align 4, !tbaa !5
  %28 = load i32, ptr @n, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 2
  %30 = icmp ne i32 %27, %29
  br i1 %30, label %31, label %33

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

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

34:                                               ; preds = %66, %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = load i32, ptr @n, align 4, !tbaa !5
  %37 = sub nsw i32 %36, 2
  %38 = icmp sle i32 %35, %37
  br i1 %38, label %40, label %39

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

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

42:                                               ; preds = %53, %40
  %43 = load i32, ptr @cnt2, align 4, !tbaa !5
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %50

45:                                               ; preds = %42
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = and i32 %46, 1
  %48 = icmp ne i32 %47, 0
  %49 = xor i1 %48, true
  br label %50

50:                                               ; preds = %45, %42
  %51 = phi i1 [ false, %42 ], [ %49, %45 ]
  br i1 %51, label %52, label %58

52:                                               ; preds = %50
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr @cnt2, align 4, !tbaa !5
  %55 = add nsw i32 %54, -1
  store i32 %55, ptr @cnt2, align 4, !tbaa !5
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = ashr i32 %56, 1
  store i32 %57, ptr %5, align 4, !tbaa !5
  br label %42, !llvm.loop !11

58:                                               ; preds = %50
  %59 = load i32, ptr @ans, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = mul nsw i64 %60, %62
  %64 = srem i64 %63, 1000000007
  %65 = trunc i64 %64 to i32
  store i32 %65, ptr @ans, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %66

66:                                               ; preds = %58
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %4, align 4, !tbaa !5
  br label %34, !llvm.loop !12

69:                                               ; preds = %39
  %70 = load i32, ptr @ans, align 4, !tbaa !5
  %71 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %70)
  store i32 0, ptr %1, align 4
  br label %72

72:                                               ; preds = %69, %31
  %73 = load i32, ptr %1, align 4
  ret i32 %73
}

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
@cnt2 = external dso_local local_unnamed_addr global i32, align 4
@sum = external dso_local local_unnamed_addr global i32, align 4
@ans = 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 = alloca i32, align 4
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.._crit_edge_crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph, %.._crit_edge_crit_edge
  %5 = phi i32 [ %.pre, %.._crit_edge_crit_edge ], [ %11, %.lr.ph ]
  %.lcssa = phi i32 [ %3, %.._crit_edge_crit_edge ], [ %17, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  %6 = add nsw i32 %.lcssa, -2
  %.not = icmp eq i32 %5, %6
  br i1 %.not, label %21, label %19

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01015 = phi i32 [ %16, %.lr.ph ], [ 0, %0 ]
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %8 = load i32, ptr %1, align 4, !tbaa !5
  %9 = add nsw i32 %8, -1
  %10 = load i32, ptr @sum, align 4, !tbaa !5
  %11 = add nsw i32 %9, %10
  store i32 %11, ptr @sum, align 4, !tbaa !5
  %12 = icmp eq i32 %8, 3
  %13 = zext i1 %12 to i32
  %14 = load i32, ptr @cnt2, align 4, !tbaa !5
  %15 = add nsw i32 %14, %13
  store i32 %15, ptr @cnt2, align 4, !tbaa !5
  %16 = add nuw nsw i32 %.01015, 1
  %17 = load i32, ptr @n, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !9

19:                                               ; preds = %._crit_edge
  %20 = call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %42

21:                                               ; preds = %._crit_edge
  store i32 1, ptr @ans, align 4, !tbaa !5
  %.not1325 = icmp slt i32 %.lcssa, 3
  br i1 %.not1325, label %28, label %.preheader.preheader

.preheader.preheader:                             ; preds = %21
  %cnt2.promoted22 = load i32, ptr @cnt2, align 4, !tbaa !5
  %22 = add nsw i32 %.lcssa, -2
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %37
  %.0927 = phi i32 [ %41, %37 ], [ 1, %.preheader.preheader ]
  %23 = phi i64 [ %40, %37 ], [ 1, %.preheader.preheader ]
  %.lcssa212426 = phi i32 [ %.lcssa2123, %37 ], [ %cnt2.promoted22, %.preheader.preheader ]
  %24 = icmp ne i32 %.lcssa212426, 0
  %25 = and i32 %.0927, 1
  %.not1416 = icmp eq i32 %25, 0
  %26 = and i1 %24, %.not1416
  br i1 %26, label %.lr.ph18, label %37

._crit_edge28:                                    ; preds = %37
  %27 = trunc nsw i64 %40 to i32
  store i32 %27, ptr @ans, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %._crit_edge28, %21
  %29 = phi i32 [ %27, %._crit_edge28 ], [ 1, %21 ]
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %29)
  br label %42

.lr.ph18:                                         ; preds = %.preheader, %.lr.ph18
  %.017 = phi i32 [ %33, %.lr.ph18 ], [ %.0927, %.preheader ]
  %31 = phi i32 [ %32, %.lr.ph18 ], [ %.lcssa212426, %.preheader ]
  %32 = add nsw i32 %31, -1
  %33 = lshr exact i32 %.017, 1
  %34 = icmp ne i32 %32, 0
  %35 = and i32 %.017, 2
  %.not14 = icmp eq i32 %35, 0
  %36 = select i1 %34, i1 %.not14, i1 false
  br i1 %36, label %.lr.ph18, label %._crit_edge19, !llvm.loop !11

._crit_edge19:                                    ; preds = %.lr.ph18
  store i32 %32, ptr @cnt2, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %._crit_edge19, %.preheader
  %.lcssa2123 = phi i32 [ %32, %._crit_edge19 ], [ %.lcssa212426, %.preheader ]
  %.0.lcssa = phi i32 [ %33, %._crit_edge19 ], [ %.0927, %.preheader ]
  %38 = zext nneg i32 %.0.lcssa to i64
  %39 = mul nsw i64 %23, %38
  %40 = srem i64 %39, 1000000007
  %41 = add nuw nsw i32 %.0927, 1
  %exitcond.not = icmp eq i32 %.0927, %22
  br i1 %exitcond.not, label %._crit_edge28, label %.preheader, !llvm.loop !12

42:                                               ; preds = %28, %19
  ret i32 0
}

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

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

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

; 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" }
attributes #2 = { mustprogress 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}


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