; LLVM IR from tangjz/acm-icpc :: buaa/oj3rd (1162)

; 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
@x = external dso_local global i32, align 4
@r = external dso_local global i32, align 4
@c = external dso_local global i32, align 4
@str = external dso_local global [1010 x i8], align 16
@ans = external dso_local global [101 x [1010 x i8]], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 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
  store i32 0, ptr %1, align 4
  br label %4

4:                                                ; preds = %70, %0
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @x)
  %6 = icmp eq i32 %5, 2
  br i1 %6, label %7, label %72

7:                                                ; preds = %4
  store i32 0, ptr @c, align 4, !tbaa !5
  store i32 0, ptr @r, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %43, %7
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @n, align 4, !tbaa !5
  %11 = icmp ne i32 %9, 0
  br i1 %11, label %12, label %44

12:                                               ; preds = %8
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, 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 %14

14:                                               ; preds = %40, %12
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [1010 x i8], ptr @str, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = icmp ne i8 %18, 0
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %14
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1010 x i8], ptr @str, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  %26 = load i32, ptr @r, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @r, align 4, !tbaa !5
  %28 = sext i32 %26 to i64
  %29 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %28
  %30 = load i32, ptr @c, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1010 x i8], ptr %29, i64 0, i64 %31
  store i8 %25, ptr %32, align 1, !tbaa !9
  %33 = load i32, ptr @r, align 4, !tbaa !5
  %34 = load i32, ptr @x, align 4, !tbaa !5
  %35 = icmp eq i32 %33, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %21
  store i32 0, ptr @r, align 4, !tbaa !5
  %37 = load i32, ptr @c, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr @c, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %36, %21
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %2, align 4, !tbaa !5
  br label %14, !llvm.loop !10

43:                                               ; preds = %20
  br label %8, !llvm.loop !12

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

45:                                               ; preds = %67, %44
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = load i32, ptr @x, align 4, !tbaa !5
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %50, label %49

49:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %70

50:                                               ; preds = %45
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %52
  %54 = load i32, ptr @c, align 4, !tbaa !5
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = load i32, ptr @r, align 4, !tbaa !5
  %57 = icmp slt i32 %55, %56
  %58 = zext i1 %57 to i32
  %59 = add nsw i32 %54, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1010 x i8], ptr %53, i64 0, i64 %60
  store i8 0, ptr %61, align 1, !tbaa !9
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %63
  %65 = getelementptr inbounds [1010 x i8], ptr %64, i64 0, i64 0
  %66 = call i32 @puts(ptr noundef %65)
  br label %67

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

70:                                               ; preds = %49
  %71 = call i32 @puts(ptr noundef @.str.2)
  br label %4, !llvm.loop !14

72:                                               ; preds = %4
  ret i32 0
}

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

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}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !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 global i32, align 4
@x = external dso_local global i32, align 4
@r = external dso_local local_unnamed_addr global i32, align 4
@c = external dso_local local_unnamed_addr global i32, align 4
@str = external dso_local global [1010 x i8], align 16
@ans = external dso_local global [101 x [1010 x i8]], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 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, ptr noundef nonnull @x)
  %2 = icmp eq i32 %1, 2
  br i1 %2, label %.lr.ph18, label %._crit_edge19

.lr.ph18:                                         ; preds = %0, %._crit_edge
  store i32 0, ptr @c, align 4, !tbaa !5
  store i32 0, ptr @r, align 4, !tbaa !5
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr @n, align 4, !tbaa !5
  %.not13 = icmp eq i32 %3, 0
  br i1 %.not13, label %.preheader, label %.lr.ph14

..loopexit_crit_edge:                             ; preds = %23
  store i32 %25, ptr @r, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %..loopexit_crit_edge, %.lr.ph14
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  store i32 %6, ptr @n, align 4, !tbaa !5
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %.preheader, label %.lr.ph14, !llvm.loop !9

.preheader:                                       ; preds = %.loopexit, %.lr.ph18
  %7 = load i32, ptr @x, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph16, label %._crit_edge

.lr.ph14:                                         ; preds = %.lr.ph18, %.loopexit
  %9 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @str)
  %10 = load i8, ptr @str, align 16, !tbaa !11
  %.not1011 = icmp eq i8 %10, 0
  br i1 %.not1011, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph14
  %c.promoted = load i32, ptr @c, align 4, !tbaa !5
  %r.promoted = load i32, ptr @r, align 4, !tbaa !5
  %11 = load i32, ptr @x, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %.lr.ph, %23
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %23 ]
  %13 = phi i8 [ %10, %.lr.ph ], [ %27, %23 ]
  %14 = phi i32 [ %r.promoted, %.lr.ph ], [ %25, %23 ]
  %15 = phi i32 [ %c.promoted, %.lr.ph ], [ %24, %23 ]
  %16 = add nsw i32 %14, 1
  %17 = sext i32 %14 to i64
  %18 = sext i32 %15 to i64
  %19 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %17, i64 %18
  store i8 %13, ptr %19, align 1, !tbaa !11
  %20 = icmp eq i32 %16, %11
  br i1 %20, label %21, label %23

21:                                               ; preds = %12
  %22 = add nsw i32 %15, 1
  store i32 %22, ptr @c, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %12, %21
  %24 = phi i32 [ %15, %12 ], [ %22, %21 ]
  %25 = phi i32 [ %16, %12 ], [ 0, %21 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = getelementptr inbounds [1010 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %.not10 = icmp eq i8 %27, 0
  br i1 %.not10, label %..loopexit_crit_edge, label %12, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph16, %.preheader
  %28 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.2)
  %29 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @x)
  %30 = icmp eq i32 %29, 2
  br i1 %30, label %.lr.ph18, label %._crit_edge19, !llvm.loop !13

.lr.ph16:                                         ; preds = %.preheader, %.lr.ph16
  %indvars.iv20 = phi i64 [ %indvars.iv.next21, %.lr.ph16 ], [ 0, %.preheader ]
  %31 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %indvars.iv20
  %32 = load i32, ptr @c, align 4, !tbaa !5
  %33 = load i32, ptr @r, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = icmp slt i64 %indvars.iv20, %34
  %36 = zext i1 %35 to i32
  %37 = add nsw i32 %32, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [101 x [1010 x i8]], ptr @ans, i64 0, i64 %indvars.iv20, i64 %38
  store i8 0, ptr %39, align 1, !tbaa !11
  %40 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) %31)
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, 1
  %41 = load i32, ptr @x, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = icmp slt i64 %indvars.iv.next21, %42
  br i1 %43, label %.lr.ph16, label %._crit_edge, !llvm.loop !14

._crit_edge19:                                    ; preds = %._crit_edge, %0
  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

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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


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