; 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: nounwind uwtable
declare dso_local i32 @_pow(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @largest_digit(i16 noundef signext, ptr noundef) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local signext i16 @desc_digits(i16 noundef signext %0) #0 {
  %2 = alloca i16, align 2
  %3 = alloca [4 x i32], align 16
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  store i16 %0, ptr %2, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  store i8 0, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  br label %6

6:                                                ; preds = %30, %1
  %7 = load i8, ptr %4, align 1, !tbaa !9
  %8 = sext i8 %7 to i32
  %9 = icmp slt i32 %8, 4
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #2
  br label %33

11:                                               ; preds = %6
  %12 = load i16, ptr %2, align 2, !tbaa !5
  %13 = call signext i8 @largest_digit(i16 noundef signext %12, ptr noundef %5)
  %14 = sext i8 %13 to i32
  %15 = load i8, ptr %4, align 1, !tbaa !9
  %16 = sext i8 %15 to i64
  %17 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 %16
  store i32 %14, ptr %17, align 4, !tbaa !10
  %18 = load i8, ptr %4, align 1, !tbaa !9
  %19 = sext i8 %18 to i64
  %20 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !10
  %22 = load i8, ptr %5, align 1, !tbaa !9
  %23 = sext i8 %22 to i32
  %24 = call i32 @_pow(i32 noundef 10, i32 noundef %23)
  %25 = mul nsw i32 %21, %24
  %26 = load i16, ptr %2, align 2, !tbaa !5
  %27 = sext i16 %26 to i32
  %28 = sub nsw i32 %27, %25
  %29 = trunc i32 %28 to i16
  store i16 %29, ptr %2, align 2, !tbaa !5
  br label %30

30:                                               ; preds = %11
  %31 = load i8, ptr %4, align 1, !tbaa !9
  %32 = add i8 %31, 1
  store i8 %32, ptr %4, align 1, !tbaa !9
  br label %6, !llvm.loop !12

33:                                               ; preds = %10
  %34 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 0
  %35 = load i32, ptr %34, align 16, !tbaa !10
  %36 = mul nsw i32 %35, 1000
  %37 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 1
  %38 = load i32, ptr %37, align 4, !tbaa !10
  %39 = mul nsw i32 %38, 100
  %40 = add nsw i32 %36, %39
  %41 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 2
  %42 = load i32, ptr %41, align 8, !tbaa !10
  %43 = mul nsw i32 %42, 10
  %44 = add nsw i32 %40, %43
  %45 = getelementptr inbounds [4 x i32], ptr %3, i64 0, i64 3
  %46 = load i32, ptr %45, align 4, !tbaa !10
  %47 = add nsw i32 %44, %46
  %48 = trunc i32 %47 to i16
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #2
  ret i16 %48
}

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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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 i32 @_pow(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @largest_digit(i16 noundef signext, ptr noundef) local_unnamed_addr #0

; 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: nounwind uwtable
define dso_local signext i16 @desc_digits(i16 noundef signext %0) local_unnamed_addr #0 {
  %2 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %2) #2
  %3 = call signext i8 @largest_digit(i16 noundef signext %0, ptr noundef nonnull %2)
  %4 = sext i8 %3 to i32
  %5 = load i8, ptr %2, align 1, !tbaa !5
  %6 = sext i8 %5 to i32
  %7 = call i32 @_pow(i32 noundef 10, i32 noundef %6)
  %8 = mul nsw i32 %7, %4
  %9 = trunc i32 %8 to i16
  %10 = sub i16 %0, %9
  %11 = call signext i8 @largest_digit(i16 noundef signext %10, ptr noundef nonnull %2)
  %12 = sext i8 %11 to i32
  %13 = load i8, ptr %2, align 1, !tbaa !5
  %14 = sext i8 %13 to i32
  %15 = call i32 @_pow(i32 noundef 10, i32 noundef %14)
  %16 = mul nsw i32 %15, %12
  %17 = trunc i32 %16 to i16
  %18 = sub i16 %10, %17
  %19 = call signext i8 @largest_digit(i16 noundef signext %18, ptr noundef nonnull %2)
  %20 = sext i8 %19 to i32
  %21 = load i8, ptr %2, align 1, !tbaa !5
  %22 = sext i8 %21 to i32
  %23 = call i32 @_pow(i32 noundef 10, i32 noundef %22)
  %24 = mul nsw i32 %23, %20
  %25 = trunc i32 %24 to i16
  %26 = sub i16 %18, %25
  %27 = call signext i8 @largest_digit(i16 noundef signext %26, ptr noundef nonnull %2)
  %28 = sext i8 %27 to i32
  %29 = load i8, ptr %2, align 1, !tbaa !5
  %30 = sext i8 %29 to i32
  %31 = call i32 @_pow(i32 noundef 10, i32 noundef %30)
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %2) #2
  %32 = mul nsw i32 %4, 1000
  %33 = mul nsw i32 %12, 100
  %34 = add nsw i32 %33, %32
  %35 = mul nsw i32 %20, 10
  %36 = add nsw i32 %34, %35
  %37 = add nsw i32 %36, %28
  %38 = trunc i32 %37 to i16
  ret i16 %38
}

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 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass