; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/299 Division 1/Problem B (B)

; 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
@z = external dso_local global [1001001 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7build_zNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %8 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %9 = trunc i64 %8 to i32
  store i32 %9, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %10 = load i32, ptr %3, align 4, !tbaa !9
  store i32 %10, ptr @z, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %106, %1
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %3, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %11
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %20, label %52

20:                                               ; preds = %16
  %21 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %21, ptr %5, align 4, !tbaa !9
  store i32 %21, ptr %4, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %42, %20
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr @n, align 4, !tbaa !9
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %40

26:                                               ; preds = %22
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = sub nsw i32 %27, %28
  %30 = sext i32 %29 to i64
  %31 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %30)
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = sext i8 %32 to i32
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %35)
  %37 = load i8, ptr %36, align 1, !tbaa !11
  %38 = sext i8 %37 to i32
  %39 = icmp eq i32 %33, %38
  br label %40

40:                                               ; preds = %26, %22
  %41 = phi i1 [ false, %22 ], [ %39, %26 ]
  br i1 %41, label %42, label %45

42:                                               ; preds = %40
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !9
  br label %22, !llvm.loop !12

45:                                               ; preds = %40
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = sub nsw i32 %46, %47
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %50
  store i32 %48, ptr %51, align 4, !tbaa !9
  br label %105

52:                                               ; preds = %16
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = load i32, ptr %4, align 4, !tbaa !9
  %55 = sub nsw i32 %53, %54
  store i32 %55, ptr %6, align 4, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !9
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = sub nsw i32 %60, %61
  %63 = icmp slt i32 %59, %62
  br i1 %63, label %64, label %72

64:                                               ; preds = %52
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %70
  store i32 %68, ptr %71, align 4, !tbaa !9
  br label %104

72:                                               ; preds = %52
  %73 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %73, ptr %4, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %94, %72
  %75 = load i32, ptr %5, align 4, !tbaa !9
  %76 = load i32, ptr @n, align 4, !tbaa !9
  %77 = icmp slt i32 %75, %76
  br i1 %77, label %78, label %92

78:                                               ; preds = %74
  %79 = load i32, ptr %5, align 4, !tbaa !9
  %80 = load i32, ptr %4, align 4, !tbaa !9
  %81 = sub nsw i32 %79, %80
  %82 = sext i32 %81 to i64
  %83 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %82)
  %84 = load i8, ptr %83, align 1, !tbaa !11
  %85 = sext i8 %84 to i32
  %86 = load i32, ptr %5, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  %88 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %87)
  %89 = load i8, ptr %88, align 1, !tbaa !11
  %90 = sext i8 %89 to i32
  %91 = icmp eq i32 %85, %90
  br label %92

92:                                               ; preds = %78, %74
  %93 = phi i1 [ false, %74 ], [ %91, %78 ]
  br i1 %93, label %94, label %97

94:                                               ; preds = %92
  %95 = load i32, ptr %5, align 4, !tbaa !9
  %96 = add nsw i32 %95, 1
  store i32 %96, ptr %5, align 4, !tbaa !9
  br label %74, !llvm.loop !14

97:                                               ; preds = %92
  %98 = load i32, ptr %5, align 4, !tbaa !9
  %99 = load i32, ptr %4, align 4, !tbaa !9
  %100 = sub nsw i32 %98, %99
  %101 = load i32, ptr %7, align 4, !tbaa !9
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %102
  store i32 %100, ptr %103, align 4, !tbaa !9
  br label %104

104:                                              ; preds = %97, %64
  br label %105

105:                                              ; preds = %104, %45
  br label %106

106:                                              ; preds = %105
  %107 = load i32, ptr %7, align 4, !tbaa !9
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %7, align 4, !tbaa !9
  br label %11, !llvm.loop !15

109:                                              ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  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
  ret void
}

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

; Function Attrs: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) #2 align 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 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 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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


; 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 local_unnamed_addr global i32, align 4
@z = external dso_local local_unnamed_addr global [1001001 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7build_zNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE(ptr noundef nonnull %0) local_unnamed_addr #0 {
  %2 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #1
  %3 = trunc i64 %2 to i32
  store i32 %3, ptr @z, align 16, !tbaa !5
  %4 = icmp sgt i32 %3, 1
  br i1 %4, label %.lr.ph48.preheader, label %._crit_edge

.lr.ph48.preheader:                               ; preds = %1
  %wide.trip.count = and i64 %2, 2147483647
  br label %.lr.ph48

._crit_edge:                                      ; preds = %43, %1
  ret void

.lr.ph48:                                         ; preds = %.lr.ph48.preheader, %43
  %indvars.iv = phi i64 [ 1, %.lr.ph48.preheader ], [ %indvars.iv.next, %43 ]
  %.03346 = phi i32 [ 0, %.lr.ph48.preheader ], [ %.1, %43 ]
  %.03445 = phi i32 [ 0, %.lr.ph48.preheader ], [ %.3, %43 ]
  %indvars57 = trunc i64 %indvars.iv to i32
  %5 = sext i32 %.03445 to i64
  %.not = icmp slt i64 %indvars.iv, %5
  br i1 %.not, label %21, label %.preheader36

.preheader36:                                     ; preds = %.lr.ph48
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = icmp slt i64 %indvars.iv, %7
  br i1 %8, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %.preheader36, %15
  %indvars.iv49 = phi i64 [ %indvars.iv.next50, %15 ], [ %indvars.iv, %.preheader36 ]
  %9 = sub nuw nsw i64 %indvars.iv49, %indvars.iv
  %10 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %9)
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %indvars.iv49)
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp eq i8 %11, %13
  br i1 %14, label %15, label %.critedge.loopexit

15:                                               ; preds = %.lr.ph
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next50, %17
  br i1 %18, label %.lr.ph, label %.critedge.loopexit, !llvm.loop !10

.critedge.loopexit:                               ; preds = %15, %.lr.ph
  %.135.lcssa.ph.in = phi i64 [ %indvars.iv49, %.lr.ph ], [ %indvars.iv.next50, %15 ]
  %.135.lcssa.ph = trunc i64 %.135.lcssa.ph.in to i32
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %.preheader36
  %.135.lcssa = phi i32 [ %indvars57, %.preheader36 ], [ %.135.lcssa.ph, %.critedge.loopexit ]
  %19 = sub nsw i32 %.135.lcssa, %indvars57
  %20 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %indvars.iv
  store i32 %19, ptr %20, align 4, !tbaa !5
  br label %43

21:                                               ; preds = %.lr.ph48
  %22 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %indvars.iv
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sub nsw i32 %.03445, %indvars57
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %28, label %.preheader

.preheader:                                       ; preds = %21
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = icmp slt i32 %.03445, %26
  br i1 %27, label %.lr.ph41, label %.critedge2

28:                                               ; preds = %21
  %29 = sext i32 %.03346 to i64
  %30 = sub nsw i64 %indvars.iv, %29
  %31 = getelementptr inbounds [1001001 x i32], ptr @z, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  store i32 %32, ptr %22, align 4, !tbaa !5
  br label %43

.lr.ph41:                                         ; preds = %.preheader, %39
  %indvars.iv52 = phi i64 [ %indvars.iv.next53, %39 ], [ %5, %.preheader ]
  %33 = sub nsw i64 %indvars.iv52, %indvars.iv
  %34 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %33)
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %indvars.iv52)
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %38 = icmp eq i8 %35, %37
  br i1 %38, label %39, label %.critedge2.loopexit

39:                                               ; preds = %.lr.ph41
  %indvars.iv.next53 = add nsw i64 %indvars.iv52, 1
  %40 = load i32, ptr @n, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = icmp slt i64 %indvars.iv.next53, %41
  br i1 %42, label %.lr.ph41, label %.critedge2.loopexit, !llvm.loop !12

.critedge2.loopexit:                              ; preds = %39, %.lr.ph41
  %.2.lcssa.ph.in = phi i64 [ %indvars.iv52, %.lr.ph41 ], [ %indvars.iv.next53, %39 ]
  %.2.lcssa.ph = trunc i64 %.2.lcssa.ph.in to i32
  %.pre = sub nsw i32 %.2.lcssa.ph, %indvars57
  br label %.critedge2

.critedge2:                                       ; preds = %.critedge2.loopexit, %.preheader
  %.pre-phi = phi i32 [ %.pre, %.critedge2.loopexit ], [ %24, %.preheader ]
  %.2.lcssa = phi i32 [ %.2.lcssa.ph, %.critedge2.loopexit ], [ %.03445, %.preheader ]
  store i32 %.pre-phi, ptr %22, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %.critedge, %.critedge2, %28
  %.3 = phi i32 [ %.135.lcssa, %.critedge ], [ %.03445, %28 ], [ %.2.lcssa, %.critedge2 ]
  %.1 = phi i32 [ %indvars57, %.critedge ], [ %.03346, %28 ], [ %indvars57, %.critedge2 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph48, !llvm.loop !13
}

; Function Attrs: mustprogress nounwind uwtable
declare noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32), i64 noundef) local_unnamed_addr #0 align 2

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 = { 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}


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