; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2019-20/Open/Plat (exercise_3000_dwr)

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

@mod = external dso_local global i32, align 4
@N = external dso_local global i32, align 4
@prime = external dso_local global [5001 x i8], align 16
@ways = external dso_local global [5001 x i32], align 16
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z10precomputev() #0

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5solvei(i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z2pwii(i32 noundef, i32 noundef) #2

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #3 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %7 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) @N)
  %8 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) %7, ptr noundef nonnull align 4 dereferenceable(4) @mod)
  call void @_Z10precomputev()
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #6
  store i32 1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #6
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %86, %0
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = load i32, ptr @N, align 4, !tbaa !5
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #6
  br label %89

14:                                               ; preds = %9
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [5001 x i8], ptr @prime, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !9, !range !11, !noundef !12
  %19 = trunc i8 %18 to i1
  br i1 %19, label %20, label %85

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  store i64 %22, ptr %5, align 8, !tbaa !13
  br label %23

23:                                               ; preds = %79, %20
  %24 = load i64, ptr %5, align 8, !tbaa !13
  %25 = load i32, ptr @N, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = mul nsw i32 %25, %26
  %28 = sext i32 %27 to i64
  %29 = icmp sle i64 %24, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %23
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  br label %84

31:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  %32 = load i64, ptr %5, align 8, !tbaa !13
  %33 = trunc i64 %32 to i32
  %34 = call noundef i32 @_Z5solvei(i32 noundef %33)
  store i32 %34, ptr %6, align 4, !tbaa !5
  %35 = load i64, ptr %5, align 8, !tbaa !13
  %36 = load i32, ptr @N, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = icmp sle i64 %35, %37
  br i1 %38, label %39, label %43

39:                                               ; preds = %31
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = load i64, ptr %5, align 8, !tbaa !13
  %42 = getelementptr inbounds [5001 x i32], ptr @ways, i64 0, i64 %41
  store i32 %40, ptr %42, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %39, %31
  %44 = load i64, ptr %5, align 8, !tbaa !13
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = icmp sgt i64 %44, %46
  br i1 %47, label %48, label %63

48:                                               ; preds = %43
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = load i32, ptr @mod, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = add nsw i32 %49, %51
  %53 = load i64, ptr %5, align 8, !tbaa !13
  %54 = load i32, ptr %3, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = sdiv i64 %53, %55
  %57 = getelementptr inbounds [5001 x i32], ptr @ways, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = sub nsw i32 %52, %58
  %60 = load i32, ptr @mod, align 4, !tbaa !5
  %61 = sub nsw i32 %60, 1
  %62 = srem i32 %59, %61
  store i32 %62, ptr %6, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %48, %43
  %64 = load i32, ptr %2, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = load i64, ptr %5, align 8, !tbaa !13
  %67 = load i32, ptr %3, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = sdiv i64 %66, %68
  %70 = trunc i64 %69 to i32
  %71 = load i32, ptr %6, align 4, !tbaa !5
  %72 = call noundef i32 @_Z2pwii(i32 noundef %70, i32 noundef %71)
  %73 = sext i32 %72 to i64
  %74 = mul nsw i64 %65, %73
  %75 = load i32, ptr @mod, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = srem i64 %74, %76
  %78 = trunc i64 %77 to i32
  store i32 %78, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %79

79:                                               ; preds = %63
  %80 = load i32, ptr %3, align 4, !tbaa !5
  %81 = sext i32 %80 to i64
  %82 = load i64, ptr %5, align 8, !tbaa !13
  %83 = mul nsw i64 %82, %81
  store i64 %83, ptr %5, align 8, !tbaa !13
  br label %23, !llvm.loop !15

84:                                               ; preds = %30
  br label %85

85:                                               ; preds = %84, %14
  br label %86

86:                                               ; preds = %85
  %87 = load i32, ptr %3, align 4, !tbaa !5
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %3, align 4, !tbaa !5
  br label %9, !llvm.loop !17

89:                                               ; preds = %13
  %90 = load i32, ptr %2, align 4, !tbaa !5
  %91 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %90)
  %92 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) %91, i8 noundef signext 10)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #6
  %93 = load i32, ptr %1, align 4
  ret i32 %93
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) #5

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #4 = { "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 #5 = { 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 #6 = { 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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@mod = external dso_local global i32, align 4
@N = external dso_local global i32, align 4
@prime = external dso_local local_unnamed_addr global [5001 x i8], align 16
@ways = external dso_local local_unnamed_addr global [5001 x i32], align 16
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z10precomputev() local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z5solvei(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z2pwii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = tail call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) @N)
  %2 = tail call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) %1, ptr noundef nonnull align 4 dereferenceable(4) @mod)
  tail call void @_Z10precomputev()
  %3 = load i32, ptr @N, align 4, !tbaa !5
  %.not32 = icmp slt i32 %3, 2
  br i1 %.not32, label %._crit_edge, label %.lr.ph36

