; LLVM IR from lzyrapx/Competitive-Programming :: 51Nod/1135（原根）.ll (Z6dividex)

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

@p = external dso_local global [100010 x i32], align 16
@t = external dso_local global i32, align 4

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

4:                                                ; preds = %37, %1
  %5 = load i32, ptr %3, align 4, !tbaa !9
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = mul nsw i32 %5, %6
  %8 = sext i32 %7 to i64
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = icmp sle i64 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %40

12:                                               ; preds = %4
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = srem i64 %13, %15
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %36

18:                                               ; preds = %12
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = load i32, ptr @t, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @t, align 4, !tbaa !9
  %22 = sext i32 %20 to i64
  %23 = getelementptr inbounds [100010 x i32], ptr @p, i64 0, i64 %22
  store i32 %19, ptr %23, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %30, %18
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = srem i64 %25, %27
  %29 = icmp eq i64 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %24
  %31 = load i32, ptr %3, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = load i64, ptr %2, align 8, !tbaa !5
  %34 = sdiv i64 %33, %32
  store i64 %34, ptr %2, align 8, !tbaa !5
  br label %24, !llvm.loop !11

35:                                               ; preds = %24
  br label %36

36:                                               ; preds = %35, %12
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %3, align 4, !tbaa !9
  br label %4, !llvm.loop !13

40:                                               ; preds = %11
  ret void
}

; 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 = !{!"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"

@p = external dso_local local_unnamed_addr global [100010 x i32], align 16
@t = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6dividex(i64 noundef %0) local_unnamed_addr #0 {
  %.not13 = icmp slt i64 %0, 4
  br i1 %.not13, label %._crit_edge, label %.lr.ph16.preheader

.lr.ph16.preheader:                               ; preds = %1
  %t.promoted = load i32, ptr @t, align 4, !tbaa !5
  br label %.lr.ph16

._crit_edge:                                      ; preds = %.loopexit, %1
  ret void

.lr.ph16:                                         ; preds = %.lr.ph16.preheader, %.loopexit
  %indvars.iv = phi i64 [ 2, %.lr.ph16.preheader ], [ %indvars.iv.next, %.loopexit ]
  %.01114 = phi i64 [ %0, %.lr.ph16.preheader ], [ %.2, %.loopexit ]
  %2 = phi i32 [ %t.promoted, %.lr.ph16.preheader ], [ %15, %.loopexit ]
  %3 = urem i64 %.01114, %indvars.iv
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %5, label %.loopexit

5:                                                ; preds = %.lr.ph16
  %6 = add nsw i32 %2, 1
  store i32 %6, ptr @t, align 4, !tbaa !5
  %7 = sext i32 %2 to i64
  %8 = getelementptr inbounds [100010 x i32], ptr @p, i64 0, i64 %7
  %9 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %9, ptr %8, align 4, !tbaa !5
  %10 = srem i64 %.01114, %indvars.iv
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.112 = phi i64 [ %12, %.lr.ph ], [ %.01114, %5 ]
  %12 = sdiv i64 %.112, %indvars.iv
  %13 = srem i64 %12, %indvars.iv
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %5, %.lr.ph16
  %15 = phi i32 [ %2, %.lr.ph16 ], [ %6, %5 ], [ %6, %.lr.ph ]
  %.2 = phi i64 [ %.01114, %.lr.ph16 ], [ %.01114, %5 ], [ %12, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %.not = icmp slt i64 %.2, %16
  br i1 %.not, label %._crit_edge, label %.lr.ph16, !llvm.loop !11
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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