; LLVM IR from manishbisht/Competitive-Programming :: Codeforces/ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)/S_87e073be (Z5Sievex)

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

@prime = external dso_local global [1000005 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5Sievex(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %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
  %8 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i32 0, ptr @prime, align 16, !tbaa !9
  store i32 0, ptr getelementptr inbounds ([1000005 x i32], ptr @prime, i64 0, i64 1), align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 2, ptr %4, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %16, %1
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = icmp slt i64 %10, 1000005
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %19

13:                                               ; preds = %9
  %14 = load i64, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %13
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = add nsw i64 %17, 1
  store i64 %18, ptr %4, align 8, !tbaa !5
  br label %9, !llvm.loop !11

19:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %20 = load i64, ptr %2, align 8, !tbaa !5
  %21 = call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %20)
  %22 = fptosi double %21 to i64
  store i64 %22, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 2, ptr %6, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %55, %19
  %24 = load i64, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %5, align 8, !tbaa !5
  %26 = icmp sle i64 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %58

28:                                               ; preds = %23
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = icmp eq i32 %31, 1
  br i1 %32, label %33, label %54

33:                                               ; preds = %28
  store i64 2, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = mul nsw i64 %34, %35
  store i64 %36, ptr %8, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %47, %33
  %38 = load i64, ptr %8, align 8, !tbaa !5
  %39 = load i64, ptr %2, align 8, !tbaa !5
  %40 = icmp sle i64 %38, %39
  br i1 %40, label %42, label %41

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

42:                                               ; preds = %37
  %43 = load i64, ptr %3, align 8, !tbaa !5
  %44 = trunc i64 %43 to i32
  %45 = load i64, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %45
  store i32 %44, ptr %46, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %42
  %48 = load i64, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = add nsw i64 %49, %48
  store i64 %50, ptr %8, align 8, !tbaa !5
  br label %37, !llvm.loop !13

51:                                               ; preds = %41
  %52 = load i64, ptr %3, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %3, align 8, !tbaa !5
  br label %54

54:                                               ; preds = %51, %28
  br label %55

55:                                               ; preds = %54
  %56 = load i64, ptr %6, align 8, !tbaa !5
  %57 = add nsw i64 %56, 1
  store i64 %57, ptr %6, align 8, !tbaa !5
  br label %23, !llvm.loop !14

58:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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"

@prime = external dso_local local_unnamed_addr global [1000005 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5Sievex(i64 noundef %0) local_unnamed_addr #0 {
vector.ph:
  store i32 0, ptr @prime, align 16, !tbaa !5
  store i32 0, ptr getelementptr inbounds (i8, ptr @prime, i64 4), align 4, !tbaa !5
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.4, %vector.body ]
  %offset.idx = or disjoint i64 %index, 2
  %1 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %offset.idx
  %2 = getelementptr inbounds i8, ptr %1, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %1, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %2, align 8, !tbaa !5
  %offset.idx.1 = add nuw nsw i64 %index, 10
  %3 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %offset.idx.1
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %3, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %4, align 8, !tbaa !5
  %offset.idx.2 = add nuw nsw i64 %index, 18
  %5 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %offset.idx.2
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %5, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %6, align 8, !tbaa !5
  %offset.idx.3 = add nuw nsw i64 %index, 26
  %7 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %offset.idx.3
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %7, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %8, align 8, !tbaa !5
  %offset.idx.4 = add nuw nsw i64 %index, 34
  %9 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %offset.idx.4
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %9, align 8, !tbaa !5
  store <4 x i32> <i32 1, i32 1, i32 1, i32 1>, ptr %10, align 8, !tbaa !5
  %index.next.4 = add nuw nsw i64 %index, 40
  %11 = icmp eq i64 %index.next.4, 1000000
  br i1 %11, label %scalar.ph, label %vector.body, !llvm.loop !9

scalar.ph:                                        ; preds = %vector.body
  store i32 1, ptr getelementptr inbounds (i8, ptr @prime, i64 4000008), align 8, !tbaa !5
  store i32 1, ptr getelementptr inbounds (i8, ptr @prime, i64 4000012), align 4, !tbaa !5
  store i32 1, ptr getelementptr inbounds (i8, ptr @prime, i64 4000016), align 16, !tbaa !5
  %12 = tail call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %0)
  %13 = fptosi double %12 to i64
  %.not23 = icmp slt i64 %13, 2
  br i1 %.not23, label %._crit_edge, label %.lr.ph26

._crit_edge:                                      ; preds = %.loopexit, %scalar.ph
  ret void

.lr.ph26:                                         ; preds = %scalar.ph, %.loopexit
  %.01724 = phi i64 [ %20, %.loopexit ], [ 2, %scalar.ph ]
  %14 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %.01724
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 1
  %17 = mul nuw nsw i64 %.01724, %.01724
  %.not1921 = icmp sgt i64 %17, %0
  %or.cond = select i1 %16, i1 true, i1 %.not1921
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph26, %.lr.ph
  %.022 = phi i64 [ %19, %.lr.ph ], [ %17, %.lr.ph26 ]
  %18 = getelementptr inbounds [1000005 x i32], ptr @prime, i64 0, i64 %.022
  store i32 2, ptr %18, align 4, !tbaa !5
  %19 = add nuw nsw i64 %.022, %.01724
  %.not19 = icmp sgt i64 %19, %0
  br i1 %.not19, label %.loopexit, label %.lr.ph, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph26
  %20 = add nuw i64 %.01724, 1
  %exitcond27.not = icmp eq i64 %.01724, %13
  br i1 %exitcond27.not, label %._crit_edge, label %.lr.ph26, !llvm.loop !14
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef) local_unnamed_addr #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 = { inlinehint 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 = !{!"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,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass