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

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

9:                                                ; preds = %16, %2
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = lshr i32 %10, 4
  store i32 %11, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %9
  br label %19

15:                                               ; preds = %9
  br label %16

16:                                               ; preds = %15
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = add i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !5
  br label %9, !llvm.loop !11

19:                                               ; preds = %14
  %20 = load ptr, ptr %4, align 8, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  store i8 0, ptr %23, align 1, !tbaa !13
  br label %24

24:                                               ; preds = %46, %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = and i32 %25, 15
  store i32 %26, ptr %7, align 4, !tbaa !5
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = lshr i32 %27, 4
  store i32 %28, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = icmp ult i32 %29, 10
  br i1 %30, label %31, label %34

31:                                               ; preds = %24
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = add i32 48, %32
  br label %38

34:                                               ; preds = %24
  %35 = load i32, ptr %7, align 4, !tbaa !5
  %36 = sub i32 %35, 10
  %37 = add i32 65, %36
  br label %38

38:                                               ; preds = %34, %31
  %39 = phi i32 [ %33, %31 ], [ %37, %34 ]
  %40 = trunc i32 %39 to i8
  %41 = load ptr, ptr %4, align 8, !tbaa !9
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add i32 %42, -1
  store i32 %43, ptr %6, align 4, !tbaa !5
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  store i8 %40, ptr %45, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %46

46:                                               ; preds = %38
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %24, label %49, !llvm.loop !14

49:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !12}


; 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(argmem: write) uwtable
define dso_local void @_Z18ConvertUInt32ToHexjPc(i32 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp ult i32 %0, 16
  br i1 %3, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01320 = phi i32 [ %5, %.lr.ph ], [ 1, %2 ]
  %.01419 = phi i32 [ %4, %.lr.ph ], [ %0, %2 ]
  %4 = lshr i32 %.01419, 4
  %5 = add nuw nsw i32 %.01320, 1
  %6 = icmp ult i32 %.01419, 256
  br i1 %6, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %7 = zext nneg i32 %5 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %.013.lcssa = phi i64 [ 1, %2 ], [ %7, %._crit_edge.loopexit ]
  %8 = getelementptr inbounds i8, ptr %1, i64 %.013.lcssa
  store i8 0, ptr %8, align 1, !tbaa !7
  %xtraiter = and i64 %.013.lcssa, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.loopexit.unr-lcssa

.prol.loopexit.unr-lcssa:                         ; preds = %._crit_edge
  %9 = and i32 %0, 15
  %10 = lshr i32 %0, 4
  %11 = icmp ult i32 %9, 10
  %12 = or disjoint i32 %9, 48
  %13 = add nuw nsw i32 %9, 55
  %14 = select i1 %11, i32 %12, i32 %13
  %15 = trunc nuw nsw i32 %14 to i8
  %16 = add nsw i64 %.013.lcssa, -1
  %17 = getelementptr inbounds i8, ptr %1, i64 %16
  store i8 %15, ptr %17, align 1, !tbaa !7
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %._crit_edge
  %indvars.iv.unr = phi i64 [ %.013.lcssa, %._crit_edge ], [ %16, %.prol.loopexit.unr-lcssa ]
  %.0.unr = phi i32 [ %0, %._crit_edge ], [ %10, %.prol.loopexit.unr-lcssa ]
  %18 = icmp eq i64 %.013.lcssa, 1
  br i1 %18, label %.unr-lcssa, label %._crit_edge.new

._crit_edge.new:                                  ; preds = %.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %1, i64 -1
  br label %19

19:                                               ; preds = %19, %._crit_edge.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %._crit_edge.new ], [ %34, %19 ]
  %.0 = phi i32 [ %.0.unr, %._crit_edge.new ], [ %28, %19 ]
  %20 = and i32 %.0, 15
  %21 = lshr i32 %.0, 4
  %22 = icmp ult i32 %20, 10
  %23 = or disjoint i32 %20, 48
  %24 = add nuw nsw i32 %20, 55
  %25 = select i1 %22, i32 %23, i32 %24
  %26 = trunc nuw nsw i32 %25 to i8
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  store i8 %26, ptr %gep, align 1, !tbaa !7
  %27 = and i32 %21, 15
  %28 = lshr i32 %.0, 8
  %29 = icmp ult i32 %27, 10
  %30 = or disjoint i32 %27, 48
  %31 = add nuw nsw i32 %27, 55
  %32 = select i1 %29, i32 %30, i32 %31
  %33 = trunc nuw nsw i32 %32 to i8
  %34 = add nsw i64 %indvars.iv, -2
  %35 = getelementptr inbounds i8, ptr %1, i64 %34
  store i8 %33, ptr %35, align 1, !tbaa !7
  %.not.wide.1 = icmp eq i64 %34, 0
  br i1 %.not.wide.1, label %.unr-lcssa, label %19, !llvm.loop !10

.unr-lcssa:                                       ; preds = %19, %.prol.loopexit
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: write) 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !6}


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