; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/math/special.ll (Z7speciali)

; 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 nounwind uwtable
define dso_local noundef i32 @_Z7speciali(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %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
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %10 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %10, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  br label %11

11:                                               ; preds = %28, %1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %34

14:                                               ; preds = %11
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = srem i32 %15, 10
  store i32 %16, ptr %5, align 4, !tbaa !5
  store i32 1, ptr %6, align 4, !tbaa !5
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %17

17:                                               ; preds = %25, %14
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = icmp sle i32 %18, %19
  br i1 %20, label %21, label %28

21:                                               ; preds = %17
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr %8, align 4, !tbaa !5
  %24 = mul nsw i32 %22, %23
  store i32 %24, ptr %6, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %21
  %26 = load i32, ptr %8, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %8, align 4, !tbaa !5
  br label %17, !llvm.loop !9

28:                                               ; preds = %17
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = add nsw i32 %29, %30
  store i32 %31, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = sdiv i32 %32, 10
  store i32 %33, ptr %3, align 4, !tbaa !5
  br label %11, !llvm.loop !11

34:                                               ; preds = %11
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = icmp eq i32 %35, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %34
  store i32 1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %40

39:                                               ; preds = %34
  store i32 0, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %40

40:                                               ; preds = %39, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %41 = load i32, ptr %2, align 4
  ret i32 %41
}

; 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

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef range(i32 0, 2) i32 @_Z7speciali(i32 noundef %0) local_unnamed_addr #0 {
  %.not22 = icmp eq i32 %0, 0
  br i1 %.not22, label %._crit_edge27, label %.lr.ph26

.lr.ph26:                                         ; preds = %1, %._crit_edge
  %.01424 = phi i32 [ %6, %._crit_edge ], [ 0, %1 ]
  %.01723 = phi i32 [ %3, %._crit_edge ], [ %0, %1 ]
  %2 = srem i32 %.01723, 10
  %3 = sdiv i32 %.01723, 10
  %.not1819 = icmp slt i32 %2, 1
  br i1 %.not1819, label %._crit_edge, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph26
  %n.rnd.up = add nuw nsw i32 %2, 3
  %n.vec = and i32 %n.rnd.up, 2147483644
  %trip.count.minus.1 = add nsw i32 %2, -1
  %broadcast.splatinsert32 = insertelement <4 x i32> poison, i32 %trip.count.minus.1, i64 0
  %broadcast.splat33 = shufflevector <4 x i32> %broadcast.splatinsert32, <4 x i32> poison, <4 x i32> zeroinitializer
  switch i32 %n.vec, label %vector.body.3 [
    i32 4, label %middle.block
    i32 8, label %middle.block.fold.split
    i32 12, label %middle.block.fold.split37
  ]

vector.body.3:                                    ; preds = %vector.ph
  br label %middle.block

middle.block.fold.split:                          ; preds = %vector.ph
  br label %middle.block

middle.block.fold.split37:                        ; preds = %vector.ph
  br label %middle.block

middle.block:                                     ; preds = %vector.ph, %middle.block.fold.split37, %middle.block.fold.split, %vector.body.3
  %index.lcssa = phi i32 [ 0, %vector.ph ], [ 12, %vector.body.3 ], [ 4, %middle.block.fold.split ], [ 8, %middle.block.fold.split37 ]
  %vec.phi.lcssa = phi <4 x i32> [ <i32 1, i32 1, i32 1, i32 1>, %vector.ph ], [ <i32 45, i32 120, i32 231, i32 384>, %vector.body.3 ], [ <i32 1, i32 2, i32 3, i32 4>, %middle.block.fold.split ], [ <i32 5, i32 12, i32 21, i32 32>, %middle.block.fold.split37 ]
  %.lcssa = phi <4 x i32> [ <i32 1, i32 2, i32 3, i32 4>, %vector.ph ], [ <i32 585, i32 1680, i32 3465, i32 6144>, %vector.body.3 ], [ <i32 5, i32 12, i32 21, i32 32>, %middle.block.fold.split ], [ <i32 45, i32 120, i32 231, i32 384>, %middle.block.fold.split37 ]
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %index.lcssa, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %vec.iv = or disjoint <4 x i32> %broadcast.splat, <i32 0, i32 1, i32 2, i32 3>
  %.not34 = icmp ugt <4 x i32> %vec.iv, %broadcast.splat33
  %4 = select <4 x i1> %.not34, <4 x i32> %vec.phi.lcssa, <4 x i32> %.lcssa
  %5 = tail call i32 @llvm.vector.reduce.mul.v4i32(<4 x i32> %4)
  br label %._crit_edge

._crit_edge:                                      ; preds = %middle.block, %.lr.ph26
  %.015.lcssa = phi i32 [ 1, %.lr.ph26 ], [ %5, %middle.block ]
  %6 = add nuw nsw i32 %.015.lcssa, %.01424
  %.01723.off = add i32 %.01723, 9
  %.not = icmp ult i32 %.01723.off, 19
  br i1 %.not, label %._crit_edge27, label %.lr.ph26, !llvm.loop !5

._crit_edge27:                                    ; preds = %._crit_edge, %1
  %.014.lcssa = phi i32 [ 0, %1 ], [ %6, %._crit_edge ]
  %7 = icmp eq i32 %.014.lcssa, %0
  %. = zext i1 %7 to i32
  ret i32 %.
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.mul.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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 speculatable willreturn memory(none) }

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


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