; LLVM IR from Muhammad-Magdi/problem-solving :: Number Theory/PhiSieve.ll (Z8phiSievev)

; 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 [1000005 x i64], align 16

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

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

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

8:                                                ; preds = %4
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = getelementptr inbounds [1000005 x i64], ptr @phi, i64 0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = load i64, ptr %1, align 8, !tbaa !5
  %13 = icmp eq i64 %11, %12
  br i1 %13, label %14, label %35

14:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %15 = load i64, ptr %1, align 8, !tbaa !5
  store i64 %15, ptr %3, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %30, %14
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = icmp slt i64 %17, 1000005
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %16
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds [1000005 x i64], ptr @phi, i64 0, i64 %21
  %23 = load i64, ptr %22, align 8, !tbaa !5
  %24 = load i64, ptr %1, align 8, !tbaa !5
  %25 = sdiv i64 %23, %24
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds [1000005 x i64], ptr @phi, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = sub nsw i64 %28, %25
  store i64 %29, ptr %27, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %20
  %31 = load i64, ptr %1, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = add nsw i64 %32, %31
  store i64 %33, ptr %3, align 8, !tbaa !5
  br label %16, !llvm.loop !9

34:                                               ; preds = %19
  br label %35

35:                                               ; preds = %34, %8
  br label %36

36:                                               ; preds = %35
  %37 = load i64, ptr %1, align 8, !tbaa !5
  %38 = and i64 %37, 1
  %39 = add nsw i64 1, %38
  %40 = load i64, ptr %1, align 8, !tbaa !5
  %41 = add nsw i64 %40, %39
  store i64 %41, ptr %1, align 8, !tbaa !5
  br label %4, !llvm.loop !11

42:                                               ; preds = %7
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZSt4iotaIPxiEvT_S1_T0_(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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" }
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 nosync 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 = !{!"long long", !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"

@phi = external dso_local global [1000005 x i64], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z8phiSievev() local_unnamed_addr #0 {
  tail call void @_ZSt4iotaIPxiEvT_S1_T0_(ptr noundef nonnull @phi, ptr noundef nonnull getelementptr inbounds (i8, ptr @phi, i64 8000040), i32 noundef 0)
  br label %2

1:                                                ; preds = %.loopexit
  ret void

2:                                                ; preds = %0, %.loopexit
  %.01214 = phi i64 [ 2, %0 ], [ %14, %.loopexit ]
  %3 = getelementptr inbounds [1000005 x i64], ptr @phi, i64 0, i64 %.01214
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i64 %4, %.01214
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.013 = phi i64 [ %10, %.lr.ph ], [ %.01214, %2 ]
  %6 = getelementptr inbounds [1000005 x i64], ptr @phi, i64 0, i64 %.013
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = sdiv i64 %7, %.01214
  %9 = sub nsw i64 %7, %8
  store i64 %9, ptr %6, align 8, !tbaa !5
  %10 = add nuw nsw i64 %.013, %.01214
  %11 = icmp ult i64 %10, 1000005
  br i1 %11, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %2
  %12 = and i64 %.01214, 1
  %13 = add nuw nsw i64 %.01214, 1
  %14 = add nuw nsw i64 %13, %12
  %15 = icmp ult i64 %14, 1000005
  br i1 %15, label %2, label %1, !llvm.loop !11
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZSt4iotaIPxiEvT_S1_T0_(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass