; LLVM IR from Shah-Shishir/Codeforces-Solutions :: 1011A. Stages (main)

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

@str = external dso_local global [55 x i8], align 16
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = 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
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  br label %8

8:                                                ; preds = %94, %0
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2, ptr noundef %3, ptr noundef @str)
  %10 = icmp ne i32 %9, -1
  br i1 %10, label %11, label %97

11:                                               ; preds = %8
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr @str, i64 %13
  call void @_ZSt4sortIPcEvT_S1_(ptr noundef @str, ptr noundef %14)
  store i32 1, ptr %6, align 4, !tbaa !5
  %15 = load i8, ptr @str, align 16, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = sub nsw i32 %16, 96
  store i32 %17, ptr %5, align 4, !tbaa !5
  %18 = load i8, ptr @str, align 16, !tbaa !9
  store i8 %18, ptr %7, align 1, !tbaa !9
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %55, %11
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = icmp slt i32 %20, %22
  br i1 %23, label %24, label %58

24:                                               ; preds = %19
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = icmp eq i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  br label %58

29:                                               ; preds = %24
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = load i8, ptr %7, align 1, !tbaa !9
  %36 = sext i8 %35 to i32
  %37 = sub nsw i32 %34, %36
  %38 = icmp sgt i32 %37, 1
  br i1 %38, label %39, label %54

39:                                               ; preds = %29
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !5
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !9
  %46 = sext i8 %45 to i32
  %47 = sub nsw i32 %46, 96
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, %47
  store i32 %49, ptr %5, align 4, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %51
  %53 = load i8, ptr %52, align 1, !tbaa !9
  store i8 %53, ptr %7, align 1, !tbaa !9
  br label %54

54:                                               ; preds = %39, %29
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !10

58:                                               ; preds = %28, %19
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = icmp ne i32 %59, %60
  br i1 %61, label %62, label %89

62:                                               ; preds = %58
  %63 = load i32, ptr %2, align 4, !tbaa !5
  %64 = sub nsw i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %65
  %67 = load i8, ptr %66, align 1, !tbaa !9
  %68 = sext i8 %67 to i32
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = sub nsw i32 %69, 2
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %71
  %73 = load i8, ptr %72, align 1, !tbaa !9
  %74 = sext i8 %73 to i32
  %75 = sub nsw i32 %68, %74
  %76 = icmp sgt i32 %75, 1
  br i1 %76, label %77, label %89

77:                                               ; preds = %62
  %78 = load i32, ptr %6, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %6, align 4, !tbaa !5
  %80 = load i32, ptr %2, align 4, !tbaa !5
  %81 = sub nsw i32 %80, 1
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %82
  %84 = load i8, ptr %83, align 1, !tbaa !9
  %85 = sext i8 %84 to i32
  %86 = sub nsw i32 %85, 96
  %87 = load i32, ptr %5, align 4, !tbaa !5
  %88 = add nsw i32 %87, %86
  store i32 %88, ptr %5, align 4, !tbaa !5
  br label %89

89:                                               ; preds = %77, %62, %58
  %90 = load i32, ptr %6, align 4, !tbaa !5
  %91 = load i32, ptr %3, align 4, !tbaa !5
  %92 = icmp ne i32 %90, %91
  br i1 %92, label %93, label %94

93:                                               ; preds = %89
  store i32 -1, ptr %5, align 4, !tbaa !5
  br label %94

94:                                               ; preds = %93, %89
  %95 = load i32, ptr %5, align 4, !tbaa !5
  %96 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %95)
  br label %8, !llvm.loop !12

97:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  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
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPcEvT_S1_(ptr noundef, ptr noundef) #3

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { inlinehint mustprogress 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 #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"

@str = external dso_local global [55 x i8], align 16
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1, ptr noundef nonnull %2, ptr noundef nonnull @str)
  %.not36 = icmp eq i32 %3, -1
  br i1 %.not36, label %._crit_edge39, label %.lr.ph38

.lr.ph38:                                         ; preds = %0, %43
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds i8, ptr @str, i64 %5
  call void @_ZSt4sortIPcEvT_S1_(ptr noundef nonnull @str, ptr noundef nonnull %6)
  %7 = load i8, ptr @str, align 16, !tbaa !9
  %8 = sext i8 %7 to i32
  %9 = add nsw i32 %8, -96
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = add nsw i32 %10, -1
  %12 = load i32, ptr %2, align 4
  %13 = icmp slt i32 %10, 3
  %14 = icmp eq i32 %12, 1
  %or.cond26 = select i1 %13, i1 true, i1 %14
  br i1 %or.cond26, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph38
  %15 = zext nneg i32 %11 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %.030 = phi i8 [ %7, %.lr.ph.preheader ], [ %.1, %.lr.ph ]
  %.01429 = phi i32 [ 1, %.lr.ph.preheader ], [ %.115, %.lr.ph ]
  %.01628 = phi i32 [ %9, %.lr.ph.preheader ], [ %.117, %.lr.ph ]
  %16 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %indvars.iv
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = sext i8 %17 to i32
  %19 = sext i8 %.030 to i32
  %20 = sub nsw i32 %18, %19
  %21 = icmp sgt i32 %20, 1
  %22 = add nsw i32 %18, -96
  %23 = select i1 %21, i32 %22, i32 0
  %.117 = add nsw i32 %23, %.01628
  %24 = zext i1 %21 to i32
  %.115 = add nuw nsw i32 %.01429, %24
  %.1 = select i1 %21, i8 %17, i8 %.030
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = icmp uge i64 %indvars.iv.next, %15
  %26 = icmp eq i32 %.115, %12
  %or.cond = select i1 %25, i1 true, i1 %26
  br i1 %or.cond, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph38
  %.016.lcssa = phi i32 [ %9, %.lr.ph38 ], [ %.117, %.lr.ph ]
  %.014.lcssa = phi i32 [ 1, %.lr.ph38 ], [ %.115, %.lr.ph ]
  %.lcssa = phi i1 [ %14, %.lr.ph38 ], [ %26, %.lr.ph ]
  br i1 %.lcssa, label %43, label %27

27:                                               ; preds = %._crit_edge
  %28 = sext i32 %11 to i64
  %29 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %31 = sext i8 %30 to i32
  %32 = add nsw i32 %10, -2
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [55 x i8], ptr @str, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = sext i8 %35 to i32
  %37 = sub nsw i32 %31, %36
  %38 = icmp sgt i32 %37, 1
  br i1 %38, label %39, label %43

39:                                               ; preds = %27
  %40 = add nuw nsw i32 %.014.lcssa, 1
  %41 = add i32 %.016.lcssa, -96
  %42 = add i32 %41, %31
  br label %43

43:                                               ; preds = %39, %27, %._crit_edge
  %.218 = phi i32 [ %42, %39 ], [ %.016.lcssa, %27 ], [ %.016.lcssa, %._crit_edge ]
  %.2 = phi i32 [ %40, %39 ], [ %.014.lcssa, %27 ], [ %.014.lcssa, %._crit_edge ]
  %.not21 = icmp eq i32 %.2, %12
  %spec.select = select i1 %.not21, i32 %.218, i32 -1
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %spec.select)
  %45 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1, ptr noundef nonnull %2, ptr noundef nonnull @str)
  %.not = icmp eq i32 %45, -1
  br i1 %.not, label %._crit_edge39, label %.lr.ph38, !llvm.loop !12

._crit_edge39:                                    ; preds = %43, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 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: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPcEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #3

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { inlinehint mustprogress 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 #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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass