; LLVM IR from UtkarshPathrabe/Competitive-Coding :: HackerRank Solutions/Algorithms/Number Theory/Equations.ll (Z10Initializev)

; 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 = external dso_local global [1000009 x i32], align 16

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

4:                                                ; preds = %39, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = sdiv i32 1000009, %6
  %8 = icmp sle i32 %5, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %4
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %42

10:                                               ; preds = %4
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1000009 x i32], ptr @Prime, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %38, label %16

16:                                               ; preds = %10
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [1000009 x i32], ptr @Prime, i64 0, i64 %19
  store i32 %17, ptr %20, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = mul nsw i32 %21, %22
  store i32 %23, ptr %3, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %33, %16
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 1000009
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %37

28:                                               ; preds = %24
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1000009 x i32], ptr @Prime, i64 0, i64 %31
  store i32 %29, ptr %32, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %28
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add nsw i32 %35, %34
  store i32 %36, ptr %3, align 4, !tbaa !5
  br label %24, !llvm.loop !9

37:                                               ; preds = %27
  br label %38

38:                                               ; preds = %37, %10
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %1, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !11

42:                                               ; preds = %9
  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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; 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 = external dso_local local_unnamed_addr global [1000009 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z10Initializev() local_unnamed_addr #0 {
  br label %2

1:                                                ; preds = %.loopexit
  ret void

2:                                                ; preds = %0, %.loopexit
  %indvars.iv24 = phi i64 [ 5, %0 ], [ %indvars.iv.next25, %.loopexit ]
  %indvars.iv19 = phi i64 [ 2, %0 ], [ %indvars.iv.next20, %.loopexit ]
  %indvars.iv17 = phi i32 [ 4, %0 ], [ %indvars.iv.next18, %.loopexit ]
  %3 = getelementptr inbounds [1000009 x i32], ptr @Prime, i64 0, i64 %indvars.iv19
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %.not14 = icmp eq i32 %4, 0
  br i1 %.not14, label %5, label %.loopexit

5:                                                ; preds = %2
  %6 = trunc nuw nsw i64 %indvars.iv19 to i32
  store i32 %6, ptr %3, align 4, !tbaa !5
  %7 = mul nuw nsw i64 %indvars.iv19, %indvars.iv19
  %8 = icmp ult i64 %7, 1000009
  br i1 %8, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %5
  %9 = zext i32 %indvars.iv17 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %.lr.ph ], [ %9, %.lr.ph.preheader ]
  %10 = getelementptr inbounds [1000009 x i32], ptr @Prime, i64 0, i64 %indvars.iv21
  store i32 %6, ptr %10, align 4, !tbaa !5
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, %indvars.iv19
  %11 = icmp ult i64 %indvars.iv.next22, 1000009
  br i1 %11, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %5, %2
  %indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, 1
  %12 = trunc nuw nsw i64 %indvars.iv.next20 to i32
  %13 = udiv i32 1000009, %12
  %14 = zext nneg i32 %13 to i64
  %.not.not = icmp ult i64 %indvars.iv19, %14
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 2
  %15 = trunc nuw nsw i64 %indvars.iv24 to i32
  %indvars.iv.next18 = add i32 %indvars.iv17, %15
  br i1 %.not.not, label %2, label %1, !llvm.loop !11
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass