; LLVM IR from ravireddy07/cpp-book :: src/Coding-Blocks (DeepakAndPrimes)

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

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@sieve = external dso_local global [100000001 x i8], align 16
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

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

8:                                                ; preds = %38, %0
  %9 = load i32, ptr %3, align 4, !tbaa !9
  %10 = load i32, ptr %3, align 4, !tbaa !9
  %11 = mul nsw i32 %9, %10
  %12 = icmp sle i32 %11, 100000000
  br i1 %12, label %14, label %13

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

14:                                               ; preds = %8
  %15 = load i32, ptr %3, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5, !range !11, !noundef !12
  %19 = trunc i8 %18 to i1
  br i1 %19, label %37, label %20

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %21 = load i32, ptr %3, align 4, !tbaa !9
  %22 = load i32, ptr %3, align 4, !tbaa !9
  %23 = mul nsw i32 %21, %22
  store i32 %23, ptr %5, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %32, %20
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = icmp sle i32 %25, 100000000
  br i1 %26, label %28, label %27

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

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

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

36:                                               ; preds = %27
  br label %37

37:                                               ; preds = %36, %14
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %3, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %3, align 4, !tbaa !9
  br label %8, !llvm.loop !15

41:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 1, ptr %6, align 8, !tbaa !16
  br label %42

42:                                               ; preds = %60, %41
  %43 = load i32, ptr %2, align 4, !tbaa !9
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %48

45:                                               ; preds = %42
  %46 = load i64, ptr %6, align 8, !tbaa !16
  %47 = icmp slt i64 %46, 100000000
  br label %48

48:                                               ; preds = %45, %42
  %49 = phi i1 [ false, %42 ], [ %47, %45 ]
  br i1 %49, label %50, label %61

50:                                               ; preds = %48
  %51 = load i64, ptr %6, align 8, !tbaa !16
  %52 = add nsw i64 %51, 1
  store i64 %52, ptr %6, align 8, !tbaa !16
  %53 = load i64, ptr %6, align 8, !tbaa !16
  %54 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !5, !range !11, !noundef !12
  %56 = trunc i8 %55 to i1
  br i1 %56, label %60, label %57

57:                                               ; preds = %50
  %58 = load i32, ptr %2, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %2, align 4, !tbaa !9
  br label %60

60:                                               ; preds = %57, %50
  br label %42, !llvm.loop !18

61:                                               ; preds = %48
  %62 = load i64, ptr %6, align 8, !tbaa !16
  %63 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %62)
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %64 = load i32, ptr %1, align 4
  ret i32 %64
}

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

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #3 align 2

attributes #0 = { mustprogress norecurse 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 = { "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 #3 = { mustprogress 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 #4 = { 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{!17, !17, i64 0}
!17 = !{!"long long", !7, i64 0}
!18 = distinct !{!18, !14}


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

%"class.std::basic_istream" = type { ptr, i64, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }

@sieve = external dso_local local_unnamed_addr global [100000001 x i8], align 16
@_ZSt3cin = external global %"class.std::basic_istream", align 8
@_ZSt4cout = external global %"class.std::basic_ostream", align 8

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %2 = call noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16) @_ZSt3cin, ptr noundef nonnull align 4 dereferenceable(4) %1)
  store i8 1, ptr getelementptr inbounds (i8, ptr @sieve, i64 1), align 1, !tbaa !5
  store i8 1, ptr @sieve, align 16, !tbaa !5
  br label %3

.preheader:                                       ; preds = %.loopexit
  %.promoted = load i32, ptr %1, align 4, !tbaa !9
  %.not = icmp eq i32 %.promoted, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph24

3:                                                ; preds = %.loopexit.1, %0
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %4 = phi i32 [ 4, %0 ], [ %21, %.loopexit.1 ]
  %5 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %indvars.iv
  %6 = load i8, ptr %5, align 2, !tbaa !5, !range !11, !noundef !12
  %7 = trunc nuw i8 %6 to i1
  %8 = icmp ugt i32 %4, 100000000
  %or.cond.not = or i1 %8, %7
  br i1 %or.cond.not, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %9 = zext nneg i32 %4 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv26 = phi i64 [ %9, %.lr.ph.preheader ], [ %indvars.iv.next27, %.lr.ph ]
  %10 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %indvars.iv26
  store i8 1, ptr %10, align 1, !tbaa !5
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, %indvars.iv
  %11 = icmp ult i64 %indvars.iv.next27, 100000001
  br i1 %11, label %.lr.ph, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %3
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %12 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %exitcond.not = icmp eq i64 %indvars.iv, 10000
  br i1 %exitcond.not, label %.preheader, label %13, !llvm.loop !15

13:                                               ; preds = %.loopexit
  %14 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %indvars.iv.next
  %15 = load i8, ptr %14, align 1, !tbaa !5, !range !11, !noundef !12
  %16 = trunc nuw i8 %15 to i1
  %17 = icmp ugt i64 %12, 100000000
  %or.cond.not.1 = or i1 %17, %16
  br i1 %or.cond.not.1, label %.loopexit.1, label %.lr.ph.1

.lr.ph.1:                                         ; preds = %13, %.lr.ph.1
  %indvars.iv26.1 = phi i64 [ %indvars.iv.next27.1, %.lr.ph.1 ], [ %12, %13 ]
  %18 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %indvars.iv26.1
  store i8 1, ptr %18, align 1, !tbaa !5
  %indvars.iv.next27.1 = add nuw nsw i64 %indvars.iv26.1, %indvars.iv.next
  %19 = icmp ult i64 %indvars.iv.next27.1, 100000001
  br i1 %19, label %.lr.ph.1, label %.loopexit.1, !llvm.loop !13

.loopexit.1:                                      ; preds = %.lr.ph.1, %13
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %20 = mul nuw nsw i64 %indvars.iv.next.1, %indvars.iv.next.1
  %21 = trunc nuw i64 %20 to i32
  br label %3

.lr.ph24:                                         ; preds = %.preheader, %29
  %.023 = phi i64 [ %23, %29 ], [ 1, %.preheader ]
  %22 = phi i32 [ %30, %29 ], [ %.promoted, %.preheader ]
  %23 = add nuw nsw i64 %.023, 1
  %24 = getelementptr inbounds [100000001 x i8], ptr @sieve, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !5, !range !11, !noundef !12
  %26 = trunc nuw i8 %25 to i1
  br i1 %26, label %29, label %27

27:                                               ; preds = %.lr.ph24
  %28 = add nsw i32 %22, -1
  store i32 %28, ptr %1, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %27, %.lr.ph24
  %30 = phi i32 [ %28, %27 ], [ %22, %.lr.ph24 ]
  %31 = icmp ne i32 %30, 0
  %32 = icmp ult i64 %.023, 99999999
  %33 = and i1 %32, %31
  br i1 %33, label %.lr.ph24, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %29, %.preheader
  %.0.lcssa = phi i64 [ 1, %.preheader ], [ %23, %29 ]
  %34 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i64 noundef %.0.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 0
}

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

declare noundef nonnull align 8 dereferenceable(16) ptr @_ZNSirsERi(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEx(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #3 align 2

attributes #0 = { mustprogress norecurse 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 #3 = { mustprogress 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 #4 = { 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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