; LLVM IR from lolosssss/leetcode :: c (202_happy_number)

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

10:                                               ; preds = %50, %1
  %11 = load i32, ptr %7, align 4, !tbaa !5
  %12 = icmp slt i32 %11, 10
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  store i32 2, ptr %8, align 4
  br label %53

14:                                               ; preds = %10
  br label %15

15:                                               ; preds = %18, %14
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %27

18:                                               ; preds = %15
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = srem i32 %19, 10
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds [10 x i32], ptr %5, i64 0, i64 %23
  store i32 %20, ptr %24, align 4, !tbaa !5
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = sdiv i32 %25, 10
  store i32 %26, ptr %6, align 4, !tbaa !5
  br label %15, !llvm.loop !9

27:                                               ; preds = %15
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %31, %27
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %45

31:                                               ; preds = %28
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  store i32 %33, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [10 x i32], ptr %5, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [10 x i32], ptr %5, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = mul nsw i32 %37, %41
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = add nsw i32 %43, %42
  store i32 %44, ptr %6, align 4, !tbaa !5
  br label %28, !llvm.loop !11

45:                                               ; preds = %28
  %46 = load i32, ptr %6, align 4, !tbaa !5
  %47 = icmp eq i32 %46, 1
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i1 true, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %53

49:                                               ; preds = %45
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %7, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %7, align 4, !tbaa !5
  br label %10, !llvm.loop !12

53:                                               ; preds = %48, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %56 [
    i32 2, label %55
  ]

55:                                               ; preds = %53
  store i1 false, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %56

56:                                               ; preds = %55, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 40, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %57 = load i1, ptr %2, align 1
  ret i1 %57
}

; 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 = { 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/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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local zeroext i1 @isHappy(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca [10 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #3
  br label %.preheader22

._crit_edge.thread:                               ; preds = %.preheader22, %._crit_edge
  %.2.lcssa42 = phi i32 [ %.lcssa44, %._crit_edge ], [ 0, %.preheader22 ]
  %3 = add nuw nsw i32 %.01432, 1
  %4 = icmp ult i32 %.01432, 9
  %exitcond.not = icmp eq i32 %3, 10
  br i1 %exitcond.not, label %.critedge, label %.preheader22, !llvm.loop !5

.preheader22:                                     ; preds = %1, %._crit_edge.thread
  %5 = phi i1 [ true, %1 ], [ %4, %._crit_edge.thread ]
  %.01432 = phi i32 [ 0, %1 ], [ %3, %._crit_edge.thread ]
  %.01531 = phi i32 [ %0, %1 ], [ %.2.lcssa42, %._crit_edge.thread ]
  %.not23 = icmp eq i32 %.01531, 0
  br i1 %.not23, label %._crit_edge.thread, label %.lr.ph

.lr.ph29.preheader:                               ; preds = %.lr.ph
  %sext = shl i64 %indvars.iv.next, 32
  %6 = ashr exact i64 %sext, 32
  %min.iters.check = icmp ult i64 %6, 8
  br i1 %min.iters.check, label %.lr.ph29.preheader48, label %vector.ph

.lr.ph29.preheader48:                             ; preds = %middle.block, %.lr.ph29.preheader
  %indvars.iv36.ph = phi i64 [ %6, %.lr.ph29.preheader ], [ %n.mod.vf, %middle.block ]
  %.228.ph = phi i32 [ 0, %.lr.ph29.preheader ], [ %19, %middle.block ]
  br label %.lr.ph29

vector.ph:                                        ; preds = %.lr.ph29.preheader
  %n.mod.vf = and i64 %indvars.iv.next, 7
  %n.vec = sub nsw i64 %6, %n.mod.vf
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %16, %vector.body ]
  %vec.phi45 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %17, %vector.body ]
  %7 = xor i64 %index, -1
  %8 = add i64 %6, %7
  %9 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 -12
  %11 = getelementptr inbounds i8, ptr %9, i64 -28
  %wide.load = load <4 x i32>, ptr %10, align 4, !tbaa !7
  %wide.load46 = load <4 x i32>, ptr %11, align 4, !tbaa !7
  %12 = mul nsw <4 x i32> %wide.load, %wide.load
  %13 = shufflevector <4 x i32> %12, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %14 = mul nsw <4 x i32> %wide.load46, %wide.load46
  %15 = shufflevector <4 x i32> %14, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %16 = add <4 x i32> %13, %vec.phi
  %17 = add <4 x i32> %15, %vec.phi45
  %index.next = add nuw i64 %index, 8
  %18 = icmp eq i64 %index.next, %n.vec
  br i1 %18, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %17, %16
  %19 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %n.mod.vf, 0
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph29.preheader48

.lr.ph:                                           ; preds = %.preheader22, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader22 ]
  %.125 = phi i32 [ %22, %.lr.ph ], [ %.01531, %.preheader22 ]
  %20 = srem i32 %.125, 10
  %indvars.iv.next = add nuw i64 %indvars.iv, 1
  %21 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %indvars.iv
  store i32 %20, ptr %21, align 4, !tbaa !7
  %22 = sdiv i32 %.125, 10
  %.125.off = add i32 %.125, 9
  %.not = icmp ult i32 %.125.off, 19
  br i1 %.not, label %.lr.ph29.preheader, label %.lr.ph, !llvm.loop !14

.lr.ph29:                                         ; preds = %.lr.ph29.preheader48, %.lr.ph29
  %indvars.iv36 = phi i64 [ %indvars.iv.next37, %.lr.ph29 ], [ %indvars.iv36.ph, %.lr.ph29.preheader48 ]
  %.228 = phi i32 [ %26, %.lr.ph29 ], [ %.228.ph, %.lr.ph29.preheader48 ]
  %indvars.iv.next37 = add nsw i64 %indvars.iv36, -1
  %23 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %indvars.iv.next37
  %24 = load i32, ptr %23, align 4, !tbaa !7
  %25 = mul nsw i32 %24, %24
  %26 = add nuw nsw i32 %25, %.228
  %.not21 = icmp eq i64 %indvars.iv.next37, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph29, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph29, %middle.block
  %.lcssa44 = phi i32 [ %19, %middle.block ], [ %26, %.lr.ph29 ]
  %27 = icmp eq i32 %.lcssa44, 1
  br i1 %27, label %.critedge, label %._crit_edge.thread

.critedge:                                        ; preds = %._crit_edge.thread, %._crit_edge
  %.lcssa = phi i1 [ %4, %._crit_edge.thread ], [ %5, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #3
  ret i1 %.lcssa
}

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

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

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

attributes #0 = { nofree norecurse nosync nounwind 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6, !12, !13}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = distinct !{!14, !6}
!15 = distinct !{!15, !6, !13, !12}


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