; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions (APS)

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

@smallestPrime = external dso_local global [10000000 x i32], align 16
@sequence = external dso_local global [10000000 x i64], align 16

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

3:                                                ; preds = %13, %0
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = icmp slt i32 %4, 10000000
  br i1 %5, label %6, label %16

6:                                                ; preds = %3
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %8
  store i32 0, ptr %9, align 4, !tbaa !5
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %11
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %6
  %14 = load i32, ptr %1, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %1, align 4, !tbaa !5
  br label %3, !llvm.loop !11

16:                                               ; preds = %3
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %77, %16
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = icmp slt i32 %18, 10000000
  br i1 %19, label %20, label %80

20:                                               ; preds = %17
  %21 = load i32, ptr %1, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %61, label %26

26:                                               ; preds = %20
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = add nsw i32 %27, %28
  store i32 %29, ptr %2, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %45, %26
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = icmp slt i32 %31, 10000000
  br i1 %32, label %33, label %49

33:                                               ; preds = %30
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %44, label %39

39:                                               ; preds = %33
  %40 = load i32, ptr %1, align 4, !tbaa !5
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %42
  store i32 %40, ptr %43, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %39, %33
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add nsw i32 %47, %46
  store i32 %48, ptr %2, align 4, !tbaa !5
  br label %30, !llvm.loop !13

49:                                               ; preds = %30
  %50 = load i32, ptr %1, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = add nsw i64 %54, %56
  %58 = load i32, ptr %1, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %59
  store i64 %57, ptr %60, align 8, !tbaa !9
  br label %76

61:                                               ; preds = %20
  %62 = load i32, ptr %1, align 4, !tbaa !5
  %63 = sub nsw i32 %62, 1
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %64
  %66 = load i64, ptr %65, align 8, !tbaa !9
  %67 = load i32, ptr %1, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = add nsw i64 %66, %71
  %73 = load i32, ptr %1, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %74
  store i64 %72, ptr %75, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %61, %49
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %1, align 4, !tbaa !5
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %1, align 4, !tbaa !5
  br label %17, !llvm.loop !14

80:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !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"

@smallestPrime = external dso_local local_unnamed_addr global [10000000 x i32], align 16
@sequence = external dso_local local_unnamed_addr global [10000000 x i64], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @pre() local_unnamed_addr #0 {
.preheader.preheader:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40000000) @smallestPrime, i8 0, i64 40000000, i1 false), !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(80000000) @sequence, i8 0, i64 80000000, i1 false), !tbaa !9
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %20
  %indvars.iv28 = phi i64 [ 2, %.preheader.preheader ], [ %indvars.iv.next29, %20 ]
  %indvars.iv = phi i64 [ 4, %.preheader.preheader ], [ %indvars.iv.next, %20 ]
  %0 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %indvars.iv28
  %1 = load i32, ptr %0, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %2, label %14

2:                                                ; preds = %.preheader
  %3 = icmp ult i64 %indvars.iv28, 5000000
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %4 = trunc nuw nsw i64 %indvars.iv28 to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %8
  %indvars.iv30 = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %indvars.iv.next31, %8 ]
  %5 = getelementptr inbounds [10000000 x i32], ptr @smallestPrime, i64 0, i64 %indvars.iv30
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not23 = icmp eq i32 %6, 0
  br i1 %.not23, label %7, label %8

7:                                                ; preds = %.lr.ph
  store i32 %4, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %.lr.ph, %7
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, %indvars.iv28
  %9 = icmp ult i64 %indvars.iv.next31, 10000000
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %8, %2
  %10 = add nsw i64 %indvars.iv28, -1
  %11 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = add nsw i64 %12, %indvars.iv28
  br label %20

14:                                               ; preds = %.preheader
  %15 = add nsw i64 %indvars.iv28, -1
  %16 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = sext i32 %1 to i64
  %19 = add nsw i64 %17, %18
  br label %20

20:                                               ; preds = %._crit_edge, %14
  %.sink = phi i64 [ %13, %._crit_edge ], [ %19, %14 ]
  %21 = getelementptr inbounds [10000000 x i64], ptr @sequence, i64 0, i64 %indvars.iv28
  store i64 %.sink, ptr %21, align 8, !tbaa !9
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not = icmp eq i64 %indvars.iv.next29, 10000000
  br i1 %exitcond.not, label %22, label %.preheader, !llvm.loop !13

22:                                               ; preds = %20
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass