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

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z21ConvertStringToUInt64PKcPS0_(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %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 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %59, %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 i64, ptr %6, align 8, !tbaa !9
  store i64 %33, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %56

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

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

38:                                               ; preds = %34
  %39 = load i64, ptr %6, align 8, !tbaa !9
  %40 = mul i64 %39, 10
  store i64 %40, ptr %6, align 8, !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 !12
  %44 = load i64, ptr %6, align 8, !tbaa !9
  %45 = load i32, ptr %9, align 4, !tbaa !12
  %46 = zext i32 %45 to i64
  %47 = sub i64 -1, %46
  %48 = icmp ugt i64 %44, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %38
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %55

50:                                               ; preds = %38
  %51 = load i32, ptr %9, align 4, !tbaa !12
  %52 = zext i32 %51 to i64
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = add i64 %53, %52
  store i64 %54, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %8, align 4
  br label %55

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

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

58:                                               ; preds = %56
  br label %59

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

62:                                               ; preds = %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %63 = load i64, ptr %3, align 8
  ret i64 %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"long long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 i64 @_Z21ConvertStringToUInt64PKcPS0_(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 = %16, %4
  %.024.lcssa = phi i64 [ 0, %4 ], [ %17, %16 ]
  %.023.lcssa = phi ptr [ %0, %4 ], [ %18, %16 ]
  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, %16
  %8 = phi i8 [ %19, %16 ], [ %5, %4 ]
  %.02341 = phi ptr [ %18, %16 ], [ %0, %4 ]
  %.02440 = phi i64 [ %17, %16 ], [ 0, %4 ]
  %9 = icmp ugt i64 %.02440, 1844674407370955161
  br i1 %9, label %select.unfold, label %10

10:                                               ; preds = %.lr.ph
  %11 = mul nuw i64 %.02440, 10
  %12 = and i8 %8, 15
  %13 = zext nneg i8 %12 to i64
  %14 = xor i64 %13, -1
  %15 = icmp ugt i64 %11, %14
  br i1 %15, label %select.unfold, label %16

16:                                               ; preds = %10
  %17 = add i64 %11, %13
  %18 = getelementptr inbounds i8, ptr %.02341, i64 1
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = add i8 %19, -58
  %or.cond = icmp ult i8 %20, -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 i64 [ %.024.lcssa, %._crit_edge ], [ %.024.lcssa, %7 ], [ 0, %.lr.ph ], [ 0, %10 ]
  ret i64 %.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