; LLVM IR from brpapa/algorithms :: notebook/math/factorial-prime-factors.ll (Z11build_sievev)

; 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::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"class.std::bitset<10000001>::reference" = type { ptr, i64 }

@primes = external dso_local global %"class.std::vector", align 8
@is_composite = external dso_local global { [156251 x i64] }, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11build_sievev() #0 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.std::bitset<10000001>::reference", align 8
  %2 = alloca %"class.std::bitset<10000001>::reference", align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca %"class.std::bitset<10000001>::reference", align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca %"class.std::bitset<10000001>::reference", align 8
  call void @_ZNSt6vectorIiSaIiEE5clearEv(ptr noundef nonnull align 8 dereferenceable(24) @primes) #3
  %11 = call noundef nonnull align 8 dereferenceable(1250008) ptr @_ZNSt6bitsetILm10000001EE5resetEv(ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #3
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind writable sret(%"class.std::bitset<10000001>::reference") align 8 %1, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef 1)
  %12 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16) %1, i1 noundef zeroext true) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %2) #3
  invoke void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind writable sret(%"class.std::bitset<10000001>::reference") align 8 %2, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef 0)
          to label %13 unwind label %19

13:                                               ; preds = %0
  %14 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSERKS1_(ptr noundef nonnull align 8 dereferenceable(16) %2, ptr noundef nonnull align 8 dereferenceable(16) %12) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %2) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %2) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %1) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 2, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %46, %13
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = icmp sle i64 %16, 10000000
  br i1 %17, label %23, label %18

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

19:                                               ; preds = %0
  %20 = landingpad { ptr, i32 }
          cleanup
  %21 = extractvalue { ptr, i32 } %20, 0
  store ptr %21, ptr %3, align 8
  %22 = extractvalue { ptr, i32 } %20, 1
  store i32 %22, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 16, ptr %2) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %1) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #3
  br label %50

23:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #3
  %24 = load i64, ptr %5, align 8, !tbaa !5
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind writable sret(%"class.std::bitset<10000001>::reference") align 8 %7, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef %24)
  %25 = call noundef zeroext i1 @_ZNKSt6bitsetILm10000001EE9referencecvbEv(ptr noundef nonnull align 8 dereferenceable(16) %7) #3
  %26 = xor i1 %25, true
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %7) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #3
  br i1 %26, label %27, label %45

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %8, align 4, !tbaa !9
  call void @_ZNSt6vectorIiSaIiEE9push_backEOi(ptr noundef nonnull align 8 dereferenceable(24) @primes, ptr noundef nonnull align 4 dereferenceable(4) %8)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %30 = load i64, ptr %5, align 8, !tbaa !5
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = mul nsw i64 %30, %31
  store i64 %32, ptr %9, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %40, %27
  %34 = load i64, ptr %9, align 8, !tbaa !5
  %35 = icmp sle i64 %34, 10000000
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 16, ptr %10) #3
  %38 = load i64, ptr %9, align 8, !tbaa !5
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind writable sret(%"class.std::bitset<10000001>::reference") align 8 %10, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef %38)
  %39 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16) %10, i1 noundef zeroext true) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %10) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %10) #3
  br label %40

40:                                               ; preds = %37
  %41 = load i64, ptr %5, align 8, !tbaa !5
  %42 = load i64, ptr %9, align 8, !tbaa !5
  %43 = add nsw i64 %42, %41
  store i64 %43, ptr %9, align 8, !tbaa !5
  br label %33, !llvm.loop !11

44:                                               ; preds = %36
  br label %45

45:                                               ; preds = %44, %23
  br label %46

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

49:                                               ; preds = %18
  ret void

50:                                               ; preds = %19
  %51 = load ptr, ptr %3, align 8
  %52 = load i32, ptr %4, align 4
  %53 = insertvalue { ptr, i32 } poison, ptr %51, 0
  %54 = insertvalue { ptr, i32 } %53, i32 %52, 1
  resume { ptr, i32 } %54
}

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind noalias writable sret(%"class.std::bitset<10000001>::reference") align 8, ptr noundef nonnull align 8 dereferenceable(1250008), i64 noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16), i1 noundef zeroext) #1 align 2

declare i32 @__gxx_personality_v0(...)

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6bitsetILm10000001EE9referencecvbEv(ptr noundef nonnull align 8 dereferenceable(16)) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backEOi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) #0 align 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 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }
%"class.std::bitset<10000001>::reference" = type { ptr, i64 }

@primes = external dso_local global %"class.std::vector", align 8
@is_composite = external dso_local global { [156251 x i64] }, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11build_sievev() local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %1 = alloca %"class.std::bitset<10000001>::reference", align 8
  %2 = alloca %"class.std::bitset<10000001>::reference", align 8
  %3 = alloca %"class.std::bitset<10000001>::reference", align 8
  %4 = alloca i32, align 4
  %5 = alloca %"class.std::bitset<10000001>::reference", align 8
  tail call void @_ZNSt6vectorIiSaIiEE5clearEv(ptr noundef nonnull align 8 dereferenceable(24) @primes) #3
  %6 = tail call noundef nonnull align 8 dereferenceable(1250008) ptr @_ZNSt6bitsetILm10000001EE5resetEv(ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #3
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind nonnull writable sret(%"class.std::bitset<10000001>::reference") align 8 %1, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef 1)
  %7 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16) %1, i1 noundef zeroext true) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #3
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind nonnull writable sret(%"class.std::bitset<10000001>::reference") align 8 %2, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef 0)
  %8 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSERKS1_(ptr noundef nonnull align 8 dereferenceable(16) %2, ptr noundef nonnull align 8 dereferenceable(16) %7) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %2) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %1) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #3
  br label %10

9:                                                ; preds = %.loopexit
  ret void

10:                                               ; preds = %0, %.loopexit
  %.01012 = phi i64 [ 2, %0 ], [ %19, %.loopexit ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind nonnull writable sret(%"class.std::bitset<10000001>::reference") align 8 %3, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef %.01012)
  %11 = call noundef zeroext i1 @_ZNKSt6bitsetILm10000001EE9referencecvbEv(ptr noundef nonnull align 8 dereferenceable(16) %3) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %3) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  br i1 %11, label %.loopexit, label %12

12:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %13 = trunc nuw i64 %.01012 to i32
  store i32 %13, ptr %4, align 4, !tbaa !5
  call void @_ZNSt6vectorIiSaIiEE9push_backEOi(ptr noundef nonnull align 8 dereferenceable(24) @primes, ptr noundef nonnull align 4 dereferenceable(4) %4)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  %14 = mul nuw nsw i64 %.01012, %.01012
  %15 = icmp ult i64 %14, 10000001
  br i1 %15, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %12, %.lr.ph
  %.011 = phi i64 [ %17, %.lr.ph ], [ %14, %12 ]
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #3
  call void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind nonnull writable sret(%"class.std::bitset<10000001>::reference") align 8 %5, ptr noundef nonnull align 8 dereferenceable(1250008) @is_composite, i64 noundef %.011)
  %16 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16) %5, i1 noundef zeroext true) #3
  call void @_ZNSt6bitsetILm10000001EE9referenceD2Ev(ptr noundef nonnull align 8 dereferenceable(16) %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #3
  %17 = add nuw nsw i64 %.011, %.01012
  %18 = icmp ult i64 %17, 10000001
  br i1 %18, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %12, %10
  %19 = add nuw nsw i64 %.01012, 1
  %exitcond.not = icmp eq i64 %19, 10000001
  br i1 %exitcond.not, label %9, label %10, !llvm.loop !11
}

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6bitsetILm10000001EEixEm(ptr dead_on_unwind noalias writable sret(%"class.std::bitset<10000001>::reference") align 8, ptr noundef nonnull align 8 dereferenceable(1250008), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSEb(ptr noundef nonnull align 8 dereferenceable(16), i1 noundef zeroext) local_unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(16) ptr @_ZNSt6bitsetILm10000001EE9referenceaSERKS1_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1 align 2

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6bitsetILm10000001EE9referencecvbEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backEOi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 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 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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