; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/Common/StringToInt.ll (Z21ConvertStringToUInt32PKcPS0_)

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z21ConvertStringToUInt32PKcPS0_(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %15

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %14, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %12, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %57, %15
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i8, ptr %17, align 1, !tbaa !11
  store i8 %18, ptr %7, align 1, !tbaa !11
  %19 = load i8, ptr %7, align 1, !tbaa !11
  %20 = zext i8 %19 to i32
  %21 = icmp slt i32 %20, 48
  br i1 %21, label %26, label %22

22:                                               ; preds = %16
  %23 = load i8, ptr %7, align 1, !tbaa !11
  %24 = zext i8 %23 to i32
  %25 = icmp sgt i32 %24, 57
  br i1 %25, label %26, label %34

26:                                               ; preds = %22, %16
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %32

29:                                               ; preds = %26
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %30, ptr %31, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %29, %26
  %33 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %33, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %54

34:                                               ; preds = %22
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = icmp ugt i32 %35, 429496729
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %54

38:                                               ; preds = %34
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = mul i32 %39, 10
  store i32 %40, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %41 = load i8, ptr %7, align 1, !tbaa !11
  %42 = zext i8 %41 to i32
  %43 = sub nsw i32 %42, 48
  store i32 %43, ptr %9, align 4, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = sub i32 -1, %45
  %47 = icmp ugt i32 %44, %46
  br i1 %47, label %48, label %49

48:                                               ; preds = %38
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %53

49:                                               ; preds = %38
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = add i32 %51, %50
  store i32 %52, ptr %6, align 4, !tbaa !9
  store i32 0, ptr %8, align 4
  br label %53

53:                                               ; preds = %49, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %54

54:                                               ; preds = %53, %37, %32
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  %55 = load i32, ptr %8, align 4
  switch i32 %55, label %60 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %57

57:                                               ; preds = %56
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %4, align 8, !tbaa !5
  br label %16, !llvm.loop !12

60:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %61 = load i32, ptr %3, align 4
  ret i32 %61
}

; 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 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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 memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z21ConvertStringToUInt32PKcPS0_(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %4, label %3

3:                                                ; preds = %2
  store ptr %0, ptr %1, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %3, %2
  %5 = load i8, ptr %0, align 1, !tbaa !9
  %6 = add i8 %5, -58
  %or.cond39 = icmp ult i8 %6, -10
  br i1 %or.cond39, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %14, %4
  %.024.lcssa = phi i32 [ 0, %4 ], [ %16, %14 ]
  %.023.lcssa = phi ptr [ %0, %4 ], [ %17, %14 ]
  br i1 %.not, label %select.unfold, label %7

7:                                                ; preds = %._crit_edge
  store ptr %.023.lcssa, ptr %1, align 8, !tbaa !5
  br label %select.unfold

.lr.ph:                                           ; preds = %4, %14
  %.in = phi i8 [ %18, %14 ], [ %5, %4 ]
  %.02341 = phi ptr [ %17, %14 ], [ %0, %4 ]
  %.02440 = phi i32 [ %16, %14 ], [ 0, %4 ]
  %8 = zext nneg i8 %.in to i32
  %9 = icmp ugt i32 %.02440, 429496729
  br i1 %9, label %select.unfold, label %10

10:                                               ; preds = %.lr.ph
  %11 = mul nuw i32 %.02440, 10
  %12 = sub nsw i32 47, %8
  %13 = icmp ugt i32 %11, %12
  br i1 %13, label %select.unfold, label %14

14:                                               ; preds = %10
  %15 = add i32 %11, -48
  %16 = add i32 %15, %8
  %17 = getelementptr inbounds i8, ptr %.02341, i64 1
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = add i8 %18, -58
  %or.cond = icmp ult i8 %19, -10
  br i1 %or.cond, label %._crit_edge, label %.lr.ph, !llvm.loop !10

select.unfold:                                    ; preds = %10, %.lr.ph, %7, %._crit_edge
  %.2.ph = phi i32 [ %.024.lcssa, %._crit_edge ], [ %.024.lcssa, %7 ], [ 0, %.lr.ph ], [ 0, %10 ]
  ret i32 %.2.ph
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: 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 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass