; LLVM IR from kimwalisch/primesieve :: src/IteratorHelper.ll (ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE)

; 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::IteratorData" = type { i64, i64, i8, ptr, %"class.primesieve::Vector", [1720 x i8] }
%"class.primesieve::Vector" = type { ptr, ptr, ptr }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(1776) %2) #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %7, i32 0, i32 2
  %9 = load i8, ptr %8, align 8, !tbaa !11, !range !15, !noundef !16
  %10 = trunc i8 %9 to i1
  br i1 %10, label %11, label %16

11:                                               ; preds = %3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %12, i32 0, i32 0
  %14 = load i64, ptr %13, align 8, !tbaa !17
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %14, ptr %15, align 8, !tbaa !9
  br label %22

16:                                               ; preds = %3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !17
  %20 = call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %19, i64 noundef 1)
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %20, ptr %21, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %16, %11
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %23, i32 0, i32 2
  store i8 0, ptr %24, align 8, !tbaa !11
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %27, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !18
  %30 = call noundef i64 @_ZN12_GLOBAL__N_111getNextDistEmm(i64 noundef %26, i64 noundef %29)
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %31, i32 0, i32 1
  store i64 %30, ptr %32, align 8, !tbaa !18
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = icmp uge i64 %33, %35
  br i1 %36, label %37, label %48

37:                                               ; preds = %22
  %38 = load i64, ptr %5, align 8, !tbaa !9
  %39 = call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #3
  %40 = icmp ult i64 %38, %39
  br i1 %40, label %41, label %48

41:                                               ; preds = %37
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = call noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef %43)
  %45 = call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %42, i64 noundef %44)
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %46, i32 0, i32 0
  store i64 %45, ptr %47, align 8, !tbaa !17
  br label %57

48:                                               ; preds = %37, %22
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !18
  %54 = call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %50, i64 noundef %53)
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %55, i32 0, i32 0
  store i64 %54, ptr %56, align 8, !tbaa !17
  br label %57

57:                                               ; preds = %48, %41
  ret void
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111getNextDistEmm(i64 noundef, i64 noundef) #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef) #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 = { inlinehint 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 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 16}
!12 = !{!"_ZTSN10primesieve12IteratorDataE", !10, i64 0, !10, i64 8, !13, i64 16, !6, i64 24, !14, i64 32, !7, i64 56}
!13 = !{!"bool", !7, i64 0}
!14 = !{!"_ZTSN10primesieve6VectorImSaImEEE", !6, i64 0, !6, i64 8, !6, i64 16}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!12, !10, i64 0}
!18 = !{!12, !10, i64 8}


; 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 @_ZN10primesieve14IteratorHelper10updateNextERmmRNS_12IteratorDataE(ptr nocapture noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, ptr nocapture noundef nonnull align 8 dereferenceable(1776) %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %2, i64 16
  %5 = load i8, ptr %4, align 8, !tbaa !5, !range !13, !noundef !14
  %6 = trunc nuw i8 %5 to i1
  %7 = load i64, ptr %2, align 8, !tbaa !15
  br i1 %6, label %10, label %8

8:                                                ; preds = %3
  %9 = tail call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %7, i64 noundef 1)
  br label %10

10:                                               ; preds = %3, %8
  %storemerge = phi i64 [ %9, %8 ], [ %7, %3 ]
  store i64 %storemerge, ptr %0, align 8, !tbaa !16
  store i8 0, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %2, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !17
  %13 = tail call noundef i64 @_ZN12_GLOBAL__N_111getNextDistEmm(i64 noundef %storemerge, i64 noundef %12)
  store i64 %13, ptr %11, align 8, !tbaa !17
  %14 = load i64, ptr %0, align 8, !tbaa !16
  %.not = icmp ugt i64 %14, %1
  br i1 %.not, label %21, label %15

15:                                               ; preds = %10
  %16 = tail call noundef i64 @_ZNSt14numeric_limitsImE3maxEv() #3
  %17 = icmp ugt i64 %16, %1
  br i1 %17, label %18, label %._crit_edge

._crit_edge:                                      ; preds = %15
  %.pre = load i64, ptr %0, align 8, !tbaa !16
  %.pre18 = load i64, ptr %11, align 8, !tbaa !17
  br label %21

18:                                               ; preds = %15
  %19 = tail call noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef %1)
  %20 = tail call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %1, i64 noundef %19)
  br label %25

21:                                               ; preds = %._crit_edge, %10
  %22 = phi i64 [ %.pre18, %._crit_edge ], [ %13, %10 ]
  %23 = phi i64 [ %.pre, %._crit_edge ], [ %14, %10 ]
  %24 = tail call noundef i64 @_ZN12_GLOBAL__N_110checkedAddEmm(i64 noundef %23, i64 noundef %22)
  br label %25

25:                                               ; preds = %21, %18
  %storemerge17 = phi i64 [ %24, %21 ], [ %20, %18 ]
  store i64 %storemerge17, ptr %2, align 8, !tbaa !15
  ret void
}

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

; Function Attrs: mustprogress uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111getNextDistEmm(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNSt14numeric_limitsImE3maxEv() local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef) local_unnamed_addr #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 = { inlinehint 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 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 #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, !10, i64 16}
!6 = !{!"_ZTSN10primesieve12IteratorDataE", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24, !12, i64 32, !8, i64 56}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"bool", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"_ZTSN10primesieve6VectorImSaImEEE", !11, i64 0, !11, i64 8, !11, i64 16}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !7, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass