; LLVM IR from kimwalisch/primesieve :: src/iterator.ll (ZN10primesieve8iterator20generate_next_primesEv)

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

%"struct.primesieve::iterator" = type { i64, i64, i64, i64, ptr, ptr }
%"struct.primesieve::IteratorData" = type { i64, i64, i8, ptr, %"class.primesieve::Vector", [1720 x i8] }
%"class.primesieve::Vector" = type { ptr, ptr, ptr }

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10primesieve12IteratorData20deletePrimeGeneratorEv(ptr noundef nonnull align 8 dereferenceable(1776)) #1 align 2

; 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 @_ZN10primesieve8iterator20generate_next_primesEv(ptr noundef nonnull align 8 dereferenceable(48) %0) #2 align 2 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8
  %8 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 5
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp ne ptr %9, null
  br i1 %10, label %21, label %11

11:                                               ; preds = %1
  %12 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 1776) #8
  %13 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 2
  %14 = load i64, ptr %13, align 8, !tbaa !12
  invoke void @_ZN10primesieve12IteratorDataC2Em(ptr noundef nonnull align 8 dereferenceable(1776) %12, i64 noundef %14)
          to label %15 unwind label %17

15:                                               ; preds = %11
  %16 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 5
  store ptr %12, ptr %16, align 8, !tbaa !9
  br label %21

17:                                               ; preds = %11
  %18 = landingpad { ptr, i32 }
          cleanup
  %19 = extractvalue { ptr, i32 } %18, 0
  store ptr %19, ptr %3, align 8
  %20 = extractvalue { ptr, i32 } %18, 1
  store i32 %20, ptr %4, align 4
  call void @_ZdlPvm(ptr noundef %12, i64 noundef 1776) #9
  br label %65

21:                                               ; preds = %15, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #10
  %22 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 5
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  store ptr %23, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #10
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %24, i32 0, i32 4
  store ptr %25, ptr %6, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %64, %21
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %28, i32 0, i32 3
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %43, label %32

32:                                               ; preds = %27
  %33 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 2
  %34 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 3
  %35 = load i64, ptr %34, align 8, !tbaa !17
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8) %33, i64 noundef %35, ptr noundef nonnull align 8 dereferenceable(1776) %36)
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !12
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %40, i32 0, i32 0
  %42 = load i64, ptr %41, align 8, !tbaa !18
  call void @_ZN10primesieve12IteratorData17newPrimeGeneratorEmm(ptr noundef nonnull align 8 dereferenceable(1776) %37, i64 noundef %39, i64 noundef %42)
  br label %43

43:                                               ; preds = %32, %27
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %44, i32 0, i32 3
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 1
  call void @_ZN10primesieve14PrimeGenerator14fillNextPrimesERNS_6VectorImSaImEEEPm(ptr noundef nonnull align 8 dereferenceable(1720) %46, ptr noundef nonnull align 8 dereferenceable(24) %47, ptr noundef %48)
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = call noundef ptr @_ZN10primesieve6VectorImSaImEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %49) #10
  %51 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 4
  store ptr %50, ptr %51, align 8, !tbaa !19
  %52 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 0
  store i64 0, ptr %52, align 8, !tbaa !20
  %53 = getelementptr inbounds %"struct.primesieve::iterator", ptr %7, i32 0, i32 1
  %54 = load i64, ptr %53, align 8, !tbaa !21
  %55 = icmp eq i64 %54, 0
  %56 = xor i1 %55, true
  %57 = xor i1 %56, true
  %58 = zext i1 %57 to i64
  %59 = call i64 @llvm.expect.i64(i64 %58, i64 0)
  %60 = icmp ne i64 %59, 0
  br i1 %60, label %61, label %63

61:                                               ; preds = %43
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  call void @_ZN10primesieve12IteratorData20deletePrimeGeneratorEv(ptr noundef nonnull align 8 dereferenceable(1776) %62)
  br label %64

63:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #10
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #10
  ret void

64:                                               ; preds = %61
  br label %26, !llvm.loop !22

65:                                               ; preds = %17
  %66 = load ptr, ptr %3, align 8
  %67 = load i32, ptr %4, align 4
  %68 = insertvalue { ptr, i32 } poison, ptr %66, 0
  %69 = insertvalue { ptr, i32 } %68, i32 %67, 1
  resume { ptr, i32 } %69
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10primesieve12IteratorDataC2Em(ptr noundef nonnull align 8 dereferenceable(1776), i64 noundef) unnamed_addr #1 align 2

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #4

declare void @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef, ptr noundef nonnull align 8 dereferenceable(1776)) #5

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

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN10primesieve14PrimeGenerator14fillNextPrimesERNS_6VectorImSaImEEEPm(ptr noundef nonnull align 8 dereferenceable(1720), ptr noundef nonnull align 8 dereferenceable(24), ptr noundef) #6 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN10primesieve6VectorImSaImEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #7

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 = { 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 = { nobuiltin allocsize(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 #4 = { nobuiltin nounwind "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 = { "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 #6 = { alwaysinline 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 #7 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #8 = { builtin allocsize(0) }
attributes #9 = { builtin nounwind }
attributes #10 = { 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, !6, i64 40}
!10 = !{!"_ZTSN10primesieve8iteratorE", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 16}
!13 = !{!14, !6, i64 24}
!14 = !{!"_ZTSN10primesieve12IteratorDataE", !11, i64 0, !11, i64 8, !15, i64 16, !6, i64 24, !16, i64 32, !7, i64 56}
!15 = !{!"bool", !7, i64 0}
!16 = !{!"_ZTSN10primesieve6VectorImSaImEEE", !6, i64 0, !6, i64 8, !6, i64 16}
!17 = !{!10, !11, i64 24}
!18 = !{!14, !11, i64 0}
!19 = !{!10, !6, i64 32}
!20 = !{!10, !11, i64 0}
!21 = !{!10, !11, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"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"

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10primesieve12IteratorData20deletePrimeGeneratorEv(ptr noundef nonnull align 8 dereferenceable(1776)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10primesieve8iterator20generate_next_primesEv(ptr noundef nonnull align 8 dereferenceable(48) %0) local_unnamed_addr #1 align 2 personality ptr @__gxx_personality_v0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %8

4:                                                ; preds = %1
  %5 = tail call noalias noundef nonnull dereferenceable(1776) ptr @_Znwm(i64 noundef 1776) #5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !11
  tail call void @_ZN10primesieve12IteratorDataC2Em(ptr noundef nonnull align 8 dereferenceable(1776) %5, i64 noundef %7)
  store ptr %5, ptr %2, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %4, %1
  %9 = phi ptr [ %5, %4 ], [ %3, %1 ]
  %10 = getelementptr inbounds i8, ptr %9, i64 32
  %11 = getelementptr inbounds i8, ptr %9, i64 24
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  br label %16

16:                                               ; preds = %27, %8
  %17 = load ptr, ptr %11, align 8, !tbaa !12
  %.not10 = icmp eq ptr %17, null
  br i1 %.not10, label %18, label %22

18:                                               ; preds = %16
  %19 = load i64, ptr %13, align 8, !tbaa !16
  tail call void @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8) %12, i64 noundef %19, ptr noundef nonnull align 8 dereferenceable(1776) %9)
  %20 = load i64, ptr %12, align 8, !tbaa !11
  %21 = load i64, ptr %9, align 8, !tbaa !17
  tail call void @_ZN10primesieve12IteratorData17newPrimeGeneratorEmm(ptr noundef nonnull align 8 dereferenceable(1776) %9, i64 noundef %20, i64 noundef %21)
  %.pre = load ptr, ptr %11, align 8, !tbaa !12
  br label %22

22:                                               ; preds = %18, %16
  %23 = phi ptr [ %.pre, %18 ], [ %17, %16 ]
  tail call void @_ZN10primesieve14PrimeGenerator14fillNextPrimesERNS_6VectorImSaImEEEPm(ptr noundef nonnull align 8 dereferenceable(1720) %23, ptr noundef nonnull align 8 dereferenceable(24) %10, ptr noundef nonnull %14)
  %24 = tail call noundef ptr @_ZN10primesieve6VectorImSaImEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %10) #6
  store ptr %24, ptr %15, align 8, !tbaa !18
  store i64 0, ptr %0, align 8, !tbaa !19
  %25 = load i64, ptr %14, align 8, !tbaa !20
  %26 = icmp eq i64 %25, 0
  br i1 %26, label %27, label %28, !prof !21

27:                                               ; preds = %22
  tail call void @_ZN10primesieve12IteratorData20deletePrimeGeneratorEv(ptr noundef nonnull align 8 dereferenceable(1776) %9)
  br label %16, !llvm.loop !22

28:                                               ; preds = %22
  ret void
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN10primesieve12IteratorDataC2Em(ptr noundef nonnull align 8 dereferenceable(1776), i64 noundef) unnamed_addr #0 align 2

declare void @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef, ptr noundef nonnull align 8 dereferenceable(1776)) local_unnamed_addr #3

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

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local void @_ZN10primesieve14PrimeGenerator14fillNextPrimesERNS_6VectorImSaImEEEPm(ptr noundef nonnull align 8 dereferenceable(1720), ptr noundef nonnull align 8 dereferenceable(24), ptr noundef) local_unnamed_addr #4 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN10primesieve6VectorImSaImEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

attributes #0 = { 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 #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 = { nobuiltin allocsize(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 = { alwaysinline 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 = { builtin allocsize(0) }
attributes #6 = { 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, !10, i64 40}
!6 = !{!"_ZTSN10primesieve8iteratorE", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = !{!13, !10, i64 24}
!13 = !{!"_ZTSN10primesieve12IteratorDataE", !7, i64 0, !7, i64 8, !14, i64 16, !10, i64 24, !15, i64 32, !8, i64 56}
!14 = !{!"bool", !8, i64 0}
!15 = !{!"_ZTSN10primesieve6VectorImSaImEEE", !10, i64 0, !10, i64 8, !10, i64 16}
!16 = !{!6, !7, i64 24}
!17 = !{!13, !7, i64 0}
!18 = !{!6, !10, i64 32}
!19 = !{!6, !7, i64 0}
!20 = !{!6, !7, i64 8}
!21 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LowerExpectIntrinsicPass,SROAPass,SimplifyCFGPass,TailCallElimPass