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

22:                                               ; preds = %42, %2
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = icmp ult ptr %23, %24
  br i1 %25, label %26, label %47

26:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i8, ptr %27, align 1, !tbaa !14
  %29 = zext i8 %28 to i32
  store i32 %29, ptr %9, align 4, !tbaa !13
  %30 = load i32, ptr %9, align 4, !tbaa !13
  %31 = icmp uge i32 %30, 97
  br i1 %31, label %32, label %38

32:                                               ; preds = %26
  %33 = load i32, ptr %9, align 4, !tbaa !13
  %34 = icmp ule i32 %33, 122
  br i1 %34, label %35, label %38

35:                                               ; preds = %32
  %36 = load i32, ptr %9, align 4, !tbaa !13
  %37 = sub i32 %36, 32
  store i32 %37, ptr %9, align 4, !tbaa !13
  br label %38

38:                                               ; preds = %35, %32, %26
  %39 = load i32, ptr %9, align 4, !tbaa !13
  %40 = trunc i32 %39 to i8
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 %40, ptr %41, align 1, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %42

42:                                               ; preds = %38
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %45, i32 1
  store ptr %46, ptr %8, align 8, !tbaa !5
  br label %22, !llvm.loop !15

47:                                               ; preds = %22
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = ptrtoint ptr %48 to i64
  %50 = trunc i64 %49 to i32
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.SBuf, ptr %51, i32 0, i32 0
  %53 = getelementptr inbounds %struct.MRef, ptr %52, i32 0, i32 0
  store i32 %50, ptr %53, align 4, !tbaa !17
  %54 = 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 %54
}

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_upper(ptr noundef returned %0, ptr nocapture noundef readonly %1) local_unnamed_addr #1 {
  %3 = ptrtoint ptr %1 to i64
  %4 = getelementptr inbounds i8, ptr %1, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = tail call ptr @lj_buf_more(ptr noundef %0, i32 noundef %5)
  %7 = ptrtoint ptr %6 to i64
  %8 = zext i32 %5 to i64
  %9 = getelementptr inbounds i8, ptr %6, i64 %8
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

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

vector.memcheck:                                  ; preds = %.lr.ph.preheader
  %14 = add i64 %7, -16
  %15 = sub i64 %14, %3
  %diff.check = icmp ult i64 %15, 32
  br i1 %diff.check, label %.lr.ph.preheader30, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %13, -32
  %ind.end = getelementptr i8, ptr %10, i64 %n.vec
  %ind.end24 = getelementptr i8, ptr %6, i64 %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %next.gep = getelementptr i8, ptr %10, i64 %index
  %next.gep27 = getelementptr i8, ptr %6, i64 %index
  %16 = getelementptr i8, ptr %next.gep, i64 16
  %wide.load = load <16 x i8>, ptr %next.gep, align 1, !tbaa !11
  %wide.load29 = load <16 x i8>, ptr %16, align 1, !tbaa !11
  %17 = add <16 x i8> %wide.load, <i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97>
  %18 = add <16 x i8> %wide.load29, <i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97, i8 -97>
  %19 = icmp ult <16 x i8> %17, <i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26>
  %20 = icmp ult <16 x i8> %18, <i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26, i8 26>
  %21 = add nsw <16 x i8> %wide.load, <i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32>
  %22 = add nsw <16 x i8> %wide.load29, <i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32, i8 -32>
  %23 = select <16 x i1> %19, <16 x i8> %21, <16 x i8> %wide.load
  %24 = select <16 x i1> %20, <16 x i8> %22, <16 x i8> %wide.load29
  %25 = getelementptr i8, ptr %next.gep27, i64 16
  store <16 x i8> %23, ptr %next.gep27, align 1, !tbaa !11
  store <16 x i8> %24, ptr %25, align 1, !tbaa !11
  %index.next = add nuw i64 %index, 32
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !12

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

.lr.ph.preheader30:                               ; preds = %middle.block, %vector.memcheck, %.lr.ph.preheader
  %.01923.ph = phi ptr [ %10, %vector.memcheck ], [ %10, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.02022.ph = phi ptr [ %6, %vector.memcheck ], [ %6, %.lr.ph.preheader ], [ %ind.end24, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader30, %.lr.ph
  %.01923 = phi ptr [ %31, %.lr.ph ], [ %.01923.ph, %.lr.ph.preheader30 ]
  %.02022 = phi ptr [ %30, %.lr.ph ], [ %.02022.ph, %.lr.ph.preheader30 ]
  %27 = load i8, ptr %.01923, align 1, !tbaa !11
  %28 = add i8 %27, -97
  %or.cond = icmp ult i8 %28, 26
  %29 = add nsw i8 %27, -32
  %spec.select = select i1 %or.cond, i8 %29, i8 %27
  store i8 %spec.select, ptr %.02022, align 1, !tbaa !11
  %30 = getelementptr inbounds i8, ptr %.02022, i64 1
  %31 = getelementptr inbounds i8, ptr %.01923, i64 1
  %32 = icmp ult ptr %30, %9
  br i1 %32, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %2
  %.020.lcssa = phi ptr [ %6, %2 ], [ %ind.end24, %middle.block ], [ %30, %.lr.ph ]
  %33 = ptrtoint ptr %.020.lcssa to i64
  %34 = trunc i64 %33 to i32
  store i32 %34, ptr %0, align 4, !tbaa !17
  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 = distinct !{!12, !13, !14, !15}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !13, !14}
!17 = !{!18, !8, i64 0}
!18 = !{!"SBuf", !19, i64 0, !19, i64 4, !19, i64 8, !19, i64 12}
!19 = !{!"MRef", !8, i64 0}


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