; LLVM IR from tangjz/acm-icpc :: codeforces/complete/1033D.ll (Z9decomposex)

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

@d = external dso_local global [1000001 x i32], align 16
@cnt = external dso_local global i32, align 4
@p = external dso_local global [2001 x i64], align 16

; 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: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z12miller_rabinx(i64 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z11pollard_rhox(i64 noundef) #2

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

5:                                                ; preds = %34, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load i32, ptr @cnt, align 4, !tbaa !9
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %37

10:                                               ; preds = %5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = srem i64 %11, %15
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %33

18:                                               ; preds = %10
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = load i32, ptr @cnt, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr @cnt, align 4, !tbaa !9
  %25 = sext i32 %23 to i64
  %26 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %25
  store i64 %22, ptr %26, align 8, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %28
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = load i64, ptr %2, align 8, !tbaa !5
  %32 = sdiv i64 %31, %30
  store i64 %32, ptr %2, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %18, %10
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %3, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !11

37:                                               ; preds = %9
  %38 = load i64, ptr %2, align 8, !tbaa !5
  %39 = icmp slt i64 %38, 1000001
  br i1 %39, label %40, label %61

40:                                               ; preds = %37
  br label %41

41:                                               ; preds = %45, %40
  %42 = load i64, ptr %2, align 8, !tbaa !5
  %43 = icmp sgt i64 %42, 1
  br i1 %43, label %44, label %60

44:                                               ; preds = %41
  br label %45

45:                                               ; preds = %44
  %46 = load i64, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = load i32, ptr @cnt, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr @cnt, align 4, !tbaa !9
  %52 = sext i32 %50 to i64
  %53 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %52
  store i64 %49, ptr %53, align 8, !tbaa !5
  %54 = load i64, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = load i64, ptr %2, align 8, !tbaa !5
  %59 = sdiv i64 %58, %57
  store i64 %59, ptr %2, align 8, !tbaa !5
  br label %41, !llvm.loop !13

60:                                               ; preds = %41
  br label %78

61:                                               ; preds = %37
  %62 = load i64, ptr %2, align 8, !tbaa !5
  %63 = call noundef zeroext i1 @_Z12miller_rabinx(i64 noundef %62)
  br i1 %63, label %64, label %70

64:                                               ; preds = %61
  %65 = load i64, ptr %2, align 8, !tbaa !5
  %66 = load i32, ptr @cnt, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr @cnt, align 4, !tbaa !9
  %68 = sext i32 %66 to i64
  %69 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %68
  store i64 %65, ptr %69, align 8, !tbaa !5
  br label %77

70:                                               ; preds = %61
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %71 = load i64, ptr %2, align 8, !tbaa !5
  %72 = call noundef i64 @_Z11pollard_rhox(i64 noundef %71)
  store i64 %72, ptr %4, align 8, !tbaa !5
  %73 = load i64, ptr %4, align 8, !tbaa !5
  call void @_Z9decomposex(i64 noundef %73)
  %74 = load i64, ptr %2, align 8, !tbaa !5
  %75 = load i64, ptr %4, align 8, !tbaa !5
  %76 = sdiv i64 %74, %75
  call void @_Z9decomposex(i64 noundef %76)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %77

77:                                               ; preds = %70, %64
  br label %78

78:                                               ; preds = %77, %60
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { 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 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}


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

@d = external dso_local local_unnamed_addr global [1000001 x i32], align 16
@cnt = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global [2001 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z12miller_rabinx(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z11pollard_rhox(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z9decomposex(i64 noundef %0) local_unnamed_addr #1 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %34, %1
  %.tr = phi i64 [ %0, %1 ], [ %36, %34 ]
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %2 = icmp sgt i32 %cnt.promoted, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %17, %tailrecurse
  %cnt.promoted23 = phi i32 [ %cnt.promoted, %tailrecurse ], [ %18, %17 ]
  %.0.lcssa = phi i64 [ %.tr, %tailrecurse ], [ %.1, %17 ]
  %3 = icmp slt i64 %.0.lcssa, 1000001
  br i1 %3, label %.preheader, label %27

.preheader:                                       ; preds = %._crit_edge
  %4 = icmp sgt i64 %.0.lcssa, 1
  br i1 %4, label %.lr.ph25.preheader, label %.loopexit

.lr.ph25.preheader:                               ; preds = %.preheader
  %5 = sext i32 %cnt.promoted23 to i64
  br label %.lr.ph25

.lr.ph:                                           ; preds = %tailrecurse, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %tailrecurse ]
  %.022 = phi i64 [ %.1, %17 ], [ %.tr, %tailrecurse ]
  %6 = phi i32 [ %18, %17 ], [ %cnt.promoted, %tailrecurse ]
  %7 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %indvars.iv
  %8 = load i64, ptr %7, align 8, !tbaa !9
  %9 = srem i64 %.022, %8
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %11, label %17

11:                                               ; preds = %.lr.ph
  %12 = add nsw i32 %6, 1
  store i32 %12, ptr @cnt, align 4, !tbaa !5
  %13 = sext i32 %6 to i64
  %14 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %13
  store i64 %8, ptr %14, align 8, !tbaa !9
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = sdiv i64 %.022, %15
  br label %17

17:                                               ; preds = %.lr.ph, %11
  %18 = phi i32 [ %12, %11 ], [ %6, %.lr.ph ]
  %.1 = phi i64 [ %16, %11 ], [ %.022, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %indvars.iv.next, %19
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !11

.lr.ph25:                                         ; preds = %.lr.ph25.preheader, %.lr.ph25
  %indvars.iv28 = phi i64 [ %5, %.lr.ph25.preheader ], [ %indvars.iv.next29, %.lr.ph25 ]
  %.224 = phi i64 [ %.0.lcssa, %.lr.ph25.preheader ], [ %25, %.lr.ph25 ]
  %21 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %.224
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %indvars.iv.next29 = add nsw i64 %indvars.iv28, 1
  %24 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %indvars.iv28
  store i64 %23, ptr %24, align 8, !tbaa !9
  %25 = sdiv i64 %.224, %23
  %26 = icmp sgt i64 %25, 1
  br i1 %26, label %.lr.ph25, label %..loopexit_crit_edge, !llvm.loop !13

27:                                               ; preds = %._crit_edge
  %28 = tail call noundef zeroext i1 @_Z12miller_rabinx(i64 noundef %.0.lcssa)
  br i1 %28, label %29, label %34

29:                                               ; preds = %27
  %30 = load i32, ptr @cnt, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @cnt, align 4, !tbaa !5
  %32 = sext i32 %30 to i64
  %33 = getelementptr inbounds [2001 x i64], ptr @p, i64 0, i64 %32
  store i64 %.0.lcssa, ptr %33, align 8, !tbaa !9
  br label %.loopexit

34:                                               ; preds = %27
  %35 = tail call noundef i64 @_Z11pollard_rhox(i64 noundef %.0.lcssa)
  tail call void @_Z9decomposex(i64 noundef %35)
  %36 = sdiv i64 %.0.lcssa, %35
  br label %tailrecurse

..loopexit_crit_edge:                             ; preds = %.lr.ph25
  %37 = trunc nsw i64 %indvars.iv.next29 to i32
  store i32 %37, ptr @cnt, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %..loopexit_crit_edge, %29
  ret void
}

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

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass