; LLVM IR from Dev-XYS/Algorithms :: Fast-Number-Theoretic-Transform.ll (Z3prePx)

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

@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@_ZZ3prePxE4temp = external hidden global [500000 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
define dso_local void @_Z3prePx(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 @_ZZ3prePxE4temp, ptr align 8 %7, i64 4000000, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %46, %1
  %9 = load i32, ptr %3, align 4, !tbaa !9
  %10 = load i32, ptr @n, align 4, !tbaa !9
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %49

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

14:                                               ; preds = %34, %13
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = load i32, ptr @k, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %37

19:                                               ; preds = %14
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = shl i32 1, %21
  %23 = and i32 %20, %22
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %33

25:                                               ; preds = %19
  %26 = load i32, ptr @k, align 4, !tbaa !9
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sub nsw i32 %26, %27
  %29 = sub nsw i32 %28, 1
  %30 = shl i32 1, %29
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = add nsw i32 %31, %30
  store i32 %32, ptr %5, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %25, %19
  br label %34

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

37:                                               ; preds = %18
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [500000 x i64], ptr @_ZZ3prePxE4temp, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = load i32, ptr %3, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i64, ptr %42, i64 %44
  store i64 %41, ptr %45, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %46

46:                                               ; preds = %37
  %47 = load i32, ptr %3, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %3, align 4, !tbaa !9
  br label %8, !llvm.loop !15

49:                                               ; preds = %12
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !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 = !{!14, !14, i64 0}
!14 = !{!"long long", !7, i64 0}
!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"

@n = external dso_local local_unnamed_addr global i32, align 4
@k = external dso_local local_unnamed_addr global i32, align 4
@_ZZ3prePxE4temp = external hidden local_unnamed_addr global [500000 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z3prePx(ptr nocapture noundef %0) local_unnamed_addr #0 {
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(4000000) @_ZZ3prePxE4temp, ptr noundef nonnull align 8 dereferenceable(4000000) %0, i64 4000000, i1 false)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.preheader.lr.ph, label %._crit_edge17

.preheader.lr.ph:                                 ; preds = %1
  %4 = load i32, ptr @k, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  %wide.trip.count24 = zext nneg i32 %2 to i64
  br i1 %5, label %.preheader.us.preheader, label %.preheader.preheader

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %xtraiter = and i32 %4, 1
  %6 = icmp eq i32 %4, 1
  %unroll_iter = and i32 %4, 2147483646
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br label %.preheader.us

.preheader.preheader:                             ; preds = %.preheader.lr.ph
  %.pre = load i64, ptr @_ZZ3prePxE4temp, align 16, !tbaa !9
  %min.iters.check = icmp ult i32 %2, 4
  br i1 %min.iters.check, label %.preheader.preheader28, label %vector.ph

.preheader.preheader28:                           ; preds = %middle.block, %.preheader.preheader
  %indvars.iv.ph = phi i64 [ 0, %.preheader.preheader ], [ %n.vec, %middle.block ]
  br label %.preheader

vector.ph:                                        ; preds = %.preheader.preheader
  %n.vec = and i64 %wide.trip.count24, 2147483644
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %.pre, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %7 = getelementptr inbounds i64, ptr %0, i64 %index
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store <2 x i64> %broadcast.splat, ptr %7, align 8, !tbaa !9
  store <2 x i64> %broadcast.splat, ptr %8, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count24
  br i1 %cmp.n, label %._crit_edge17, label %.preheader.preheader28

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %10 = trunc nuw nsw i64 %indvars.iv21 to i32
  br i1 %6, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %.015.us = phi i32 [ %23, %.preheader.us.new ], [ 0, %.preheader.us ]
  %.01114.us = phi i32 [ %.1.us.1, %.preheader.us.new ], [ 0, %.preheader.us ]
  %niter = phi i32 [ %niter.next.1, %.preheader.us.new ], [ 0, %.preheader.us ]
  %11 = shl nuw i32 1, %.015.us
  %12 = and i32 %11, %10
  %.not.us = icmp eq i32 %12, 0
  %13 = xor i32 %.015.us, -1
  %14 = add nsw i32 %4, %13
  %15 = shl nuw i32 1, %14
  %16 = select i1 %.not.us, i32 0, i32 %15
  %.1.us = add nsw i32 %16, %.01114.us
  %17 = shl nuw i32 2, %.015.us
  %18 = and i32 %17, %10
  %.not.us.1 = icmp eq i32 %18, 0
  %19 = xor i32 %.015.us, -2
  %20 = add nsw i32 %4, %19
  %21 = shl nuw i32 1, %20
  %22 = select i1 %.not.us.1, i32 0, i32 %21
  %.1.us.1 = add nsw i32 %22, %.1.us
  %23 = add nuw nsw i32 %.015.us, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !15

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.1.us.lcssa.ph = phi i32 [ poison, %.preheader.us ], [ %.1.us.1, %.preheader.us.new ]
  %.015.us.unr = phi i32 [ 0, %.preheader.us ], [ %23, %.preheader.us.new ]
  %.01114.us.unr = phi i32 [ 0, %.preheader.us ], [ %.1.us.1, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %._crit_edge.us.epilog-lcssa

._crit_edge.us.epilog-lcssa:                      ; preds = %._crit_edge.us.unr-lcssa
  %24 = shl nuw i32 1, %.015.us.unr
  %25 = and i32 %24, %10
  %.not.us.epil = icmp eq i32 %25, 0
  %26 = xor i32 %.015.us.unr, -1
  %27 = add nsw i32 %4, %26
  %28 = shl nuw i32 1, %27
  %29 = select i1 %.not.us.epil, i32 0, i32 %28
  %.1.us.epil = add nsw i32 %29, %.01114.us.unr
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %._crit_edge.us.unr-lcssa, %._crit_edge.us.epilog-lcssa
  %.1.us.lcssa = phi i32 [ %.1.us.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %.1.us.epil, %._crit_edge.us.epilog-lcssa ]
  %30 = sext i32 %.1.us.lcssa to i64
  %31 = getelementptr inbounds [500000 x i64], ptr @_ZZ3prePxE4temp, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv21
  store i64 %32, ptr %33, align 8, !tbaa !9
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  %exitcond25.not = icmp eq i64 %indvars.iv.next22, %wide.trip.count24
  br i1 %exitcond25.not, label %._crit_edge17, label %.preheader.us, !llvm.loop !16

.preheader:                                       ; preds = %.preheader.preheader28, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader ], [ %indvars.iv.ph, %.preheader.preheader28 ]
  %34 = getelementptr inbounds i64, ptr %0, i64 %indvars.iv
  store i64 %.pre, ptr %34, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count24
  br i1 %exitcond.not, label %._crit_edge17, label %.preheader, !llvm.loop !17

._crit_edge17:                                    ; preds = %.preheader, %._crit_edge.us, %middle.block, %1
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

!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, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}
!17 = distinct !{!17, !12, !14, !13}


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