._crit_edge:                                      ; preds = %.loopexit, %0
  %.025.lcssa = phi i32 [ 1, %0 ], [ %.2, %.loopexit ]
  %4 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %.025.lcssa)
  %5 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) %4, i8 noundef signext 10)
  ret i32 0

.lr.ph36:                                         ; preds = %0, %.loopexit
  %6 = phi i32 [ %44, %.loopexit ], [ %3, %0 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 2, %0 ]
  %.02533 = phi i32 [ %.2, %.loopexit ], [ 1, %0 ]
  %7 = getelementptr inbounds [5001 x i8], ptr @prime, i64 0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !9, !range !11, !noundef !12
  %9 = trunc nuw i8 %8 to i1
  br i1 %9, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %.lr.ph36
  %10 = trunc nuw nsw i64 %indvars.iv to i32
  %11 = mul nsw i32 %6, %10
  %12 = sext i32 %11 to i64
  %.not2729 = icmp sgt i64 %indvars.iv, %12
  br i1 %.not2729, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %30
  %.02331 = phi i64 [ %40, %30 ], [ %indvars.iv, %.preheader ]
  %.130 = phi i32 [ %39, %30 ], [ %.02533, %.preheader ]
  %13 = trunc i64 %.02331 to i32
  %14 = tail call noundef i32 @_Z5solvei(i32 noundef %13)
  %15 = load i32, ptr @N, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %.not28 = icmp sgt i64 %.02331, %16
  br i1 %.not28, label %19, label %17

17:                                               ; preds = %.lr.ph
  %18 = getelementptr inbounds [5001 x i32], ptr @ways, i64 0, i64 %.02331
  store i32 %14, ptr %18, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %17, %.lr.ph
  %20 = icmp ugt i64 %.02331, %indvars.iv
  br i1 %20, label %21, label %._crit_edge39

._crit_edge39:                                    ; preds = %19
  %.pre = udiv i64 %.02331, %indvars.iv
  br label %30

21:                                               ; preds = %19
  %22 = load i32, ptr @mod, align 4, !tbaa !5
  %23 = add nsw i32 %22, -1
  %24 = add nsw i32 %23, %14
  %25 = udiv i64 %.02331, %indvars.iv
  %26 = getelementptr inbounds [5001 x i32], ptr @ways, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sub i32 %24, %27
  %29 = srem i32 %28, %23
  br label %30

30:                                               ; preds = %._crit_edge39, %21
  %.pre-phi = phi i64 [ %.pre, %._crit_edge39 ], [ %25, %21 ]
  %.0 = phi i32 [ %14, %._crit_edge39 ], [ %29, %21 ]
  %31 = sext i32 %.130 to i64
  %32 = trunc i64 %.pre-phi to i32
  %33 = tail call noundef i32 @_Z2pwii(i32 noundef %32, i32 noundef %.0)
  %34 = sext i32 %33 to i64
  %35 = mul nsw i64 %34, %31
  %36 = load i32, ptr @mod, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = srem i64 %35, %37
  %39 = trunc nsw i64 %38 to i32
  %40 = mul nuw nsw i64 %.02331, %indvars.iv
  %41 = load i32, ptr @N, align 4, !tbaa !5
  %42 = mul nsw i32 %41, %10
  %43 = sext i32 %42 to i64
  %.not27 = icmp sgt i64 %40, %43
  br i1 %.not27, label %.loopexit, label %.lr.ph, !llvm.loop !13

.loopexit:                                        ; preds = %30, %.preheader, %.lr.ph36
  %44 = phi i32 [ %6, %.lr.ph36 ], [ %6, %.preheader ], [ %41, %30 ]
  %.2 = phi i32 [ %.02533, %.lr.ph36 ], [ %.02533, %.preheader ], [ %39, %30 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %45 = sext i32 %44 to i64
  %.not.not = icmp slt i64 %indvars.iv, %45
  br i1 %.not.not, label %.lr.ph36, label %._crit_edge, !llvm.loop !15
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) local_unnamed_addr #4

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

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


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