; LLVM IR from Tunghohin/Competitive_Coding :: Daimayuan/517.ll (Z12linear_sievei)

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

@cnt = external dso_local global i32, align 4
@p = external dso_local global [50000010 x i32], align 16
@primes = external dso_local global [50000010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z12linear_sievei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %73, %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %76

11:                                               ; preds = %6
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %27, label %17

17:                                               ; preds = %11
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %20
  store i32 %18, ptr %21, align 4, !tbaa !5
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = load i32, ptr @cnt, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr @cnt, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %25
  store i32 %22, ptr %26, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %17, %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %68, %27
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr @cnt, align 4, !tbaa !5
  %31 = icmp sle i32 %29, %30
  br i1 %31, label %32, label %41

32:                                               ; preds = %28
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = mul nsw i32 %36, %37
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = icmp sle i32 %38, %39
  br label %41

41:                                               ; preds = %32, %28
  %42 = phi i1 [ false, %28 ], [ %40, %32 ]
  br i1 %42, label %44, label %43

43:                                               ; preds = %41
  store i32 5, ptr %4, align 4
  br label %71

44:                                               ; preds = %41
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = mul nsw i32 %49, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %55
  store i32 %48, ptr %56, align 4, !tbaa !5
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = load i32, ptr %5, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %62
  %64 = load i32, ptr %63, align 4, !tbaa !5
  %65 = icmp eq i32 %60, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %44
  store i32 5, ptr %4, align 4
  br label %71

67:                                               ; preds = %44
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %5, align 4, !tbaa !5
  br label %28, !llvm.loop !9

71:                                               ; preds = %66, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %72

72:                                               ; preds = %71
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %3, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %3, align 4, !tbaa !5
  br label %6, !llvm.loop !11

76:                                               ; preds = %10
  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"

@cnt = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global [50000010 x i32], align 16
@primes = external dso_local local_unnamed_addr global [50000010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z12linear_sievei(i32 noundef %0) local_unnamed_addr #0 {
  %.not27 = icmp slt i32 %0, 2
  br i1 %.not27, label %._crit_edge, label %.lr.ph29.preheader

.lr.ph29.preheader:                               ; preds = %1
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %2 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %2 to i64
  br label %.lr.ph29

._crit_edge:                                      ; preds = %.critedge, %1
  ret void

.lr.ph29:                                         ; preds = %.lr.ph29.preheader, %.critedge
  %indvars.iv31 = phi i64 [ 2, %.lr.ph29.preheader ], [ %indvars.iv.next32, %.critedge ]
  %3 = phi i32 [ %cnt.promoted, %.lr.ph29.preheader ], [ %12, %.critedge ]
  %4 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %indvars.iv31
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not20 = icmp eq i32 %5, 0
  br i1 %.not20, label %6, label %11

6:                                                ; preds = %.lr.ph29
  %7 = trunc nuw nsw i64 %indvars.iv31 to i32
  store i32 %7, ptr %4, align 4, !tbaa !5
  %8 = add nsw i32 %3, 1
  store i32 %8, ptr @cnt, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %9
  store i32 %7, ptr %10, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %6, %.lr.ph29
  %12 = phi i32 [ %8, %6 ], [ %3, %.lr.ph29 ]
  %.not2123 = icmp slt i32 %12, 1
  br i1 %.not2123, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %11
  %13 = zext nneg i32 %12 to i64
  %14 = trunc nuw nsw i64 %indvars.iv31 to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %18
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %18 ]
  %15 = getelementptr inbounds [50000010 x i32], ptr @primes, i64 0, i64 %indvars.iv
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = mul nsw i32 %16, %14
  %.not22 = icmp sgt i32 %17, %0
  br i1 %.not22, label %.critedge, label %18

18:                                               ; preds = %.lr.ph
  %19 = sext i32 %17 to i64
  %20 = getelementptr inbounds [50000010 x i32], ptr @p, i64 0, i64 %19
  store i32 %16, ptr %20, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = icmp eq i32 %21, %16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.not21 = icmp uge i64 %indvars.iv, %13
  %or.cond = or i1 %22, %.not21
  br i1 %or.cond, label %.critedge, label %.lr.ph, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph, %18, %11
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next32, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph29, !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,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass