; LLVM IR from hanzohasashi33/Competetive_programming :: Snippets/Sieve of Erathosthens/Sieve.ll (Z20sieveoferathosthenesxx)

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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z20sieveoferathosthenesxx(i64 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  %11 = load i64, ptr %4, align 8, !tbaa !5
  %12 = add nsw i64 %11, 1
  %13 = call ptr @llvm.stacksave.p0()
  store ptr %13, ptr %5, align 8
  %14 = alloca i8, i64 %12, align 16
  store i64 %12, ptr %6, align 8
  call void @llvm.memset.p0.i64(ptr align 16 %14, i8 1, i64 %12, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store i64 2, ptr %7, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %47, %2
  %16 = load i64, ptr %7, align 8, !tbaa !5
  %17 = load i64, ptr %7, align 8, !tbaa !5
  %18 = mul nsw i64 %16, %17
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = icmp sle i64 %18, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %15
  %23 = load i64, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %14, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9, !range !11, !noundef !12
  %26 = trunc i8 %25 to i1
  %27 = zext i1 %26 to i32
  %28 = icmp eq i32 %27, 1
  br i1 %28, label %29, label %46

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %7, align 8, !tbaa !5
  %32 = mul nsw i64 %30, %31
  store i64 %32, ptr %9, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %41, %29
  %34 = load i64, ptr %9, align 8, !tbaa !5
  %35 = load i64, ptr %4, align 8, !tbaa !5
  %36 = icmp sle i64 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %45

38:                                               ; preds = %33
  %39 = load i64, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %14, i64 %39
  store i8 0, ptr %40, align 1, !tbaa !9
  br label %41

41:                                               ; preds = %38
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = load i64, ptr %9, align 8, !tbaa !5
  %44 = add nsw i64 %43, %42
  store i64 %44, ptr %9, align 8, !tbaa !5
  br label %33, !llvm.loop !13

45:                                               ; preds = %37
  br label %46

46:                                               ; preds = %45, %22
  br label %47

47:                                               ; preds = %46
  %48 = load i64, ptr %7, align 8, !tbaa !5
  %49 = add nsw i64 %48, 1
  store i64 %49, ptr %7, align 8, !tbaa !5
  br label %15, !llvm.loop !15

50:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %51 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %51, ptr %10, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %72, %50
  %53 = load i64, ptr %10, align 8, !tbaa !5
  %54 = load i64, ptr %4, align 8, !tbaa !5
  %55 = icmp sle i64 %53, %54
  br i1 %55, label %57, label %56

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

57:                                               ; preds = %52
  %58 = load i64, ptr %10, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %14, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !9, !range !11, !noundef !12
  %61 = trunc i8 %60 to i1
  %62 = zext i1 %61 to i32
  %63 = icmp eq i32 %62, 1
  br i1 %63, label %64, label %71

64:                                               ; preds = %57
  %65 = load i64, ptr %10, align 8, !tbaa !5
  %66 = icmp ne i64 %65, 1
  br i1 %66, label %67, label %71

67:                                               ; preds = %64
  %68 = load i64, ptr %10, align 8, !tbaa !5
  %69 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %68)
  %70 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %69, ptr noundef @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %71

71:                                               ; preds = %67, %64, %57
  br label %72

72:                                               ; preds = %71
  %73 = load i64, ptr %10, align 8, !tbaa !5
  %74 = add nsw i64 %73, 1
  store i64 %74, ptr %10, align 8, !tbaa !5
  br label %52, !llvm.loop !16

75:                                               ; preds = %56
  %76 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %76)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #3

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #4

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

attributes #0 = { mustprogress 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 }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { inlinehint mustprogress 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 #5 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !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"

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z20sieveoferathosthenesxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = add i64 %1, 1
  %4 = alloca i8, i64 %3, align 16
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %4, i8 1, i64 %3, i1 false)
  %.not30 = icmp slt i64 %1, 4
  br i1 %.not30, label %.preheader, label %.lr.ph32

.preheader:                                       ; preds = %.loopexit, %2
  %.not2533 = icmp sgt i64 %0, %1
  br i1 %.not2533, label %._crit_edge, label %.lr.ph35

.lr.ph32:                                         ; preds = %2, %.loopexit
  %5 = phi i64 [ %12, %.loopexit ], [ 4, %2 ]
  %.02231 = phi i64 [ %11, %.loopexit ], [ 2, %2 ]
  %6 = getelementptr inbounds i8, ptr %4, i64 %.02231
  %7 = load i8, ptr %6, align 1, !tbaa !5, !range !9, !noundef !10
  %8 = trunc nuw i8 %7 to i1
  %.not2628 = icmp sle i64 %5, %1
  %or.cond37.not = and i1 %.not2628, %8
  br i1 %or.cond37.not, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph32, %.lr.ph
  %.02129 = phi i64 [ %10, %.lr.ph ], [ %5, %.lr.ph32 ]
  %9 = getelementptr inbounds i8, ptr %4, i64 %.02129
  store i8 0, ptr %9, align 1, !tbaa !5
  %10 = add nuw nsw i64 %.02129, %.02231
  %.not26 = icmp sgt i64 %10, %1
  br i1 %.not26, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph32
  %11 = add nuw nsw i64 %.02231, 1
  %12 = mul nuw nsw i64 %11, %11
  %.not = icmp sgt i64 %12, %1
  br i1 %.not, label %.preheader, label %.lr.ph32, !llvm.loop !13

._crit_edge:                                      ; preds = %20, %.preheader
  ret void

.lr.ph35:                                         ; preds = %.preheader, %20
  %.034 = phi i64 [ %21, %20 ], [ %0, %.preheader ]
  %13 = getelementptr inbounds i8, ptr %4, i64 %.034
  %14 = load i8, ptr %13, align 1, !tbaa !5, !range !9, !noundef !10
  %15 = trunc nuw i8 %14 to i1
  %16 = icmp ne i64 %.034, 1
  %or.cond = and i1 %16, %15
  br i1 %or.cond, label %17, label %20

17:                                               ; preds = %.lr.ph35
  %18 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %.034)
  %19 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) %18, ptr noundef nonnull @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  br label %20

20:                                               ; preds = %.lr.ph35, %17
  %21 = add i64 %.034, 1
  %exitcond.not = icmp eq i64 %.034, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph35, !llvm.loop !14
}

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #2

attributes #0 = { mustprogress 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 nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint mustprogress 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 = !{!"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}
!14 = distinct !{!14, !12}


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