; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Codeforces/Codeforces Round 674 (Div 3)/a.ll (Z5sievev)

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

@spf = external dso_local global [200005 x i64], align 16

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5sievev() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  call void @llvm.memset.p0.i64(ptr align 16 @spf, i8 -1, i64 1600040, i1 false)
  store i64 1, ptr getelementptr inbounds ([200005 x i64], ptr @spf, i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr @spf, align 16, !tbaa !5
  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 = %40, %0
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = icmp slt i64 %5, 200005
  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 %43

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

13:                                               ; preds = %8
  %14 = load i64, ptr %1, align 8, !tbaa !5
  %15 = load i64, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds [200005 x i64], ptr @spf, i64 0, i64 %15
  store i64 %14, ptr %16, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %13, %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %18 = load i64, ptr %1, align 8, !tbaa !5
  %19 = load i64, ptr %1, align 8, !tbaa !5
  %20 = mul nsw i64 %18, %19
  store i64 %20, ptr %3, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %35, %17
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = icmp slt i64 %22, 200005
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %21
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds [200005 x i64], ptr @spf, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = icmp eq i64 %28, -1
  br i1 %29, label %30, label %34

30:                                               ; preds = %25
  %31 = load i64, ptr %1, align 8, !tbaa !5
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds [200005 x i64], ptr @spf, i64 0, i64 %32
  store i64 %31, ptr %33, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %30, %25
  br label %35

35:                                               ; preds = %34
  %36 = load i64, ptr %1, align 8, !tbaa !5
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = add nsw i64 %37, %36
  store i64 %38, ptr %3, align 8, !tbaa !5
  br label %21, !llvm.loop !9

39:                                               ; preds = %24
  br label %40

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

43:                                               ; preds = %7
  ret void
}

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

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

@spf = external dso_local local_unnamed_addr global [200005 x i64], align 16

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

1:                                                ; preds = %._crit_edge
  ret void

2:                                                ; preds = %0, %._crit_edge
  %.01315 = phi i64 [ 2, %0 ], [ %10, %._crit_edge ]
  %3 = getelementptr inbounds [200005 x i64], ptr @spf, i64 0, i64 %.01315
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i64 %4, -1
  br i1 %5, label %6, label %7

6:                                                ; preds = %2
  store i64 %.01315, ptr %3, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %6, %2
  %8 = mul nuw nsw i64 %.01315, %.01315
  %9 = icmp ult i64 %8, 200005
  br i1 %9, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %15, %7
  %10 = add nuw nsw i64 %.01315, 1
  %exitcond.not = icmp eq i64 %10, 200005
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !9

.lr.ph:                                           ; preds = %7, %15
  %.014 = phi i64 [ %16, %15 ], [ %8, %7 ]
  %11 = getelementptr inbounds [200005 x i64], ptr @spf, i64 0, i64 %.014
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = icmp eq i64 %12, -1
  br i1 %13, label %14, label %15

14:                                               ; preds = %.lr.ph
  store i64 %.01315, ptr %11, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %.lr.ph, %14
  %16 = add nuw nsw i64 %.014, %.01315
  %17 = icmp ult i64 %16, 200005
  br i1 %17, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: mustprogress 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 = { mustprogress 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 = !{!"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: DSEPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass