; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0158 Easy - The Torn Number/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 @checkDigits(i32 noundef %0) #0 {
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  %4 = alloca [4 x i8], align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.memset.p0.i64(ptr align 1 %4, i8 0, i64 4, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %47, %1
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 4
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  store i32 2, ptr %6, align 4
  br label %50

12:                                               ; preds = %8
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = srem i32 %13, 10
  %15 = trunc i32 %14 to i8
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 %17
  store i8 %15, ptr %18, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sub nsw i32 %19, 1
  store i32 %20, ptr %7, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %39, %12
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = icmp sge i32 %22, 0
  br i1 %23, label %25, label %24

24:                                               ; preds = %21
  store i32 5, ptr %6, align 4
  br label %42

25:                                               ; preds = %21
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !9
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %30, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %25
  store i8 1, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %42

38:                                               ; preds = %25
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %7, align 4, !tbaa !5
  %41 = add nsw i32 %40, -1
  store i32 %41, ptr %7, align 4, !tbaa !5
  br label %21, !llvm.loop !10

42:                                               ; preds = %37, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %43 = load i32, ptr %6, align 4
  switch i32 %43, label %50 [
    i32 5, label %44
  ]

44:                                               ; preds = %42
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = sdiv i32 %45, 10
  store i32 %46, ptr %3, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %44
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !12

50:                                               ; preds = %42, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %51 = load i32, ptr %6, align 4
  switch i32 %51, label %53 [
    i32 2, label %52
  ]

52:                                               ; preds = %50
  store i8 0, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %53

53:                                               ; preds = %52, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %54 = load i8, ptr %2, align 1
  ret i8 %54
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!6, !6, i64 0}
!6 = !{!"int", !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"}
!12 = distinct !{!12, !11}


; 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(none) uwtable
define dso_local noundef signext i8 @checkDigits(i32 noundef %0) local_unnamed_addr #0 {
.critedge:
  %1 = srem i32 %0, 10
  %2 = sdiv i32 %0, 10
  %3 = srem i32 %2, 10
  %4 = icmp eq i32 %1, %3
  br i1 %4, label %.loopexit.loopexit, label %.critedge.1, !llvm.loop !5

.critedge.1:                                      ; preds = %.critedge
  %5 = sdiv i32 %0, 100
  %6 = srem i32 %5, 10
  %7 = icmp eq i32 %3, %6
  %8 = icmp eq i32 %1, %6
  %or.cond = or i1 %7, %8
  br i1 %or.cond, label %.loopexit.loopexit, label %.critedge.2, !llvm.loop !5

.critedge.2:                                      ; preds = %.critedge.1
  %9 = sdiv i32 %0, 1000
  %10 = srem i32 %9, 10
  %11 = icmp eq i32 %6, %10
  %12 = icmp eq i32 %3, %10
  %or.cond39 = or i1 %11, %12
  %13 = icmp eq i32 %1, %10
  %or.cond40 = or i1 %or.cond39, %13
  br i1 %or.cond40, label %.loopexit.loopexit, label %.loopexit, !llvm.loop !5

.loopexit.loopexit:                               ; preds = %.critedge.2, %.critedge.1, %.critedge
  br label %.loopexit

.loopexit:                                        ; preds = %.critedge.2, %.loopexit.loopexit
  %spec.select = phi i8 [ 1, %.loopexit.loopexit ], [ 0, %.critedge.2 ]
  ret i8 %spec.select
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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" }

!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,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass