; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/181 Division 2/Problem E (E)

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

@lp = external dso_local global [10000001 x i32], align 16
@k = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@cnt = external dso_local global [10000001 x i32], align 16
@mx = external dso_local global i32, align 4
@cp = external dso_local global [10000001 x i64], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z8getSievev() #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 i32 @_Z1fx(i64 noundef) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %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
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store i32 0, ptr %1, align 4
  call void @_Z8getSievev()
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @k)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %27, %0
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr @k, align 4, !tbaa !5
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %30

15:                                               ; preds = %10
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a)
  %17 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @mx, ptr noundef nonnull align 4 dereferenceable(4) @a)
  %18 = load i32, ptr %17, align 4, !tbaa !5
  store i32 %18, ptr @mx, align 4, !tbaa !5
  %19 = load i32, ptr getelementptr inbounds ([10000001 x i32], ptr @cnt, i64 0, i64 1), align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr getelementptr inbounds ([10000001 x i32], ptr @cnt, i64 0, i64 1), align 4, !tbaa !5
  %21 = load i32, ptr @a, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %24, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %15
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %2, align 4, !tbaa !5
  br label %10, !llvm.loop !9

30:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %79, %30
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr @mx, align 4, !tbaa !5
  %34 = icmp sle i32 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %82

36:                                               ; preds = %31
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sub nsw i32 %37, 1
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = add nsw i32 %45, %41
  store i32 %46, ptr %44, align 4, !tbaa !5
  %47 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %47, ptr %4, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %77, %36
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = icmp sgt i32 %49, 1
  br i1 %50, label %51, label %78

51:                                               ; preds = %48
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [10000001 x i32], ptr @lp, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  store i32 %55, ptr %3, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %63, %51
  %57 = load i32, ptr %4, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [10000001 x i32], ptr @lp, i64 0, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !5
  %61 = load i32, ptr %3, align 4, !tbaa !5
  %62 = icmp eq i32 %60, %61
  br i1 %62, label %63, label %77

63:                                               ; preds = %56
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = load i32, ptr %3, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [10000001 x i64], ptr @cp, i64 0, i64 %70
  %72 = load i64, ptr %71, align 8, !tbaa !11
  %73 = add nsw i64 %72, %68
  store i64 %73, ptr %71, align 8, !tbaa !11
  %74 = load i32, ptr %3, align 4, !tbaa !5
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = sdiv i32 %75, %74
  store i32 %76, ptr %4, align 4, !tbaa !5
  br label %56, !llvm.loop !13

77:                                               ; preds = %56
  br label %48, !llvm.loop !14

78:                                               ; preds = %48
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %5, align 4, !tbaa !5
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %5, align 4, !tbaa !5
  br label %31, !llvm.loop !15

82:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %83 = load i32, ptr @mx, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  store i64 %84, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  store i64 35184372088832, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  br label %85

85:                                               ; preds = %102, %82
  %86 = load i64, ptr %6, align 8, !tbaa !11
  %87 = load i64, ptr %7, align 8, !tbaa !11
  %88 = icmp slt i64 %86, %87
  br i1 %88, label %89, label %103

89:                                               ; preds = %85
  %90 = load i64, ptr %6, align 8, !tbaa !11
  %91 = load i64, ptr %7, align 8, !tbaa !11
  %92 = add nsw i64 %90, %91
  %93 = ashr i64 %92, 1
  store i64 %93, ptr %8, align 8, !tbaa !11
  %94 = load i64, ptr %8, align 8, !tbaa !11
  %95 = call noundef i32 @_Z1fx(i64 noundef %94)
  %96 = icmp ne i32 %95, 0
  br i1 %96, label %97, label %99

97:                                               ; preds = %89
  %98 = load i64, ptr %8, align 8, !tbaa !11
  store i64 %98, ptr %7, align 8, !tbaa !11
  br label %102

99:                                               ; preds = %89
  %100 = load i64, ptr %8, align 8, !tbaa !11
  %101 = add nsw i64 %100, 1
  store i64 %101, ptr %6, align 8, !tbaa !11
  br label %102

102:                                              ; preds = %99, %97
  br label %85, !llvm.loop !16

103:                                              ; preds = %85
  %104 = load i64, ptr %7, align 8, !tbaa !11
  %105 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %104)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #4

declare i32 @printf(ptr noundef, ...) #3

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 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 #3 = { "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 = { inlinehint 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 #5 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !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"

@lp = external dso_local local_unnamed_addr global [10000001 x i32], align 16
@k = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@cnt = external dso_local local_unnamed_addr global [10000001 x i32], align 16
@mx = external dso_local global i32, align 4
@cp = external dso_local local_unnamed_addr global [10000001 x i64], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z8getSievev() local_unnamed_addr #0

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

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  tail call void @_Z8getSievev()
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @k)
  %2 = load i32, ptr @k, align 4, !tbaa !5
  %.not33 = icmp slt i32 %2, 1
  br i1 %.not33, label %..preheader_crit_edge, label %.lr.ph

