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

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

9:                                                ; preds = %16, %2
  %10 = load i64, ptr %5, align 8, !tbaa !5
  %11 = lshr i64 %10, 4
  store i64 %11, ptr %5, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = icmp eq i64 %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 !11
  %18 = add i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !11
  br label %9, !llvm.loop !13

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

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

32:                                               ; preds = %24
  %33 = load i32, ptr %7, align 4, !tbaa !11
  %34 = add i32 48, %33
  br label %39

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

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

47:                                               ; preds = %39
  %48 = load i32, ptr %6, align 4, !tbaa !11
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %24, label %50, !llvm.loop !16

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


; 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 @_Z18ConvertUInt64ToHexyPc(i64 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp ult i64 %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 i64 [ %4, %.lr.ph ], [ %0, %2 ]
  %4 = lshr i64 %.01419, 4
  %5 = add nuw nsw i32 %.01320, 1
  %6 = icmp ult i64 %.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 = trunc i64 %0 to i32
  %10 = and i32 %9, 15
  %11 = lshr i64 %0, 4
  %12 = icmp ult i32 %10, 10
  %13 = or disjoint i32 %10, 48
  %14 = add nuw nsw i32 %10, 55
  %15 = select i1 %12, i32 %13, i32 %14
  %16 = trunc nuw nsw i32 %15 to i8
  %17 = add nsw i64 %.013.lcssa, -1
  %18 = getelementptr inbounds i8, ptr %1, i64 %17
  store i8 %16, ptr %18, 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 ], [ %17, %.prol.loopexit.unr-lcssa ]
  %.0.unr = phi i64 [ %0, %._crit_edge ], [ %11, %.prol.loopexit.unr-lcssa ]
  %19 = icmp eq i64 %.013.lcssa, 1
  br i1 %19, label %.unr-lcssa, label %._crit_edge.new

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

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

.unr-lcssa:                                       ; preds = %20, %.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