; LLVM IR from sgtlaugh/algovault :: code_library/fast_sieve.ll (Z15populate_primesj)

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

@prime_cnt = external dso_local global i32, align 4
@primes = external dso_local global [106000000 x i32], align 16
@is_composite = external dso_local global [8192 x i64], align 16

; 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 nounwind uwtable
define dso_local void @_Z15populate_primesj(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %47, %1
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = shl i32 %7, 7
  %9 = icmp ult i32 %8, 1048576
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %50

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds [8192 x i64], ptr @is_composite, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = xor i64 %15, -1
  store i64 %16, ptr %4, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %39, %11
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = icmp ne i64 %18, 0
  br i1 %19, label %20, label %46

20:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = shl i32 %22, 7
  %24 = add i32 %21, %23
  %25 = load i64, ptr %4, align 8, !tbaa !9
  %26 = call i64 @llvm.cttz.i64(i64 %25, i1 true)
  %27 = trunc i64 %26 to i32
  %28 = shl i32 %27, 1
  %29 = add i32 %24, %28
  %30 = add i32 %29, 1
  store i32 %30, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = icmp ule i32 %31, 2147483647
  br i1 %32, label %33, label %39

33:                                               ; preds = %20
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = load i32, ptr @prime_cnt, align 4, !tbaa !5
  %36 = add i32 %35, 1
  store i32 %36, ptr @prime_cnt, align 4, !tbaa !5
  %37 = zext i32 %35 to i64
  %38 = getelementptr inbounds [106000000 x i32], ptr @primes, i64 0, i64 %37
  store i32 %34, ptr %38, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %33, %20
  %40 = load i64, ptr %4, align 8, !tbaa !9
  %41 = sub i64 0, %40
  %42 = load i64, ptr %4, align 8, !tbaa !9
  %43 = and i64 %41, %42
  %44 = load i64, ptr %4, align 8, !tbaa !9
  %45 = xor i64 %44, %43
  store i64 %45, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %17, !llvm.loop !11

46:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = add i32 %48, 1
  store i32 %49, ptr %3, align 4, !tbaa !5
  br label %6, !llvm.loop !13

50:                                               ; preds = %10
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.cttz.i64(i64, i1 immarg) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!10, !10, i64 0}
!10 = !{!"long", !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"

@prime_cnt = external dso_local local_unnamed_addr global i32, align 4
@primes = external dso_local local_unnamed_addr global [106000000 x i32], align 16
@is_composite = external dso_local local_unnamed_addr global [8192 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z15populate_primesj(i32 noundef %0) local_unnamed_addr #0 {
  %prime_cnt.promoted17 = load i32, ptr @prime_cnt, align 4, !tbaa !5
  %2 = add i32 %0, 1
  br label %4

3:                                                ; preds = %._crit_edge
  ret void

4:                                                ; preds = %1, %._crit_edge
  %indvars.iv = phi i64 [ 0, %1 ], [ %indvars.iv.next, %._crit_edge ]
  %5 = phi i32 [ 0, %1 ], [ %25, %._crit_edge ]
  %prime_cnt.promoted2122 = phi i32 [ %prime_cnt.promoted17, %1 ], [ %prime_cnt.promoted20, %._crit_edge ]
  %6 = getelementptr inbounds [8192 x i64], ptr @is_composite, i64 0, i64 %indvars.iv
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %.not15 = icmp eq i64 %7, -1
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %8 = xor i64 %7, -1
  %9 = add i32 %2, %5
  br label %10

10:                                               ; preds = %.lr.ph, %21
  %prime_cnt.promoted19 = phi i32 [ %prime_cnt.promoted2122, %.lr.ph ], [ %prime_cnt.promoted18, %21 ]
  %.01216 = phi i64 [ %8, %.lr.ph ], [ %24, %21 ]
  %11 = phi i32 [ %prime_cnt.promoted2122, %.lr.ph ], [ %22, %21 ]
  %12 = tail call range(i64 0, 65) i64 @llvm.cttz.i64(i64 %.01216, i1 true)
  %13 = trunc nuw nsw i64 %12 to i32
  %14 = shl nuw nsw i32 %13, 1
  %15 = add i32 %9, %14
  %16 = icmp sgt i32 %15, -1
  br i1 %16, label %17, label %21

17:                                               ; preds = %10
  %18 = add i32 %11, 1
  store i32 %18, ptr @prime_cnt, align 4, !tbaa !5
  %19 = zext i32 %11 to i64
  %20 = getelementptr inbounds [106000000 x i32], ptr @primes, i64 0, i64 %19
  store i32 %15, ptr %20, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %17, %10
  %prime_cnt.promoted18 = phi i32 [ %18, %17 ], [ %prime_cnt.promoted19, %10 ]
  %22 = phi i32 [ %18, %17 ], [ %11, %10 ]
  %23 = add i64 %.01216, -1
  %24 = and i64 %23, %.01216
  %.not = icmp eq i64 %24, 0
  br i1 %.not, label %._crit_edge, label %10, !llvm.loop !11

._crit_edge:                                      ; preds = %21, %4
  %prime_cnt.promoted20 = phi i32 [ %prime_cnt.promoted2122, %4 ], [ %prime_cnt.promoted18, %21 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next.tr = trunc i64 %indvars.iv.next to i32
  %25 = shl i32 %indvars.iv.next.tr, 7
  %exitcond.not = icmp eq i64 %indvars.iv.next, 8192
  br i1 %exitcond.not, label %3, label %4, !llvm.loop !13
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.cttz.i64(i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass