; LLVM IR from tangjz/acm-icpc :: lydsy/2082.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
@e = external dso_local global [10001 x i32], align 16
@p = external dso_local global [10001 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 = %32, %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 %35

10:                                               ; preds = %5
  br label %11

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

19:                                               ; preds = %11
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %22, align 4, !tbaa !9
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = load i64, ptr %2, align 8, !tbaa !5
  %30 = sdiv i64 %29, %28
  store i64 %30, ptr %2, align 8, !tbaa !5
  br label %11, !llvm.loop !11

31:                                               ; preds = %11
  br label %32

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

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

38:                                               ; preds = %35
  br label %39

39:                                               ; preds = %76, %38
  %40 = load i64, ptr %2, align 8, !tbaa !5
  %41 = icmp sgt i64 %40, 1
  br i1 %41, label %42, label %79

42:                                               ; preds = %39
  %43 = load i64, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = load i32, ptr @cnt, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %48
  store i64 %46, ptr %49, align 8, !tbaa !5
  %50 = load i32, ptr @cnt, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %51
  store i32 0, ptr %52, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %64, %42
  %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 i32, ptr @cnt, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %59
  %61 = load i64, ptr %60, align 8, !tbaa !5
  %62 = icmp eq i64 %57, %61
  br i1 %62, label %63, label %76

63:                                               ; preds = %53
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr @cnt, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %67, align 4, !tbaa !9
  %70 = load i64, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = load i64, ptr %2, align 8, !tbaa !5
  %75 = sdiv i64 %74, %73
  store i64 %75, ptr %2, align 8, !tbaa !5
  br label %53, !llvm.loop !14

76:                                               ; preds = %53
  %77 = load i32, ptr @cnt, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr @cnt, align 4, !tbaa !9
  br label %39, !llvm.loop !15

79:                                               ; preds = %39
  br label %100

80:                                               ; preds = %35
  %81 = load i64, ptr %2, align 8, !tbaa !5
  %82 = call noundef zeroext i1 @_Z12miller_rabinx(i64 noundef %81)
  br i1 %82, label %83, label %92

83:                                               ; preds = %80
  %84 = load i64, ptr %2, align 8, !tbaa !5
  %85 = load i32, ptr @cnt, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %86
  store i64 %84, ptr %87, align 8, !tbaa !5
  %88 = load i32, ptr @cnt, align 4, !tbaa !9
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr @cnt, align 4, !tbaa !9
  %90 = sext i32 %88 to i64
  %91 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %90
  store i32 1, ptr %91, align 4, !tbaa !9
  br label %99

92:                                               ; preds = %80
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %93 = load i64, ptr %2, align 8, !tbaa !5
  %94 = call noundef i64 @_Z11pollard_rhox(i64 noundef %93)
  store i64 %94, ptr %4, align 8, !tbaa !5
  %95 = load i64, ptr %4, align 8, !tbaa !5
  call void @_Z9decomposex(i64 noundef %95)
  %96 = load i64, ptr %2, align 8, !tbaa !5
  %97 = load i64, ptr %4, align 8, !tbaa !5
  %98 = sdiv i64 %96, %97
  call void @_Z9decomposex(i64 noundef %98)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %99

99:                                               ; preds = %92, %83
  br label %100

100:                                              ; preds = %99, %79
  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}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !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
