; LLVM IR from ShahjalalShohag/code-library :: Number Theory/Phi Function.ll (Z7totientv)

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

@phi = external dso_local global [100009 x i32], align 16

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

5:                                                ; preds = %14, %0
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 100009
  br i1 %7, label %9, label %8

8:                                                ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %17

9:                                                ; preds = %5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %12
  store i32 %10, ptr %13, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %9
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %1, align 4, !tbaa !5
  br label %5, !llvm.loop !9

17:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %53, %17
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = icmp slt i32 %19, 100009
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %18
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = icmp eq i32 %26, %27
  br i1 %28, label %29, label %52

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %30 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %30, ptr %4, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %47, %29
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = icmp slt i32 %32, 100009
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %31
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = sdiv i32 %39, %40
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = sub nsw i32 %45, %41
  store i32 %46, ptr %44, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %35
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, %48
  store i32 %50, ptr %4, align 4, !tbaa !5
  br label %31, !llvm.loop !11

51:                                               ; preds = %34
  br label %52

52:                                               ; preds = %51, %22
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %2, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %2, align 4, !tbaa !5
  br label %18, !llvm.loop !12

56:                                               ; preds = %21
  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 = !{!"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}
!12 = distinct !{!12, !10}


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

@phi = external dso_local local_unnamed_addr global [100009 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z7totientv() local_unnamed_addr #0 {
vector.ph:
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.2, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 1, i32 2, i32 3, i32 4>, %vector.ph ], [ %vec.ind.next.2, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %offset.idx = or disjoint i64 %index, 1
  %0 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %offset.idx
  %1 = getelementptr inbounds i8, ptr %0, i64 16
  store <4 x i32> %vec.ind, ptr %0, align 4, !tbaa !5
  store <4 x i32> %step.add, ptr %1, align 4, !tbaa !5
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %step.add.1 = add <4 x i32> %vec.ind, <i32 12, i32 12, i32 12, i32 12>
  %offset.idx.1 = add nuw nsw i64 %index, 9
  %2 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %offset.idx.1
  %3 = getelementptr inbounds i8, ptr %2, i64 16
  store <4 x i32> %vec.ind.next, ptr %2, align 4, !tbaa !5
  store <4 x i32> %step.add.1, ptr %3, align 4, !tbaa !5
  %vec.ind.next.1 = add <4 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16>
  %step.add.2 = add <4 x i32> %vec.ind, <i32 20, i32 20, i32 20, i32 20>
  %offset.idx.2 = add nuw nsw i64 %index, 17
  %4 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %offset.idx.2
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  store <4 x i32> %vec.ind.next.1, ptr %4, align 4, !tbaa !5
  store <4 x i32> %step.add.2, ptr %5, align 4, !tbaa !5
  %index.next.2 = add nuw nsw i64 %index, 24
  %vec.ind.next.2 = add <4 x i32> %vec.ind, <i32 24, i32 24, i32 24, i32 24>
  %6 = icmp eq i64 %index.next.2, 100008
  br i1 %6, label %.preheader17, label %vector.body, !llvm.loop !9

7:                                                ; preds = %.loopexit
  ret void

.preheader17:                                     ; preds = %vector.body, %.loopexit
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %.loopexit ], [ 2, %vector.body ]
  %8 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %indvars.iv23
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = zext i32 %9 to i64
  %11 = icmp eq i64 %indvars.iv23, %10
  br i1 %11, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.preheader17
  %12 = trunc nuw nsw i64 %indvars.iv23 to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv25 = phi i64 [ %indvars.iv23, %.lr.ph.preheader ], [ %indvars.iv.next26, %.lr.ph ]
  %13 = getelementptr inbounds [100009 x i32], ptr @phi, i64 0, i64 %indvars.iv25
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = sdiv i32 %14, %12
  %16 = sub nsw i32 %14, %15
  store i32 %16, ptr %13, align 4, !tbaa !5
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, %indvars.iv23
  %17 = icmp ult i64 %indvars.iv.next26, 100009
  br i1 %17, label %.lr.ph, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %.preheader17
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next24, 100009
  br i1 %exitcond29.not, label %7, label %.preheader17, !llvm.loop !14
}

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass