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

; 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 @_Z21ConvertStringToUInt64PKwPS0_(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 i32, align 4
  %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 = %56, %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i32, ptr %17, align 4, !tbaa !11
  store i32 %18, ptr %7, align 4, !tbaa !11
  %19 = load i32, ptr %7, align 4, !tbaa !11
  %20 = icmp slt i32 %19, 48
  br i1 %20, label %24, label %21

21:                                               ; preds = %16
  %22 = load i32, ptr %7, align 4, !tbaa !11
  %23 = icmp sgt i32 %22, 57
  br i1 %23, label %24, label %32

24:                                               ; preds = %21, %16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %30

27:                                               ; preds = %24
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %28, ptr %29, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %27, %24
  %31 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %31, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %53

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

35:                                               ; preds = %32
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %53

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

46:                                               ; preds = %36
  store i64 0, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %52

47:                                               ; preds = %36
  %48 = load i32, ptr %9, align 4, !tbaa !13
  %49 = zext i32 %48 to i64
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = add i64 %50, %49
  store i64 %51, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %8, align 4
  br label %52

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

53:                                               ; preds = %52, %35, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %59 [
    i32 0, label %55
  ]

55:                                               ; preds = %53
  br label %56

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

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

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 = !{!12, !12, i64 0}
!12 = !{!"wchar_t", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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 @_Z21ConvertStringToUInt64PKwPS0_(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 i32, ptr %0, align 4, !tbaa !9
  %6 = add i32 %5, -58
  %or.cond38 = icmp ult i32 %6, -10
  br i1 %or.cond38, label %._crit_edge, label %.lr.ph

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

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

.lr.ph:                                           ; preds = %4, %16
  %8 = phi i32 [ %19, %16 ], [ %5, %4 ]
  %.02240 = phi ptr [ %18, %16 ], [ %0, %4 ]
  %.02339 = phi i64 [ %17, %16 ], [ 0, %4 ]
  %9 = icmp ugt i64 %.02339, 1844674407370955161
  br i1 %9, label %select.unfold, label %10

10:                                               ; preds = %.lr.ph
  %11 = mul nuw i64 %.02339, 10
  %12 = add nsw i32 %8, -48
  %13 = zext nneg i32 %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 %.02240, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = add i32 %19, -58
  %or.cond = icmp ult i32 %20, -10
  br i1 %or.cond, label %._crit_edge, label %.lr.ph, !llvm.loop !11

select.unfold:                                    ; preds = %10, %.lr.ph, %7, %._crit_edge
  %.2.ph = phi i64 [ %.023.lcssa, %._crit_edge ], [ %.023.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 = !{!10, !10, i64 0}
!10 = !{!"wchar_t", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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