; LLVM IR from tangjz/acm-icpc :: luogu (1198)

; 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
@d = external dso_local global i32, align 4
@q = external dso_local global [200010 x i32], align 16
@loc = external dso_local global [200010 x i32], align 16
@head = external dso_local global i32, align 4
@tail = external dso_local global i32, align 4
@len = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5queryi(i32 noundef) #0

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

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

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

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

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

12:                                               ; preds = %7
  %13 = getelementptr inbounds [10 x i8], ptr %2, i64 0, i64 0
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %13, ptr noundef %5)
  %15 = getelementptr inbounds [10 x i8], ptr %2, i64 0, i64 0
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = icmp eq i32 %17, 81
  br i1 %18, label %19, label %26

19:                                               ; preds = %12
  %20 = load i32, ptr @len, align 4, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = sub nsw i32 %20, %21
  %23 = add nsw i32 %22, 1
  %24 = call noundef i32 @_Z5queryi(i32 noundef %23)
  store i32 %24, ptr %4, align 4, !tbaa !5
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %24)
  br label %26

26:                                               ; preds = %19, %12
  %27 = getelementptr inbounds [10 x i8], ptr %2, i64 0, i64 0
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = sext i8 %28 to i32
  %30 = icmp eq i32 %29, 65
  br i1 %30, label %31, label %64

31:                                               ; preds = %26
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %32, %33
  %35 = load i32, ptr @d, align 4, !tbaa !5
  %36 = srem i32 %34, %35
  store i32 %36, ptr %5, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %50, %31
  %38 = load i32, ptr @head, align 4, !tbaa !5
  %39 = load i32, ptr @tail, align 4, !tbaa !5
  %40 = icmp sle i32 %38, %39
  br i1 %40, label %41, label %48

41:                                               ; preds = %37
  %42 = load i32, ptr @tail, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [200010 x i32], ptr @q, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = icmp slt i32 %45, %46
  br label %48

48:                                               ; preds = %41, %37
  %49 = phi i1 [ false, %37 ], [ %47, %41 ]
  br i1 %49, label %50, label %53

50:                                               ; preds = %48
  %51 = load i32, ptr @tail, align 4, !tbaa !5
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr @tail, align 4, !tbaa !5
  br label %37, !llvm.loop !10

53:                                               ; preds = %48
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = load i32, ptr @tail, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr @tail, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [200010 x i32], ptr @q, i64 0, i64 %57
  store i32 %54, ptr %58, align 4, !tbaa !5
  %59 = load i32, ptr @len, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr @len, align 4, !tbaa !5
  %61 = load i32, ptr @tail, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [200010 x i32], ptr @loc, i64 0, i64 %62
  store i32 %60, ptr %63, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %53, %26
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %3, align 4, !tbaa !5
  br label %7, !llvm.loop !12

68:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 10, ptr %2) #4
  ret i32 0
}

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

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

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

@n = external dso_local global i32, align 4
@d = external dso_local global i32, align 4
@q = external dso_local local_unnamed_addr global [200010 x i32], align 16
@loc = external dso_local local_unnamed_addr global [200010 x i32], align 16
@head = external dso_local local_unnamed_addr global i32, align 4
@tail = external dso_local local_unnamed_addr global i32, align 4
@len = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5queryi(i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca [10 x i8], align 1
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %1) #4
  store i32 1, ptr @head, align 4, !tbaa !5
  store i32 0, ptr @tail, align 4, !tbaa !5
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @d)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %.not9 = icmp slt i32 %4, 1
  br i1 %.not9, label %._crit_edge, label %.lr.ph12

._crit_edge:                                      ; preds = %38, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %1) #4
  ret i32 0

.lr.ph12:                                         ; preds = %0, %38
  %.011 = phi i32 [ %.1, %38 ], [ 0, %0 ]
  %.0310 = phi i32 [ %39, %38 ], [ 1, %0 ]
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %6 = load i8, ptr %1, align 1, !tbaa !9
  %7 = icmp eq i8 %6, 81
  br i1 %7, label %8, label %15

8:                                                ; preds = %.lr.ph12
  %9 = load i32, ptr @len, align 4, !tbaa !5
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = add i32 %9, 1
  %12 = sub i32 %11, %10
  %13 = call noundef i32 @_Z5queryi(i32 noundef %12)
  %14 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %13)
  %.pr = load i8, ptr %1, align 1, !tbaa !9
  br label %15

15:                                               ; preds = %8, %.lr.ph12
  %16 = phi i8 [ %.pr, %8 ], [ %6, %.lr.ph12 ]
  %.1 = phi i32 [ %13, %8 ], [ %.011, %.lr.ph12 ]
  %17 = icmp eq i8 %16, 65
  br i1 %17, label %18, label %38

18:                                               ; preds = %15
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = add nsw i32 %19, %.1
  %21 = load i32, ptr @d, align 4, !tbaa !5
  %22 = srem i32 %20, %21
  store i32 %22, ptr %2, align 4, !tbaa !5
  %23 = load i32, ptr @head, align 4, !tbaa !5
  %tail.promoted = load i32, ptr @tail, align 4, !tbaa !5
  %.not56 = icmp sgt i32 %23, %tail.promoted
  br i1 %.not56, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %18
  %24 = sext i32 %tail.promoted to i64
  %25 = sext i32 %23 to i64
  %26 = add i32 %23, -1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %30
  %indvars.iv = phi i64 [ %24, %.lr.ph.preheader ], [ %indvars.iv.next, %30 ]
  %27 = getelementptr inbounds [200010 x i32], ptr @q, i64 0, i64 %indvars.iv
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp slt i32 %28, %22
  br i1 %29, label %30, label %.critedge.loopexit.split.loop.exit14

30:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not5.not = icmp sgt i64 %indvars.iv, %25
  br i1 %.not5.not, label %.lr.ph, label %.critedge, !llvm.loop !10

.critedge.loopexit.split.loop.exit14:             ; preds = %.lr.ph
  %31 = trunc nsw i64 %indvars.iv to i32
  br label %.critedge

.critedge:                                        ; preds = %30, %.critedge.loopexit.split.loop.exit14, %18
  %.lcssa = phi i32 [ %tail.promoted, %18 ], [ %31, %.critedge.loopexit.split.loop.exit14 ], [ %26, %30 ]
  %32 = add nsw i32 %.lcssa, 1
  store i32 %32, ptr @tail, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [200010 x i32], ptr @q, i64 0, i64 %33
  store i32 %22, ptr %34, align 4, !tbaa !5
  %35 = load i32, ptr @len, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr @len, align 4, !tbaa !5
  %37 = getelementptr inbounds [200010 x i32], ptr @loc, i64 0, i64 %33
  store i32 %36, ptr %37, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %15, %.critedge
  %39 = add nuw nsw i32 %.0310, 1
  %40 = load i32, ptr @n, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.0310, %40
  br i1 %.not.not, label %.lr.ph12, label %._crit_edge, !llvm.loop !12
}

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

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

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 #3 = { 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 #4 = { 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}


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