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

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

@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7isPrimei(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 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
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = call noundef zeroext i1 @_Z7isPrimei(i32 noundef %6)
  br i1 %7, label %8, label %11

8:                                                ; preds = %0
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %9)
  br label %68

11:                                               ; preds = %0
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 2
  %14 = call noundef zeroext i1 @_Z7isPrimei(i32 noundef %13)
  br i1 %14, label %15, label %19

15:                                               ; preds = %11
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = sub nsw i32 %16, 2
  %18 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %17)
  br label %67

19:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 4, ptr %2, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %61, %19
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 2, ptr %3, align 4
  br label %64

25:                                               ; preds = %20
  %26 = load i32, ptr @n, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = sub nsw i32 %26, %27
  %29 = call noundef zeroext i1 @_Z7isPrimei(i32 noundef %28)
  br i1 %29, label %30, label %60

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

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

35:                                               ; preds = %31
  store i32 5, ptr %3, align 4
  br label %57

36:                                               ; preds = %31
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = call noundef zeroext i1 @_Z7isPrimei(i32 noundef %37)
  br i1 %38, label %39, label %53

39:                                               ; preds = %36
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = sub nsw i32 %40, %41
  %43 = call noundef zeroext i1 @_Z7isPrimei(i32 noundef %42)
  br i1 %43, label %44, label %53

44:                                               ; preds = %39
  %45 = load i32, ptr @n, align 4, !tbaa !5
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = sub nsw i32 %45, %46
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = sub nsw i32 %49, %50
  %52 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %47, i32 noundef %48, i32 noundef %51)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %57

53:                                               ; preds = %39, %36
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %4, align 4, !tbaa !5
  br label %31, !llvm.loop !9

57:                                               ; preds = %44, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %58 = load i32, ptr %3, align 4
  switch i32 %58, label %64 [
    i32 5, label %59
  ]

59:                                               ; preds = %57
  br label %60

60:                                               ; preds = %59, %25
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = add nsw i32 %62, 2
  store i32 %63, ptr %2, align 4, !tbaa !5
  br label %20, !llvm.loop !11

64:                                               ; preds = %57, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %65 = load i32, ptr %3, align 4
  switch i32 %65, label %71 [
    i32 2, label %66
    i32 1, label %69
  ]

66:                                               ; preds = %64
  br label %67

67:                                               ; preds = %66, %15
  br label %68

68:                                               ; preds = %67, %8
  store i32 0, ptr %1, align 4
  br label %69

69:                                               ; preds = %68, %64
  %70 = load i32, ptr %1, align 4
  ret i32 %70

71:                                               ; preds = %64
  unreachable
}

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

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 = { 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}


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

@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1

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

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = tail call noundef zeroext i1 @_Z7isPrimei(i32 noundef %2)
  %4 = load i32, ptr @n, align 4, !tbaa !5
  br i1 %3, label %5, label %7

5:                                                ; preds = %0
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %4)
  br label %.loopexit

7:                                                ; preds = %0
  %8 = add nsw i32 %4, -2
  %9 = tail call noundef zeroext i1 @_Z7isPrimei(i32 noundef %8)
  %10 = load i32, ptr @n, align 4, !tbaa !5
  br i1 %9, label %12, label %.preheader21

.preheader21:                                     ; preds = %7
  %11 = icmp sgt i32 %10, 4
  br i1 %11, label %.lr.ph, label %.loopexit

12:                                               ; preds = %7
  %13 = add nsw i32 %10, -2
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %13)
  br label %.loopexit

.lr.ph:                                           ; preds = %.preheader21, %.critedge
  %indvars.iv = phi i32 [ %indvars.iv.next, %.critedge ], [ 5, %.preheader21 ]
  %15 = phi i32 [ %29, %.critedge ], [ %10, %.preheader21 ]
  %.01725 = phi i32 [ %28, %.critedge ], [ 4, %.preheader21 ]
  %16 = sub nsw i32 %15, %.01725
  %17 = tail call noundef zeroext i1 @_Z7isPrimei(i32 noundef %16)
  br i1 %17, label %.preheader, label %.critedge

.preheader:                                       ; preds = %.lr.ph, %26
  %.024 = phi i32 [ %27, %26 ], [ 2, %.lr.ph ]
  %18 = tail call noundef zeroext i1 @_Z7isPrimei(i32 noundef %.024)
  br i1 %18, label %19, label %26

19:                                               ; preds = %.preheader
  %20 = sub nsw i32 %.01725, %.024
  %21 = tail call noundef zeroext i1 @_Z7isPrimei(i32 noundef %20)
  br i1 %21, label %22, label %26

22:                                               ; preds = %19
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = sub nsw i32 %23, %.01725
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %24, i32 noundef %.024, i32 noundef %20)
  br label %.loopexit

26:                                               ; preds = %.preheader, %19
  %27 = add nuw i32 %.024, 1
  %exitcond.not = icmp eq i32 %27, %indvars.iv
  br i1 %exitcond.not, label %.critedge, label %.preheader, !llvm.loop !9

.critedge:                                        ; preds = %26, %.lr.ph
  %28 = add nuw nsw i32 %.01725, 2
  %29 = load i32, ptr @n, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  %indvars.iv.next = add nuw i32 %indvars.iv, 2
  br i1 %30, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.critedge, %.preheader21, %22, %5, %12
  ret i32 0
}

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

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

!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}


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