; LLVM IR from codeIIEST/Algorithms :: Competitive Coding/Math/kthPrimeFactor/kthPrimeFactor.ll (Z12kPrimeFactorii)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z12kPrimeFactorii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %20, %2
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = srem i32 %9, 2
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %21

12:                                               ; preds = %8
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  store i32 %14, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = sdiv i32 %15, 2
  store i32 %16, ptr %4, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %12
  store i32 2, ptr %3, align 4
  br label %61

20:                                               ; preds = %12
  br label %8, !llvm.loop !9

21:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 3, ptr %6, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %47, %21
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sitofp i32 %23 to double
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %25)
  %27 = fcmp ole double %24, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %22
  store i32 4, ptr %7, align 4
  br label %50

29:                                               ; preds = %22
  br label %30

30:                                               ; preds = %40, %29
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = srem i32 %31, %32
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %46

35:                                               ; preds = %30
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = icmp eq i32 %36, 1
  br i1 %37, label %38, label %40

38:                                               ; preds = %35
  %39 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %39, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %50

40:                                               ; preds = %35
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = add nsw i32 %41, -1
  store i32 %42, ptr %5, align 4, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sdiv i32 %43, %44
  store i32 %45, ptr %4, align 4, !tbaa !5
  br label %30, !llvm.loop !11

46:                                               ; preds = %30
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = add nsw i32 %48, 2
  store i32 %49, ptr %6, align 4, !tbaa !5
  br label %22, !llvm.loop !12

50:                                               ; preds = %38, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %51 = load i32, ptr %7, align 4
  switch i32 %51, label %63 [
    i32 4, label %52
    i32 1, label %61
  ]

52:                                               ; preds = %50
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = icmp sgt i32 %53, 2
  br i1 %54, label %55, label %60

55:                                               ; preds = %52
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = icmp eq i32 %56, 1
  br i1 %57, label %58, label %60

58:                                               ; preds = %55
  %59 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %59, ptr %3, align 4
  br label %61

60:                                               ; preds = %55, %52
  store i32 -1, ptr %3, align 4
  br label %61

61:                                               ; preds = %60, %58, %50, %19
  %62 = load i32, ptr %3, align 4
  ret i32 %62

63:                                               ; preds = %50
  unreachable
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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}


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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z12kPrimeFactorii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %3

3:                                                ; preds = %8, %2
  %.022 = phi i32 [ %0, %2 ], [ %10, %8 ]
  %.021 = phi i32 [ %1, %2 ], [ %9, %8 ]
  %4 = and i32 %.022, 1
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %8, label %.preheader36

.preheader36:                                     ; preds = %3
  %6 = tail call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %.022)
  %7 = fcmp ult double %6, 3.000000e+00
  br i1 %7, label %._crit_edge51, label %.preheader

8:                                                ; preds = %3
  %9 = add nsw i32 %.021, -1
  %10 = ashr exact i32 %.022, 1
  %11 = icmp eq i32 %9, 0
  br i1 %11, label %.loopexit, label %3, !llvm.loop !5

.preheader:                                       ; preds = %.preheader36, %._crit_edge
  %.02050 = phi i32 [ %20, %._crit_edge ], [ 3, %.preheader36 ]
  %.149 = phi i32 [ %.2.lcssa, %._crit_edge ], [ %.021, %.preheader36 ]
  %.12348 = phi i32 [ %.224.lcssa, %._crit_edge ], [ %.022, %.preheader36 ]
  %12 = srem i32 %.12348, %.02050
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %15
  %.246 = phi i32 [ %16, %15 ], [ %.149, %.preheader ]
  %.22445 = phi i32 [ %17, %15 ], [ %.12348, %.preheader ]
  %14 = icmp eq i32 %.246, 1
  br i1 %14, label %.loopexit, label %15

15:                                               ; preds = %.lr.ph
  %16 = add nsw i32 %.246, -1
  %17 = sdiv i32 %.22445, %.02050
  %18 = srem i32 %17, %.02050
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %.lr.ph, label %._crit_edge, !llvm.loop !7

._crit_edge:                                      ; preds = %15, %.preheader
  %.224.lcssa = phi i32 [ %.12348, %.preheader ], [ %17, %15 ]
  %.2.lcssa = phi i32 [ %.149, %.preheader ], [ %16, %15 ]
  %20 = add nuw nsw i32 %.02050, 2
  %21 = uitofp nneg i32 %20 to double
  %22 = tail call noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef %.224.lcssa)
  %23 = fcmp ult double %22, %21
  br i1 %23, label %._crit_edge51, label %.preheader, !llvm.loop !8

._crit_edge51:                                    ; preds = %._crit_edge, %.preheader36
  %.123.lcssa = phi i32 [ %.022, %.preheader36 ], [ %.224.lcssa, %._crit_edge ]
  %.1.lcssa = phi i32 [ %.021, %.preheader36 ], [ %.2.lcssa, %._crit_edge ]
  %24 = icmp eq i32 %.1.lcssa, 1
  %25 = icmp sgt i32 %.123.lcssa, 2
  %or.cond = select i1 %25, i1 %24, i1 false
  %.325. = select i1 %or.cond, i32 %.123.lcssa, i32 -1
  br label %.loopexit

.loopexit:                                        ; preds = %8, %.lr.ph, %._crit_edge51
  %.127 = phi i32 [ %.325., %._crit_edge51 ], [ %.02050, %.lr.ph ], [ 2, %8 ]
  ret i32 %.127
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIiEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i32 noundef) local_unnamed_addr #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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


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