; LLVM IR from bqi343/cp-notebook :: Contests/USACO Training/Ch 6/6.4 prime3.ll (Z3genv)

; 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::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@prime = external dso_local global [100000 x %"class.std::vector"], align 16
@nozero = external dso_local global %"class.std::vector", align 8
@sum = external dso_local global i32, align 4
@ok = external dso_local global [100000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6getsumi(i32 noundef) #0

; 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3nozi(i32 noundef) #0

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

6:                                                ; preds = %58, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 100000
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %6
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = call noundef i32 @_Z6getsumi(i32 noundef %11)
  %13 = load i32, ptr @sum, align 4, !tbaa !5
  %14 = icmp eq i32 %12, %13
  br i1 %14, label %15, label %57

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  store i8 1, ptr %3, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 2, ptr %4, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %30, %15
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = srem i32 %17, %18
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  store i8 0, ptr %3, align 1, !tbaa !9
  store i32 5, ptr %2, align 4
  br label %33

22:                                               ; preds = %16
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = mul nsw i32 %23, %24
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = icmp sgt i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %22
  store i32 5, ptr %2, align 4
  br label %33

29:                                               ; preds = %22
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %4, align 4, !tbaa !5
  br label %16, !llvm.loop !11

33:                                               ; preds = %28, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %34

34:                                               ; preds = %33
  %35 = load i8, ptr %3, align 1, !tbaa !9, !range !13, !noundef !14
  %36 = trunc i8 %35 to i1
  br i1 %36, label %37, label %56

37:                                               ; preds = %34
  %38 = load i32, ptr %1, align 4, !tbaa !5
  %39 = call noundef zeroext i1 @_Z3nozi(i32 noundef %38)
  br i1 %39, label %40, label %41

40:                                               ; preds = %37
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) @nozero, ptr noundef nonnull align 4 dereferenceable(4) %1)
  br label %41

41:                                               ; preds = %40, %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %42 = load i32, ptr %1, align 4, !tbaa !5
  store i32 %42, ptr %5, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %46, %41
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %55

46:                                               ; preds = %43
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [100000 x %"class.std::vector"], ptr @prime, i64 0, i64 %48
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) %49, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [100000 x i32], ptr @ok, i64 0, i64 %51
  store i32 1, ptr %52, align 4, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = sdiv i32 %53, 10
  store i32 %54, ptr %5, align 4, !tbaa !5
  br label %43, !llvm.loop !15

55:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %56

56:                                               ; preds = %55, %34
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  br label %57

57:                                               ; preds = %56, %10
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !16

61:                                               ; preds = %9
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) #2 align 2

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 = { 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 #3 = { 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 = !{!10, !10, i64 0}
!10 = !{!"bool", !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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@prime = external dso_local global [100000 x %"class.std::vector"], align 16
@nozero = external dso_local global %"class.std::vector", align 8
@sum = external dso_local local_unnamed_addr global i32, align 4
@ok = external dso_local local_unnamed_addr global [100000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6getsumi(i32 noundef) local_unnamed_addr #0

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

; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_Z3nozi(i32 noundef) local_unnamed_addr #0

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

2:                                                ; preds = %.critedge
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #3
  ret void

3:                                                ; preds = %0, %.critedge
  %storemerge20 = phi i32 [ 10000, %0 ], [ %23, %.critedge ]
  %4 = call noundef i32 @_Z6getsumi(i32 noundef %storemerge20)
  %5 = load i32, ptr @sum, align 4, !tbaa !5
  %6 = icmp ne i32 %4, %5
  %.pre22 = load i32, ptr %1, align 4, !tbaa !5
  %7 = and i32 %.pre22, 1
  %.not15 = icmp eq i32 %7, 0
  %or.cond = select i1 %6, i1 true, i1 %.not15
  br i1 %or.cond, label %.critedge, label %.lr.ph

8:                                                ; preds = %.lr.ph
  %9 = add nuw nsw i32 %.0916, 1
  %10 = srem i32 %.pre22, %9
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %.critedge, label %.lr.ph, !llvm.loop !9

.lr.ph:                                           ; preds = %3, %8
  %.0916 = phi i32 [ %9, %8 ], [ 2, %3 ]
  %11 = mul nuw nsw i32 %.0916, %.0916
  %12 = icmp sgt i32 %11, %.pre22
  br i1 %12, label %13, label %8

13:                                               ; preds = %.lr.ph
  %14 = call noundef zeroext i1 @_Z3nozi(i32 noundef %.pre22)
  br i1 %14, label %15, label %16

15:                                               ; preds = %13
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) @nozero, ptr noundef nonnull align 4 dereferenceable(4) %1)
  br label %16

16:                                               ; preds = %15, %13
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %.not1217 = icmp eq i32 %17, 0
  br i1 %.not1217, label %.critedge, label %.lr.ph19

.lr.ph19:                                         ; preds = %16, %.lr.ph19
  %.018 = phi i32 [ %21, %.lr.ph19 ], [ %17, %16 ]
  %18 = sext i32 %.018 to i64
  %19 = getelementptr inbounds [100000 x %"class.std::vector"], ptr @prime, i64 0, i64 %18
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) %19, ptr noundef nonnull align 4 dereferenceable(4) %1)
  %20 = getelementptr inbounds [100000 x i32], ptr @ok, i64 0, i64 %18
  store i32 1, ptr %20, align 4, !tbaa !5
  %21 = sdiv i32 %.018, 10
  %.018.off = add i32 %.018, 9
  %.not12 = icmp ult i32 %.018.off, 19
  br i1 %.not12, label %.critedge.loopexit, label %.lr.ph19, !llvm.loop !11

.critedge.loopexit:                               ; preds = %.lr.ph19
  %.pre = load i32, ptr %1, align 4, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %8, %.critedge.loopexit, %16, %3
  %22 = phi i32 [ %.pre, %.critedge.loopexit ], [ 0, %16 ], [ %.pre22, %3 ], [ %.pre22, %8 ]
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %1, align 4, !tbaa !5
  %24 = icmp slt i32 %22, 99999
  br i1 %24, label %3, label %2, !llvm.loop !12
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2 align 2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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}
!12 = distinct !{!12, !10}


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