; LLVM IR from tangjz/acm-icpc :: codeforces/complete/300E.ll (Z5checkv)

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

@tot = external dso_local global i32, align 4
@prime = external dso_local global [10000001 x i32], align 16
@cnt = external dso_local global [10000001 x i64], align 16
@M = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5checkv() #0 {
  %1 = alloca i1, align 1
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %42, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @tot, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %3, align 4
  br label %45

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %12 = load i64, ptr @M, align 8, !tbaa !9
  store i64 %12, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %16, %11
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %27

16:                                               ; preds = %13
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10000001 x i32], ptr @prime, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = sdiv i64 %22, %21
  store i64 %23, ptr %4, align 8, !tbaa !9
  %24 = load i64, ptr %4, align 8, !tbaa !9
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = add nsw i64 %25, %24
  store i64 %26, ptr %5, align 8, !tbaa !9
  br label %13, !llvm.loop !11

27:                                               ; preds = %13
  %28 = load i64, ptr %5, align 8, !tbaa !9
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [10000001 x i32], ptr @prime, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [10000001 x i64], ptr @cnt, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = icmp slt i64 %28, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %27
  store i1 false, ptr %1, align 1
  store i32 1, ptr %3, align 4
  br label %39

38:                                               ; preds = %27
  store i32 0, ptr %3, align 4
  br label %39

39:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %40 = load i32, ptr %3, align 4
  switch i32 %40, label %45 [
    i32 0, label %41
  ]

41:                                               ; preds = %39
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !13

45:                                               ; preds = %39, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %46 = load i32, ptr %3, align 4
  switch i32 %46, label %50 [
    i32 2, label %47
    i32 1, label %48
  ]

47:                                               ; preds = %45
  store i1 true, ptr %1, align 1
  br label %48

48:                                               ; preds = %47, %45
  %49 = load i1, ptr %1, align 1
  ret i1 %49

50:                                               ; preds = %45
  unreachable
}

; 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

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


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

@tot = external dso_local local_unnamed_addr global i32, align 4
@prime = external dso_local local_unnamed_addr global [10000001 x i32], align 16
@cnt = external dso_local local_unnamed_addr global [10000001 x i64], align 16
@M = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5checkv() local_unnamed_addr #0 {
  %1 = load i32, ptr @tot, align 4, !tbaa !5
  %2 = icmp slt i32 %1, 1
  br i1 %2, label %._crit_edge23, label %.lr.ph22

.lr.ph22:                                         ; preds = %0
  %3 = load i64, ptr @M, align 8, !tbaa !9
  %.not17 = icmp eq i64 %3, 0
  br i1 %.not17, label %.lr.ph22.split.us, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph22
  %4 = zext nneg i32 %1 to i64
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph

.lr.ph22.split.us:                                ; preds = %.lr.ph22
  %5 = load i32, ptr @prime, align 16, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [10000001 x i64], ptr @cnt, i64 0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %.not16.us28 = icmp sgt i64 %8, 0
  br i1 %.not16.us28, label %._crit_edge23, label %.lr.ph29.preheader

.lr.ph29.preheader:                               ; preds = %.lr.ph22.split.us
  %9 = zext nneg i32 %1 to i64
  %wide.trip.count37 = zext nneg i32 %1 to i64
  br label %.lr.ph29

10:                                               ; preds = %.lr.ph29
  %11 = getelementptr inbounds [10000001 x i32], ptr @prime, i64 0, i64 %indvars.iv34
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [10000001 x i64], ptr @cnt, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %.not16.us = icmp sgt i64 %15, 0
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  br i1 %.not16.us, label %._crit_edge23.loopexit, label %.lr.ph29, !llvm.loop !11

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %10
  %indvars.iv34 = phi i64 [ 1, %.lr.ph29.preheader ], [ %indvars.iv.next35, %10 ]
  %exitcond38 = icmp eq i64 %indvars.iv34, %wide.trip.count37
  br i1 %exitcond38, label %._crit_edge23.loopexit, label %10, !llvm.loop !11

16:                                               ; preds = %._crit_edge
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = icmp uge i64 %indvars.iv.next, %4
  %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond, label %._crit_edge23, label %.lr.ph, !llvm.loop !11

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %16 ]
  %18 = phi i1 [ false, %.lr.ph.preheader ], [ %17, %16 ]
  %19 = getelementptr inbounds [10000001 x i32], ptr @prime, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  br label %22

22:                                               ; preds = %.lr.ph, %22
  %.019 = phi i64 [ 0, %.lr.ph ], [ %24, %22 ]
  %.01118 = phi i64 [ %3, %.lr.ph ], [ %23, %22 ]
  %23 = sdiv i64 %.01118, %21
  %24 = add nsw i64 %23, %.019
  %.not = icmp eq i64 %23, 0
  br i1 %.not, label %._crit_edge, label %22, !llvm.loop !13

._crit_edge:                                      ; preds = %22
  %25 = getelementptr inbounds [10000001 x i64], ptr @cnt, i64 0, i64 %21
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %.not16 = icmp slt i64 %24, %26
  br i1 %.not16, label %._crit_edge23, label %16

._crit_edge23.loopexit:                           ; preds = %.lr.ph29, %10
  %27 = icmp uge i64 %indvars.iv34, %9
  br label %._crit_edge23

._crit_edge23:                                    ; preds = %._crit_edge, %16, %._crit_edge23.loopexit, %.lr.ph22.split.us, %0
  %.lcssa = phi i1 [ true, %0 ], [ false, %.lr.ph22.split.us ], [ %27, %._crit_edge23.loopexit ], [ %17, %16 ], [ %18, %._crit_edge ]
  ret i1 %.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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