; 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 @asc_digits(i16 noundef signext %0) #0 {
  %2 = alloca i16, align 2
  %3 = alloca [4 x i16], align 2
  %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 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  store i8 3, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  br label %6

6:                                                ; preds = %31, %1
  %7 = load i8, ptr %4, align 1, !tbaa !9
  %8 = sext i8 %7 to i32
  %9 = icmp sge i32 %8, 0
  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 %34

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 i16
  %15 = load i8, ptr %4, align 1, !tbaa !9
  %16 = sext i8 %15 to i64
  %17 = getelementptr inbounds [4 x i16], ptr %3, i64 0, i64 %16
  store i16 %14, ptr %17, align 2, !tbaa !5
  %18 = load i8, ptr %4, align 1, !tbaa !9
  %19 = sext i8 %18 to i64
  %20 = getelementptr inbounds [4 x i16], ptr %3, i64 0, i64 %19
  %21 = load i16, ptr %20, align 2, !tbaa !5
  %22 = sext i16 %21 to i32
  %23 = load i8, ptr %5, align 1, !tbaa !9
  %24 = sext i8 %23 to i32
  %25 = call i32 @_pow(i32 noundef 10, i32 noundef %24)
  %26 = mul nsw i32 %22, %25
  %27 = load i16, ptr %2, align 2, !tbaa !5
  %28 = sext i16 %27 to i32
  %29 = sub nsw i32 %28, %26
  %30 = trunc i32 %29 to i16
  store i16 %30, ptr %2, align 2, !tbaa !5
  br label %31

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

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

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

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: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass