; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/COOK126B_PTUPLES.ll (Z5harryv)

; 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 [1000010 x i64], align 16
@storeRes = external dso_local global [1000010 x i64], align 16

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

5:                                                ; preds = %33, %0
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = mul nsw i64 %6, %7
  %9 = icmp sle i64 %8, 1000010
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %5
  %12 = load i64, ptr %1, align 8, !tbaa !5
  %13 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %12
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = icmp eq i64 %14, 0
  br i1 %15, label %16, label %32

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

20:                                               ; preds = %27, %16
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = icmp sle i64 %21, 1000010
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %20
  %25 = load i64, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %25
  store i64 1, ptr %26, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %24
  %28 = load i64, ptr %1, align 8, !tbaa !5
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr %3, align 8, !tbaa !5
  br label %20, !llvm.loop !9

31:                                               ; preds = %23
  br label %32

32:                                               ; preds = %31, %11
  br label %33

33:                                               ; preds = %32
  %34 = load i64, ptr %1, align 8, !tbaa !5
  %35 = add nsw i64 %34, 1
  store i64 %35, ptr %1, align 8, !tbaa !5
  br label %5, !llvm.loop !11

36:                                               ; preds = %10
  store i64 0, ptr @storeRes, align 16, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([1000010 x i64], ptr @storeRes, i64 0, i64 1), align 8, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([1000010 x i64], ptr @storeRes, i64 0, i64 2), align 16, !tbaa !5
  store i64 0, ptr getelementptr inbounds ([1000010 x i64], ptr @storeRes, i64 0, i64 3), align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 4, ptr %4, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %68, %36
  %38 = load i64, ptr %4, align 8, !tbaa !5
  %39 = icmp sle i64 %38, 1000010
  br i1 %39, label %41, label %40

40:                                               ; preds = %37
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %71

41:                                               ; preds = %37
  %42 = load i64, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = icmp eq i64 %44, 0
  br i1 %45, label %46, label %60

46:                                               ; preds = %41
  %47 = load i64, ptr %4, align 8, !tbaa !5
  %48 = sub nsw i64 %47, 2
  %49 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = icmp eq i64 %50, 0
  br i1 %51, label %52, label %60

52:                                               ; preds = %46
  %53 = load i64, ptr %4, align 8, !tbaa !5
  %54 = sub nsw i64 %53, 1
  %55 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %54
  %56 = load i64, ptr %55, align 8, !tbaa !5
  %57 = add nsw i64 %56, 1
  %58 = load i64, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %58
  store i64 %57, ptr %59, align 8, !tbaa !5
  br label %67

60:                                               ; preds = %46, %41
  %61 = load i64, ptr %4, align 8, !tbaa !5
  %62 = sub nsw i64 %61, 1
  %63 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %62
  %64 = load i64, ptr %63, align 8, !tbaa !5
  %65 = load i64, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %65
  store i64 %64, ptr %66, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %60, %52
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %4, align 8, !tbaa !5
  %70 = add nsw i64 %69, 1
  store i64 %70, ptr %4, align 8, !tbaa !5
  br label %37, !llvm.loop !12

71:                                               ; preds = %40
  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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !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 [1000010 x i64], align 16
@storeRes = external dso_local local_unnamed_addr global [1000010 x i64], align 16

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

1:                                                ; preds = %.loopexit
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) @storeRes, i8 0, i64 32, i1 false)
  br label %14

2:                                                ; preds = %0, %.loopexit
  %3 = phi i64 [ 4, %0 ], [ %12, %.loopexit ]
  %.01922 = phi i64 [ 2, %0 ], [ %11, %.loopexit ]
  %4 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %.01922
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, 0
  %7 = icmp ult i64 %3, 1000011
  %or.cond = and i1 %6, %7
  br i1 %or.cond, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01821 = phi i64 [ %9, %.lr.ph ], [ %3, %2 ]
  %8 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %.01821
  store i64 1, ptr %8, align 8, !tbaa !5
  %9 = add nuw nsw i64 %.01821, %.01922
  %10 = icmp ult i64 %9, 1000011
  br i1 %10, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %2
  %11 = add nuw nsw i64 %.01922, 1
  %12 = mul nuw nsw i64 %11, %11
  %exitcond.not = icmp eq i64 %11, 1001
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !11

13:                                               ; preds = %32
  ret void

14:                                               ; preds = %1, %32
  %.023 = phi i64 [ 4, %1 ], [ %34, %32 ]
  %15 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %.023
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %28

18:                                               ; preds = %14
  %19 = add nsw i64 %.023, -2
  %20 = getelementptr inbounds [1000010 x i64], ptr @prime, i64 0, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = icmp eq i64 %21, 0
  br i1 %22, label %23, label %28

23:                                               ; preds = %18
  %24 = add nsw i64 %.023, -1
  %25 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = add nsw i64 %26, 1
  br label %32

28:                                               ; preds = %18, %14
  %29 = add nsw i64 %.023, -1
  %30 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %23, %28
  %.sink = phi i64 [ %27, %23 ], [ %31, %28 ]
  %33 = getelementptr inbounds [1000010 x i64], ptr @storeRes, i64 0, i64 %.023
  store i64 %.sink, ptr %33, align 8, !tbaa !5
  %34 = add nuw nsw i64 %.023, 1
  %exitcond24.not = icmp eq i64 %34, 1000011
  br i1 %exitcond24.not, label %13, label %14, !llvm.loop !12
}

; 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 = { 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 = { 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 = !{!"long long", !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}
!12 = distinct !{!12, !10}


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