..preheader_crit_edge:                            ; preds = %0
  %.pre = load i32, ptr @mx, align 4, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph, %..preheader_crit_edge
  %3 = phi i32 [ %.pre, %..preheader_crit_edge ], [ %7, %.lr.ph ]
  %.not3140 = icmp slt i32 %3, 1
  br i1 %.not3140, label %._crit_edge43, label %.lr.ph42.preheader

.lr.ph42.preheader:                               ; preds = %.preheader
  %4 = add nuw i32 %3, 1
  %wide.trip.count = zext i32 %4 to i64
  %.pre49 = load i32, ptr @cnt, align 16, !tbaa !5
  br label %.lr.ph42

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.034 = phi i32 [ %16, %.lr.ph ], [ 1, %0 ]
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a)
  %6 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @mx, ptr noundef nonnull align 4 dereferenceable(4) @a)
  %7 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %7, ptr @mx, align 4, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @cnt, i64 4), align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr getelementptr inbounds (i8, ptr @cnt, i64 4), align 4, !tbaa !5
  %10 = load i32, ptr @a, align 4, !tbaa !5
  %11 = add nsw i32 %10, 1
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = add nsw i32 %14, -1
  store i32 %15, ptr %13, align 4, !tbaa !5
  %16 = add nuw nsw i32 %.034, 1
  %17 = load i32, ptr @k, align 4, !tbaa !5
  %.not.not = icmp slt i32 %.034, %17
  br i1 %.not.not, label %.lr.ph, label %.preheader, !llvm.loop !9

._crit_edge43:                                    ; preds = %._crit_edge, %.preheader
  %18 = sext i32 %3 to i64
  br label %40

.lr.ph42:                                         ; preds = %.lr.ph42.preheader, %._crit_edge
  %19 = phi i32 [ %.pre49, %.lr.ph42.preheader ], [ %22, %._crit_edge ]
  %indvars.iv = phi i64 [ 1, %.lr.ph42.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %20 = getelementptr inbounds [10000001 x i32], ptr @cnt, i64 0, i64 %indvars.iv
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = add nsw i32 %21, %19
  store i32 %22, ptr %20, align 4, !tbaa !5
  %23 = icmp ugt i64 %indvars.iv, 1
  br i1 %23, label %.lr.ph39, label %._crit_edge

.lr.ph39:                                         ; preds = %.lr.ph42
  %24 = sext i32 %22 to i64
  %25 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.lr.ph36

.loopexit:                                        ; preds = %32
  store i64 %34, ptr %31, align 8, !tbaa !11
  %26 = icmp sgt i32 %35, 1
  br i1 %26, label %.lr.ph36, label %._crit_edge, !llvm.loop !13

.lr.ph36:                                         ; preds = %.loopexit, %.lr.ph39
  %.02738 = phi i32 [ %25, %.lr.ph39 ], [ %35, %.loopexit ]
  %27 = zext nneg i32 %.02738 to i64
  %28 = getelementptr inbounds [10000001 x i32], ptr @lp, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [10000001 x i64], ptr @cp, i64 0, i64 %30
  %.promoted = load i64, ptr %31, align 8, !tbaa !11
  br label %32

32:                                               ; preds = %.lr.ph36, %32
  %33 = phi i64 [ %.promoted, %.lr.ph36 ], [ %34, %32 ]
  %.12835 = phi i32 [ %.02738, %.lr.ph36 ], [ %35, %32 ]
  %34 = add nsw i64 %33, %24
  %35 = sdiv i32 %.12835, %29
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10000001 x i32], ptr @lp, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp eq i32 %38, %29
  br i1 %39, label %32, label %.loopexit, !llvm.loop !14

._crit_edge:                                      ; preds = %.loopexit, %.lr.ph42
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge43, label %.lr.ph42, !llvm.loop !15

40:                                               ; preds = %._crit_edge43, %40
  %.02346 = phi i64 [ 35184372088832, %._crit_edge43 ], [ %.1, %40 ]
  %.02445 = phi i64 [ %18, %._crit_edge43 ], [ %.125, %40 ]
  %41 = add nsw i64 %.02346, %.02445
  %42 = ashr i64 %41, 1
  %43 = tail call noundef i32 @_Z1fx(i64 noundef %42)
  %.not32 = icmp eq i32 %43, 0
  %44 = add nsw i64 %42, 1
  %.125 = select i1 %.not32, i64 %44, i64 %.02445
  %.1 = select i1 %.not32, i64 %.02346, i64 %42
  %45 = icmp slt i64 %.125, %.1
  br i1 %45, label %40, label %46, !llvm.loop !16

46:                                               ; preds = %40
  %47 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %.1)
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 norecurse 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 #2 = { nofree nounwind "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 = { inlinehint 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" }

!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 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !10}


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