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

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

@mob = external dso_local global [500009 x i32], align 16

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

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

7:                                                ; preds = %4
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %39

8:                                                ; preds = %4
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [500009 x i32], ptr @mob, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  store i32 %16, ptr %3, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %31, %8
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp slt i32 %18, 500009
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %17
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [500009 x i32], ptr @mob, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [500009 x i32], ptr @mob, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sub nsw i32 %29, %25
  store i32 %30, ptr %28, align 4, !tbaa !5
  br label %31

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

35:                                               ; preds = %20
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %1, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !11

39:                                               ; preds = %7
  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}


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

@mob = external dso_local local_unnamed_addr global [500009 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z6mobiusv() local_unnamed_addr #0 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @mob, i64 4), align 4, !tbaa !5
  br label %2

1:                                                ; preds = %._crit_edge
  ret void

2:                                                ; preds = %0, %._crit_edge
  %indvars.iv13 = phi i64 [ 2, %0 ], [ %indvars.iv.next14, %._crit_edge ]
  %indvars.iv = phi i64 [ 4, %0 ], [ %indvars.iv.next, %._crit_edge ]
  %3 = getelementptr inbounds [500009 x i32], ptr @mob, i64 0, i64 %indvars.iv13
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr %3, align 4, !tbaa !5
  %6 = icmp ult i64 %indvars.iv13, 250005
  br i1 %6, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %2
  %indvars.iv.next14 = add nuw nsw i64 %indvars.iv13, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not = icmp eq i64 %indvars.iv.next14, 500009
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !9

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv15 = phi i64 [ %indvars.iv.next16, %.lr.ph ], [ %indvars.iv, %2 ]
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = getelementptr inbounds [500009 x i32], ptr @mob, i64 0, i64 %indvars.iv15
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sub nsw i32 %9, %7
  store i32 %10, ptr %8, align 4, !tbaa !5
  %indvars.iv.next16 = add nuw nsw i64 %indvars.iv15, %indvars.iv13
  %11 = icmp ult i64 %indvars.iv.next16, 500009
  br i1 %11, label %.lr.ph, label %._crit_edge, !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: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass