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

; 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
declare dso_local void @_Z21ConvertUInt32ToStringjPw(i32 noundef, ptr noundef) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z21ConvertUInt64ToStringyPw(i64 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca [24 x i8], align 16
  %6 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = icmp ule i64 %7, 4294967295
  br i1 %8, label %9, label %13

9:                                                ; preds = %2
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = trunc i64 %10 to i32
  %12 = load ptr, ptr %4, align 8, !tbaa !9
  call void @_Z21ConvertUInt32ToStringjPw(i32 noundef %11, ptr noundef %12) #2
  br label %50

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 24, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %17, %13
  %15 = load i64, ptr %3, align 8, !tbaa !5
  %16 = icmp uge i64 %15, 10
  br i1 %16, label %17, label %29

17:                                               ; preds = %14
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = urem i64 %18, 10
  %20 = trunc i64 %19 to i32
  %21 = add i32 48, %20
  %22 = trunc i32 %21 to i8
  %23 = load i32, ptr %6, align 4, !tbaa !11
  %24 = add i32 %23, 1
  store i32 %24, ptr %6, align 4, !tbaa !11
  %25 = zext i32 %23 to i64
  %26 = getelementptr inbounds [24 x i8], ptr %5, i64 0, i64 %25
  store i8 %22, ptr %26, align 1, !tbaa !13
  %27 = load i64, ptr %3, align 8, !tbaa !5
  %28 = udiv i64 %27, 10
  store i64 %28, ptr %3, align 8, !tbaa !5
  br label %14, !llvm.loop !14

29:                                               ; preds = %14
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = trunc i64 %30 to i32
  %32 = add i32 48, %31
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = getelementptr inbounds i32, ptr %33, i32 1
  store ptr %34, ptr %4, align 8, !tbaa !9
  store i32 %32, ptr %33, align 4, !tbaa !16
  br label %35

35:                                               ; preds = %38, %29
  %36 = load i32, ptr %6, align 4, !tbaa !11
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %48

38:                                               ; preds = %35
  %39 = load i32, ptr %6, align 4, !tbaa !11
  %40 = add i32 %39, -1
  store i32 %40, ptr %6, align 4, !tbaa !11
  %41 = load i32, ptr %6, align 4, !tbaa !11
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds [24 x i8], ptr %5, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !13
  %45 = zext i8 %44 to i32
  %46 = load ptr, ptr %4, align 8, !tbaa !9
  %47 = getelementptr inbounds i32, ptr %46, i32 1
  store ptr %47, ptr %4, align 8, !tbaa !9
  store i32 %45, ptr %46, align 4, !tbaa !16
  br label %35, !llvm.loop !18

48:                                               ; preds = %35
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 0, ptr %49, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 24, ptr %5) #2
  br label %50

50:                                               ; preds = %48, %9
  ret void
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"wchar_t", !7, i64 0}
!18 = distinct !{!18, !15}


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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z21ConvertUInt32ToStringjPw(i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z21ConvertUInt64ToStringyPw(i64 noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = alloca [24 x i8], align 16
  %4 = icmp ult i64 %0, 4294967296
  br i1 %4, label %5, label %7

5:                                                ; preds = %2
  %6 = trunc nuw i64 %0 to i32
  tail call void @_Z21ConvertUInt32ToStringjPw(i32 noundef %6, ptr noundef %1) #2
  br label %35

7:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #2
  br label %8

8:                                                ; preds = %7, %8
  %indvars.iv = phi i32 [ 1, %7 ], [ %indvars.iv.next, %8 ]
  %.018 = phi i32 [ 0, %7 ], [ %12, %8 ]
  %.01417 = phi i64 [ %0, %7 ], [ %15, %8 ]
  %9 = urem i64 %.01417, 10
  %10 = trunc nuw nsw i64 %9 to i8
  %11 = or disjoint i8 %10, 48
  %12 = add i32 %.018, 1
  %13 = zext i32 %.018 to i64
  %14 = getelementptr inbounds [24 x i8], ptr %3, i64 0, i64 %13
  store i8 %11, ptr %14, align 1, !tbaa !5
  %15 = udiv i64 %.01417, 10
  %16 = icmp ugt i64 %.01417, 99
  %indvars.iv.next = add i32 %indvars.iv, 1
  br i1 %16, label %8, label %17, !llvm.loop !8

17:                                               ; preds = %8
  %18 = trunc nuw nsw i64 %15 to i32
  %19 = or disjoint i32 %18, 48
  store i32 %19, ptr %1, align 4, !tbaa !10
  %.01319 = getelementptr inbounds i8, ptr %1, i64 4
  %.not20 = icmp eq i32 %12, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %17
  %20 = zext i32 %indvars.iv to i64
  %min.iters.check = icmp ult i32 %indvars.iv, 8
  br i1 %min.iters.check, label %.lr.ph.preheader38, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %20, 4294967288
  %ind.end = and i64 %20, 7
  %21 = shl nuw nsw i64 %n.vec, 2
  %ind.end32 = getelementptr i8, ptr %.01319, i64 %21
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx34 = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %.01319, i64 %offset.idx34
  %22 = xor i64 %index, -1
  %23 = add i64 %22, %20
  %24 = getelementptr inbounds [24 x i8], ptr %3, i64 0, i64 %23
  %25 = getelementptr inbounds i8, ptr %24, i64 -3
  %26 = getelementptr inbounds i8, ptr %24, i64 -7
  %wide.load = load <4 x i8>, ptr %25, align 1, !tbaa !5
  %reverse = shufflevector <4 x i8> %wide.load, <4 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %wide.load36 = load <4 x i8>, ptr %26, align 1, !tbaa !5
  %reverse37 = shufflevector <4 x i8> %wide.load36, <4 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %27 = zext <4 x i8> %reverse to <4 x i32>
  %28 = zext <4 x i8> %reverse37 to <4 x i32>
  %29 = getelementptr i8, ptr %next.gep, i64 16
  store <4 x i32> %27, ptr %next.gep, align 4, !tbaa !10
  store <4 x i32> %28, ptr %29, align 4, !tbaa !10
  %index.next = add nuw i64 %index, 8
  %30 = icmp eq i64 %index.next, %n.vec
  br i1 %30, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %20
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader38

.lr.ph.preheader38:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv25.ph = phi i64 [ %20, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01322.ph = phi ptr [ %.01319, %.lr.ph.preheader ], [ %ind.end32, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader38, %.lr.ph
  %indvars.iv25 = phi i64 [ %31, %.lr.ph ], [ %indvars.iv25.ph, %.lr.ph.preheader38 ]
  %.01322 = phi ptr [ %.013, %.lr.ph ], [ %.01322.ph, %.lr.ph.preheader38 ]
  %31 = add nsw i64 %indvars.iv25, -1
  %32 = getelementptr inbounds [24 x i8], ptr %3, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %34 = zext i8 %33 to i32
  store i32 %34, ptr %.01322, align 4, !tbaa !10
  %.013 = getelementptr inbounds i8, ptr %.01322, i64 4
  %.not.wide = icmp eq i64 %31, 0
  br i1 %.not.wide, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %17
  %.013.lcssa = phi ptr [ %.01319, %17 ], [ %ind.end32, %middle.block ], [ %.013, %.lr.ph ]
  store i32 0, ptr %.013.lcssa, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #2
  br label %35

35:                                               ; preds = %._crit_edge, %5
  ret void
}

attributes #0 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"wchar_t", !6, i64 0}
!12 = distinct !{!12, !9, !13, !14}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !9, !14, !13}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass