; LLVM IR from lzyrapx/Competitive-Programming :: 51Nod/1181（素数筛）.ll (Z4initi)

; 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 [10001000 x i32], align 16
@num = external dso_local global [10001000 x i32], align 16
@k = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4initi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1000
  store i32 %6, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr getelementptr inbounds ([10001000 x i32], ptr @prime, i64 0, i64 1), align 4, !tbaa !5
  store i32 1, ptr @prime, align 16, !tbaa !5
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %7

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

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

17:                                               ; preds = %11
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = mul nsw i32 2, %18
  store i32 %19, ptr %4, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %28, %17
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %24, label %32

24:                                               ; preds = %20
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %26
  store i32 1, ptr %27, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %24
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr %4, align 4, !tbaa !5
  br label %20, !llvm.loop !9

32:                                               ; preds = %20
  br label %33

33:                                               ; preds = %32, %11
  br label %34

34:                                               ; preds = %33
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %3, align 4, !tbaa !5
  br label %7, !llvm.loop !11

37:                                               ; preds = %7
  store i32 0, ptr %4, align 4, !tbaa !5
  store i32 0, ptr @k, align 4, !tbaa !5
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %64, %37
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = icmp sle i32 %39, %40
  br i1 %41, label %42, label %67

42:                                               ; preds = %38
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !5
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %63, label %48

48:                                               ; preds = %42
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %4, align 4, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %62

56:                                               ; preds = %48
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = load i32, ptr @k, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr @k, align 4, !tbaa !5
  %60 = sext i32 %58 to i64
  %61 = getelementptr inbounds [10001000 x i32], ptr @num, i64 0, i64 %60
  store i32 %57, ptr %61, align 4, !tbaa !5
  br label %62

62:                                               ; preds = %56, %48
  br label %63

63:                                               ; preds = %62, %42
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %3, align 4, !tbaa !5
  br label %38, !llvm.loop !12

67:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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}
!12 = distinct !{!12, !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"

@prime = external dso_local local_unnamed_addr global [10001000 x i32], align 16
@num = external dso_local local_unnamed_addr global [10001000 x i32], align 16
@k = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4initi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = add i32 %0, 1000
  store i32 1, ptr getelementptr inbounds (i8, ptr @prime, i64 4), align 4, !tbaa !5
  store i32 1, ptr @prime, align 16, !tbaa !5
  %.not28 = icmp slt i32 %0, -998
  br i1 %.not28, label %._crit_edge.thread, label %.lr.ph30.preheader

._crit_edge.thread:                               ; preds = %1
  store i32 0, ptr @k, align 4, !tbaa !5
  br label %._crit_edge36

.lr.ph30.preheader:                               ; preds = %1
  %smax = tail call i32 @llvm.smax.i32(i32 %2, i32 2)
  %3 = add nuw i32 %smax, 1
  %wide.trip.count = zext i32 %3 to i64
  br label %.lr.ph30

.lr.ph30:                                         ; preds = %.lr.ph30.preheader, %.loopexit
  %indvars.iv37 = phi i64 [ 2, %.lr.ph30.preheader ], [ %indvars.iv.next38, %.loopexit ]
  %indvars.iv = phi i64 [ 4, %.lr.ph30.preheader ], [ %indvars.iv.next, %.loopexit ]
  %4 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %indvars.iv37
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not24 = icmp ne i32 %5, 0
  %indvars.iv37.tr = trunc i64 %indvars.iv37 to i32
  %6 = shl i32 %indvars.iv37.tr, 1
  %.not2526 = icmp sgt i32 %6, %2
  %or.cond = or i1 %.not24, %.not2526
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph30, %.lr.ph
  %indvars.iv39 = phi i64 [ %indvars.iv.next40, %.lr.ph ], [ %indvars.iv, %.lr.ph30 ]
  %7 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %indvars.iv39
  store i32 1, ptr %7, align 4, !tbaa !5
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, %indvars.iv37
  %8 = trunc nuw i64 %indvars.iv.next40 to i32
  %.not25 = icmp slt i32 %2, %8
  br i1 %.not25, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph30
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not = icmp eq i64 %indvars.iv.next38, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph30, !llvm.loop !11

._crit_edge:                                      ; preds = %.loopexit
  store i32 0, ptr @k, align 4, !tbaa !5
  br i1 %.not28, label %._crit_edge36, label %.lr.ph35.preheader

.lr.ph35.preheader:                               ; preds = %._crit_edge
  %smax46 = tail call i32 @llvm.smax.i32(i32 %2, i32 2)
  %9 = add nuw i32 %smax46, 1
  %wide.trip.count47 = zext i32 %9 to i64
  br label %.lr.ph35

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %24
  %indvars.iv43 = phi i64 [ 2, %.lr.ph35.preheader ], [ %indvars.iv.next44, %24 ]
  %.133 = phi i32 [ 0, %.lr.ph35.preheader ], [ %.2, %24 ]
  %10 = phi i32 [ 0, %.lr.ph35.preheader ], [ %25, %24 ]
  %11 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %indvars.iv43
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %.not23 = icmp eq i32 %12, 0
  br i1 %.not23, label %13, label %24

13:                                               ; preds = %.lr.ph35
  %14 = add nsw i32 %.133, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10001000 x i32], ptr @prime, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %24

19:                                               ; preds = %13
  %20 = add nsw i32 %10, 1
  store i32 %20, ptr @k, align 4, !tbaa !5
  %21 = sext i32 %10 to i64
  %22 = getelementptr inbounds [10001000 x i32], ptr @num, i64 0, i64 %21
  %23 = trunc nuw nsw i64 %indvars.iv43 to i32
  store i32 %23, ptr %22, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %.lr.ph35, %19, %13
  %25 = phi i32 [ %10, %.lr.ph35 ], [ %20, %19 ], [ %10, %13 ]
  %.2 = phi i32 [ %.133, %.lr.ph35 ], [ %14, %19 ], [ %14, %13 ]
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next44, %wide.trip.count47
  br i1 %exitcond48.not, label %._crit_edge36, label %.lr.ph35, !llvm.loop !12

._crit_edge36:                                    ; preds = %24, %._crit_edge.thread, %._crit_edge
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #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 nosync nounwind speculatable willreturn memory(none) }

!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}
!12 = distinct !{!12, !10}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass