; LLVM IR from kimwalisch/primesieve :: src/IteratorHelper.ll (ZN10primesieve14IteratorHelper10updatePrevERmmRNS_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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10primesieve14IteratorHelper10updatePrevERmmRNS_12IteratorDataE(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, ptr noundef nonnull align 8 dereferenceable(1776) %2) #1 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 %4, align 8, !tbaa !5
  %13 = load i64, ptr %12, align 8, !tbaa !9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %14, i32 0, i32 0
  store i64 %13, ptr %15, align 8, !tbaa !17
  br label %22

16:                                               ; preds = %3
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr %17, align 8, !tbaa !9
  %19 = call noundef i64 @_ZN12_GLOBAL__N_110checkedSubEmm(i64 noundef %18, i64 noundef 1)
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %20, i32 0, i32 0
  store i64 %19, ptr %21, align 8, !tbaa !17
  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 %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %25, i32 0, i32 0
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %28, i32 0, i32 1
  %30 = load i64, ptr %29, align 8, !tbaa !18
  %31 = call noundef i64 @_ZN12_GLOBAL__N_111getPrevDistEmm(i64 noundef %27, i64 noundef %30)
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %32, i32 0, i32 1
  store i64 %31, ptr %33, align 8, !tbaa !18
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !17
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %37, i32 0, i32 1
  %39 = load i64, ptr %38, align 8, !tbaa !18
  %40 = call noundef i64 @_ZN12_GLOBAL__N_110checkedSubEmm(i64 noundef %36, i64 noundef %39)
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %40, ptr %41, align 8, !tbaa !9
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i64, ptr %43, align 8, !tbaa !9
  %45 = icmp uge i64 %42, %44
  br i1 %45, label %46, label %58

46:                                               ; preds = %22
  %47 = load i64, ptr %5, align 8, !tbaa !9
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %"struct.primesieve::IteratorData", ptr %48, i32 0, i32 0
  %50 = load i64, ptr %49, align 8, !tbaa !17
  %51 = icmp ule i64 %47, %50
  br i1 %51, label %52, label %58

52:                                               ; preds = %46
  %53 = load i64, ptr %5, align 8, !tbaa !9
  %54 = load i64, ptr %5, align 8, !tbaa !9
  %55 = call noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef %54)
  %56 = call noundef i64 @_ZN12_GLOBAL__N_110checkedSubEmm(i64 noundef %53, i64 noundef %55)
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  store i64 %56, ptr %57, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %52, %46, %22
  ret void
}

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

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

attributes #0 = { 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 #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" }

!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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10primesieve14IteratorHelper10updatePrevERmmRNS_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 #1 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 %0, align 8, !tbaa !15
  br i1 %6, label %10, label %8

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

10:                                               ; preds = %3, %8
  %storemerge = phi i64 [ %9, %8 ], [ %7, %3 ]
  store i64 %storemerge, ptr %2, 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_111getPrevDistEmm(i64 noundef %storemerge, i64 noundef %12)
  store i64 %13, ptr %11, align 8, !tbaa !17
  %14 = load i64, ptr %2, align 8, !tbaa !16
  %15 = tail call noundef i64 @_ZN12_GLOBAL__N_110checkedSubEmm(i64 noundef %14, i64 noundef %13)
  store i64 %15, ptr %0, align 8, !tbaa !15
  %.not = icmp ugt i64 %15, %1
  %16 = load i64, ptr %2, align 8
  %.not18 = icmp ult i64 %16, %1
  %or.cond = select i1 %.not, i1 true, i1 %.not18
  br i1 %or.cond, label %20, label %17

17:                                               ; preds = %10
  %18 = tail call noundef i64 @_ZN12_GLOBAL__N_111maxPrimeGapImEET_S1_(i64 noundef %1)
  %19 = tail call noundef i64 @_ZN12_GLOBAL__N_110checkedSubEmm(i64 noundef %1, i64 noundef %18)
  store i64 %19, ptr %0, align 8, !tbaa !15
  br label %20

20:                                               ; preds = %17, %10
  ret void
}

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

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

attributes #0 = { 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 #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" }

!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 = !{!7, !7, i64 0}
!16 = !{!6, !7, i64 0}
!17 = !{!6, !7, i64 8}


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