; LLVM IR from AdarshGaur/Competitive-Programming :: Codeforces/VK Cup 2012 Finals, Practice Session/A. Multicolored Marbles.ll (Z5solvev)

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

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %12 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = add nsw i64 %13, 1
  %15 = call ptr @llvm.stacksave.p0()
  store ptr %15, ptr %2, align 8
  %16 = alloca i64, i64 %14, align 16
  store i64 %14, ptr %3, align 8
  %17 = getelementptr inbounds i64, ptr %16, i64 0
  store i64 0, ptr %17, align 16, !tbaa !5
  %18 = getelementptr inbounds i64, ptr %16, i64 1
  store i64 1, ptr %18, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 2, ptr %6, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %59, %0
  %20 = load i64, ptr %6, align 8, !tbaa !5
  %21 = load i64, ptr %1, align 8, !tbaa !5
  %22 = icmp sle i64 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %62

24:                                               ; preds = %19
  %25 = load i64, ptr %6, align 8, !tbaa !5
  %26 = and i64 %25, 1
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %24
  %29 = load i64, ptr %5, align 8, !tbaa !5
  br label %32

30:                                               ; preds = %24
  %31 = load i64, ptr %4, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %30, %28
  %33 = phi i64 [ %29, %28 ], [ %31, %30 ]
  %34 = add nsw i64 %33, 1
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds i64, ptr %16, i64 %35
  store i64 %34, ptr %36, align 8, !tbaa !5
  %37 = load i64, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds i64, ptr %16, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = srem i64 %39, 1000000007
  store i64 %40, ptr %38, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = and i64 %41, 1
  %43 = icmp ne i64 %42, 0
  br i1 %43, label %44, label %51

44:                                               ; preds = %32
  %45 = load i64, ptr %4, align 8, !tbaa !5
  %46 = load i64, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds i64, ptr %16, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !5
  %49 = add nsw i64 %45, %48
  %50 = srem i64 %49, 1000000007
  store i64 %50, ptr %4, align 8, !tbaa !5
  br label %58

51:                                               ; preds = %32
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = load i64, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds i64, ptr %16, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !5
  %56 = add nsw i64 %52, %55
  %57 = srem i64 %56, 1000000007
  store i64 %57, ptr %5, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %51, %44
  br label %59

59:                                               ; preds = %58
  %60 = load i64, ptr %6, align 8, !tbaa !5
  %61 = add nsw i64 %60, 1
  store i64 %61, ptr %6, align 8, !tbaa !5
  br label %19, !llvm.loop !9

62:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store ptr %16, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %63 = load ptr, ptr %8, align 8, !tbaa !11
  store ptr %63, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %64 = load ptr, ptr %8, align 8, !tbaa !11
  %65 = mul nuw i64 8, %14
  %66 = udiv i64 %65, 8
  %67 = getelementptr inbounds i64, ptr %64, i64 %66
  store ptr %67, ptr %10, align 8, !tbaa !11
  br label %68

68:                                               ; preds = %80, %62
  %69 = load ptr, ptr %9, align 8, !tbaa !11
  %70 = load ptr, ptr %10, align 8, !tbaa !11
  %71 = icmp ne ptr %69, %70
  br i1 %71, label %73, label %72

72:                                               ; preds = %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %83

73:                                               ; preds = %68
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %74 = load ptr, ptr %9, align 8, !tbaa !11
  %75 = load i64, ptr %74, align 8, !tbaa !5
  store i64 %75, ptr %11, align 8, !tbaa !5
  %76 = load i64, ptr %7, align 8, !tbaa !5
  %77 = load i64, ptr %11, align 8, !tbaa !5
  %78 = add nsw i64 %76, %77
  %79 = srem i64 %78, 1000000007
  store i64 %79, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %80

80:                                               ; preds = %73
  %81 = load ptr, ptr %9, align 8, !tbaa !11
  %82 = getelementptr inbounds i64, ptr %81, i32 1
  store ptr %82, ptr %9, align 8, !tbaa !11
  br label %68

83:                                               ; preds = %72
  %84 = load i64, ptr %7, align 8, !tbaa !5
  %85 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %84)
  %86 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %85, ptr noundef @.str)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %87 = load ptr, ptr %2, align 8
  call void @llvm.stackrestore.p0(ptr %87)
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) #2 align 2

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #2 align 2

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}


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

@.str = external hidden unnamed_addr constant [2 x i8], align 1
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #2 {
  %1 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %2 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %1)
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = add nsw i64 %3, 1
  %5 = call ptr @llvm.stacksave.p0()
  %6 = alloca i64, i64 %4, align 16
  store i64 0, ptr %6, align 16, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  store i64 1, ptr %7, align 8, !tbaa !5
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %.not28 = icmp slt i64 %8, 2
  br i1 %.not28, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %23, %0
  %.not2632 = icmp eq i64 %4, 0
  br i1 %.not2632, label %._crit_edge37, label %.lr.ph36.preheader

.lr.ph36.preheader:                               ; preds = %._crit_edge
  %9 = and i64 %3, 2305843009213693951
  %10 = add nuw nsw i64 %9, 1
  %xtraiter = and i64 %10, 3
  %11 = icmp ult i64 %9, 3
  br i1 %11, label %._crit_edge37.loopexit.unr-lcssa, label %.lr.ph36.preheader.new

.lr.ph36.preheader.new:                           ; preds = %.lr.ph36.preheader
  %unroll_iter = and i64 %10, 4611686018427387900
  br label %.lr.ph36

.lr.ph:                                           ; preds = %0, %23
  %.031 = phi i64 [ %.1, %23 ], [ 1, %0 ]
  %.02130 = phi i64 [ %.122, %23 ], [ 0, %0 ]
  %.02429 = phi i64 [ %24, %23 ], [ 2, %0 ]
  %12 = and i64 %.02429, 1
  %.not27 = icmp eq i64 %12, 0
  %13 = select i1 %.not27, i64 %.031, i64 %.02130
  %14 = add nsw i64 %13, 1
  %15 = getelementptr inbounds i64, ptr %6, i64 %.02429
  %16 = srem i64 %14, 1000000007
  store i64 %16, ptr %15, align 8, !tbaa !5
  br i1 %.not27, label %20, label %17

17:                                               ; preds = %.lr.ph
  %18 = add nsw i64 %16, %.031
  %19 = srem i64 %18, 1000000007
  br label %23

20:                                               ; preds = %.lr.ph
  %21 = add nsw i64 %16, %.02130
  %22 = srem i64 %21, 1000000007
  br label %23

23:                                               ; preds = %17, %20
  %.122 = phi i64 [ %.02130, %17 ], [ %22, %20 ]
  %.1 = phi i64 [ %19, %17 ], [ %.031, %20 ]
  %24 = add nuw i64 %.02429, 1
  %exitcond.not = icmp eq i64 %.02429, %8
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge37.loopexit.unr-lcssa:                 ; preds = %.lr.ph36, %.lr.ph36.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph36.preheader ], [ %45, %.lr.ph36 ]
  %.02334.unr = phi ptr [ %6, %.lr.ph36.preheader ], [ %46, %.lr.ph36 ]
  %.02533.unr = phi i64 [ 0, %.lr.ph36.preheader ], [ %45, %.lr.ph36 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge37, label %.lr.ph36.epil

.lr.ph36.epil:                                    ; preds = %._crit_edge37.loopexit.unr-lcssa, %.lr.ph36.epil
  %.02334.epil = phi ptr [ %28, %.lr.ph36.epil ], [ %.02334.unr, %._crit_edge37.loopexit.unr-lcssa ]
  %.02533.epil = phi i64 [ %27, %.lr.ph36.epil ], [ %.02533.unr, %._crit_edge37.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph36.epil ], [ 0, %._crit_edge37.loopexit.unr-lcssa ]
  %25 = load i64, ptr %.02334.epil, align 8, !tbaa !5
  %26 = add nsw i64 %25, %.02533.epil
  %27 = srem i64 %26, 1000000007
  %28 = getelementptr inbounds i8, ptr %.02334.epil, i64 8
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge37, label %.lr.ph36.epil, !llvm.loop !11

._crit_edge37:                                    ; preds = %._crit_edge37.loopexit.unr-lcssa, %.lr.ph36.epil, %._crit_edge
  %.025.lcssa = phi i64 [ 0, %._crit_edge ], [ %.lcssa.ph, %._crit_edge37.loopexit.unr-lcssa ], [ %27, %.lr.ph36.epil ]
  %29 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %.025.lcssa)
  %30 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %29, ptr noundef nonnull @.str)
  call void @llvm.stackrestore.p0(ptr %5)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #4
  ret void

.lr.ph36:                                         ; preds = %.lr.ph36, %.lr.ph36.preheader.new
  %.02334 = phi ptr [ %6, %.lr.ph36.preheader.new ], [ %46, %.lr.ph36 ]
  %.02533 = phi i64 [ 0, %.lr.ph36.preheader.new ], [ %45, %.lr.ph36 ]
  %niter = phi i64 [ 0, %.lr.ph36.preheader.new ], [ %niter.next.3, %.lr.ph36 ]
  %31 = load i64, ptr %.02334, align 8, !tbaa !5
  %32 = add nsw i64 %31, %.02533
  %33 = srem i64 %32, 1000000007
  %34 = getelementptr inbounds i8, ptr %.02334, i64 8
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add nsw i64 %35, %33
  %37 = srem i64 %36, 1000000007
  %38 = getelementptr inbounds i8, ptr %.02334, i64 16
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = add nsw i64 %39, %37
  %41 = srem i64 %40, 1000000007
  %42 = getelementptr inbounds i8, ptr %.02334, i64 24
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = add nsw i64 %43, %41
  %45 = srem i64 %44, 1000000007
  %46 = getelementptr inbounds i8, ptr %.02334, i64 32
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge37.loopexit.unr-lcssa, label %.lr.ph36
}

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #2 align 2

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { mustprogress 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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