; LLVM IR from wuzhouhui/leetcode :: 204_count_primes (countPrimes)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @countPrimes(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 2
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 0, ptr %2, align 4
  br label %83

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = zext i32 %13 to i64
  %15 = call ptr @llvm.stacksave.p0()
  store ptr %15, ptr %7, align 8
  %16 = alloca i8, i64 %14, align 16
  store i64 %14, ptr %8, align 8
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %25, %12
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %21, label %28

21:                                               ; preds = %17
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %16, i64 %23
  store i8 1, ptr %24, align 1, !tbaa !9
  br label %25

25:                                               ; preds = %21
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %5, align 4, !tbaa !5
  br label %17, !llvm.loop !10

28:                                               ; preds = %17
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %59, %28
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = mul nsw i32 %30, %31
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %35, label %62

35:                                               ; preds = %29
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i8, ptr %16, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !9
  %40 = icmp ne i8 %39, 0
  br i1 %40, label %42, label %41

41:                                               ; preds = %35
  br label %59

42:                                               ; preds = %35
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = mul nsw i32 %43, %44
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %54, %42
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = icmp slt i32 %47, %48
  br i1 %49, label %50, label %58

50:                                               ; preds = %46
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %16, i64 %52
  store i8 0, ptr %53, align 1, !tbaa !9
  br label %54

54:                                               ; preds = %50
  %55 = load i32, ptr %5, align 4, !tbaa !5
  %56 = load i32, ptr %6, align 4, !tbaa !5
  %57 = add nsw i32 %56, %55
  store i32 %57, ptr %6, align 4, !tbaa !5
  br label %46, !llvm.loop !12

58:                                               ; preds = %46
  br label %59

59:                                               ; preds = %58, %41
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %5, align 4, !tbaa !5
  br label %29, !llvm.loop !13

62:                                               ; preds = %29
  store i32 0, ptr %4, align 4, !tbaa !5
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %77, %62
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %67, label %80

67:                                               ; preds = %63
  %68 = load i32, ptr %5, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds i8, ptr %16, i64 %69
  %71 = load i8, ptr %70, align 1, !tbaa !9
  %72 = icmp ne i8 %71, 0
  br i1 %72, label %73, label %76

73:                                               ; preds = %67
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %4, align 4, !tbaa !5
  br label %76

76:                                               ; preds = %73, %67
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %5, align 4, !tbaa !5
  br label %63, !llvm.loop !14

80:                                               ; preds = %63
  %81 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %81, ptr %2, align 4
  %82 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %82)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %83

83:                                               ; preds = %80, %11
  %84 = load i32, ptr %2, align 4
  ret i32 %84
}

; 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
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !11}


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

; Function Attrs: nofree norecurse nosync nounwind uwtable
define dso_local i32 @countPrimes(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 2
  br i1 %2, label %35, label %3

3:                                                ; preds = %1
  %4 = zext nneg i32 %0 to i64
  %5 = tail call ptr @llvm.stacksave.p0()
  %6 = alloca i8, i64 %4, align 16
  %.not42 = icmp eq i32 %0, 2
  br i1 %.not42, label %._crit_edge, label %.preheader33

.preheader33:                                     ; preds = %3
  %scevgep = getelementptr i8, ptr %6, i64 2
  %7 = add nsw i32 %0, -2
  %8 = zext nneg i32 %7 to i64
  call void @llvm.memset.p0.i64(ptr align 2 %scevgep, i8 1, i64 %8, i1 false), !tbaa !5
  %9 = icmp ugt i32 %0, 4
  br i1 %9, label %.lr.ph38, label %.lr.ph41.preheader

.preheader:                                       ; preds = %.loopexit
  br i1 %.not42, label %._crit_edge, label %.lr.ph41.preheader

.lr.ph41.preheader:                               ; preds = %.preheader33, %.preheader
  %smax = tail call i32 @llvm.smax.i32(i32 %0, i32 3)
  %wide.trip.count = zext nneg i32 %smax to i64
  %10 = add nsw i64 %wide.trip.count, -2
  %min.iters.check = icmp ult i64 %10, 8
  br i1 %min.iters.check, label %.lr.ph41.preheader55, label %vector.ph

.lr.ph41.preheader55:                             ; preds = %middle.block, %.lr.ph41.preheader
  %indvars.iv49.ph = phi i64 [ 2, %.lr.ph41.preheader ], [ %ind.end, %middle.block ]
  %.02540.ph = phi i32 [ 0, %.lr.ph41.preheader ], [ %20, %middle.block ]
  br label %.lr.ph41

vector.ph:                                        ; preds = %.lr.ph41.preheader
  %n.vec = and i64 %10, -8
  %ind.end = or disjoint i64 %n.vec, 2
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %17, %vector.body ]
  %vec.phi53 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %18, %vector.body ]
  %offset.idx = or disjoint i64 %index, 2
  %11 = getelementptr inbounds i8, ptr %6, i64 %offset.idx
  %12 = getelementptr inbounds i8, ptr %11, i64 4
  %wide.load = load <4 x i8>, ptr %11, align 2, !tbaa !5
  %wide.load54 = load <4 x i8>, ptr %12, align 2, !tbaa !5
  %13 = icmp ne <4 x i8> %wide.load, zeroinitializer
  %14 = icmp ne <4 x i8> %wide.load54, zeroinitializer
  %15 = zext <4 x i1> %13 to <4 x i32>
  %16 = zext <4 x i1> %14 to <4 x i32>
  %17 = add <4 x i32> %vec.phi, %15
  %18 = add <4 x i32> %vec.phi53, %16
  %index.next = add nuw i64 %index, 8
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !8

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %18, %17
  %20 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %10, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph41.preheader55

.lr.ph38:                                         ; preds = %.preheader33, %.loopexit
  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 2, %.preheader33 ]
  %21 = phi i32 [ %30, %.loopexit ], [ 4, %.preheader33 ]
  %22 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %.not31 = icmp ne i8 %23, 0
  %24 = icmp slt i32 %21, %0
  %or.cond = and i1 %.not31, %24
  br i1 %or.cond, label %.lr.ph36.preheader, label %.loopexit

.lr.ph36.preheader:                               ; preds = %.lr.ph38
  %25 = zext nneg i32 %21 to i64
  br label %.lr.ph36

.lr.ph36:                                         ; preds = %.lr.ph36.preheader, %.lr.ph36
  %indvars.iv45 = phi i64 [ %25, %.lr.ph36.preheader ], [ %indvars.iv.next46, %.lr.ph36 ]
  %26 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv45
  store i8 0, ptr %26, align 1, !tbaa !5
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, %indvars.iv
  %27 = trunc nuw i64 %indvars.iv.next46 to i32
  %28 = icmp slt i32 %27, %0
  br i1 %28, label %.lr.ph36, label %.loopexit, !llvm.loop !12

.loopexit:                                        ; preds = %.lr.ph36, %.lr.ph38
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %29 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %30 = trunc nuw i64 %29 to i32
  %31 = icmp slt i32 %30, %0
  br i1 %31, label %.lr.ph38, label %.preheader, !llvm.loop !13

.lr.ph41:                                         ; preds = %.lr.ph41.preheader55, %.lr.ph41
  %indvars.iv49 = phi i64 [ %indvars.iv.next50, %.lr.ph41 ], [ %indvars.iv49.ph, %.lr.ph41.preheader55 ]
  %.02540 = phi i32 [ %spec.select, %.lr.ph41 ], [ %.02540.ph, %.lr.ph41.preheader55 ]
  %32 = getelementptr inbounds i8, ptr %6, i64 %indvars.iv49
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %.not = icmp ne i8 %33, 0
  %34 = zext i1 %.not to i32
  %spec.select = add nuw nsw i32 %.02540, %34
  %indvars.iv.next50 = add nuw nsw i64 %indvars.iv49, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next50, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph41, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph41, %middle.block, %3, %.preheader
  %.025.lcssa = phi i32 [ 0, %.preheader ], [ 0, %3 ], [ %20, %middle.block ], [ %spec.select, %.lr.ph41 ]
  tail call void @llvm.stackrestore.p0(ptr %5)
  br label %35

35:                                               ; preds = %1, %._crit_edge
  %.0 = phi i32 [ %.025.lcssa, %._crit_edge ], [ 0, %1 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #3

attributes #0 = { nofree norecurse nosync 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9, !10, !11}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !9}
!13 = distinct !{!13, !9}
!14 = distinct !{!14, !9, !11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass