; LLVM IR from Shah-Shishir/Codeforces-Solutions :: 230B. T-primes.ll (Z5sievev)

; 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 [1000001 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5sievev() #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 i8 1, ptr getelementptr inbounds ([1000001 x i8], ptr @prime, i64 0, i64 1), align 1, !tbaa !5
  store i8 1, ptr @prime, align 16, !tbaa !5
  store i32 4, ptr %1, align 4, !tbaa !9
  br label %3

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

6:                                                ; preds = %3
  %7 = load i32, ptr %1, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %8
  store i8 1, ptr %9, align 1, !tbaa !5
  br label %10

10:                                               ; preds = %6
  %11 = load i32, ptr %1, align 4, !tbaa !9
  %12 = add nsw i32 %11, 2
  store i32 %12, ptr %1, align 4, !tbaa !9
  br label %3, !llvm.loop !11

13:                                               ; preds = %3
  store i32 3, ptr %1, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %43, %13
  %15 = load i32, ptr %1, align 4, !tbaa !9
  %16 = load i32, ptr %1, align 4, !tbaa !9
  %17 = mul nsw i32 %15, %16
  %18 = icmp sle i32 %17, 1000001
  br i1 %18, label %19, label %46

19:                                               ; preds = %14
  %20 = load i32, ptr %1, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !5, !range !13, !noundef !14
  %24 = trunc i8 %23 to i1
  br i1 %24, label %42, label %25

25:                                               ; preds = %19
  %26 = load i32, ptr %1, align 4, !tbaa !9
  %27 = load i32, ptr %1, align 4, !tbaa !9
  %28 = mul nsw i32 %26, %27
  store i32 %28, ptr %2, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %36, %25
  %30 = load i32, ptr %2, align 4, !tbaa !9
  %31 = icmp slt i32 %30, 1000001
  br i1 %31, label %32, label %41

32:                                               ; preds = %29
  %33 = load i32, ptr %2, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %34
  store i8 1, ptr %35, align 1, !tbaa !5
  br label %36

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

41:                                               ; preds = %29
  br label %42

42:                                               ; preds = %41, %19
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %1, align 4, !tbaa !9
  %45 = add nsw i32 %44, 2
  store i32 %45, ptr %1, align 4, !tbaa !9
  br label %14, !llvm.loop !16

46:                                               ; preds = %14
  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 = { 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !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"

@prime = external dso_local local_unnamed_addr global [1000001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5sievev() local_unnamed_addr #0 {
  store i8 1, ptr getelementptr inbounds (i8, ptr @prime, i64 1), align 1, !tbaa !5
  store i8 1, ptr @prime, align 16, !tbaa !5
  br label %1

1:                                                ; preds = %10, %0
  %indvars.iv = phi i64 [ 4, %0 ], [ %indvars.iv.next.7, %10 ]
  %2 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv
  store i8 1, ptr %2, align 4, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 2
  %3 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next
  store i8 1, ptr %3, align 2, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 4
  %4 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.1
  store i8 1, ptr %4, align 4, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 6
  %5 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.2
  store i8 1, ptr %5, align 2, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 8
  %6 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.3
  store i8 1, ptr %6, align 4, !tbaa !5
  %indvars.iv.next.4 = add nuw nsw i64 %indvars.iv, 10
  %7 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.4
  store i8 1, ptr %7, align 2, !tbaa !5
  %indvars.iv.next.5 = add nuw nsw i64 %indvars.iv, 12
  %8 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.5
  store i8 1, ptr %8, align 4, !tbaa !5
  %9 = icmp ult i64 %indvars.iv, 999987
  br i1 %9, label %10, label %.preheader15, !llvm.loop !9

10:                                               ; preds = %1
  %indvars.iv.next.6 = add nuw nsw i64 %indvars.iv, 14
  %11 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv.next.6
  store i8 1, ptr %11, align 2, !tbaa !5
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 16
  br label %1

.preheader15:                                     ; preds = %1, %.loopexit
  %indvars.iv27 = phi i64 [ %indvars.iv.next28, %.loopexit ], [ 3, %1 ]
  %indvars.iv22 = phi i64 [ %indvars.iv.next23, %.loopexit ], [ 6, %1 ]
  %12 = phi i32 [ %23, %.loopexit ], [ 9, %1 ]
  %13 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv27
  %14 = load i8, ptr %13, align 1, !tbaa !5, !range !11, !noundef !12
  %15 = trunc nuw i8 %14 to i1
  %16 = icmp ugt i32 %12, 1000000
  %or.cond.not = or i1 %16, %15
  br i1 %or.cond.not, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader15
  %17 = zext nneg i32 %12 to i64
  br label %18

18:                                               ; preds = %.lr.ph, %18
  %indvars.iv24 = phi i64 [ %17, %.lr.ph ], [ %indvars.iv.next25, %18 ]
  %19 = getelementptr inbounds [1000001 x i8], ptr @prime, i64 0, i64 %indvars.iv24
  store i8 1, ptr %19, align 1, !tbaa !5
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, %indvars.iv22
  %20 = icmp ult i64 %indvars.iv.next25, 1000001
  br i1 %20, label %18, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %18, %.preheader15
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 2
  %21 = mul nuw nsw i64 %indvars.iv.next28, %indvars.iv.next28
  %22 = icmp ult i64 %21, 1000002
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 4
  %23 = trunc nuw i64 %21 to i32
  br i1 %22, label %.preheader15, label %24, !llvm.loop !14

24:                                               ; preds = %.loopexit
  ret void
}

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


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