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

; 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 void @_Z21ConvertUInt32ToStringjPw(i32 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca [16 x i8], align 16
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %10, %2
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = icmp uge i32 %8, 10
  br i1 %9, label %10, label %21

10:                                               ; preds = %7
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = urem i32 %11, 10
  %13 = add i32 48, %12
  %14 = trunc i32 %13 to i8
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = add i32 %15, 1
  store i32 %16, ptr %6, align 4, !tbaa !5
  %17 = zext i32 %15 to i64
  %18 = getelementptr inbounds [16 x i8], ptr %5, i64 0, i64 %17
  store i8 %14, ptr %18, align 1, !tbaa !11
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = udiv i32 %19, 10
  store i32 %20, ptr %3, align 4, !tbaa !5
  br label %7, !llvm.loop !12

21:                                               ; preds = %7
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = add i32 48, %22
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds i32, ptr %24, i32 1
  store ptr %25, ptr %4, align 8, !tbaa !9
  store i32 %23, ptr %24, align 4, !tbaa !14
  br label %26

26:                                               ; preds = %29, %21
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %39

29:                                               ; preds = %26
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = add i32 %30, -1
  store i32 %31, ptr %6, align 4, !tbaa !5
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [16 x i8], ptr %5, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = zext i8 %35 to i32
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds i32, ptr %37, i32 1
  store ptr %38, ptr %4, align 8, !tbaa !9
  store i32 %36, ptr %37, align 4, !tbaa !14
  br label %26, !llvm.loop !16

39:                                               ; preds = %26
  %40 = load ptr, ptr %4, align 8, !tbaa !9
  store i32 0, ptr %40, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #2
  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 = !{!"int", !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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"wchar_t", !7, i64 0}
!16 = distinct !{!16, !13}


; 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 nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local void @_Z21ConvertUInt32ToStringjPw(i32 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #2
  %4 = icmp ugt i32 %0, 9
  br i1 %4, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2
  %5 = or disjoint i32 %0, 48
  store i32 %5, ptr %1, align 4, !tbaa !5
  %.0101528 = getelementptr inbounds i8, ptr %1, i64 4
  br label %._crit_edge21

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.013 = phi i32 [ %9, %.lr.ph ], [ 0, %2 ]
  %.01112 = phi i32 [ %12, %.lr.ph ], [ %0, %2 ]
  %6 = urem i32 %.01112, 10
  %7 = trunc nuw nsw i32 %6 to i8
  %8 = or disjoint i8 %7, 48
  %9 = add i32 %.013, 1
  %10 = zext i32 %.013 to i64
  %11 = getelementptr inbounds [16 x i8], ptr %3, i64 0, i64 %10
  store i8 %8, ptr %11, align 1, !tbaa !9
  %12 = udiv i32 %.01112, 10
  %13 = icmp ugt i32 %.01112, 99
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph
  %14 = or disjoint i32 %12, 48
  store i32 %14, ptr %1, align 4, !tbaa !5
  %.01015 = getelementptr inbounds i8, ptr %1, i64 4
  %.not16 = icmp eq i32 %9, 0
  br i1 %.not16, label %._crit_edge21, label %.lr.ph20.preheader

.lr.ph20.preheader:                               ; preds = %._crit_edge
  %15 = zext i32 %9 to i64
  %min.iters.check = icmp ult i32 %9, 8
  br i1 %min.iters.check, label %.lr.ph20.preheader40, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph20.preheader
  %n.vec = and i64 %15, 4294967288
  %ind.end = and i64 %15, 7
  %16 = shl nuw nsw i64 %n.vec, 2
  %ind.end34 = getelementptr i8, ptr %.01015, i64 %16
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx36 = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %.01015, i64 %offset.idx36
  %17 = xor i64 %index, -1
  %18 = add i64 %17, %15
  %19 = getelementptr inbounds [16 x i8], ptr %3, i64 0, i64 %18
  %20 = getelementptr inbounds i8, ptr %19, i64 -3
  %21 = getelementptr inbounds i8, ptr %19, i64 -7
  %wide.load = load <4 x i8>, ptr %20, align 1, !tbaa !9
  %reverse = shufflevector <4 x i8> %wide.load, <4 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %wide.load38 = load <4 x i8>, ptr %21, align 1, !tbaa !9
  %reverse39 = shufflevector <4 x i8> %wide.load38, <4 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %22 = zext <4 x i8> %reverse to <4 x i32>
  %23 = zext <4 x i8> %reverse39 to <4 x i32>
  %24 = getelementptr i8, ptr %next.gep, i64 16
  store <4 x i32> %22, ptr %next.gep, align 4, !tbaa !5
  store <4 x i32> %23, ptr %24, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %15
  br i1 %cmp.n, label %._crit_edge21, label %.lr.ph20.preheader40

.lr.ph20.preheader40:                             ; preds = %middle.block, %.lr.ph20.preheader
  %indvars.iv.ph = phi i64 [ %15, %.lr.ph20.preheader ], [ %ind.end, %middle.block ]
  %.01018.ph = phi ptr [ %.01015, %.lr.ph20.preheader ], [ %ind.end34, %middle.block ]
  br label %.lr.ph20

.lr.ph20:                                         ; preds = %.lr.ph20.preheader40, %.lr.ph20
  %indvars.iv = phi i64 [ %26, %.lr.ph20 ], [ %indvars.iv.ph, %.lr.ph20.preheader40 ]
  %.01018 = phi ptr [ %.010, %.lr.ph20 ], [ %.01018.ph, %.lr.ph20.preheader40 ]
  %26 = add nsw i64 %indvars.iv, -1
  %27 = getelementptr inbounds [16 x i8], ptr %3, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = zext i8 %28 to i32
  store i32 %29, ptr %.01018, align 4, !tbaa !5
  %.010 = getelementptr inbounds i8, ptr %.01018, i64 4
  %.not.wide = icmp eq i64 %26, 0
  br i1 %.not.wide, label %._crit_edge21, label %.lr.ph20, !llvm.loop !15

._crit_edge21:                                    ; preds = %.lr.ph20, %middle.block, %._crit_edge.thread, %._crit_edge
  %.010.lcssa = phi ptr [ %.01015, %._crit_edge ], [ %.0101528, %._crit_edge.thread ], [ %ind.end34, %middle.block ], [ %.010, %.lr.ph20 ]
  store i32 0, ptr %.010.lcssa, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #2
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree norecurse nosync nounwind memory(write, 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" }
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 = !{!"wchar_t", !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"}
!12 = distinct !{!12, !11, !13, !14}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !11, !14, !13}


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