; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_strfmt)

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

%struct.GCstr = type { %struct.GCRef, i8, i8, i8, i8, i32, i32 }
%struct.GCRef = type { i32 }
%struct.SBuf = type { %struct.MRef, %struct.MRef, %struct.MRef, %struct.MRef }
%struct.MRef = type { i32 }

; 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: alwaysinline nounwind uwtable
declare hidden ptr @lj_buf_more(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define hidden ptr @lj_strfmt_putfstr(ptr noundef %0, i32 noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.GCstr, ptr %10, i32 0, i32 6
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = lshr i32 %13, 24
  %15 = and i32 %14, 255
  %16 = sub i32 %15, 1
  %17 = icmp ule i32 %12, %16
  br i1 %17, label %18, label %22

18:                                               ; preds = %3
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.GCstr, ptr %19, i32 0, i32 6
  %21 = load i32, ptr %20, align 4, !tbaa !11
  br label %27

22:                                               ; preds = %3
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = lshr i32 %23, 24
  %25 = and i32 %24, 255
  %26 = sub i32 %25, 1
  br label %27

27:                                               ; preds = %22, %18
  %28 = phi i32 [ %21, %18 ], [ %26, %22 ]
  store i32 %28, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = lshr i32 %29, 16
  %31 = and i32 %30, 255
  store i32 %31, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = load i32, ptr %7, align 4, !tbaa !9
  %35 = icmp ugt i32 %33, %34
  br i1 %35, label %36, label %38

36:                                               ; preds = %27
  %37 = load i32, ptr %8, align 4, !tbaa !9
  br label %40

38:                                               ; preds = %27
  %39 = load i32, ptr %7, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %38, %36
  %41 = phi i32 [ %37, %36 ], [ %39, %38 ]
  %42 = call ptr @lj_buf_more(ptr noundef %32, i32 noundef %41)
  store ptr %42, ptr %9, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = and i32 %43, 256
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %52

46:                                               ; preds = %40
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.GCstr, ptr %48, i64 1
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = call ptr @lj_buf_wmem(ptr noundef %47, ptr noundef %49, i32 noundef %50)
  store ptr %51, ptr %9, align 8, !tbaa !5
  br label %52

52:                                               ; preds = %46, %40
  br label %53

53:                                               ; preds = %58, %52
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = add i32 %54, -1
  store i32 %55, ptr %8, align 4, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = icmp ugt i32 %54, %56
  br i1 %57, label %58, label %61

58:                                               ; preds = %53
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %59, i32 1
  store ptr %60, ptr %9, align 8, !tbaa !5
  store i8 32, ptr %59, align 1, !tbaa !14
  br label %53, !llvm.loop !15

61:                                               ; preds = %53
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = and i32 %62, 256
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %71, label %65

65:                                               ; preds = %61
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.GCstr, ptr %67, i64 1
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = call ptr @lj_buf_wmem(ptr noundef %66, ptr noundef %68, i32 noundef %69)
  store ptr %70, ptr %9, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %65, %61
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = ptrtoint ptr %72 to i64
  %74 = trunc i64 %73 to i32
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.SBuf, ptr %75, i32 0, i32 0
  %77 = getelementptr inbounds %struct.MRef, ptr %76, i32 0, i32 0
  store i32 %74, ptr %77, align 4, !tbaa !17
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret ptr %78
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @lj_buf_wmem(ptr noundef, ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"GCstr", !13, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !10, i64 8, !10, i64 12}
!13 = !{!"GCRef", !10, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !10, i64 0}
!18 = !{!"SBuf", !19, i64 0, !19, i64 4, !19, i64 8, !19, i64 12}
!19 = !{!"MRef", !10, i64 0}


; 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: alwaysinline nounwind uwtable
declare hidden ptr @lj_buf_more(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden noundef ptr @lj_strfmt_putfstr(ptr noundef returned %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %2, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = lshr i32 %1, 24
  %7 = add nsw i32 %6, -1
  %. = tail call i32 @llvm.umin.i32(i32 %5, i32 %7)
  %8 = lshr i32 %1, 16
  %9 = and i32 %8, 255
  %10 = tail call i32 @llvm.umax.i32(i32 %9, i32 %.)
  %11 = tail call ptr @lj_buf_more(ptr noundef %0, i32 noundef %10)
  %12 = and i32 %1, 256
  %.not27 = icmp eq i32 %12, 0
  br i1 %.not27, label %16, label %13

13:                                               ; preds = %3
  %14 = getelementptr inbounds i8, ptr %2, i64 16
  %15 = tail call ptr @lj_buf_wmem(ptr noundef %11, ptr noundef nonnull %14, i32 noundef %.)
  br label %16

16:                                               ; preds = %13, %3
  %.0 = phi ptr [ %15, %13 ], [ %11, %3 ]
  %17 = icmp ugt i32 %9, %.
  br i1 %17, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %16
  %18 = xor i32 %., -1
  %19 = add nsw i32 %9, %18
  %20 = zext i32 %19 to i64
  %21 = add nuw nsw i64 %20, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %.0, i8 32, i64 %21, i1 false), !tbaa !11
  %scevgep = getelementptr i8, ptr %.0, i64 %21
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %16
  %.1.lcssa = phi ptr [ %.0, %16 ], [ %scevgep, %.lr.ph.preheader ]
  br i1 %.not27, label %22, label %25

22:                                               ; preds = %._crit_edge
  %23 = getelementptr inbounds i8, ptr %2, i64 16
  %24 = tail call ptr @lj_buf_wmem(ptr noundef %.1.lcssa, ptr noundef nonnull %23, i32 noundef %.)
  br label %25

25:                                               ; preds = %22, %._crit_edge
  %.2 = phi ptr [ %.1.lcssa, %._crit_edge ], [ %24, %22 ]
  %26 = ptrtoint ptr %.2 to i64
  %27 = trunc i64 %26 to i32
  store i32 %27, ptr %0, align 4, !tbaa !12
  ret ptr %0
}

; Function Attrs: alwaysinline nounwind uwtable
declare hidden ptr @lj_buf_wmem(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #2

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

attributes #0 = { alwaysinline 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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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, !8, i64 12}
!6 = !{!"GCstr", !7, i64 0, !9, i64 4, !9, i64 5, !9, i64 6, !9, i64 7, !8, i64 8, !8, i64 12}
!7 = !{!"GCRef", !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!9, !9, i64 0}
!12 = !{!13, !8, i64 0}
!13 = !{!"SBuf", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12}
!14 = !{!"MRef", !8, i64 0}


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