; LLVM IR from upupming/algorithm :: kick-start/2021/RoundB/C. Consecutive Primes.ll (Z5solvev)

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

@z = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [101 x i8], align 1
@__PRETTY_FUNCTION__._Z5solvev = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z8is_primex(i64 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 uwtable
define dso_local noundef i64 @_Z5solvev() #2 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %8 = load i64, ptr @z, align 8, !tbaa !5
  %9 = call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %8)
  %10 = fptosi double %9 to i64
  store i64 %10, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %11 = load i64, ptr %2, align 8, !tbaa !5
  store i64 %11, ptr %6, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %17, %0
  %13 = load i64, ptr %6, align 8, !tbaa !5
  %14 = call noundef zeroext i1 @_Z8is_primex(i64 noundef %13)
  %15 = xor i1 %14, true
  br i1 %15, label %16, label %20

16:                                               ; preds = %12
  br label %17

17:                                               ; preds = %16
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = add nsw i64 %18, -1
  store i64 %19, ptr %6, align 8, !tbaa !5
  br label %12, !llvm.loop !9

20:                                               ; preds = %12
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = add nsw i64 %21, -1
  store i64 %22, ptr %6, align 8, !tbaa !5
  store i64 %21, ptr %4, align 8, !tbaa !5
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = icmp slt i64 %23, 2
  br i1 %24, label %25, label %27

25:                                               ; preds = %20
  %26 = load i64, ptr @z, align 8, !tbaa !5
  store i64 %26, ptr %3, align 8, !tbaa !5
  br label %38

27:                                               ; preds = %20
  br label %28

28:                                               ; preds = %33, %27
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = call noundef zeroext i1 @_Z8is_primex(i64 noundef %29)
  %31 = xor i1 %30, true
  br i1 %31, label %32, label %36

32:                                               ; preds = %28
  br label %33

33:                                               ; preds = %32
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = add nsw i64 %34, -1
  store i64 %35, ptr %6, align 8, !tbaa !5
  br label %28, !llvm.loop !11

36:                                               ; preds = %28
  %37 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %37, ptr %3, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %36, %25
  %39 = load i64, ptr %2, align 8, !tbaa !5
  %40 = add nsw i64 %39, 1
  store i64 %40, ptr %6, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %46, %38
  %42 = load i64, ptr %6, align 8, !tbaa !5
  %43 = call noundef zeroext i1 @_Z8is_primex(i64 noundef %42)
  %44 = xor i1 %43, true
  br i1 %44, label %45, label %49

45:                                               ; preds = %41
  br label %46

46:                                               ; preds = %45
  %47 = load i64, ptr %6, align 8, !tbaa !5
  %48 = add nsw i64 %47, 1
  store i64 %48, ptr %6, align 8, !tbaa !5
  br label %41, !llvm.loop !12

49:                                               ; preds = %41
  %50 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %50, ptr %5, align 8, !tbaa !5
  %51 = load i64, ptr %4, align 8, !tbaa !5
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = mul nsw i64 %51, %52
  %54 = load i64, ptr @z, align 8, !tbaa !5
  %55 = icmp sle i64 %53, %54
  br i1 %55, label %56, label %60

56:                                               ; preds = %49
  %57 = load i64, ptr %4, align 8, !tbaa !5
  %58 = load i64, ptr %5, align 8, !tbaa !5
  %59 = mul nsw i64 %57, %58
  store i64 %59, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %71

60:                                               ; preds = %49
  %61 = load i64, ptr %3, align 8, !tbaa !5
  %62 = load i64, ptr %4, align 8, !tbaa !5
  %63 = mul nsw i64 %61, %62
  %64 = load i64, ptr @z, align 8, !tbaa !5
  %65 = icmp sle i64 %63, %64
  br i1 %65, label %66, label %70

66:                                               ; preds = %60
  %67 = load i64, ptr %3, align 8, !tbaa !5
  %68 = load i64, ptr %4, align 8, !tbaa !5
  %69 = mul nsw i64 %67, %68
  store i64 %69, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %71

70:                                               ; preds = %60
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 45, ptr noundef @__PRETTY_FUNCTION__._Z5solvev) #6
  unreachable

71:                                               ; preds = %66, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  %72 = load i64, ptr %1, align 8
  ret i64 %72
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef) #3

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #4

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 = { 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 #4 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!"long long", !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}


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

@z = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [101 x i8], align 1
@__PRETTY_FUNCTION__._Z5solvev = external hidden unnamed_addr constant [11 x i8], align 1

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z5solvev() local_unnamed_addr #0 {
  %1 = load i64, ptr @z, align 8, !tbaa !5
  %2 = tail call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %1)
  %3 = fptosi double %2 to i64
  br label %4

4:                                                ; preds = %4, %0
  %.0 = phi i64 [ %3, %0 ], [ %6, %4 ]
  %5 = tail call noundef zeroext i1 @_Z8is_primex(i64 noundef %.0)
  %6 = add nsw i64 %.0, -1
  br i1 %5, label %7, label %4, !llvm.loop !9

7:                                                ; preds = %4
  %8 = icmp slt i64 %.0, 3
  br i1 %8, label %9, label %.preheader

9:                                                ; preds = %7
  %10 = load i64, ptr @z, align 8, !tbaa !5
  br label %.loopexit

.preheader:                                       ; preds = %7, %.preheader
  %.1.in = phi i64 [ %.1, %.preheader ], [ %.0, %7 ]
  %.1 = add nsw i64 %.1.in, -1
  %11 = tail call noundef zeroext i1 @_Z8is_primex(i64 noundef %.1)
  br i1 %11, label %.loopexit, label %.preheader, !llvm.loop !11

.loopexit:                                        ; preds = %.preheader, %9
  %.022 = phi i64 [ %10, %9 ], [ %.1, %.preheader ]
  br label %12

12:                                               ; preds = %12, %.loopexit
  %.2.in = phi i64 [ %3, %.loopexit ], [ %.2, %12 ]
  %.2 = add nsw i64 %.2.in, 1
  %13 = tail call noundef zeroext i1 @_Z8is_primex(i64 noundef %.2)
  br i1 %13, label %14, label %12, !llvm.loop !12

14:                                               ; preds = %12
  %15 = mul nsw i64 %.2, %.0
  %16 = load i64, ptr @z, align 8, !tbaa !5
  %.not = icmp sgt i64 %15, %16
  br i1 %.not, label %17, label %20

17:                                               ; preds = %14
  %18 = mul nsw i64 %.022, %.0
  %.not27 = icmp sgt i64 %18, %16
  br i1 %.not27, label %19, label %20

19:                                               ; preds = %17
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 45, ptr noundef nonnull @__PRETTY_FUNCTION__._Z5solvev) #3
  unreachable

20:                                               ; preds = %17, %14
  %.021 = phi i64 [ %15, %14 ], [ %18, %17 ]
  ret i64 %.021
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) 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 = { 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 #2 = { noreturn 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 = { noreturn 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 = !{!"long long", !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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass