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

; 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_buf_putstr_reverse(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.GCstr, ptr %9, i32 0, i32 6
  %11 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %11, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !13
  %14 = call ptr @lj_buf_more(ptr noundef %12, i32 noundef %13)
  store ptr %14, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !13
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  store ptr %18, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.GCstr, ptr %19, i64 1
  %21 = load i32, ptr %5, align 4, !tbaa !13
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 -1
  store ptr %24, ptr %8, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %29, %2
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = icmp ult ptr %26, %27
  br i1 %28, label %29, label %35

29:                                               ; preds = %25
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %30, i32 -1
  store ptr %31, ptr %8, align 8, !tbaa !5
  %32 = load i8, ptr %30, align 1, !tbaa !14
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i32 1
  store ptr %34, ptr %6, align 8, !tbaa !5
  store i8 %32, ptr %33, align 1, !tbaa !14
  br label %25, !llvm.loop !15

35:                                               ; preds = %25
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = ptrtoint ptr %36 to i64
  %38 = trunc i64 %37 to i32
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.SBuf, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.MRef, ptr %40, i32 0, i32 0
  store i32 %38, ptr %41, align 4, !tbaa !17
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret ptr %42
}

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, !12, i64 12}
!10 = !{!"GCstr", !11, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !12, i64 8, !12, i64 12}
!11 = !{!"GCRef", !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !12, i64 0}
!18 = !{!"SBuf", !19, i64 0, !19, i64 4, !19, i64 8, !19, i64 12}
!19 = !{!"MRef", !12, 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_buf_putstr_reverse(ptr noundef returned %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %1, i64 12
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = tail call ptr @lj_buf_more(ptr noundef %0, i32 noundef %4)
  %6 = ptrtoint ptr %5 to i64
  %7 = zext i32 %4 to i64
  %8 = getelementptr inbounds i8, ptr %5, i64 %7
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %9 = getelementptr i8, ptr %1, i64 16
  %10 = getelementptr i8, ptr %9, i64 %7
  %11 = add i64 %6, %7
  %12 = add i64 %6, 1
  %umax17 = tail call i64 @llvm.umax.i64(i64 %11, i64 %12)
  %13 = sub i64 %umax17, %6
  %min.iters.check = icmp ult i64 %13, 32
  br i1 %min.iters.check, label %.lr.ph.preheader25, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %14 = add i64 %6, %7
  %15 = add i64 %6, 1
  %umax = tail call i64 @llvm.umax.i64(i64 %14, i64 %15)
  %16 = sub i64 %umax, %6
  %scevgep = getelementptr i8, ptr %5, i64 %16
  %17 = add i64 %14, 16
  %18 = sub i64 %17, %umax
  %scevgep16 = getelementptr i8, ptr %1, i64 %18
  %bound0 = icmp ult ptr %5, %10
  %bound1 = icmp ult ptr %scevgep16, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %.lr.ph.preheader25, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %13, -32
  %19 = sub i64 0, %n.vec
  %ind.end = getelementptr i8, ptr %10, i64 %19
  %ind.end18 = getelementptr i8, ptr %5, i64 %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i64 0, %index
  %next.gep = getelementptr i8, ptr %10, i64 %offset.idx
  %next.gep21 = getelementptr i8, ptr %5, i64 %index
  %20 = getelementptr inbounds i8, ptr %next.gep, i64 -16
  %21 = getelementptr inbounds i8, ptr %next.gep, i64 -32
  %wide.load = load <16 x i8>, ptr %20, align 1, !tbaa !11, !alias.scope !12
  %reverse = shufflevector <16 x i8> %wide.load, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %wide.load23 = load <16 x i8>, ptr %21, align 1, !tbaa !11, !alias.scope !12
  %reverse24 = shufflevector <16 x i8> %wide.load23, <16 x i8> poison, <16 x i32> <i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
  %22 = getelementptr i8, ptr %next.gep21, i64 16
  store <16 x i8> %reverse, ptr %next.gep21, align 1, !tbaa !11, !alias.scope !15, !noalias !12
  store <16 x i8> %reverse24, ptr %22, align 1, !tbaa !11, !alias.scope !15, !noalias !12
  %index.next = add nuw i64 %index, 32
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %13, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader25

.lr.ph.preheader25:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %.pn15.ph = phi ptr [ %10, %vector.memcheck ], [ %10, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01314.ph = phi ptr [ %5, %vector.memcheck ], [ %5, %.lr.ph.preheader ], [ %ind.end18, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader25, %.lr.ph
  %.pn15 = phi ptr [ %.0, %.lr.ph ], [ %.pn15.ph, %.lr.ph.preheader25 ]
  %.01314 = phi ptr [ %25, %.lr.ph ], [ %.01314.ph, %.lr.ph.preheader25 ]
  %.0 = getelementptr inbounds i8, ptr %.pn15, i64 -1
  %24 = load i8, ptr %.0, align 1, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %.01314, i64 1
  store i8 %24, ptr %.01314, align 1, !tbaa !11
  %26 = icmp ult ptr %25, %8
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  %.013.lcssa = phi ptr [ %5, %2 ], [ %ind.end18, %middle.block ], [ %25, %.lr.ph ]
  %27 = ptrtoint ptr %.013.lcssa to i64
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %0, align 4, !tbaa !22
  ret ptr %0
}

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

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

!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}
!13 = distinct !{!13, !14}
!14 = distinct !{!14, !"LVerDomain"}
!15 = !{!16}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !19}
!22 = !{!23, !8, i64 0}
!23 = !{!"SBuf", !24, i64 0, !24, i64 4, !24, i64 8, !24, i64 12}
!24 = !{!"MRef", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass