; LLVM IR from viveakrt/Competitive-programing :: Codeforces/1592A_Gamer_Hemose.ll (ZN13number_theory5sieveEi)

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

@_ZN13number_theory5primeE = external dso_local global [15000105 x i8], 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 @_ZN13number_theory5sieveEi(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !9
  br label %7

7:                                                ; preds = %16, %1
  %8 = load i64, ptr %3, align 8, !tbaa !9
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = icmp sle i64 %8, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %19

13:                                               ; preds = %7
  %14 = load i64, ptr %3, align 8, !tbaa !9
  %15 = getelementptr inbounds [15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 %14
  store i8 1, ptr %15, align 1, !tbaa !11
  br label %16

16:                                               ; preds = %13
  %17 = load i64, ptr %3, align 8, !tbaa !9
  %18 = add nsw i64 %17, 1
  store i64 %18, ptr %3, align 8, !tbaa !9
  br label %7, !llvm.loop !13

19:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 2, ptr %4, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %54, %19
  %21 = load i64, ptr %4, align 8, !tbaa !9
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = mul nsw i64 %21, %22
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = icmp sle i64 %23, %25
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %20
  %29 = load i64, ptr %4, align 8, !tbaa !9
  %30 = getelementptr inbounds [15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11, !range !15, !noundef !16
  %32 = trunc i8 %31 to i1
  %33 = zext i1 %32 to i32
  %34 = icmp eq i32 %33, 1
  br i1 %34, label %35, label %53

35:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %36 = load i64, ptr %4, align 8, !tbaa !9
  %37 = load i64, ptr %4, align 8, !tbaa !9
  %38 = mul nsw i64 %36, %37
  store i64 %38, ptr %6, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %48, %35
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = icmp sle i64 %40, %42
  br i1 %43, label %45, label %44

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

45:                                               ; preds = %39
  %46 = load i64, ptr %6, align 8, !tbaa !9
  %47 = getelementptr inbounds [15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 %46
  store i8 0, ptr %47, align 1, !tbaa !11
  br label %48

48:                                               ; preds = %45
  %49 = load i64, ptr %4, align 8, !tbaa !9
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = add nsw i64 %50, %49
  store i64 %51, ptr %6, align 8, !tbaa !9
  br label %39, !llvm.loop !17

52:                                               ; preds = %44
  br label %53

53:                                               ; preds = %52, %28
  br label %54

54:                                               ; preds = %53
  %55 = load i64, ptr %4, align 8, !tbaa !9
  %56 = add nsw i64 %55, 1
  store i64 %56, ptr %4, align 8, !tbaa !9
  br label %20, !llvm.loop !18

57:                                               ; preds = %27
  store i8 0, ptr @_ZN13number_theory5primeE, align 16, !tbaa !11
  store i8 0, ptr getelementptr inbounds ([15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 1), align 1, !tbaa !11
  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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !14}
!18 = distinct !{!18, !14}


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

@_ZN13number_theory5primeE = external dso_local local_unnamed_addr global [15000105 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_ZN13number_theory5sieveEi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %.not23 = icmp slt i32 %0, 0
  br i1 %.not23, label %._crit_edge, label %.preheader22

.preheader22:                                     ; preds = %1
  %3 = add nuw nsw i64 %2, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1) @_ZN13number_theory5primeE, i8 1, i64 %3, i1 false), !tbaa !5
  %.not2028 = icmp ult i32 %0, 4
  br i1 %.not2028, label %._crit_edge, label %.lr.ph30

._crit_edge:                                      ; preds = %.loopexit, %1, %.preheader22
  store i8 0, ptr @_ZN13number_theory5primeE, align 16, !tbaa !5
  store i8 0, ptr getelementptr inbounds (i8, ptr @_ZN13number_theory5primeE, i64 1), align 1, !tbaa !5
  ret void

.lr.ph30:                                         ; preds = %.preheader22, %.loopexit
  %4 = phi i64 [ %11, %.loopexit ], [ 4, %.preheader22 ]
  %.01629 = phi i64 [ %10, %.loopexit ], [ 2, %.preheader22 ]
  %5 = getelementptr inbounds [15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 %.01629
  %6 = load i8, ptr %5, align 1, !tbaa !5, !range !9, !noundef !10
  %7 = trunc nuw i8 %6 to i1
  %.not2125 = icmp sle i64 %4, %2
  %or.cond.not = and i1 %.not2125, %7
  br i1 %or.cond.not, label %.lr.ph27, label %.loopexit

.lr.ph27:                                         ; preds = %.lr.ph30, %.lr.ph27
  %.026 = phi i64 [ %9, %.lr.ph27 ], [ %4, %.lr.ph30 ]
  %8 = getelementptr inbounds [15000105 x i8], ptr @_ZN13number_theory5primeE, i64 0, i64 %.026
  store i8 0, ptr %8, align 1, !tbaa !5
  %9 = add nuw nsw i64 %.026, %.01629
  %.not21 = icmp sgt i64 %9, %2
  br i1 %.not21, label %.loopexit, label %.lr.ph27, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph27, %.lr.ph30
  %10 = add nuw nsw i64 %.01629, 1
  %11 = mul nuw nsw i64 %10, %10
  %.not20 = icmp sgt i64 %11, %2
  br i1 %.not20, label %._crit_edge, label %.lr.ph30, !llvm.loop !13
}

; Function Attrs: 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 = { 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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