; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Good Bye 2015/Problem D (D)

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

@z = external dso_local global [5100 x [5100 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7build_zNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %10 = call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #3
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %14
  %16 = getelementptr inbounds [5100 x i32], ptr %15, i64 0, i64 0
  store i32 %12, ptr %16, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %132, %2
  %18 = load i32, ptr %9, align 4, !tbaa !9
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %135

22:                                               ; preds = %17
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %63

26:                                               ; preds = %22
  %27 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %27, ptr %7, align 4, !tbaa !9
  store i32 %27, ptr %6, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %48, %26
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %46

32:                                               ; preds = %28
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sub nsw i32 %33, %34
  %36 = sext i32 %35 to i64
  %37 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %36)
  %38 = load i8, ptr %37, align 1, !tbaa !11
  %39 = sext i8 %38 to i32
  %40 = load i32, ptr %7, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %41)
  %43 = load i8, ptr %42, align 1, !tbaa !11
  %44 = sext i8 %43 to i32
  %45 = icmp eq i32 %39, %44
  br label %46

46:                                               ; preds = %32, %28
  %47 = phi i1 [ false, %28 ], [ %45, %32 ]
  br i1 %47, label %48, label %51

48:                                               ; preds = %46
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %7, align 4, !tbaa !9
  br label %28, !llvm.loop !12

51:                                               ; preds = %46
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = sub nsw i32 %52, %53
  %55 = load i32, ptr %4, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %56
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [5100 x i32], ptr %57, i64 0, i64 %59
  store i32 %54, ptr %60, align 4, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = add nsw i32 %61, -1
  store i32 %62, ptr %7, align 4, !tbaa !9
  br label %131

63:                                               ; preds = %22
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = sub nsw i32 %64, %65
  store i32 %66, ptr %8, align 4, !tbaa !9
  %67 = load i32, ptr %4, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %68
  %70 = load i32, ptr %8, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [5100 x i32], ptr %69, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !9
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = load i32, ptr %9, align 4, !tbaa !9
  %76 = sub nsw i32 %74, %75
  %77 = add nsw i32 %76, 1
  %78 = icmp slt i32 %73, %77
  br i1 %78, label %79, label %93

79:                                               ; preds = %63
  %80 = load i32, ptr %4, align 4, !tbaa !9
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %81
  %83 = load i32, ptr %8, align 4, !tbaa !9
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [5100 x i32], ptr %82, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !9
  %87 = load i32, ptr %4, align 4, !tbaa !9
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %88
  %90 = load i32, ptr %9, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [5100 x i32], ptr %89, i64 0, i64 %91
  store i32 %86, ptr %92, align 4, !tbaa !9
  br label %130

93:                                               ; preds = %63
  %94 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %94, ptr %6, align 4, !tbaa !9
  br label %95

95:                                               ; preds = %115, %93
  %96 = load i32, ptr %7, align 4, !tbaa !9
  %97 = load i32, ptr %5, align 4, !tbaa !9
  %98 = icmp slt i32 %96, %97
  br i1 %98, label %99, label %113

99:                                               ; preds = %95
  %100 = load i32, ptr %7, align 4, !tbaa !9
  %101 = load i32, ptr %6, align 4, !tbaa !9
  %102 = sub nsw i32 %100, %101
  %103 = sext i32 %102 to i64
  %104 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %103)
  %105 = load i8, ptr %104, align 1, !tbaa !11
  %106 = sext i8 %105 to i32
  %107 = load i32, ptr %7, align 4, !tbaa !9
  %108 = sext i32 %107 to i64
  %109 = call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %108)
  %110 = load i8, ptr %109, align 1, !tbaa !11
  %111 = sext i8 %110 to i32
  %112 = icmp eq i32 %106, %111
  br label %113

113:                                              ; preds = %99, %95
  %114 = phi i1 [ false, %95 ], [ %112, %99 ]
  br i1 %114, label %115, label %118

115:                                              ; preds = %113
  %116 = load i32, ptr %7, align 4, !tbaa !9
  %117 = add nsw i32 %116, 1
  store i32 %117, ptr %7, align 4, !tbaa !9
  br label %95, !llvm.loop !14

118:                                              ; preds = %113
  %119 = load i32, ptr %7, align 4, !tbaa !9
  %120 = load i32, ptr %6, align 4, !tbaa !9
  %121 = sub nsw i32 %119, %120
  %122 = load i32, ptr %4, align 4, !tbaa !9
  %123 = sext i32 %122 to i64
  %124 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %123
  %125 = load i32, ptr %9, align 4, !tbaa !9
  %126 = sext i32 %125 to i64
  %127 = getelementptr inbounds [5100 x i32], ptr %124, i64 0, i64 %126
  store i32 %121, ptr %127, align 4, !tbaa !9
  %128 = load i32, ptr %7, align 4, !tbaa !9
  %129 = add nsw i32 %128, -1
  store i32 %129, ptr %7, align 4, !tbaa !9
  br label %130

130:                                              ; preds = %118, %79
  br label %131

131:                                              ; preds = %130, %51
  br label %132

132:                                              ; preds = %131
  %133 = load i32, ptr %9, align 4, !tbaa !9
  %134 = add nsw i32 %133, 1
  store i32 %134, ptr %9, align 4, !tbaa !9
  br label %17, !llvm.loop !15

135:                                              ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #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"

@z = external dso_local local_unnamed_addr global [5100 x [5100 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z7build_zNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr noundef nonnull %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noundef i64 @_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6lengthEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #1
  %4 = trunc i64 %3 to i32
  %5 = sext i32 %1 to i64
  %6 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %5
  store i32 %4, ptr %6, align 16, !tbaa !5
  %7 = icmp sgt i32 %4, 1
  br i1 %7, label %.lr.ph60.preheader, label %._crit_edge

.lr.ph60.preheader:                               ; preds = %2
  %8 = and i64 %3, 2147483647
  %wide.trip.count69 = and i64 %3, 2147483647
  br label %.lr.ph60

._crit_edge:                                      ; preds = %41, %2
  ret void

.lr.ph60:                                         ; preds = %.lr.ph60.preheader, %41
  %indvars.iv62 = phi i64 [ 1, %.lr.ph60.preheader ], [ %indvars.iv.next63, %41 ]
  %.04358 = phi i32 [ 0, %.lr.ph60.preheader ], [ %.1, %41 ]
  %.04457 = phi i32 [ 0, %.lr.ph60.preheader ], [ %.3, %41 ]
  %indvars68 = trunc i64 %indvars.iv62 to i32
  %9 = sext i32 %.04457 to i64
  %10 = icmp sgt i64 %indvars.iv62, %9
  br i1 %10, label %.lr.ph53, label %22

.lr.ph53:                                         ; preds = %.lr.ph60, %17
  %indvars.iv64 = phi i64 [ %indvars.iv.next65, %17 ], [ %indvars.iv62, %.lr.ph60 ]
  %11 = sub nuw nsw i64 %indvars.iv64, %indvars.iv62
  %12 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %11)
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %indvars.iv64)
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = icmp eq i8 %13, %15
  br i1 %16, label %17, label %.critedge.split.loop.exit

17:                                               ; preds = %.lr.ph53
  %indvars.iv.next65 = add nuw nsw i64 %indvars.iv64, 1
  %exitcond67.not = icmp eq i64 %indvars.iv.next65, %wide.trip.count69
  br i1 %exitcond67.not, label %.critedge, label %.lr.ph53, !llvm.loop !10

.critedge.split.loop.exit:                        ; preds = %.lr.ph53
  %18 = trunc nuw nsw i64 %indvars.iv64 to i32
  br label %.critedge

.critedge:                                        ; preds = %17, %.critedge.split.loop.exit
  %.145.lcssa.ph = phi i32 [ %18, %.critedge.split.loop.exit ], [ %4, %17 ]
  %19 = sub nsw i32 %.145.lcssa.ph, %indvars68
  %20 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %5, i64 %indvars.iv62
  store i32 %19, ptr %20, align 4, !tbaa !5
  %21 = add nsw i32 %.145.lcssa.ph, -1
  br label %41

22:                                               ; preds = %.lr.ph60
  %23 = sext i32 %.04358 to i64
  %24 = sub nsw i64 %indvars.iv62, %23
  %25 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %5, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = sub nsw i32 %.04457, %indvars68
  %.not = icmp sgt i32 %26, %27
  br i1 %.not, label %.preheader48, label %29

.preheader48:                                     ; preds = %22
  %28 = icmp slt i32 %.04457, %4
  br i1 %28, label %.lr.ph, label %.critedge2

29:                                               ; preds = %22
  %30 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %5, i64 %indvars.iv62
  store i32 %26, ptr %30, align 4, !tbaa !5
  br label %41

.lr.ph:                                           ; preds = %.preheader48, %37
  %indvars.iv = phi i64 [ %indvars.iv.next, %37 ], [ %9, %.preheader48 ]
  %31 = sub nsw i64 %indvars.iv, %indvars.iv62
  %32 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %31)
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = tail call noundef nonnull align 1 dereferenceable(1) ptr @_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEixEm(ptr noundef nonnull align 8 dereferenceable(32) %0, i64 noundef %indvars.iv)
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = icmp eq i8 %33, %35
  br i1 %36, label %37, label %.critedge2.loopexit.split.loop.exit71

37:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %8
  br i1 %exitcond.not, label %.critedge2.loopexit, label %.lr.ph, !llvm.loop !12

.critedge2.loopexit.split.loop.exit71:            ; preds = %.lr.ph
  %38 = trunc nsw i64 %indvars.iv to i32
  br label %.critedge2.loopexit

.critedge2.loopexit:                              ; preds = %37, %.critedge2.loopexit.split.loop.exit71
  %.2.lcssa.ph = phi i32 [ %38, %.critedge2.loopexit.split.loop.exit71 ], [ %4, %37 ]
  %.pre = sub nsw i32 %.2.lcssa.ph, %indvars68
  br label %.critedge2

.critedge2:                                       ; preds = %.critedge2.loopexit, %.preheader48
  %.pre-phi = phi i32 [ %.pre, %.critedge2.loopexit ], [ %27, %.preheader48 ]
  %.2.lcssa = phi i32 [ %.2.lcssa.ph, %.critedge2.loopexit ], [ %.04457, %.preheader48 ]
  %39 = getelementptr inbounds [5100 x [5100 x i32]], ptr @z, i64 0, i64 %5, i64 %indvars.iv62
  store i32 %.pre-phi, ptr %39, align 4, !tbaa !5
  %40 = add nsw i32 %.2.lcssa, -1
  br label %41

41:                                               ; preds = %.critedge, %.critedge2, %29
  %.3 = phi i32 [ %21, %.critedge ], [ %.04457, %29 ], [ %40, %.critedge2 ]
  %.1 = phi i32 [ %indvars68, %.critedge ], [ %.04358, %29 ], [ %indvars68, %.critedge2 ]
  %indvars.iv.next63 = add nuw nsw i64 %indvars.iv62, 1
  %exitcond70.not = icmp eq i64 %indvars.iv.next63, %wide.trip.count69
  br i1 %exitcond70.not, label %._crit_edge, label %.lr.ph60, !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,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass