; 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(none)
declare i64 @llvm.expect.i64(i64, i64) #0

; Function Attrs: noreturn
declare hidden void @lj_err_mem(ptr noundef) #1

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

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

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

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

18:                                               ; preds = %3
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %97

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = zext i32 %24 to i64
  %26 = mul i64 %23, %25
  store i64 %26, ptr %8, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %27 = load i64, ptr %8, align 8, !tbaa !14
  %28 = icmp ugt i64 %27, 2147483392
  %29 = xor i1 %28, true
  %30 = xor i1 %29, true
  %31 = zext i1 %30 to i32
  %32 = sext i32 %31 to i64
  %33 = call i64 @llvm.expect.i64(i64 %32, i64 0)
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %42

35:                                               ; preds = %21
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.SBuf, ptr %36, i32 0, i32 3
  %38 = getelementptr inbounds %struct.MRef, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = zext i32 %39 to i64
  %41 = inttoptr i64 %40 to ptr
  call void @lj_err_mem(ptr noundef %41) #6
  unreachable

42:                                               ; preds = %21
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i64, ptr %8, align 8, !tbaa !14
  %45 = trunc i64 %44 to i32
  %46 = call ptr @lj_buf_more(ptr noundef %43, i32 noundef %45)
  store ptr %46, ptr %9, align 8, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = icmp eq i32 %47, 1
  br i1 %48, label %49, label %65

49:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.GCstr, ptr %50, i64 1
  %52 = getelementptr inbounds i8, ptr %51, i64 0
  %53 = load i8, ptr %52, align 1, !tbaa !19
  %54 = sext i8 %53 to i32
  store i32 %54, ptr %10, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %60, %49
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = trunc i32 %56 to i8
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %9, align 8, !tbaa !5
  store i8 %57, ptr %58, align 1, !tbaa !19
  br label %60

60:                                               ; preds = %55
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = add nsw i32 %61, -1
  store i32 %62, ptr %6, align 4, !tbaa !9
  %63 = icmp sgt i32 %62, 0
  br i1 %63, label %55, label %64, !llvm.loop !20

64:                                               ; preds = %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %90

65:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.GCstr, ptr %66, i64 1
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = zext i32 %68 to i64
  %70 = getelementptr inbounds i8, ptr %67, i64 %69
  store ptr %70, ptr %11, align 8, !tbaa !5
  br label %71

71:                                               ; preds = %85, %65
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.GCstr, ptr %72, i64 1
  store ptr %73, ptr %12, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %80, %71
  %75 = load ptr, ptr %12, align 8, !tbaa !5
  %76 = getelementptr inbounds i8, ptr %75, i32 1
  store ptr %76, ptr %12, align 8, !tbaa !5
  %77 = load i8, ptr %75, align 1, !tbaa !19
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = getelementptr inbounds i8, ptr %78, i32 1
  store ptr %79, ptr %9, align 8, !tbaa !5
  store i8 %77, ptr %78, align 1, !tbaa !19
  br label %80

80:                                               ; preds = %74
  %81 = load ptr, ptr %12, align 8, !tbaa !5
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  %83 = icmp ult ptr %81, %82
  br i1 %83, label %74, label %84, !llvm.loop !22

84:                                               ; preds = %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %85

85:                                               ; preds = %84
  %86 = load i32, ptr %6, align 4, !tbaa !9
  %87 = add nsw i32 %86, -1
  store i32 %87, ptr %6, align 4, !tbaa !9
  %88 = icmp sgt i32 %87, 0
  br i1 %88, label %71, label %89, !llvm.loop !23

89:                                               ; preds = %85
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %90

90:                                               ; preds = %89, %64
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = ptrtoint ptr %91 to i64
  %93 = trunc i64 %92 to i32
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.SBuf, ptr %94, i32 0, i32 0
  %96 = getelementptr inbounds %struct.MRef, ptr %95, i32 0, i32 0
  store i32 %93, ptr %96, align 4, !tbaa !24
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %97

97:                                               ; preds = %90, %18, %3
  %98 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  ret ptr %98
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #1 = { noreturn "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 willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { noreturn }

!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 = !{!15, !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !10, i64 12}
!17 = !{!"SBuf", !18, i64 0, !18, i64 4, !18, i64 8, !18, i64 12}
!18 = !{!"MRef", !10, i64 0}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}
!23 = distinct !{!23, !21}
!24 = !{!17, !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: noreturn
declare hidden void @lj_err_mem(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define hidden noundef ptr @lj_buf_putstr_rep(ptr noundef returned %0, ptr noundef readonly %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = ptrtoint ptr %1 to i64
  %5 = getelementptr inbounds i8, ptr %1, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp sgt i32 %2, 0
  %8 = icmp ne i32 %6, 0
  %or.cond = select i1 %7, i1 %8, i1 false
  br i1 %or.cond, label %9, label %46

9:                                                ; preds = %3
  %10 = zext nneg i32 %2 to i64
  %11 = zext i32 %6 to i64
  %12 = mul nuw nsw i64 %11, %10
  %13 = icmp ugt i64 %12, 2147483392
  br i1 %13, label %14, label %19, !prof !11

14:                                               ; preds = %9
  %15 = getelementptr inbounds i8, ptr %0, i64 12
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = zext i32 %16 to i64
  %18 = inttoptr i64 %17 to ptr
  tail call void @lj_err_mem(ptr noundef %18) #5
  unreachable

19:                                               ; preds = %9
  %20 = trunc nuw nsw i64 %12 to i32
  %21 = tail call ptr @lj_buf_more(ptr noundef %0, i32 noundef %20)
  %22 = icmp eq i32 %6, 1
  %23 = getelementptr inbounds i8, ptr %1, i64 16
  br i1 %22, label %.loopexit.loopexit, label %26

.loopexit.loopexit:                               ; preds = %19
  %24 = load i8, ptr %23, align 1, !tbaa !15
  tail call void @llvm.memset.p0.i64(ptr align 1 %21, i8 %24, i64 %10, i1 false), !tbaa !15
  %25 = zext nneg i32 %2 to i64
  %scevgep = getelementptr i8, ptr %21, i64 %25
  br label %.loopexit

26:                                               ; preds = %19
  %27 = getelementptr inbounds i8, ptr %23, i64 %11
  %28 = add i64 %4, %11
  %29 = add i64 %28, 16
  %30 = add i64 %4, 17
  %umax = tail call i64 @llvm.umax.i64(i64 %29, i64 %30)
  %31 = add i64 %umax, -16
  %32 = sub i64 %31, %4
  %min.iters.check = icmp ult i64 %32, 32
  %n.vec = and i64 %32, -32
  %ind.end39 = getelementptr i8, ptr %23, i64 %n.vec
  %cmp.n = icmp eq i64 %32, %n.vec
  br label %33

33:                                               ; preds = %.loopexit45, %26
  %.127 = phi ptr [ %21, %26 ], [ %.lcssa, %.loopexit45 ]
  %.1 = phi i32 [ %2, %26 ], [ %42, %.loopexit45 ]
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %33
  %.12738 = ptrtoint ptr %.127 to i64
  %reass.sub = sub i64 %.12738, %4
  %34 = add i64 %reass.sub, -16
  %diff.check = icmp ult i64 %34, 32
  br i1 %diff.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %ind.end = getelementptr i8, ptr %.127, 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 %.127, i64 %index
  %next.gep42 = getelementptr i8, ptr %23, i64 %index
  %35 = getelementptr i8, ptr %next.gep42, i64 16
  %wide.load = load <16 x i8>, ptr %next.gep42, align 1, !tbaa !15
  %wide.load44 = load <16 x i8>, ptr %35, align 1, !tbaa !15
  %36 = getelementptr i8, ptr %next.gep, i64 16
  store <16 x i8> %wide.load, ptr %next.gep, align 1, !tbaa !15
  store <16 x i8> %wide.load44, ptr %36, align 1, !tbaa !15
  %index.next = add nuw i64 %index, 32
  %37 = icmp eq i64 %index.next, %n.vec
  br i1 %37, label %middle.block, label %vector.body, !llvm.loop !16

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %.loopexit45, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %33
  %.2.ph = phi ptr [ %.127, %vector.memcheck ], [ %.127, %33 ], [ %ind.end, %middle.block ]
  %.0.ph = phi ptr [ %23, %vector.memcheck ], [ %23, %33 ], [ %ind.end39, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.2 = phi ptr [ %40, %scalar.ph ], [ %.2.ph, %scalar.ph.preheader ]
  %.0 = phi ptr [ %38, %scalar.ph ], [ %.0.ph, %scalar.ph.preheader ]
  %38 = getelementptr inbounds i8, ptr %.0, i64 1
  %39 = load i8, ptr %.0, align 1, !tbaa !15
  %40 = getelementptr inbounds i8, ptr %.2, i64 1
  store i8 %39, ptr %.2, align 1, !tbaa !15
  %41 = icmp ult ptr %38, %27
  br i1 %41, label %scalar.ph, label %.loopexit45, !llvm.loop !20

.loopexit45:                                      ; preds = %scalar.ph, %middle.block
  %.lcssa = phi ptr [ %ind.end, %middle.block ], [ %40, %scalar.ph ]
  %42 = add nsw i32 %.1, -1
  %43 = icmp sgt i32 %.1, 1
  br i1 %43, label %33, label %.loopexit, !llvm.loop !21

.loopexit:                                        ; preds = %.loopexit45, %.loopexit.loopexit
  %.3 = phi ptr [ %scevgep, %.loopexit.loopexit ], [ %.lcssa, %.loopexit45 ]
  %44 = ptrtoint ptr %.3 to i64
  %45 = trunc i64 %44 to i32
  store i32 %45, ptr %0, align 4, !tbaa !22
  br label %46

46:                                               ; preds = %.loopexit, %3
  ret ptr %0
}

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

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

attributes #0 = { noreturn "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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { noreturn 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, !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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!12 = !{!13, !8, i64 12}
!13 = !{!"SBuf", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12}
!14 = !{!"MRef", !8, i64 0}
!15 = !{!9, !9, i64 0}
!16 = distinct !{!16, !17, !18, !19}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!"llvm.loop.isvectorized", i32 1}
!19 = !{!"llvm.loop.unroll.runtime.disable"}
!20 = distinct !{!20, !17, !18}
!21 = distinct !{!21, !17}
!22 = !{!13, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass