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

; 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
declare dso_local void @_Z21ConvertUInt32ToStringjPc(i32 noundef, ptr 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: mustprogress nounwind uwtable
define dso_local void @_Z21ConvertUInt64ToStringyPc(i64 noundef %0, ptr noundef %1) #0 {
  %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 @_Z21ConvertUInt32ToStringjPc(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 = trunc i32 %32 to i8
  %34 = load ptr, ptr %4, align 8, !tbaa !9
  %35 = getelementptr inbounds i8, ptr %34, i32 1
  store ptr %35, ptr %4, align 8, !tbaa !9
  store i8 %33, ptr %34, align 1, !tbaa !13
  br label %36

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

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

48:                                               ; preds = %36
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  store i8 0, ptr %49, align 1, !tbaa !13
  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 = { 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 = !{!"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 = distinct !{!16, !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 nounwind uwtable
declare dso_local void @_Z21ConvertUInt32ToStringjPc(i32 noundef, ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z21ConvertUInt64ToStringyPc(i64 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %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 @_Z21ConvertUInt32ToStringjPc(i32 noundef %6, ptr noundef %1) #2
  br label %31

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 i64 %15 to i8
  %19 = or disjoint i8 %18, 48
  store i8 %19, ptr %1, align 1, !tbaa !5
  %.01319 = getelementptr inbounds i8, ptr %1, i64 1
  %.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, 32
  br i1 %min.iters.check, label %.lr.ph.preheader37, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %20, 4294967264
  %ind.end = and i64 %20, 31
  %ind.end32 = getelementptr i8, ptr %.01319, i64 %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %next.gep = getelementptr i8, ptr %.01319, i64 %index
  %21 = xor i64 %index, -1
  %22 = add i64 %21, %20
  %23 = getelementptr inbounds [24 x i8], ptr %3, i64 0, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 -15
  %25 = getelementptr inbounds i8, ptr %23, i64 -31
  %wide.load = load <16 x i8>, ptr %24, align 1, !tbaa !5
  %reverse = shufflevector <16 x i8> %wide.load, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %wide.load35 = load <16 x i8>, ptr %25, align 1, !tbaa !5
  %reverse36 = shufflevector <16 x i8> %wide.load35, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %26 = getelementptr i8, ptr %next.gep, i64 16
  store <16 x i8> %reverse, ptr %next.gep, align 1, !tbaa !5
  store <16 x i8> %reverse36, ptr %26, align 1, !tbaa !5
  %index.next = add nuw i64 %index, 32
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !10

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

.lr.ph.preheader37:                               ; 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.preheader37, %.lr.ph
  %indvars.iv25 = phi i64 [ %28, %.lr.ph ], [ %indvars.iv25.ph, %.lr.ph.preheader37 ]
  %.01322 = phi ptr [ %.013, %.lr.ph ], [ %.01322.ph, %.lr.ph.preheader37 ]
  %28 = add nsw i64 %indvars.iv25, -1
  %29 = getelementptr inbounds [24 x i8], ptr %3, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  store i8 %30, ptr %.01322, align 1, !tbaa !5
  %.013 = getelementptr inbounds i8, ptr %.01322, i64 1
  %.not.wide = icmp eq i64 %28, 0
  br i1 %.not.wide, label %._crit_edge, label %.lr.ph, !llvm.loop !13

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

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

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


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