; LLVM IR from TLE-MEC/Hack-CP-DSA :: Codeforces/T-Primes/solution.ll (Z8allPrimev)

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

@_ZZ8allPrimevE7isPrime = external hidden global [1000001 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_Z8allPrimev() #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 0, ptr %1, align 4, !tbaa !5
  br label %5

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

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

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

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

16:                                               ; preds = %8
  store i8 0, ptr @_ZZ8allPrimevE7isPrime, align 16, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 1), align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %49, %16
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = mul nsw i32 %18, %19
  %21 = icmp sle i32 %20, 1000000
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %17
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !9, !range !13, !noundef !14
  %28 = trunc i8 %27 to i1
  %29 = zext i1 %28 to i32
  %30 = icmp eq i32 %29, 1
  br i1 %30, label %31, label %48

31:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = mul nsw i32 %32, %33
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %43, %31
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = icmp sle i32 %36, 1000000
  br i1 %37, label %39, label %38

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

39:                                               ; preds = %35
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %41
  store i8 0, ptr %42, align 1, !tbaa !9
  br label %43

43:                                               ; preds = %39
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = add nsw i32 %45, %44
  store i32 %46, ptr %4, align 4, !tbaa !5
  br label %35, !llvm.loop !15

47:                                               ; preds = %38
  br label %48

48:                                               ; preds = %47, %23
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %2, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %2, align 4, !tbaa !5
  br label %17, !llvm.loop !16

52:                                               ; preds = %22
  ret ptr @_ZZ8allPrimevE7isPrime
}

; 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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !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"

@_ZZ8allPrimevE7isPrime = external hidden global [1000001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef nonnull ptr @_Z8allPrimev() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1000001) @_ZZ8allPrimevE7isPrime, i8 1, i64 1000001, i1 false), !tbaa !5
  store i8 0, ptr @_ZZ8allPrimevE7isPrime, align 16, !tbaa !5
  store i8 0, ptr getelementptr inbounds (i8, ptr @_ZZ8allPrimevE7isPrime, i64 1), align 1, !tbaa !5
  br label %2

1:                                                ; preds = %.loopexit
  ret ptr @_ZZ8allPrimevE7isPrime

2:                                                ; preds = %.loopexit.1, %0
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %3 = phi i32 [ 4, %0 ], [ %20, %.loopexit.1 ]
  %4 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %indvars.iv
  %5 = load i8, ptr %4, align 2, !tbaa !5, !range !9, !noundef !10
  %6 = trunc nuw i8 %5 to i1
  %7 = icmp ult i32 %3, 1000001
  %or.cond = and i1 %7, %6
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %2
  %8 = zext nneg i32 %3 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv20 = phi i64 [ %8, %.lr.ph.preheader ], [ %indvars.iv.next21, %.lr.ph ]
  %9 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %indvars.iv20
  store i8 0, ptr %9, align 1, !tbaa !5
  %indvars.iv.next21 = add nuw nsw i64 %indvars.iv20, %indvars.iv
  %10 = icmp ult i64 %indvars.iv.next21, 1000001
  br i1 %10, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %2
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %11 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %exitcond.not = icmp eq i64 %indvars.iv, 1000
  br i1 %exitcond.not, label %1, label %12, !llvm.loop !13

12:                                               ; preds = %.loopexit
  %13 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %indvars.iv.next
  %14 = load i8, ptr %13, align 1, !tbaa !5, !range !9, !noundef !10
  %15 = trunc nuw i8 %14 to i1
  %16 = icmp ult i64 %11, 1000001
  %or.cond.1 = and i1 %16, %15
  br i1 %or.cond.1, label %.lr.ph.1, label %.loopexit.1

.lr.ph.1:                                         ; preds = %12, %.lr.ph.1
  %indvars.iv20.1 = phi i64 [ %indvars.iv.next21.1, %.lr.ph.1 ], [ %11, %12 ]
  %17 = getelementptr inbounds [1000001 x i8], ptr @_ZZ8allPrimevE7isPrime, i64 0, i64 %indvars.iv20.1
  store i8 0, ptr %17, align 1, !tbaa !5
  %indvars.iv.next21.1 = add nuw nsw i64 %indvars.iv20.1, %indvars.iv.next
  %18 = icmp ult i64 %indvars.iv.next21.1, 1000001
  br i1 %18, label %.lr.ph.1, label %.loopexit.1, !llvm.loop !11

.loopexit.1:                                      ; preds = %.lr.ph.1, %12
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %19 = mul nuw nsw i64 %indvars.iv.next.1, %indvars.iv.next.1
  %20 = trunc nuw i64 %19 to i32
  br label %2
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass