; LLVM IR from ShahjalalShohag/code-library :: Number Theory/Number of Distinct Kth Powers Modulo n.ll (Z2yoxx)

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

; 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 uwtable
declare dso_local noundef i64 @_Z1fxxx(i64 noundef, i64 noundef, i64 noundef) #1

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

8:                                                ; preds = %41, %2
  %9 = load i64, ptr %6, align 8, !tbaa !5
  %10 = load i64, ptr %6, align 8, !tbaa !5
  %11 = mul nsw i64 %9, %10
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = icmp sle i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %44

15:                                               ; preds = %8
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %6, align 8, !tbaa !5
  %18 = srem i64 %16, %17
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %40

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %26, %20
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = srem i64 %22, %23
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %32

26:                                               ; preds = %21
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %7, align 4, !tbaa !9
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = load i64, ptr %4, align 8, !tbaa !5
  %31 = sdiv i64 %30, %29
  store i64 %31, ptr %4, align 8, !tbaa !5
  br label %21, !llvm.loop !11

32:                                               ; preds = %21
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = call noundef i64 @_Z1fxxx(i64 noundef %33, i64 noundef %35, i64 noundef %36)
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = mul nsw i64 %38, %37
  store i64 %39, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %40

40:                                               ; preds = %32, %15
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %6, align 8, !tbaa !5
  %43 = add nsw i64 %42, 1
  store i64 %43, ptr %6, align 8, !tbaa !5
  br label %8, !llvm.loop !13

44:                                               ; preds = %14
  %45 = load i64, ptr %4, align 8, !tbaa !5
  %46 = icmp sgt i64 %45, 1
  br i1 %46, label %47, label %53

47:                                               ; preds = %44
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = load i64, ptr %3, align 8, !tbaa !5
  %50 = call noundef i64 @_Z1fxxx(i64 noundef %48, i64 noundef 1, i64 noundef %49)
  %51 = load i64, ptr %5, align 8, !tbaa !5
  %52 = mul nsw i64 %51, %50
  store i64 %52, ptr %5, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %47, %44
  %54 = load i64, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i64 %54
}

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z1fxxx(i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z2yoxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not28 = icmp slt i64 %1, 4
  br i1 %.not28, label %._crit_edge34, label %.lr.ph33

._crit_edge34:                                    ; preds = %13, %2
  %.022.lcssa = phi i64 [ %1, %2 ], [ %.224, %13 ]
  %.021.lcssa = phi i64 [ 1, %2 ], [ %.1, %13 ]
  %3 = icmp sgt i64 %.022.lcssa, 1
  br i1 %3, label %16, label %19

.lr.ph33:                                         ; preds = %2, %13
  %.02031 = phi i64 [ %14, %13 ], [ 2, %2 ]
  %.02130 = phi i64 [ %.1, %13 ], [ 1, %2 ]
  %.02229 = phi i64 [ %.224, %13 ], [ %1, %2 ]
  %4 = srem i64 %.02229, %.02031
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %.lr.ph, label %13

.lr.ph:                                           ; preds = %.lr.ph33, %.lr.ph
  %.026 = phi i32 [ %6, %.lr.ph ], [ 0, %.lr.ph33 ]
  %.12325 = phi i64 [ %7, %.lr.ph ], [ %.02229, %.lr.ph33 ]
  %6 = add nuw nsw i32 %.026, 1
  %7 = sdiv i64 %.12325, %.02031
  %8 = srem i64 %7, %.02031
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph
  %10 = zext nneg i32 %6 to i64
  %11 = tail call noundef i64 @_Z1fxxx(i64 noundef %.02031, i64 noundef %10, i64 noundef %0)
  %12 = mul nsw i64 %11, %.02130
  br label %13

13:                                               ; preds = %.lr.ph33, %._crit_edge
  %.224 = phi i64 [ %7, %._crit_edge ], [ %.02229, %.lr.ph33 ]
  %.1 = phi i64 [ %12, %._crit_edge ], [ %.02130, %.lr.ph33 ]
  %14 = add nuw nsw i64 %.02031, 1
  %15 = mul nuw nsw i64 %14, %14
  %.not = icmp sgt i64 %15, %.224
  br i1 %.not, label %._crit_edge34, label %.lr.ph33, !llvm.loop !7

16:                                               ; preds = %._crit_edge34
  %17 = tail call noundef i64 @_Z1fxxx(i64 noundef %.022.lcssa, i64 noundef 1, i64 noundef %0)
  %18 = mul nsw i64 %17, %.021.lcssa
  br label %19

19:                                               ; preds = %16, %._crit_edge34
  %.2 = phi i64 [ %18, %16 ], [ %.021.lcssa, %._crit_edge34 ]
  ret i64 %.2
}

attributes #0 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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