; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0287 Easy - Kaprekar's Routine/solutions (solution)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; 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
declare dso_local signext i16 @desc_digits(i16 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @asc_digits(i16 noundef signext) #1

; Function Attrs: nounwind uwtable
define dso_local signext i8 @kaprekar(i16 noundef signext %0) #1 {
  %2 = alloca i8, align 1
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i8, align 1
  store i16 %0, ptr %3, align 2, !tbaa !5
  %7 = load i16, ptr %3, align 2, !tbaa !5
  %8 = sext i16 %7 to i32
  %9 = icmp eq i32 %8, 6174
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  store i8 0, ptr %2, align 1
  br label %37

11:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %12 = load i16, ptr %3, align 2, !tbaa !5
  %13 = call signext i16 @desc_digits(i16 noundef signext %12)
  store i16 %13, ptr %4, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %14 = load i16, ptr %4, align 2, !tbaa !5
  %15 = call signext i16 @asc_digits(i16 noundef signext %14)
  store i16 %15, ptr %5, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  store i8 1, ptr %6, align 1, !tbaa !9
  br label %16

16:                                               ; preds = %23, %11
  %17 = load i16, ptr %4, align 2, !tbaa !5
  %18 = sext i16 %17 to i32
  %19 = load i16, ptr %5, align 2, !tbaa !5
  %20 = sext i16 %19 to i32
  %21 = sub nsw i32 %18, %20
  %22 = icmp ne i32 %21, 6174
  br i1 %22, label %23, label %35

23:                                               ; preds = %16
  %24 = load i16, ptr %4, align 2, !tbaa !5
  %25 = sext i16 %24 to i32
  %26 = load i16, ptr %5, align 2, !tbaa !5
  %27 = sext i16 %26 to i32
  %28 = sub nsw i32 %25, %27
  %29 = trunc i32 %28 to i16
  %30 = call signext i16 @desc_digits(i16 noundef signext %29)
  store i16 %30, ptr %4, align 2, !tbaa !5
  %31 = load i16, ptr %4, align 2, !tbaa !5
  %32 = call signext i16 @asc_digits(i16 noundef signext %31)
  store i16 %32, ptr %5, align 2, !tbaa !5
  %33 = load i8, ptr %6, align 1, !tbaa !9
  %34 = add i8 %33, 1
  store i8 %34, ptr %6, align 1, !tbaa !9
  br label %16, !llvm.loop !10

35:                                               ; preds = %16
  %36 = load i8, ptr %6, align 1, !tbaa !9
  store i8 %36, ptr %2, align 1
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %37

37:                                               ; preds = %35, %10
  %38 = load i8, ptr %2, align 1
  ret i8 %38
}

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 = !{!"short", !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"}


; 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: nounwind uwtable
declare dso_local signext i16 @desc_digits(i16 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i16 @asc_digits(i16 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local signext i8 @kaprekar(i16 noundef signext %0) local_unnamed_addr #0 {
  %2 = icmp eq i16 %0, 6174
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = tail call signext i16 @desc_digits(i16 noundef signext %0)
  %5 = tail call signext i16 @asc_digits(i16 noundef signext %4)
  %6 = sext i16 %4 to i32
  %7 = sext i16 %5 to i32
  %8 = sub nsw i32 %6, %7
  %.not15 = icmp eq i32 %8, 6174
  br i1 %.not15, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %9 = phi i32 [ %16, %.lr.ph ], [ %8, %3 ]
  %.016 = phi i8 [ %13, %.lr.ph ], [ 1, %3 ]
  %10 = trunc i32 %9 to i16
  %11 = tail call signext i16 @desc_digits(i16 noundef signext %10)
  %12 = tail call signext i16 @asc_digits(i16 noundef signext %11)
  %13 = add i8 %.016, 1
  %14 = sext i16 %11 to i32
  %15 = sext i16 %12 to i32
  %16 = sub nsw i32 %14, %15
  %.not = icmp eq i32 %16, 6174
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %3, %1
  %.013 = phi i8 [ 0, %1 ], [ 1, %3 ], [ %13, %.lr.ph ]
  ret i8 %.013
}

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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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