; LLVM IR from upupming/algorithm :: kick-start/2021/RoundB/C. naive.ll (Z12get_primes_lx)

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

@primes = external dso_local global [1000000010 x i64], align 16
@cnt = external dso_local global i64, align 8
@st = external dso_local global [1000000010 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z12get_primes_lx(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %70, %1
  %7 = load i32, ptr %3, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = icmp sle i64 %8, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %6
  %13 = load i32, ptr %3, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1000000010 x i8], ptr @st, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !11, !range !13, !noundef !14
  %17 = trunc i8 %16 to i1
  br i1 %17, label %24, label %18

18:                                               ; preds = %12
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = load i64, ptr @cnt, align 8, !tbaa !5
  %22 = add nsw i64 %21, 1
  store i64 %22, ptr @cnt, align 8, !tbaa !5
  %23 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %21
  store i64 %20, ptr %23, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %18, %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %65, %24
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = sdiv i64 %30, %32
  %34 = icmp sle i64 %29, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %25
  store i32 5, ptr %4, align 4
  br label %68

36:                                               ; preds = %25
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = mul nsw i64 %40, %42
  %44 = icmp slt i64 %43, 1000000010
  br i1 %44, label %45, label %54

45:                                               ; preds = %36
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = mul nsw i64 %49, %51
  %53 = getelementptr inbounds [1000000010 x i8], ptr @st, i64 0, i64 %52
  store i8 1, ptr %53, align 1, !tbaa !11
  br label %54

54:                                               ; preds = %45, %36
  %55 = load i32, ptr %3, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %58
  %60 = load i64, ptr %59, align 8, !tbaa !5
  %61 = srem i64 %56, %60
  %62 = icmp eq i64 %61, 0
  br i1 %62, label %63, label %64

63:                                               ; preds = %54
  store i32 5, ptr %4, align 4
  br label %68

64:                                               ; preds = %54
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %5, align 4, !tbaa !9
  br label %25, !llvm.loop !15

68:                                               ; preds = %63, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %69

69:                                               ; preds = %68
  br label %70

70:                                               ; preds = %69
  %71 = load i32, ptr %3, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %3, align 4, !tbaa !9
  br label %6, !llvm.loop !17

73:                                               ; preds = %11
  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 = { 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

@primes = external dso_local local_unnamed_addr global [1000000010 x i64], align 16
@cnt = external dso_local local_unnamed_addr global i64, align 8
@st = external dso_local local_unnamed_addr global [1000000010 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z12get_primes_lx(i64 noundef %0) local_unnamed_addr #0 {
  %.not24 = icmp slt i64 %0, 2
  br i1 %.not24, label %._crit_edge28, label %.lr.ph27.preheader

.lr.ph27.preheader:                               ; preds = %1
  %cnt.promoted = load i64, ptr @cnt, align 8, !tbaa !5
  br label %.lr.ph27

._crit_edge28:                                    ; preds = %._crit_edge, %1
  ret void

.lr.ph27:                                         ; preds = %.lr.ph27.preheader, %._crit_edge
  %indvars.iv30 = phi i64 [ 2, %.lr.ph27.preheader ], [ %indvars.iv.next31, %._crit_edge ]
  %2 = phi i64 [ %cnt.promoted, %.lr.ph27.preheader ], [ %10, %._crit_edge ]
  %3 = getelementptr inbounds [1000000010 x i8], ptr @st, i64 0, i64 %indvars.iv30
  %4 = load i8, ptr %3, align 1, !tbaa !9, !range !11, !noundef !12
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %9, label %6

6:                                                ; preds = %.lr.ph27
  %7 = add nsw i64 %2, 1
  store i64 %7, ptr @cnt, align 8, !tbaa !5
  %8 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %2
  store i64 %indvars.iv30, ptr %8, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %6, %.lr.ph27
  %10 = phi i64 [ %7, %6 ], [ %2, %.lr.ph27 ]
  %11 = udiv i64 %0, %indvars.iv30
  %12 = load i64, ptr @primes, align 16, !tbaa !5
  %.not2021 = icmp sgt i64 %12, %11
  br i1 %.not2021, label %._crit_edge, label %.lr.ph

13:                                               ; preds = %21
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = getelementptr inbounds [1000000010 x i64], ptr @primes, i64 0, i64 %indvars.iv.next
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %.not20 = icmp sgt i64 %15, %11
  br i1 %.not20, label %._crit_edge, label %.lr.ph, !llvm.loop !13

.lr.ph:                                           ; preds = %9, %13
  %indvars.iv = phi i64 [ %indvars.iv.next, %13 ], [ 0, %9 ]
  %16 = phi i64 [ %15, %13 ], [ %12, %9 ]
  %17 = mul nsw i64 %16, %indvars.iv30
  %18 = icmp slt i64 %17, 1000000010
  br i1 %18, label %19, label %21

19:                                               ; preds = %.lr.ph
  %20 = getelementptr inbounds [1000000010 x i8], ptr @st, i64 0, i64 %17
  store i8 1, ptr %20, align 1, !tbaa !9
  br label %21

21:                                               ; preds = %19, %.lr.ph
  %22 = srem i64 %indvars.iv30, %16
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %._crit_edge, label %13

._crit_edge:                                      ; preds = %13, %21, %9
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond = icmp eq i64 %indvars.iv30, %0
  br i1 %exitcond, label %._crit_edge28, label %.lr.ph27, !llvm.loop !15
}

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

!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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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