; LLVM IR from freakout42/macrocalc :: mc/mex (buffer)

; 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: nounwind uwtable
define dso_local void @pltoa(ptr noundef %0, i32 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !11
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  store i8 0, ptr %10, align 1, !tbaa !13
  br label %11

11:                                               ; preds = %14, %3
  %12 = load i64, ptr %6, align 8, !tbaa !11
  %13 = icmp sge i64 %12, 10
  br i1 %13, label %14, label %26

14:                                               ; preds = %11
  %15 = load i64, ptr %6, align 8, !tbaa !11
  %16 = srem i64 %15, 10
  %17 = add nsw i64 %16, 48
  %18 = trunc i64 %17 to i8
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = add nsw i32 %20, -1
  store i32 %21, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %19, i64 %22
  store i8 %18, ptr %23, align 1, !tbaa !13
  %24 = load i64, ptr %6, align 8, !tbaa !11
  %25 = sdiv i64 %24, 10
  store i64 %25, ptr %6, align 8, !tbaa !11
  br label %11, !llvm.loop !14

26:                                               ; preds = %11
  %27 = load i64, ptr %6, align 8, !tbaa !11
  %28 = add nsw i64 %27, 48
  %29 = trunc i64 %28 to i8
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %5, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %30, i64 %33
  store i8 %29, ptr %34, align 1, !tbaa !13
  br label %35

35:                                               ; preds = %38, %26
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %44

38:                                               ; preds = %35
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add nsw i32 %40, -1
  store i32 %41, ptr %5, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %39, i64 %42
  store i8 32, ptr %43, align 1, !tbaa !13
  br label %35, !llvm.loop !16

44:                                               ; preds = %35
  ret void
}

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

!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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: nofree norecurse nosync nounwind memory(argmem: write) uwtable
define dso_local void @pltoa(ptr nocapture noundef writeonly %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = getelementptr inbounds i8, ptr %0, i64 %4
  store i8 0, ptr %5, align 1, !tbaa !5
  %6 = icmp sgt i64 %2, 9
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %4, %3 ]
  %.014 = phi i64 [ %11, %.lr.ph ], [ %2, %3 ]
  %7 = urem i64 %.014, 10
  %8 = trunc nuw nsw i64 %7 to i8
  %9 = or disjoint i8 %8, 48
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %10 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  store i8 %9, ptr %10, align 1, !tbaa !5
  %11 = udiv i64 %.014, 10
  %12 = icmp ugt i64 %.014, 99
  br i1 %12, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !8

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %13 = trunc nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.012.lcssa = phi i32 [ %1, %3 ], [ %13, %._crit_edge.loopexit ]
  %.0.lcssa = phi i64 [ %2, %3 ], [ %11, %._crit_edge.loopexit ]
  %14 = trunc i64 %.0.lcssa to i8
  %15 = add i8 %14, 48
  %16 = add nsw i32 %.012.lcssa, -1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %0, i64 %17
  store i8 %15, ptr %18, align 1, !tbaa !5
  %.not16 = icmp eq i32 %16, 0
  br i1 %.not16, label %._crit_edge20, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %._crit_edge
  %19 = add i32 %.012.lcssa, -2
  %20 = sext i32 %19 to i64
  %21 = zext i32 %19 to i64
  %22 = sub nsw i64 %20, %21
  %scevgep = getelementptr i8, ptr %0, i64 %22
  %23 = zext i32 %16 to i64
  tail call void @llvm.memset.p0.i64(ptr align 1 %scevgep, i8 32, i64 %23, i1 false), !tbaa !5
  br label %._crit_edge20

._crit_edge20:                                    ; preds = %.lr.ph19.preheader, %._crit_edge
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass