; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/334 Division 2/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"

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

@p = external dso_local global i64, align 8
@k = external dso_local global i64, align 8
@v = external dso_local global [1000100 x i32], align 16
@ans = external dso_local global i64, align 8
@MOD = external dso_local global i64, align 8
@_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 i32, align 4
  %4 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  %5 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) @p)
  %6 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) %5, ptr noundef nonnull align 8 dereferenceable(8) @k)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %27, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = load i64, ptr @p, align 8, !tbaa !9
  %11 = icmp slt i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %30

13:                                               ; preds = %7
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = load i64, ptr @k, align 8, !tbaa !9
  %17 = mul nsw i64 %15, %16
  %18 = load i64, ptr @p, align 8, !tbaa !9
  %19 = srem i64 %17, %18
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = icmp eq i64 %19, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %13
  %24 = load i64, ptr @ans, align 8, !tbaa !9
  %25 = add nsw i64 %24, 1
  store i64 %25, ptr @ans, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %23, %13
  br label %27

27:                                               ; preds = %26
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !11

30:                                               ; preds = %12
  store i32 1, ptr @v, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 1, ptr %3, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %67, %30
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = load i64, ptr @p, align 8, !tbaa !9
  %35 = icmp slt i64 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %70

37:                                               ; preds = %31
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [1000100 x i32], ptr @v, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %66, label %43

43:                                               ; preds = %37
  %44 = load i64, ptr @ans, align 8, !tbaa !9
  %45 = load i64, ptr @p, align 8, !tbaa !9
  %46 = mul nsw i64 %44, %45
  %47 = load i64, ptr @MOD, align 8, !tbaa !9
  %48 = srem i64 %46, %47
  store i64 %48, ptr @ans, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  store i64 %50, ptr %4, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %57, %43
  %52 = load i64, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds [1000100 x i32], ptr @v, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = icmp ne i32 %54, 0
  %56 = xor i1 %55, true
  br i1 %56, label %57, label %65

57:                                               ; preds = %51
  %58 = load i64, ptr %4, align 8, !tbaa !9
  %59 = getelementptr inbounds [1000100 x i32], ptr @v, i64 0, i64 %58
  store i32 1, ptr %59, align 4, !tbaa !5
  %60 = load i64, ptr @k, align 8, !tbaa !9
  %61 = load i64, ptr %4, align 8, !tbaa !9
  %62 = mul nsw i64 %60, %61
  %63 = load i64, ptr @p, align 8, !tbaa !9
  %64 = srem i64 %62, %63
  store i64 %64, ptr %4, align 8, !tbaa !9
  br label %51, !llvm.loop !13

65:                                               ; preds = %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %66

66:                                               ; preds = %65, %37
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %3, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %3, align 4, !tbaa !5
  br label %31, !llvm.loop !14

70:                                               ; preds = %36
  %71 = load i64, ptr @ans, align 8, !tbaa !9
  %72 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %71)
  %73 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %72, ptr noundef @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  ret i32 0
}

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

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

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

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

@p = external dso_local global i64, align 8
@k = external dso_local global i64, align 8
@v = external dso_local local_unnamed_addr global [1000100 x i32], align 16
@ans = external dso_local local_unnamed_addr global i64, align 8
@MOD = external dso_local local_unnamed_addr global i64, align 8
@_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 = tail call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 8 dereferenceable(8) @p)
  %2 = tail call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERx(ptr noundef nonnull align 8 dereferenceable(16) %1, ptr noundef nonnull align 8 dereferenceable(8) @k)
  %3 = load i64, ptr @p, align 8, !tbaa !5
  %4 = icmp sgt i64 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %0
  store i32 1, ptr @v, align 16, !tbaa !9
  br label %._crit_edge.._crit_edge27_crit_edge

.lr.ph:                                           ; preds = %0
  %ans.promoted = load i64, ptr @ans, align 8, !tbaa !5
  %5 = load i64, ptr @k, align 8, !tbaa !5
  %xtraiter = and i64 %3, 1
  %6 = icmp eq i64 %3, 1
  br i1 %6, label %._crit_edge.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %3, 9223372036854775806
  br label %14

._crit_edge.unr-lcssa:                            ; preds = %32, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %32 ]
  %.unr = phi i64 [ %ans.promoted, %.lr.ph ], [ %25, %32 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.unr-lcssa
  %7 = mul nsw i64 %5, %indvars.iv.unr
  %8 = srem i64 %7, %3
  %9 = icmp eq i64 %8, %indvars.iv.unr
  br i1 %9, label %10, label %._crit_edge

10:                                               ; preds = %.epil.preheader
  %11 = add nsw i64 %.unr, 1
  store i64 %11, ptr @ans, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.epil.preheader, %10, %._crit_edge.unr-lcssa
  store i32 1, ptr @v, align 16, !tbaa !9
  %.not33 = icmp eq i64 %3, 1
  br i1 %.not33, label %._crit_edge.._crit_edge27_crit_edge, label %.lr.ph26

._crit_edge.._crit_edge27_crit_edge:              ; preds = %._crit_edge.thread, %._crit_edge
  %.pre = load i64, ptr @ans, align 8, !tbaa !5
  br label %._crit_edge27

.lr.ph26:                                         ; preds = %._crit_edge
  %ans.promoted23 = load i64, ptr @ans, align 8, !tbaa !5
  %12 = load i64, ptr @MOD, align 8
  %13 = load i64, ptr @k, align 8
  br label %36

14:                                               ; preds = %32, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %32 ]
  %15 = phi i64 [ %ans.promoted, %.lr.ph.new ], [ %25, %32 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %32 ]
  %16 = mul nsw i64 %5, %indvars.iv
  %17 = srem i64 %16, %3
  %18 = icmp eq i64 %17, %indvars.iv
  %19 = add nsw i64 %15, 1
  %20 = select i1 %18, i64 %19, i64 %15
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %21 = mul nsw i64 %5, %indvars.iv.next
  %22 = srem i64 %21, %3
  %23 = icmp eq i64 %22, %indvars.iv.next
  %24 = add nsw i64 %20, 1
  %25 = select i1 %23, i64 %24, i64 %20
  %26 = xor i1 %18, true
  %27 = xor i1 %23, true
  %28 = xor i1 %26, true
  %29 = xor i1 %27, true
  %30 = or i1 %28, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %14
  store i64 %25, ptr @ans, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %14, %31
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.unr-lcssa, label %14, !llvm.loop !11

._crit_edge27:                                    ; preds = %.loopexit, %._crit_edge.._crit_edge27_crit_edge
  %33 = phi i64 [ %.pre, %._crit_edge.._crit_edge27_crit_edge ], [ %48, %.loopexit ]
  %34 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %33)
  %35 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %34, ptr noundef nonnull @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  ret i32 0

36:                                               ; preds = %.lr.ph26, %.loopexit
  %37 = phi i64 [ %ans.promoted23, %.lr.ph26 ], [ %48, %.loopexit ]
  %indvars.iv29 = phi i64 [ 1, %.lr.ph26 ], [ %indvars.iv.next30, %.loopexit ]
  %38 = phi i64 [ %ans.promoted23, %.lr.ph26 ], [ %49, %.loopexit ]
  %39 = getelementptr inbounds [1000100 x i32], ptr @v, i64 0, i64 %indvars.iv29
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %.not = icmp eq i32 %40, 0
  br i1 %.not, label %.lr.ph22.preheader, label %.loopexit

.lr.ph22.preheader:                               ; preds = %36
  %41 = mul nsw i64 %38, %3
  %42 = srem i64 %41, %12
  store i64 %42, ptr @ans, align 8, !tbaa !5
  br label %.lr.ph22

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %.lr.ph22
  %43 = phi ptr [ %46, %.lr.ph22 ], [ %39, %.lr.ph22.preheader ]
  %.020 = phi i64 [ %45, %.lr.ph22 ], [ %indvars.iv29, %.lr.ph22.preheader ]
  store i32 1, ptr %43, align 4, !tbaa !9
  %44 = mul nsw i64 %13, %.020
  %45 = srem i64 %44, %3
  %46 = getelementptr inbounds [1000100 x i32], ptr @v, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %.not17 = icmp eq i32 %47, 0
  br i1 %.not17, label %.lr.ph22, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph22, %36
  %48 = phi i64 [ %37, %36 ], [ %42, %.lr.ph22 ]
  %49 = phi i64 [ %38, %36 ], [ %42, %.lr.ph22 ]
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond32.not = icmp eq i64 %indvars.iv.next30, %3
  br i1 %exitcond32.not, label %._crit_edge27, label %36, !llvm.loop !14
}

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

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #2

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

!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 long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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