; LLVM IR from MakeContributions/DSA :: algorithms/C/arrays (sieve-of-eratosthenes)

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

@sieve = external dso_local global [1000001 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @sieve_of_eratosthenes(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i32 1, ptr getelementptr inbounds ([1000001 x i32], ptr @sieve, i64 0, i64 1), align 4, !tbaa !9
  store i32 1, ptr @sieve, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 4, ptr %3, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %15, %1
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = icmp ule i64 %8, %9
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %18

12:                                               ; preds = %7
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %13
  store i32 1, ptr %14, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %12
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = add i64 %16, 2
  store i64 %17, ptr %3, align 8, !tbaa !5
  br label %7, !llvm.loop !11

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 3, ptr %4, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %44, %18
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = mul i64 %20, %21
  %23 = load i64, ptr %2, align 8, !tbaa !5
  %24 = icmp ule i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %19
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %47

26:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = mul i64 %27, %28
  store i64 %29, ptr %6, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %38, %26
  %31 = load i64, ptr %6, align 8, !tbaa !5
  %32 = load i64, ptr %2, align 8, !tbaa !5
  %33 = icmp ule i64 %31, %32
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %30
  %36 = load i64, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %36
  store i32 1, ptr %37, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %35
  %39 = load i64, ptr %4, align 8, !tbaa !5
  %40 = mul i64 2, %39
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = add i64 %41, %40
  store i64 %42, ptr %6, align 8, !tbaa !5
  br label %30, !llvm.loop !13

43:                                               ; preds = %34
  br label %44

44:                                               ; preds = %43
  %45 = load i64, ptr %4, align 8, !tbaa !5
  %46 = add i64 %45, 2
  store i64 %46, ptr %4, align 8, !tbaa !5
  br label %19, !llvm.loop !14

47:                                               ; preds = %25
  ret void
}

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

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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"

@sieve = external dso_local local_unnamed_addr global [1000001 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @sieve_of_eratosthenes(i64 noundef %0) local_unnamed_addr #0 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @sieve, i64 4), align 4, !tbaa !5
  store i32 1, ptr @sieve, align 16, !tbaa !5
  %.not22 = icmp ult i64 %0, 4
  br i1 %.not22, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %2 = add i64 %0, -4
  %3 = lshr i64 %2, 1
  %4 = add nuw i64 %3, 1
  %xtraiter = and i64 %4, 7
  %5 = icmp ult i64 %2, 14
  br i1 %5, label %.preheader21.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, -8
  br label %.lr.ph

.preheader21.unr-lcssa:                           ; preds = %.lr.ph, %.lr.ph.preheader
  %.01623.unr = phi i64 [ 4, %.lr.ph.preheader ], [ %23, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader21, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader21.unr-lcssa, %.lr.ph.epil
  %.01623.epil = phi i64 [ %7, %.lr.ph.epil ], [ %.01623.unr, %.preheader21.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader21.unr-lcssa ]
  %6 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %.01623.epil
  store i32 1, ptr %6, align 8, !tbaa !5
  %7 = add nuw i64 %.01623.epil, 2
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader21, label %.lr.ph.epil, !llvm.loop !9

.preheader21:                                     ; preds = %.lr.ph.epil, %.preheader21.unr-lcssa
  %.not1925 = icmp ult i64 %0, 9
  br i1 %.not1925, label %._crit_edge, label %.preheader

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01623 = phi i64 [ 4, %.lr.ph.preheader.new ], [ %23, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.7, %.lr.ph ]
  %8 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %.01623
  store i32 1, ptr %8, align 16, !tbaa !5
  %9 = or disjoint i64 %.01623, 2
  %10 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %9
  store i32 1, ptr %10, align 8, !tbaa !5
  %11 = add nuw nsw i64 %.01623, 4
  %12 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %11
  store i32 1, ptr %12, align 16, !tbaa !5
  %13 = add nuw nsw i64 %.01623, 6
  %14 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %13
  store i32 1, ptr %14, align 8, !tbaa !5
  %15 = add nuw nsw i64 %.01623, 8
  %16 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %15
  store i32 1, ptr %16, align 16, !tbaa !5
  %17 = add nuw nsw i64 %.01623, 10
  %18 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %17
  store i32 1, ptr %18, align 8, !tbaa !5
  %19 = add nuw i64 %.01623, 12
  %20 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %19
  store i32 1, ptr %20, align 16, !tbaa !5
  %21 = add nuw i64 %.01623, 14
  %22 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %21
  store i32 1, ptr %22, align 8, !tbaa !5
  %23 = add nuw i64 %.01623, 16
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %.preheader21.unr-lcssa, label %.lr.ph, !llvm.loop !11

.preheader:                                       ; preds = %.preheader21, %26
  %24 = phi i64 [ %28, %26 ], [ 9, %.preheader21 ]
  %.01526 = phi i64 [ %27, %26 ], [ 3, %.preheader21 ]
  %25 = shl i64 %.01526, 1
  br label %29

._crit_edge:                                      ; preds = %26, %1, %.preheader21
  ret void

26:                                               ; preds = %29
  %27 = add i64 %.01526, 2
  %28 = mul i64 %27, %27
  %.not19 = icmp ugt i64 %28, %0
  br i1 %.not19, label %._crit_edge, label %.preheader, !llvm.loop !13

29:                                               ; preds = %.preheader, %29
  %.024 = phi i64 [ %24, %.preheader ], [ %31, %29 ]
  %30 = getelementptr inbounds [1000001 x i32], ptr @sieve, i64 0, i64 %.024
  store i32 1, ptr %30, align 4, !tbaa !5
  %31 = add i64 %.024, %25
  %.not20 = icmp ugt i64 %31, %0
  br i1 %.not20, label %26, label %29, !llvm.loop !14
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, 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" }

!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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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