; 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"

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @checkDigits(i32 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 i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1000, ptr %2, align 4, !tbaa !5
  br label %4

4:                                                ; preds = %28, %0
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp slt i32 %5, 10000
  br i1 %6, label %8, label %7

7:                                                ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %31

8:                                                ; preds = %4
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call signext i8 @checkDigits(i32 noundef %9)
  %11 = icmp ne i8 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %8
  br label %28

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sdiv i32 %14, 100
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = srem i32 %16, 100
  %18 = add nsw i32 %15, %17
  store i32 %18, ptr %3, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = mul nsw i32 %19, %20
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = icmp eq i32 %21, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %13
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %25)
  br label %27

27:                                               ; preds = %24, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %28

28:                                               ; preds = %27, %12
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !9

31:                                               ; preds = %7
  %32 = load i32, ptr %1, align 4
  ret i32 %32
}

declare i32 @printf(ptr noundef, ...) #2

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 = { "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 #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 = distinct !{!9, !10}
!10 = !{!"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"

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @checkDigits(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  br label %2

1:                                                ; preds = %12
  ret i32 0

2:                                                ; preds = %0, %12
  %.012 = phi i32 [ 1000, %0 ], [ %13, %12 ]
  %3 = tail call signext i8 @checkDigits(i32 noundef %.012)
  %.not = icmp eq i8 %3, 0
  br i1 %.not, label %4, label %12

4:                                                ; preds = %2
  %.lhs.trunc = trunc nuw i32 %.012 to i16
  %5 = udiv i16 %.lhs.trunc, 100
  %6 = mul i16 %5, -99
  %narrow = add i16 %6, %.lhs.trunc
  %7 = zext nneg i16 %narrow to i32
  %8 = mul nuw nsw i32 %7, %7
  %9 = icmp eq i32 %8, %.012
  br i1 %9, label %10, label %12

10:                                               ; preds = %4
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.012)
  br label %12

12:                                               ; preds = %4, %10, %2
  %13 = add nuw nsw i32 %.012, 1
  %exitcond.not = icmp eq i32 %13, 10000
  br i1 %exitcond.not, label %1, label %2, !llvm.loop !5
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 = { nofree nounwind "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: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass