; LLVM IR from xtaci/algorithms :: src (prime_decompose)

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

@bit_pos = external dso_local global [30 x i8], align 16
@pbits = external dso_local global ptr, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @next_prime(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @is_prime(i64 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = icmp ugt i64 %6, 5
  br i1 %7, label %8, label %50

8:                                                ; preds = %1
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = icmp ult i64 %9, 4294967295
  br i1 %10, label %11, label %24

11:                                               ; preds = %8
  %12 = load ptr, ptr @pbits, align 8, !tbaa !9
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = udiv i64 %13, 30
  %15 = getelementptr inbounds i8, ptr %12, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !11
  %17 = zext i8 %16 to i32
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = urem i64 %18, 30
  %20 = getelementptr inbounds [30 x i8], ptr @bit_pos, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !11
  %22 = zext i8 %21 to i32
  %23 = and i32 %17, %22
  store i32 %23, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %62

24:                                               ; preds = %8
  store i32 2, ptr %4, align 4, !tbaa !12
  br label %25

25:                                               ; preds = %46, %24
  %26 = load i32, ptr %4, align 4, !tbaa !12
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %36

28:                                               ; preds = %25
  %29 = load i32, ptr %4, align 4, !tbaa !12
  %30 = zext i32 %29 to i64
  %31 = load i32, ptr %4, align 4, !tbaa !12
  %32 = zext i32 %31 to i64
  %33 = mul i64 %30, %32
  %34 = load i64, ptr %3, align 8, !tbaa !5
  %35 = icmp ule i64 %33, %34
  br label %36

36:                                               ; preds = %28, %25
  %37 = phi i1 [ false, %25 ], [ %35, %28 ]
  br i1 %37, label %38, label %49

38:                                               ; preds = %36
  %39 = load i64, ptr %3, align 8, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !12
  %41 = zext i32 %40 to i64
  %42 = urem i64 %39, %41
  %43 = icmp eq i64 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %38
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %62

45:                                               ; preds = %38
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %4, align 4, !tbaa !12
  %48 = call i32 @next_prime(i32 noundef %47)
  store i32 %48, ptr %4, align 4, !tbaa !12
  br label %25, !llvm.loop !14

49:                                               ; preds = %36
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %62

50:                                               ; preds = %1
  %51 = load i64, ptr %3, align 8, !tbaa !5
  %52 = icmp eq i64 %51, 2
  br i1 %52, label %59, label %53

53:                                               ; preds = %50
  %54 = load i64, ptr %3, align 8, !tbaa !5
  %55 = icmp eq i64 %54, 3
  br i1 %55, label %59, label %56

56:                                               ; preds = %53
  %57 = load i64, ptr %3, align 8, !tbaa !5
  %58 = icmp eq i64 %57, 5
  br label %59

59:                                               ; preds = %56, %53, %50
  %60 = phi i1 [ true, %53 ], [ true, %50 ], [ %58, %56 ]
  %61 = zext i1 %60 to i32
  store i32 %61, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %62

62:                                               ; preds = %59, %49, %44, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %63 = load i32, ptr %2, align 4
  ret i32 %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@bit_pos = external dso_local local_unnamed_addr global [30 x i8], align 16
@pbits = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 256) i32 @is_prime(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ugt i64 %0, 5
  br i1 %2, label %3, label %22

3:                                                ; preds = %1
  %4 = icmp ult i64 %0, 4294967295
  br i1 %4, label %5, label %.preheader

5:                                                ; preds = %3
  %6 = load ptr, ptr @pbits, align 8, !tbaa !5
  %.lhs.trunc = trunc nuw i64 %0 to i32
  %7 = udiv i32 %.lhs.trunc, 30
  %.zext = zext nneg i32 %7 to i64
  %8 = getelementptr inbounds i8, ptr %6, i64 %.zext
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = urem i32 %.lhs.trunc, 30
  %.zext20 = zext nneg i32 %10 to i64
  %11 = getelementptr inbounds [30 x i8], ptr @bit_pos, i64 0, i64 %.zext20
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %13 = and i8 %12, %9
  %14 = zext i8 %13 to i32
  br label %.critedge

.preheader:                                       ; preds = %3, %20
  %.021 = phi i32 [ %21, %20 ], [ 2, %3 ]
  %15 = zext i32 %.021 to i64
  %16 = mul nuw i64 %15, %15
  %.not18 = icmp ugt i64 %16, %0
  br i1 %.not18, label %.critedge, label %17

17:                                               ; preds = %.preheader
  %18 = urem i64 %0, %15
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %.critedge, label %20

20:                                               ; preds = %17
  %21 = tail call i32 @next_prime(i32 noundef %.021)
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %.critedge, label %.preheader, !llvm.loop !10

22:                                               ; preds = %1
  %23 = and i64 %0, 6
  %or.cond = icmp eq i64 %23, 2
  %24 = icmp eq i64 %0, 5
  %narrow = or i1 %24, %or.cond
  %25 = zext i1 %narrow to i32
  br label %.critedge

.critedge:                                        ; preds = %.preheader, %20, %17, %22, %5
  %.016 = phi i32 [ %14, %5 ], [ %25, %22 ], [ 1, %.preheader ], [ 1, %20 ], [ 0, %17 ]
  ret i32 %.016
}

attributes #0 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass