; LLVM IR from TLE-MEC/Hack-CP-DSA :: Codeforces/cupboards (solution)

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

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %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
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %13 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %2)
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = zext i32 %14 to i64
  %16 = call ptr @llvm.stacksave.p0()
  store ptr %16, ptr %3, align 8
  %17 = alloca [2 x i32], i64 %15, align 16
  store i64 %15, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %18

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

22:                                               ; preds = %18
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %98

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %91, %23
  %25 = load i32, ptr %11, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 2
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %94

28:                                               ; preds = %24
  %29 = load i32, ptr %9, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2 x i32], ptr %17, i64 %30
  %32 = load i32, ptr %11, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [2 x i32], ptr %31, i64 0, i64 %33
  %35 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %34)
  %36 = load i32, ptr %11, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %48

38:                                               ; preds = %28
  %39 = load i32, ptr %9, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [2 x i32], ptr %17, i64 %40
  %42 = getelementptr inbounds [2 x i32], ptr %41, i64 0, i64 0
  %43 = load i32, ptr %42, align 8, !tbaa !5
  %44 = icmp eq i32 %43, 1
  br i1 %44, label %45, label %48

45:                                               ; preds = %38
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %5, align 4, !tbaa !5
  br label %90

48:                                               ; preds = %38, %28
  %49 = load i32, ptr %11, align 4, !tbaa !5
  %50 = icmp eq i32 %49, 0
  br i1 %50, label %51, label %61

51:                                               ; preds = %48
  %52 = load i32, ptr %9, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [2 x i32], ptr %17, i64 %53
  %55 = getelementptr inbounds [2 x i32], ptr %54, i64 0, i64 0
  %56 = load i32, ptr %55, align 8, !tbaa !5
  %57 = icmp eq i32 %56, 0
  br i1 %57, label %58, label %61

58:                                               ; preds = %51
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %6, align 4, !tbaa !5
  br label %89

61:                                               ; preds = %51, %48
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = icmp eq i32 %62, 1
  br i1 %63, label %64, label %74

64:                                               ; preds = %61
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [2 x i32], ptr %17, i64 %66
  %68 = getelementptr inbounds [2 x i32], ptr %67, i64 0, i64 1
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = icmp eq i32 %69, 1
  br i1 %70, label %71, label %74

71:                                               ; preds = %64
  %72 = load i32, ptr %8, align 4, !tbaa !5
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %8, align 4, !tbaa !5
  br label %88

74:                                               ; preds = %64, %61
  %75 = load i32, ptr %11, align 4, !tbaa !5
  %76 = icmp eq i32 %75, 1
  br i1 %76, label %77, label %87

77:                                               ; preds = %74
  %78 = load i32, ptr %9, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [2 x i32], ptr %17, i64 %79
  %81 = getelementptr inbounds [2 x i32], ptr %80, i64 0, i64 1
  %82 = load i32, ptr %81, align 4, !tbaa !5
  %83 = icmp eq i32 %82, 0
  br i1 %83, label %84, label %87

84:                                               ; preds = %77
  %85 = load i32, ptr %7, align 4, !tbaa !5
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %7, align 4, !tbaa !5
  br label %87

87:                                               ; preds = %84, %77, %74
  br label %88

88:                                               ; preds = %87, %71
  br label %89

89:                                               ; preds = %88, %58
  br label %90

90:                                               ; preds = %89, %45
  br label %91

91:                                               ; preds = %90
  %92 = load i32, ptr %11, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %11, align 4, !tbaa !5
  br label %24, !llvm.loop !9

94:                                               ; preds = %27
  br label %95

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

98:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !5
  %99 = load i32, ptr %5, align 4, !tbaa !5
  %100 = load i32, ptr %6, align 4, !tbaa !5
  %101 = icmp sge i32 %99, %100
  br i1 %101, label %102, label %108

102:                                              ; preds = %98
  %103 = load i32, ptr %12, align 4, !tbaa !5
  %104 = load i32, ptr %2, align 4, !tbaa !5
  %105 = add nsw i32 %103, %104
  %106 = load i32, ptr %5, align 4, !tbaa !5
  %107 = sub nsw i32 %105, %106
  store i32 %107, ptr %12, align 4, !tbaa !5
  br label %114

108:                                              ; preds = %98
  %109 = load i32, ptr %12, align 4, !tbaa !5
  %110 = load i32, ptr %2, align 4, !tbaa !5
  %111 = add nsw i32 %109, %110
  %112 = load i32, ptr %6, align 4, !tbaa !5
  %113 = sub nsw i32 %111, %112
  store i32 %113, ptr %12, align 4, !tbaa !5
  br label %114

114:                                              ; preds = %108, %102
  %115 = load i32, ptr %8, align 4, !tbaa !5
  %116 = load i32, ptr %7, align 4, !tbaa !5
  %117 = icmp sge i32 %115, %116
  br i1 %117, label %118, label %124

118:                                              ; preds = %114
  %119 = load i32, ptr %12, align 4, !tbaa !5
  %120 = load i32, ptr %2, align 4, !tbaa !5
  %121 = add nsw i32 %119, %120
  %122 = load i32, ptr %8, align 4, !tbaa !5
  %123 = sub nsw i32 %121, %122
  store i32 %123, ptr %12, align 4, !tbaa !5
  br label %130

124:                                              ; preds = %114
  %125 = load i32, ptr %12, align 4, !tbaa !5
  %126 = load i32, ptr %2, align 4, !tbaa !5
  %127 = add nsw i32 %125, %126
  %128 = load i32, ptr %7, align 4, !tbaa !5
  %129 = sub nsw i32 %127, %128
  store i32 %129, ptr %12, align 4, !tbaa !5
  br label %130

130:                                              ; preds = %124, %118
  %131 = load i32, ptr %12, align 4, !tbaa !5
  %132 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %131)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, 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
  %133 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %133)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %134 = load i32, ptr %1, align 4
  ret i32 %134
}

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

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

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

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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 = { nocallback nofree nosync nounwind willreturn }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  %2 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = zext i32 %3 to i64
  %5 = call ptr @llvm.stacksave.p0()
  %6 = alloca [2 x i32], i64 %4, align 16
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.preheader.preheader, label %._crit_edge

._crit_edge:                                      ; preds = %.critedge48.1, %0
  %.042.lcssa = phi i32 [ 0, %0 ], [ %.244, %.critedge48.1 ]
  %.039.lcssa = phi i32 [ 0, %0 ], [ %.241, %.critedge48.1 ]
  %.036.lcssa = phi i32 [ 0, %0 ], [ %spec.select.1, %.critedge48.1 ]
  %.034.lcssa = phi i32 [ 0, %0 ], [ %.2.1, %.critedge48.1 ]
  %.lcssa = phi i32 [ %7, %0 ], [ %25, %.critedge48.1 ]
  %.039..042 = call i32 @llvm.smax.i32(i32 %.042.lcssa, i32 %.039.lcssa)
  %.0 = sub nsw i32 %.lcssa, %.039..042
  %.1.v.v = call i32 @llvm.smax.i32(i32 %.034.lcssa, i32 %.036.lcssa)
  %.1.v = sub i32 %.lcssa, %.1.v.v
  %.1 = add i32 %.1.v, %.0
  %9 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %.1)
  call void @llvm.stackrestore.p0(ptr %5)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  ret i32 0

.preheader.preheader:                             ; preds = %0, %.critedge48.1
  %indvars.iv = phi i64 [ %indvars.iv.next, %.critedge48.1 ], [ 0, %0 ]
  %.03457 = phi i32 [ %.2.1, %.critedge48.1 ], [ 0, %0 ]
  %.03656 = phi i32 [ %spec.select.1, %.critedge48.1 ], [ 0, %0 ]
  %.03955 = phi i32 [ %.241, %.critedge48.1 ], [ 0, %0 ]
  %.04254 = phi i32 [ %.244, %.critedge48.1 ], [ 0, %0 ]
  %10 = getelementptr inbounds [2 x i32], ptr %6, i64 %indvars.iv
  %11 = getelementptr inbounds i8, ptr %10, i64 4
  %12 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %10)
  %13 = load i32, ptr %10, align 8, !tbaa !5
  switch i32 %13, label %.critedge48.1 [
    i32 1, label %14
    i32 0, label %16
  ]

14:                                               ; preds = %.preheader.preheader
  %15 = add nsw i32 %.04254, 1
  br label %.critedge48.1

16:                                               ; preds = %.preheader.preheader
  %17 = add nsw i32 %.03955, 1
  br label %.critedge48.1

.critedge48.1:                                    ; preds = %16, %14, %.preheader.preheader
  %.244 = phi i32 [ %15, %14 ], [ %.04254, %16 ], [ %.04254, %.preheader.preheader ]
  %.241 = phi i32 [ %.03955, %14 ], [ %17, %16 ], [ %.03955, %.preheader.preheader ]
  %18 = getelementptr inbounds [2 x i32], ptr %6, i64 %indvars.iv, i64 1
  %19 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %18)
  %20 = load i32, ptr %11, align 4, !tbaa !5
  %21 = icmp eq i32 %20, 1
  %22 = icmp eq i32 %20, 0
  %23 = zext i1 %22 to i32
  %spec.select.1 = add nuw nsw i32 %.03656, %23
  %24 = zext i1 %21 to i32
  %.2.1 = add nuw nsw i32 %.03457, %24
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = icmp slt i64 %indvars.iv.next, %26
  br i1 %27, label %.preheader.preheader, label %._crit_edge, !llvm.loop !9
}

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

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

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

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

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 = { "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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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