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

; 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 @_Z18ConvertUInt64ToOctyPc(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, 3
  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 = %38, %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, 7
  %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, 3
  store i64 %29, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !11
  %31 = add i32 48, %30
  %32 = trunc i32 %31 to i8
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = add i32 %34, -1
  store i32 %35, ptr %6, align 4, !tbaa !11
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %33, i64 %36
  store i8 %32, ptr %37, align 1, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %38

38:                                               ; preds = %24
  %39 = load i32, ptr %6, align 4, !tbaa !11
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %24, label %41, !llvm.loop !16

41:                                               ; preds = %38
  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 @_Z18ConvertUInt64ToOctyPc(i64 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = icmp ult i64 %0, 8
  br i1 %3, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01115 = phi i32 [ %5, %.lr.ph ], [ 1, %2 ]
  %.01214 = phi i64 [ %4, %.lr.ph ], [ %0, %2 ]
  %4 = lshr i64 %.01214, 3
  %5 = add nuw nsw i32 %.01115, 1
  %6 = icmp ult i64 %.01214, 64
  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
  %.011.lcssa = phi i64 [ 1, %2 ], [ %7, %._crit_edge.loopexit ]
  %8 = getelementptr inbounds i8, ptr %1, i64 %.011.lcssa
  store i8 0, ptr %8, align 1, !tbaa !7
  %9 = add nsw i64 %.011.lcssa, -1
  %xtraiter = and i64 %.011.lcssa, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %._crit_edge, %.prol.preheader
  %indvars.iv.prol = phi i64 [ %14, %.prol.preheader ], [ %.011.lcssa, %._crit_edge ]
  %.0.prol = phi i64 [ %12, %.prol.preheader ], [ %0, %._crit_edge ]
  %prol.iter = phi i64 [ %prol.iter.next, %.prol.preheader ], [ 0, %._crit_edge ]
  %10 = trunc i64 %.0.prol to i8
  %11 = and i8 %10, 7
  %12 = lshr i64 %.0.prol, 3
  %13 = or disjoint i8 %11, 48
  %14 = add nsw i64 %indvars.iv.prol, -1
  %15 = getelementptr inbounds i8, ptr %1, i64 %14
  store i8 %13, ptr %15, align 1, !tbaa !7
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !10

.prol.loopexit:                                   ; preds = %.prol.preheader, %._crit_edge
  %indvars.iv.unr = phi i64 [ %.011.lcssa, %._crit_edge ], [ %14, %.prol.preheader ]
  %.0.unr = phi i64 [ %0, %._crit_edge ], [ %12, %.prol.preheader ]
  %16 = icmp ult i64 %9, 3
  br i1 %16, label %.unr-lcssa, label %._crit_edge.new

._crit_edge.new:                                  ; preds = %.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %1, i64 -1
  %invariant.gep19 = getelementptr i8, ptr %1, i64 -2
  %invariant.gep21 = getelementptr i8, ptr %1, i64 -3
  br label %17

17:                                               ; preds = %17, %._crit_edge.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %._crit_edge.new ], [ %34, %17 ]
  %.0 = phi i64 [ %.0.unr, %._crit_edge.new ], [ %32, %17 ]
  %18 = trunc i64 %.0 to i8
  %19 = and i8 %18, 7
  %20 = or disjoint i8 %19, 48
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  store i8 %20, ptr %gep, align 1, !tbaa !7
  %21 = trunc i64 %.0 to i8
  %22 = lshr i8 %21, 3
  %23 = and i8 %22, 7
  %24 = lshr i64 %.0, 6
  %25 = or disjoint i8 %23, 48
  %gep20 = getelementptr i8, ptr %invariant.gep19, i64 %indvars.iv
  store i8 %25, ptr %gep20, align 1, !tbaa !7
  %26 = trunc i64 %24 to i8
  %27 = and i8 %26, 7
  %28 = lshr i64 %.0, 9
  %29 = or disjoint i8 %27, 48
  %gep22 = getelementptr i8, ptr %invariant.gep21, i64 %indvars.iv
  store i8 %29, ptr %gep22, align 1, !tbaa !7
  %30 = trunc i64 %28 to i8
  %31 = and i8 %30, 7
  %32 = lshr i64 %.0, 12
  %33 = or disjoint i8 %31, 48
  %34 = add nsw i64 %indvars.iv, -4
  %35 = getelementptr inbounds i8, ptr %1, i64 %34
  store i8 %33, ptr %35, align 1, !tbaa !7
  %.not.wide.3 = icmp eq i64 %34, 0
  br i1 %.not.wide.3, label %.unr-lcssa, label %17, !llvm.loop !12

.unr-lcssa:                                       ; preds = %17, %.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, !11}
!11 = !{!"llvm.loop.unroll.disable"}
!12 = distinct !{!12, !6}


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