; LLVM IR from yinqiwen/ardb :: src/common/util/string_helper.ll (ZN4ardb9ll2stringEPcmx)

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN4ardb9ll2stringEPcmx(ptr noundef %0, i64 noundef %1, i64 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca [32 x i8], align 16
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

16:                                               ; preds = %3
  %17 = load i64, ptr %7, align 8, !tbaa !11
  %18 = icmp slt i64 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %16
  %20 = load i64, ptr %7, align 8, !tbaa !11
  %21 = sub nsw i64 0, %20
  br label %24

22:                                               ; preds = %16
  %23 = load i64, ptr %7, align 8, !tbaa !11
  br label %24

24:                                               ; preds = %22, %19
  %25 = phi i64 [ %21, %19 ], [ %23, %22 ]
  store i64 %25, ptr %10, align 8, !tbaa !11
  %26 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  %27 = getelementptr inbounds i8, ptr %26, i64 31
  store ptr %27, ptr %9, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %37, %24
  %29 = load i64, ptr %10, align 8, !tbaa !11
  %30 = urem i64 %29, 10
  %31 = add i64 48, %30
  %32 = trunc i64 %31 to i8
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i32 -1
  store ptr %34, ptr %9, align 8, !tbaa !5
  store i8 %32, ptr %33, align 1, !tbaa !13
  %35 = load i64, ptr %10, align 8, !tbaa !11
  %36 = udiv i64 %35, 10
  store i64 %36, ptr %10, align 8, !tbaa !11
  br label %37

37:                                               ; preds = %28
  %38 = load i64, ptr %10, align 8, !tbaa !11
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %28, label %40, !llvm.loop !14

40:                                               ; preds = %37
  %41 = load i64, ptr %7, align 8, !tbaa !11
  %42 = icmp slt i64 %41, 0
  br i1 %42, label %43, label %46

43:                                               ; preds = %40
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i32 -1
  store ptr %45, ptr %9, align 8, !tbaa !5
  store i8 45, ptr %44, align 1, !tbaa !13
  br label %46

46:                                               ; preds = %43, %40
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i32 1
  store ptr %48, ptr %9, align 8, !tbaa !5
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds [32 x i8], ptr %8, i64 0, i64 0
  %51 = ptrtoint ptr %49 to i64
  %52 = ptrtoint ptr %50 to i64
  %53 = sub i64 %51, %52
  %54 = sub nsw i64 32, %53
  store i64 %54, ptr %11, align 8, !tbaa !9
  %55 = load i64, ptr %11, align 8, !tbaa !9
  %56 = add i64 %55, 1
  %57 = load i64, ptr %6, align 8, !tbaa !9
  %58 = icmp ugt i64 %56, %57
  br i1 %58, label %59, label %62

59:                                               ; preds = %46
  %60 = load i64, ptr %6, align 8, !tbaa !9
  %61 = sub i64 %60, 1
  store i64 %61, ptr %11, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %59, %46
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %63, ptr align 1 %64, i64 %65, i1 false)
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load i64, ptr %11, align 8, !tbaa !9
  %68 = getelementptr inbounds i8, ptr %66, i64 %67
  store i8 0, ptr %68, align 1, !tbaa !13
  %69 = load i64, ptr %11, align 8, !tbaa !9
  %70 = trunc i64 %69 to i32
  store i32 %70, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

71:                                               ; preds = %62, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #3
  %72 = load i32, ptr %4, align 4
  ret i32 %72
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; 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 nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @_ZN4ardb9ll2stringEPcmx(ptr nocapture noundef writeonly %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [32 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #4
  %5 = icmp eq i64 %1, 0
  br i1 %5, label %30, label %6

6:                                                ; preds = %3
  %7 = tail call i64 @llvm.abs.i64(i64 %2, i1 true)
  %8 = getelementptr inbounds i8, ptr %4, i64 31
  br label %9

9:                                                ; preds = %9, %6
  %.022 = phi ptr [ %8, %6 ], [ %13, %9 ]
  %.021 = phi i64 [ %7, %6 ], [ %14, %9 ]
  %10 = urem i64 %.021, 10
  %11 = trunc nuw nsw i64 %10 to i8
  %12 = or disjoint i8 %11, 48
  %13 = getelementptr inbounds i8, ptr %.022, i64 -1
  store i8 %12, ptr %.022, align 1, !tbaa !5
  %14 = udiv i64 %.021, 10
  %.not = icmp ult i64 %.021, 10
  br i1 %.not, label %15, label %9, !llvm.loop !8

15:                                               ; preds = %9
  %16 = icmp slt i64 %2, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %15
  store i8 45, ptr %13, align 1, !tbaa !5
  br label %18

18:                                               ; preds = %17, %15
  %19 = phi i64 [ -2, %17 ], [ -1, %15 ]
  %20 = getelementptr inbounds i8, ptr %.022, i64 %19
  %21 = getelementptr inbounds i8, ptr %20, i64 1
  %22 = ptrtoint ptr %21 to i64
  %23 = ptrtoint ptr %4 to i64
  %.neg = sub i64 %23, %22
  %24 = add i64 %.neg, 32
  %25 = add i64 %.neg, 33
  %26 = icmp ugt i64 %25, %1
  %27 = add i64 %1, -1
  %spec.select = select i1 %26, i64 %27, i64 %24
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %0, ptr nonnull align 1 %21, i64 %spec.select, i1 false)
  %28 = getelementptr inbounds i8, ptr %0, i64 %spec.select
  store i8 0, ptr %28, align 1, !tbaa !5
  %29 = trunc i64 %spec.select to i32
  br label %30

30:                                               ; preds = %3, %18
  %.023 = phi i32 [ %29, %18 ], [ 0, %3 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #4
  ret i32 %.023
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass