; LLVM IR from upupming/algorithm :: acwing/算法竞赛进阶指南/0x11-栈/130. 火车进出栈问题.ll (Z9calPrimesv)

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

@n = external dso_local global i32, align 4
@primes = external dso_local global [120010 x i32], align 16
@st = external dso_local global [120010 x i32], align 16
@cnt = external dso_local global i32, align 4

; 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 @_Z9calPrimesv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %40, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = mul nsw i32 2, %6
  %8 = icmp sle i32 %5, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %4
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %43

10:                                               ; preds = %4
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [120010 x i32], ptr @st, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %39, label %16

16:                                               ; preds = %10
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr @cnt, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr @cnt, align 4, !tbaa !5
  %20 = sext i32 %18 to i64
  %21 = getelementptr inbounds [120010 x i32], ptr @primes, i64 0, i64 %20
  store i32 %17, ptr %21, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = mul nsw i32 2, %22
  store i32 %23, ptr %3, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %34, %16
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = mul nsw i32 2, %26
  %28 = icmp sle i32 %25, %27
  br i1 %28, label %30, label %29

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

30:                                               ; preds = %24
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [120010 x i32], ptr @st, i64 0, i64 %32
  store i32 1, ptr %33, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %30
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = add nsw i32 %36, %35
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %24, !llvm.loop !9

38:                                               ; preds = %29
  br label %39

39:                                               ; preds = %38, %10
  br label %40

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

43:                                               ; preds = %9
  ret void
}

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 = { 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"

@n = external dso_local local_unnamed_addr global i32, align 4
@primes = external dso_local local_unnamed_addr global [120010 x i32], align 16
@st = external dso_local local_unnamed_addr global [120010 x i32], align 16
@cnt = 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 @_Z9calPrimesv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = shl i32 %1, 1
  %.not14 = icmp slt i32 %1, 1
  br i1 %.not14, label %._crit_edge, label %.lr.ph16.preheader

.lr.ph16.preheader:                               ; preds = %0
  %cnt.promoted = load i32, ptr @cnt, align 4, !tbaa !5
  %smax = tail call i32 @llvm.smax.i32(i32 %2, i32 2)
  %3 = or disjoint i32 %smax, 1
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %.lr.ph16

._crit_edge:                                      ; preds = %.loopexit, %0
  ret void

.lr.ph16:                                         ; preds = %.lr.ph16.preheader, %.loopexit
  %indvars.iv17 = phi i64 [ 2, %.lr.ph16.preheader ], [ %indvars.iv.next18, %.loopexit ]
  %indvars.iv = phi i64 [ 4, %.lr.ph16.preheader ], [ %indvars.iv.next, %.loopexit ]
  %4 = phi i32 [ %cnt.promoted, %.lr.ph16.preheader ], [ %15, %.loopexit ]
  %5 = getelementptr inbounds [120010 x i32], ptr @st, i64 0, i64 %indvars.iv17
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not10 = icmp eq i32 %6, 0
  br i1 %.not10, label %7, label %.loopexit

7:                                                ; preds = %.lr.ph16
  %8 = add nsw i32 %4, 1
  store i32 %8, ptr @cnt, align 4, !tbaa !5
  %9 = sext i32 %4 to i64
  %10 = getelementptr inbounds [120010 x i32], ptr @primes, i64 0, i64 %9
  %11 = trunc nuw nsw i64 %indvars.iv17 to i32
  store i32 %11, ptr %10, align 4, !tbaa !5
  %indvars.iv17.tr = trunc i64 %indvars.iv17 to i32
  %12 = shl i32 %indvars.iv17.tr, 1
  %.not1112 = icmp sgt i32 %12, %2
  br i1 %.not1112, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %indvars.iv19 = phi i64 [ %indvars.iv.next20, %.lr.ph ], [ %indvars.iv, %7 ]
  %13 = getelementptr inbounds [120010 x i32], ptr @st, i64 0, i64 %indvars.iv19
  store i32 1, ptr %13, align 4, !tbaa !5
  %indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, %indvars.iv17
  %14 = trunc nuw i64 %indvars.iv.next20 to i32
  %.not11 = icmp slt i32 %2, %14
  br i1 %.not11, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %7, %.lr.ph16
  %15 = phi i32 [ %4, %.lr.ph16 ], [ %8, %7 ], [ %8, %.lr.ph ]
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not = icmp eq i64 %indvars.iv.next18, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph16, !llvm.loop !11
}

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


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