; LLVM IR from kimwalisch/primesieve :: src/PrimeGenerator.ll (ZN10primesieve14PrimeGenerator12sieveSegmentEv)

; 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.primesieve::Erat" = type { i64, i64, i64, i64, %"class.primesieve::Vector", i64, i64, %"class.primesieve::EratSmall", %"class.primesieve::EratBig", %"class.primesieve::EratMedium" }
%"class.primesieve::Vector" = type { ptr, ptr, ptr }
%"class.primesieve::EratSmall" = type { %"class.primesieve::Wheel", i64, i64, %"class.primesieve::Vector.0" }
%"class.primesieve::Wheel" = type { ptr, i64 }
%"class.primesieve::Vector.0" = type { ptr, ptr, ptr }
%"class.primesieve::EratBig" = type { %"class.primesieve::Wheel.1", i64, i64, i64, ptr, %"class.primesieve::Vector.2" }
%"class.primesieve::Wheel.1" = type { ptr, i64 }
%"class.primesieve::Vector.2" = type { ptr, ptr, ptr }
%"class.primesieve::EratMedium" = type { %"class.primesieve::Wheel", i64, ptr, %"class.primesieve::Vector.2", %"class.primesieve::Vector.2" }
%"class.primesieve::PrimeGenerator" = type { %"class.primesieve::Erat", i8, i64, i64, i64, %"class.primesieve::MemoryPool", %"class.primesieve::SievingPrimes" }
%"class.primesieve::MemoryPool" = type { ptr, i64, %"class.primesieve::Vector.3" }
%"class.primesieve::Vector.3" = type { ptr, ptr, ptr }
%"class.primesieve::SievingPrimes" = type { %"class.primesieve::Erat", i64, i64, i64, i64, i64, %"class.primesieve::Array", %"class.primesieve::Vector.4" }
%"class.primesieve::Array" = type { [128 x i64] }
%"class.primesieve::Vector.4" = type { ptr, ptr, ptr }

; 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 uwtable
define dso_local void @_ZN10primesieve14PrimeGenerator12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(1720) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %5 = getelementptr inbounds %"class.primesieve::Erat", ptr %4, i32 0, i32 3
  %6 = load i64, ptr %5, align 8, !tbaa !9
  %7 = call noundef i64 @_ZN12_GLOBAL__N_15isqrtImEET_S1_(i64 noundef %6)
  store i64 %7, ptr %3, align 8, !tbaa !20
  %8 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 4
  store i64 0, ptr %8, align 8, !tbaa !21
  %9 = getelementptr inbounds %"class.primesieve::Erat", ptr %4, i32 0, i32 2
  %10 = load i64, ptr %9, align 8, !tbaa !29
  %11 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 2
  store i64 %10, ptr %11, align 8, !tbaa !30
  %12 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 3
  %13 = load i64, ptr %12, align 8, !tbaa !31
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %19, label %15

15:                                               ; preds = %1
  %16 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 6
  %17 = call noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368) %16)
  %18 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 3
  store i64 %17, ptr %18, align 8, !tbaa !31
  br label %19

19:                                               ; preds = %15, %1
  br label %20

20:                                               ; preds = %25, %19
  %21 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 3
  %22 = load i64, ptr %21, align 8, !tbaa !31
  %23 = load i64, ptr %3, align 8, !tbaa !20
  %24 = icmp ule i64 %22, %23
  br i1 %24, label %25, label %31

25:                                               ; preds = %20
  %26 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !31
  call void @_ZN10primesieve4Erat15addSievingPrimeEm(ptr noundef nonnull align 8 dereferenceable(280) %4, i64 noundef %27)
  %28 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 6
  %29 = call noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368) %28)
  %30 = getelementptr inbounds %"class.primesieve::PrimeGenerator", ptr %4, i32 0, i32 3
  store i64 %29, ptr %30, align 8, !tbaa !31
  br label %20, !llvm.loop !32

31:                                               ; preds = %20
  call void @_ZN10primesieve4Erat12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(280) %4)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_15isqrtImEET_S1_(i64 noundef) #2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368)) #2 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN10primesieve4Erat15addSievingPrimeEm(ptr noundef nonnull align 8 dereferenceable(280), i64 noundef) #2 align 2

declare void @_ZN10primesieve4Erat12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(280)) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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" }
attributes #3 = { "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 #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 24}
!10 = !{!"_ZTSN10primesieve4EratE", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !11, i64 56, !11, i64 64, !13, i64 72, !16, i64 128, !19, i64 200}
!11 = !{!"long", !7, i64 0}
!12 = !{!"_ZTSN10primesieve6VectorIhSaIhEEE", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!"_ZTSN10primesieve9EratSmallE", !14, i64 0, !11, i64 16, !11, i64 24, !15, i64 32}
!14 = !{!"_ZTSN10primesieve5WheelILi30ELi8ELi6EXadsoKNS_9WheelInitEL_ZNS_11wheel30InitEEEEEE", !11, i64 8}
!15 = !{!"_ZTSN10primesieve6VectorINS_12SievingPrimeESaIS1_EEE", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!"_ZTSN10primesieve7EratBigE", !17, i64 0, !11, i64 16, !11, i64 24, !11, i64 32, !6, i64 40, !18, i64 48}
!17 = !{!"_ZTSN10primesieve5WheelILi210ELi48ELi10EXadsoKNS_9WheelInitEL_ZNS_12wheel210InitEEEEEE", !11, i64 8}
!18 = !{!"_ZTSN10primesieve6VectorIPNS_12SievingPrimeESaIS2_EEE", !6, i64 0, !6, i64 8, !6, i64 16}
!19 = !{!"_ZTSN10primesieve10EratMediumE", !14, i64 0, !11, i64 16, !6, i64 24, !18, i64 32, !18, i64 56}
!20 = !{!11, !11, i64 0}
!21 = !{!22, !11, i64 304}
!22 = !{!"_ZTSN10primesieve14PrimeGeneratorE", !10, i64 0, !23, i64 280, !11, i64 288, !11, i64 296, !11, i64 304, !24, i64 312, !26, i64 352}
!23 = !{!"bool", !7, i64 0}
!24 = !{!"_ZTSN10primesieve10MemoryPoolE", !6, i64 0, !11, i64 8, !25, i64 16}
!25 = !{!"_ZTSN10primesieve6VectorINS0_IcSaIcEEESaIS2_EEE", !6, i64 0, !6, i64 8, !6, i64 16}
!26 = !{!"_ZTSN10primesieve13SievingPrimesE", !10, i64 0, !11, i64 280, !11, i64 288, !11, i64 296, !11, i64 304, !11, i64 312, !27, i64 320, !28, i64 1344}
!27 = !{!"_ZTSN10primesieve5ArrayImLm128EEE", !7, i64 0}
!28 = !{!"_ZTSN10primesieve6VectorIbSaIbEEE", !6, i64 0, !6, i64 8, !6, i64 16}
!29 = !{!10, !11, i64 16}
!30 = !{!22, !11, i64 288}
!31 = !{!22, !11, i64 296}
!32 = distinct !{!32, !33}
!33 = !{!"llvm.loop.mustprogress"}


; 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: mustprogress uwtable
define dso_local void @_ZN10primesieve14PrimeGenerator12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(1720) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load i64, ptr %2, align 8, !tbaa !5
  %4 = tail call noundef i64 @_ZN12_GLOBAL__N_15isqrtImEET_S1_(i64 noundef %3)
  %5 = getelementptr inbounds i8, ptr %0, i64 304
  store i64 0, ptr %5, align 8, !tbaa !19
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !27
  %8 = getelementptr inbounds i8, ptr %0, i64 288
  store i64 %7, ptr %8, align 8, !tbaa !28
  %9 = getelementptr inbounds i8, ptr %0, i64 296
  %10 = load i64, ptr %9, align 8, !tbaa !29
  %.not = icmp eq i64 %10, 0
  br i1 %.not, label %11, label %14

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %0, i64 352
  %13 = tail call noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368) %12)
  store i64 %13, ptr %9, align 8, !tbaa !29
  br label %14

14:                                               ; preds = %11, %1
  %15 = phi i64 [ %13, %11 ], [ %10, %1 ]
  %.not12 = icmp ugt i64 %15, %4
  br i1 %.not12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %16 = getelementptr inbounds i8, ptr %0, i64 352
  br label %17

17:                                               ; preds = %.lr.ph, %17
  %18 = phi i64 [ %15, %.lr.ph ], [ %19, %17 ]
  tail call void @_ZN10primesieve4Erat15addSievingPrimeEm(ptr noundef nonnull align 8 dereferenceable(280) %0, i64 noundef %18)
  %19 = tail call noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368) %16)
  store i64 %19, ptr %9, align 8, !tbaa !29
  %.not1 = icmp ugt i64 %19, %4
  br i1 %.not1, label %._crit_edge, label %17, !llvm.loop !30

._crit_edge:                                      ; preds = %17, %14
  tail call void @_ZN10primesieve4Erat12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(280) %0)
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_15isqrtImEET_S1_(i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZN10primesieve13SievingPrimes4nextEv(ptr noundef nonnull align 8 dereferenceable(1368)) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZN10primesieve4Erat15addSievingPrimeEm(ptr noundef nonnull align 8 dereferenceable(280), i64 noundef) local_unnamed_addr #1 align 2

declare void @_ZN10primesieve4Erat12sieveSegmentEv(ptr noundef nonnull align 8 dereferenceable(280)) local_unnamed_addr #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 = { 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 #2 = { "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, !7, i64 24}
!6 = !{!"_ZTSN10primesieve4EratE", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !7, i64 56, !7, i64 64, !12, i64 72, !15, i64 128, !18, i64 200}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN10primesieve6VectorIhSaIhEEE", !11, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"_ZTSN10primesieve9EratSmallE", !13, i64 0, !7, i64 16, !7, i64 24, !14, i64 32}
!13 = !{!"_ZTSN10primesieve5WheelILi30ELi8ELi6EXadsoKNS_9WheelInitEL_ZNS_11wheel30InitEEEEEE", !7, i64 8}
!14 = !{!"_ZTSN10primesieve6VectorINS_12SievingPrimeESaIS1_EEE", !11, i64 0, !11, i64 8, !11, i64 16}
!15 = !{!"_ZTSN10primesieve7EratBigE", !16, i64 0, !7, i64 16, !7, i64 24, !7, i64 32, !11, i64 40, !17, i64 48}
!16 = !{!"_ZTSN10primesieve5WheelILi210ELi48ELi10EXadsoKNS_9WheelInitEL_ZNS_12wheel210InitEEEEEE", !7, i64 8}
!17 = !{!"_ZTSN10primesieve6VectorIPNS_12SievingPrimeESaIS2_EEE", !11, i64 0, !11, i64 8, !11, i64 16}
!18 = !{!"_ZTSN10primesieve10EratMediumE", !13, i64 0, !7, i64 16, !11, i64 24, !17, i64 32, !17, i64 56}
!19 = !{!20, !7, i64 304}
!20 = !{!"_ZTSN10primesieve14PrimeGeneratorE", !6, i64 0, !21, i64 280, !7, i64 288, !7, i64 296, !7, i64 304, !22, i64 312, !24, i64 352}
!21 = !{!"bool", !8, i64 0}
!22 = !{!"_ZTSN10primesieve10MemoryPoolE", !11, i64 0, !7, i64 8, !23, i64 16}
!23 = !{!"_ZTSN10primesieve6VectorINS0_IcSaIcEEESaIS2_EEE", !11, i64 0, !11, i64 8, !11, i64 16}
!24 = !{!"_ZTSN10primesieve13SievingPrimesE", !6, i64 0, !7, i64 280, !7, i64 288, !7, i64 296, !7, i64 304, !7, i64 312, !25, i64 320, !26, i64 1344}
!25 = !{!"_ZTSN10primesieve5ArrayImLm128EEE", !8, i64 0}
!26 = !{!"_ZTSN10primesieve6VectorIbSaIbEEE", !11, i64 0, !11, i64 8, !11, i64 16}
!27 = !{!6, !7, i64 16}
!28 = !{!20, !7, i64 288}
!29 = !{!20, !7, i64 296}
!30 = distinct !{!30, !31}
!31 = !{!"llvm.loop.mustprogress"}


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