@e = external dso_local local_unnamed_addr global [10001 x i32], align 16
@p = external dso_local local_unnamed_addr global [10001 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 = %41, %1
  %.tr = phi i64 [ %0, %1 ], [ %43, %41 ]
  %2 = load i32, ptr @cnt, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.preheader20.preheader, label %._crit_edge25

.preheader20.preheader:                           ; preds = %tailrecurse
  %wide.trip.count = zext nneg i32 %2 to i64
  br label %.preheader20

.preheader20:                                     ; preds = %.preheader20.preheader, %18
  %indvars.iv = phi i64 [ 0, %.preheader20.preheader ], [ %indvars.iv.next, %18 ]
  %.024 = phi i64 [ %.tr, %.preheader20.preheader ], [ %.1.lcssa, %18 ]
  %4 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %indvars.iv
  %5 = load i64, ptr %4, align 8, !tbaa !9
  %6 = srem i64 %.024, %5
  %7 = icmp eq i64 %6, 0
  br i1 %7, label %.lr.ph, label %18

.lr.ph:                                           ; preds = %.preheader20
  %8 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %indvars.iv
  %.promoted = load i32, ptr %8, align 4, !tbaa !5
  br label %12

._crit_edge25:                                    ; preds = %18, %tailrecurse
  %.0.lcssa = phi i64 [ %.tr, %tailrecurse ], [ %.1.lcssa, %18 ]
  %9 = icmp slt i64 %.0.lcssa, 1000001
  br i1 %9, label %.preheader, label %33

.preheader:                                       ; preds = %._crit_edge25
  %10 = icmp sgt i64 %.0.lcssa, 1
  br i1 %10, label %.lr.ph35.preheader, label %.loopexit

.lr.ph35.preheader:                               ; preds = %.preheader
  %11 = sext i32 %2 to i64
  br label %.lr.ph29

12:                                               ; preds = %.lr.ph, %12
  %13 = phi i32 [ %.promoted, %.lr.ph ], [ %14, %12 ]
  %.122 = phi i64 [ %.024, %.lr.ph ], [ %15, %12 ]
  %14 = add nsw i32 %13, 1
  %15 = sdiv i64 %.122, %5
  %16 = srem i64 %15, %5
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %12, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %12
  store i32 %14, ptr %8, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %.preheader20
  %.1.lcssa = phi i64 [ %15, %._crit_edge ], [ %.024, %.preheader20 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge25, label %.preheader20, !llvm.loop !13

.lr.ph29:                                         ; preds = %31, %.lr.ph35.preheader
  %indvars.iv43 = phi i64 [ %11, %.lr.ph35.preheader ], [ %indvars.iv.next44, %31 ]
  %.234 = phi i64 [ %.0.lcssa, %.lr.ph35.preheader ], [ %27, %31 ]
  %19 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %.234
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %indvars.iv43
  store i64 %21, ptr %22, align 8, !tbaa !9
  %23 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %indvars.iv43
  br label %24

24:                                               ; preds = %.lr.ph29, %24
  %25 = phi i32 [ 0, %.lr.ph29 ], [ %26, %24 ]
  %.327 = phi i64 [ %.234, %.lr.ph29 ], [ %27, %24 ]
  %26 = add nuw nsw i32 %25, 1
  %27 = sdiv i64 %.327, %21
  %28 = getelementptr inbounds [1000001 x i32], ptr @d, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp eq i32 %20, %29
  br i1 %30, label %24, label %31, !llvm.loop !14

31:                                               ; preds = %24
  store i32 %26, ptr %23, align 4, !tbaa !5
  %indvars.iv.next44 = add nsw i64 %indvars.iv43, 1
  %32 = icmp sgt i64 %27, 1
  br i1 %32, label %.lr.ph29, label %..loopexit_crit_edge, !llvm.loop !15

33:                                               ; preds = %._crit_edge25
  %34 = tail call noundef zeroext i1 @_Z12miller_rabinx(i64 noundef %.0.lcssa)
  br i1 %34, label %35, label %41

35:                                               ; preds = %33
  %36 = load i32, ptr @cnt, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10001 x i64], ptr @p, i64 0, i64 %37
  store i64 %.0.lcssa, ptr %38, align 8, !tbaa !9
  %39 = add nsw i32 %36, 1
  store i32 %39, ptr @cnt, align 4, !tbaa !5
  %40 = getelementptr inbounds [10001 x i32], ptr @e, i64 0, i64 %37
  store i32 1, ptr %40, align 4, !tbaa !5
  br label %.loopexit

41:                                               ; preds = %33
  %42 = tail call noundef i64 @_Z11pollard_rhox(i64 noundef %.0.lcssa)
  tail call void @_Z9decomposex(i64 noundef %42)
  %43 = sdiv i64 %.0.lcssa, %42
  br label %tailrecurse

..loopexit_crit_edge:                             ; preds = %31
  %44 = trunc nsw i64 %indvars.iv.next44 to i32
  store i32 %44, ptr @cnt, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %..loopexit_crit_edge, %35
  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}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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