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

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

11:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  br label %38

12:                                               ; preds = %7
  %13 = load i16, ptr %3, align 2, !tbaa !5
  %14 = sext i16 %13 to i32
  %15 = srem i32 %14, 10
  %16 = load i8, ptr %5, align 1, !tbaa !11
  %17 = sext i8 %16 to i32
  %18 = icmp sgt i32 %15, %17
  br i1 %18, label %19, label %30

19:                                               ; preds = %12
  %20 = load i16, ptr %3, align 2, !tbaa !5
  %21 = sext i16 %20 to i32
  %22 = srem i32 %21, 10
  %23 = trunc i32 %22 to i8
  store i8 %23, ptr %5, align 1, !tbaa !11
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %29

26:                                               ; preds = %19
  %27 = load i8, ptr %6, align 1, !tbaa !11
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  store i8 %27, ptr %28, align 1, !tbaa !11
  br label %29

29:                                               ; preds = %26, %19
  br label %30

30:                                               ; preds = %29, %12
  %31 = load i16, ptr %3, align 2, !tbaa !5
  %32 = sext i16 %31 to i32
  %33 = sdiv i32 %32, 10
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %3, align 2, !tbaa !5
  br label %35

35:                                               ; preds = %30
  %36 = load i8, ptr %6, align 1, !tbaa !11
  %37 = add i8 %36, 1
  store i8 %37, ptr %6, align 1, !tbaa !11
  br label %7, !llvm.loop !12

38:                                               ; preds = %11
  %39 = load i8, ptr %5, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  ret i8 %39
}

; 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) uwtable
define dso_local signext range(i8 0, 10) i8 @largest_digit(i16 noundef signext %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %1, null
  %3 = srem i16 %0, 10
  %4 = sdiv i16 %0, 10
  br i1 %.not, label %.split.us.preheader, label %.split.preheader

.split.preheader:                                 ; preds = %2
  %5 = icmp sgt i16 %3, 0
  br i1 %5, label %12, label %.split.1

.split.us.preheader:                              ; preds = %2
  %6 = sdiv i16 %0, 10
  %7 = srem i16 %6, 10
  %spec.select23 = tail call i16 @llvm.smax.i16(i16 %3, i16 %7)
  %8 = sdiv i16 %0, 100
  %9 = srem i16 %8, 10
  %spec.select.1.v = tail call i16 @llvm.smax.i16(i16 %spec.select23, i16 %9)
  %10 = sdiv i16 %0, 1000
  %.lhs.trunc = trunc nsw i16 %10 to i8
  %11 = srem i8 %.lhs.trunc, 10
  %.sext = sext i8 %11 to i16
  %spec.select.2.v = tail call i16 @llvm.smax.i16(i16 %spec.select.1.v, i16 %.sext)
  %spec.select.3.v = tail call i16 @llvm.smax.i16(i16 %spec.select.2.v, i16 0)
  %spec.select.3 = trunc i16 %spec.select.3.v to i8
  br label %.split19.us

.split19.us:                                      ; preds = %.split.3, %27, %.split.us.preheader
  %.us-phi = phi i8 [ %spec.select.3, %.split.us.preheader ], [ %25, %27 ], [ %.1.2, %.split.3 ]
  ret i8 %.us-phi

12:                                               ; preds = %.split.preheader
  %13 = trunc nsw i16 %3 to i8
  store i8 0, ptr %1, align 1, !tbaa !5
  br label %.split.1

.split.1:                                         ; preds = %12, %.split.preheader
  %.1 = phi i8 [ %13, %12 ], [ 0, %.split.preheader ]
  %14 = srem i16 %4, 10
  %15 = zext nneg i8 %.1 to i16
  %16 = icmp sgt i16 %14, %15
  br i1 %16, label %17, label %.split.2

17:                                               ; preds = %.split.1
  %18 = trunc nsw i16 %14 to i8
  store i8 1, ptr %1, align 1, !tbaa !5
  br label %.split.2

.split.2:                                         ; preds = %17, %.split.1
  %.pre-phi = phi i16 [ %14, %17 ], [ %15, %.split.1 ]
  %.1.1 = phi i8 [ %18, %17 ], [ %.1, %.split.1 ]
  %19 = sdiv i16 %0, 100
  %20 = srem i16 %19, 10
  %21 = icmp sgt i16 %20, %.pre-phi
  br i1 %21, label %22, label %.split.3

22:                                               ; preds = %.split.2
  %23 = trunc nsw i16 %20 to i8
  store i8 2, ptr %1, align 1, !tbaa !5
  br label %.split.3

.split.3:                                         ; preds = %22, %.split.2
  %.pre-phi22 = phi i16 [ %20, %22 ], [ %.pre-phi, %.split.2 ]
  %.1.2 = phi i8 [ %23, %22 ], [ %.1.1, %.split.2 ]
  %24 = sdiv i16 %0, 1000
  %.lhs.trunc26 = trunc nsw i16 %24 to i8
  %25 = srem i8 %.lhs.trunc26, 10
  %.sext27 = sext i8 %25 to i16
  %26 = icmp slt i16 %.pre-phi22, %.sext27
  br i1 %26, label %27, label %.split19.us

27:                                               ; preds = %.split.3
  store i8 3, ptr %1, align 1, !tbaa !5
  br label %.split19.us
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smax.i16(i16, i16) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) 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 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: CorrelatedValuePropagationPass,DivRemPairsPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass