; LLVM IR from asllop/AppOS :: term (ansi)

; 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"

@__const.ansi_itoa.digit = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @ansi_itoa(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca [11 x i8], align 1
  %6 = alloca ptr, align 8
  %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 11, ptr %5) #3
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %5, ptr align 1 @__const.ansi_itoa.digit, i64 11, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %8, ptr %6, align 8, !tbaa !9
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 0
  br i1 %10, label %11, label %16

11:                                               ; preds = %2
  %12 = load ptr, ptr %6, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %12, i32 1
  store ptr %13, ptr %6, align 8, !tbaa !9
  store i8 45, ptr %12, align 1, !tbaa !11
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = mul nsw i32 %14, -1
  store i32 %15, ptr %3, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %11, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %17 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %17, ptr %7, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %23, %16
  %19 = load ptr, ptr %6, align 8, !tbaa !9
  %20 = getelementptr inbounds i8, ptr %19, i32 1
  store ptr %20, ptr %6, align 8, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = sdiv i32 %21, 10
  store i32 %22, ptr %7, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %18
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %18, label %26, !llvm.loop !12

26:                                               ; preds = %23
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  store i8 0, ptr %27, align 1, !tbaa !11
  br label %28

28:                                               ; preds = %38, %26
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = srem i32 %29, 10
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [11 x i8], ptr %5, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = load ptr, ptr %6, align 8, !tbaa !9
  %35 = getelementptr inbounds i8, ptr %34, i32 -1
  store ptr %35, ptr %6, align 8, !tbaa !9
  store i8 %33, ptr %35, align 1, !tbaa !11
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sdiv i32 %36, 10
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %28
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %28, label %41, !llvm.loop !14

41:                                               ; preds = %38
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 11, ptr %5) #3
  ret ptr %42
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; 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) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/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 = distinct !{!14, !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"

@__const.ansi_itoa.digit = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local noundef ptr @ansi_itoa(i32 noundef %0, ptr noundef returned writeonly %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %0, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %1, i64 1
  store i8 45, ptr %1, align 1, !tbaa !5
  %6 = sub nsw i32 0, %0
  br label %7

7:                                                ; preds = %4, %2
  %.014 = phi i32 [ %6, %4 ], [ %0, %2 ]
  %.013 = phi ptr [ %5, %4 ], [ %1, %2 ]
  br label %8

8:                                                ; preds = %8, %7
  %.1 = phi ptr [ %.013, %7 ], [ %9, %8 ]
  %.0 = phi i32 [ %.014, %7 ], [ %10, %8 ]
  %9 = getelementptr inbounds i8, ptr %.1, i64 1
  %10 = udiv i32 %.0, 10
  %.not = icmp ult i32 %.0, 10
  br i1 %.not, label %11, label %8, !llvm.loop !8

11:                                               ; preds = %8
  store i8 0, ptr %9, align 1, !tbaa !5
  br label %12

12:                                               ; preds = %12, %11
  %.115 = phi i32 [ %.014, %11 ], [ %18, %12 ]
  %.2 = phi ptr [ %9, %11 ], [ %17, %12 ]
  %13 = urem i32 %.115, 10
  %14 = zext nneg i32 %13 to i64
  %15 = getelementptr inbounds [11 x i8], ptr @__const.ansi_itoa.digit, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %.2, i64 -1
  store i8 %16, ptr %17, align 1, !tbaa !5
  %18 = udiv i32 %.115, 10
  %.not18 = icmp ult i32 %.115, 10
  br i1 %.not18, label %19, label %12, !llvm.loop !10

19:                                               ; preds = %12
  ret ptr %1
}

attributes #0 = { 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" }

!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/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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