; LLVM IR from AdarshGaur/Competitive-Programming :: Codeforces/Codeforces Round #803 (Div. 2)/B. Rising Sand.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" }

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

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

21:                                               ; preds = %30, %0
  %22 = load ptr, ptr %7, align 8, !tbaa !9
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = icmp ne ptr %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %33

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %27 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %27, ptr %9, align 8, !tbaa !9
  %28 = load ptr, ptr %9, align 8, !tbaa !9
  %29 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %28)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %30

30:                                               ; preds = %26
  %31 = load ptr, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds i64, ptr %31, i32 1
  store ptr %32, ptr %7, align 8, !tbaa !9
  br label %21

33:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 1, ptr %10, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %59, %33
  %35 = load i64, ptr %10, align 8, !tbaa !5
  %36 = load i64, ptr %1, align 8, !tbaa !5
  %37 = sub nsw i64 %36, 1
  %38 = icmp slt i64 %35, %37
  br i1 %38, label %40, label %39

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

40:                                               ; preds = %34
  %41 = load i64, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds i64, ptr %15, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = load i64, ptr %10, align 8, !tbaa !5
  %45 = add nsw i64 %44, 1
  %46 = getelementptr inbounds i64, ptr %15, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !5
  %48 = load i64, ptr %10, align 8, !tbaa !5
  %49 = sub nsw i64 %48, 1
  %50 = getelementptr inbounds i64, ptr %15, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !5
  %52 = add nsw i64 %47, %51
  %53 = icmp sgt i64 %43, %52
  %54 = zext i1 %53 to i64
  %55 = select i1 %53, i32 1, i32 0
  %56 = sext i32 %55 to i64
  %57 = load i64, ptr %3, align 8, !tbaa !5
  %58 = add nsw i64 %57, %56
  store i64 %58, ptr %3, align 8, !tbaa !5
  br label %59

59:                                               ; preds = %40
  %60 = load i64, ptr %10, align 8, !tbaa !5
  %61 = add nsw i64 %60, 1
  store i64 %61, ptr %10, align 8, !tbaa !5
  br label %34, !llvm.loop !11

62:                                               ; preds = %39
  %63 = load i64, ptr %2, align 8, !tbaa !5
  %64 = icmp eq i64 %63, 1
  br i1 %64, label %65, label %69

65:                                               ; preds = %62
  %66 = load i64, ptr %1, align 8, !tbaa !5
  %67 = sub nsw i64 %66, 1
  %68 = sdiv i64 %67, 2
  store i64 %68, ptr %3, align 8, !tbaa !5
  br label %69

69:                                               ; preds = %65, %62
  %70 = load i64, ptr %3, align 8, !tbaa !5
  %71 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %70)
  %72 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %71, ptr noundef @.str)
  %73 = load ptr, ptr %4, align 8
  call void @llvm.stackrestore.p0(ptr %73)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  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 uwtable
declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(8)) #0 align 2

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

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

; 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) #3

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

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

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


; 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
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5solvev() local_unnamed_addr #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %3 = 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)
  %4 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = call ptr @llvm.stacksave.p0()
  %7 = alloca i64, i64 %5, align 16
  %8 = getelementptr inbounds i64, ptr %7, i64 %5
  %.not17 = icmp eq i64 %5, 0
  br i1 %.not17, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph, %0
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = add i64 %9, -1
  %11 = icmp sgt i64 %9, 2
  br i1 %11, label %.lr.ph21.preheader, label %._crit_edge

.lr.ph21.preheader:                               ; preds = %.preheader
  %.phi.trans.insert = getelementptr inbounds i8, ptr %7, i64 8
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !5
  %invariant.gep = getelementptr i8, ptr %7, i64 -8
  %12 = add nsw i64 %9, -2
  %min.iters.check = icmp ult i64 %12, 4
  br i1 %min.iters.check, label %.lr.ph21.preheader27, label %vector.ph

.lr.ph21.preheader27:                             ; preds = %middle.block, %.lr.ph21.preheader
  %.ph = phi i64 [ %.pre, %.lr.ph21.preheader ], [ %vector.recur.extract, %middle.block ]
  %.020.ph = phi i64 [ 1, %.lr.ph21.preheader ], [ %ind.end, %middle.block ]
  %.01419.ph = phi i64 [ 0, %.lr.ph21.preheader ], [ %29, %middle.block ]
  br label %.lr.ph21

vector.ph:                                        ; preds = %.lr.ph21.preheader
  %n.vec = and i64 %12, -4
  %ind.end = or disjoint i64 %n.vec, 1
  %vector.recur.init = insertelement <2 x i64> poison, i64 %.pre, i64 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vector.recur = phi <2 x i64> [ %vector.recur.init, %vector.ph ], [ %wide.load24, %vector.body ]
  %vec.phi = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %vec.phi23 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %27, %vector.body ]
  %13 = or disjoint i64 %index, 2
  %14 = getelementptr inbounds i64, ptr %7, i64 %13
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %wide.load = load <2 x i64>, ptr %14, align 16, !tbaa !5
  %wide.load24 = load <2 x i64>, ptr %15, align 16, !tbaa !5
  %16 = shufflevector <2 x i64> %vector.recur, <2 x i64> %wide.load, <2 x i32> <i32 1, i32 2>
  %17 = shufflevector <2 x i64> %wide.load, <2 x i64> %wide.load24, <2 x i32> <i32 1, i32 2>
  %18 = getelementptr i64, ptr %7, i64 %index
  %19 = getelementptr i8, ptr %18, i64 16
  %wide.load25 = load <2 x i64>, ptr %18, align 16, !tbaa !5
  %wide.load26 = load <2 x i64>, ptr %19, align 16, !tbaa !5
  %20 = add nsw <2 x i64> %wide.load25, %wide.load
  %21 = add nsw <2 x i64> %wide.load26, %wide.load24
  %22 = icmp sgt <2 x i64> %16, %20
  %23 = icmp sgt <2 x i64> %17, %21
  %24 = zext <2 x i1> %22 to <2 x i64>
  %25 = zext <2 x i1> %23 to <2 x i64>
  %26 = add <2 x i64> %vec.phi, %24
  %27 = add <2 x i64> %vec.phi23, %25
  %index.next = add nuw i64 %index, 4
  %28 = icmp eq i64 %index.next, %n.vec
  br i1 %28, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %27, %26
  %29 = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  %vector.recur.extract = extractelement <2 x i64> %wide.load24, i64 1
  %cmp.n = icmp eq i64 %12, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph21.preheader27

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01518 = phi ptr [ %31, %.lr.ph ], [ %7, %0 ]
  %30 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERl(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) %.01518)
  %31 = getelementptr inbounds i8, ptr %.01518, i64 8
  %.not = icmp eq ptr %31, %8
  br i1 %.not, label %.preheader, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph21, %middle.block, %.preheader
  %.014.lcssa = phi i64 [ 0, %.preheader ], [ %29, %middle.block ], [ %45, %.lr.ph21 ]
  %32 = load i64, ptr %2, align 8, !tbaa !5
  %33 = icmp eq i64 %32, 1
  %34 = sdiv i64 %10, 2
  %.1 = select i1 %33, i64 %34, i64 %.014.lcssa
  %35 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEl(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %.1)
  %36 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %35, ptr noundef nonnull @.str)
  call void @llvm.stackrestore.p0(ptr %6)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #5
  ret void

.lr.ph21:                                         ; preds = %.lr.ph21.preheader27, %.lr.ph21
  %37 = phi i64 [ %40, %.lr.ph21 ], [ %.ph, %.lr.ph21.preheader27 ]
  %.020 = phi i64 [ %38, %.lr.ph21 ], [ %.020.ph, %.lr.ph21.preheader27 ]
  %.01419 = phi i64 [ %45, %.lr.ph21 ], [ %.01419.ph, %.lr.ph21.preheader27 ]
  %38 = add nuw nsw i64 %.020, 1
  %39 = getelementptr inbounds i64, ptr %7, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !5
  %gep = getelementptr i64, ptr %invariant.gep, i64 %.020
  %41 = load i64, ptr %gep, align 8, !tbaa !5
  %42 = add nsw i64 %41, %40
  %43 = icmp sgt i64 %37, %42
  %44 = zext i1 %43 to i64
  %45 = add nuw nsw i64 %.01419, %44
  %exitcond.not = icmp eq i64 %38, %10
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph21, !llvm.loop !13
}

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

; 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 #0 align 2

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

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

; 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 #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 #0 align 2

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #4

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


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